• 手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于Tensorflow,Python)


    通过:

    手写数字识别  ----卷积神经网络模型官方案例详解(基于Tensorflow,Python)

    手写数字识别  ----Softmax回归模型官方案例详解(基于Tensorflow,Python)

    运行程序后得的四个文件,再通过手写的图片判断识别概率

    代码:

    import numpy as np
    import tensorflow as tf
    from flask import Flask, jsonify, render_template, request
    import numpy as np
    from PIL import Image
    from mnist import model
    import matplotlib.pyplot as plt
    from matplotlib.image import imread
    
    # tf.placeholder(dtype, shape=None, name=None)
    # 此函数可以理解为形参,用于定义过程,在执行的时候再赋具体的
    # dtype:数据类型。常用的是tf.float32,tf.float64等数值类型
    # shape:数据形状。默认是None,就是一维值,也可以是多维,比如[2,3], [None, 3]表示列是3,行不定
    # name:名称。
    # 返回:Tensor 类型
    
    x = tf.placeholder("float", [None, 784])
    
    '''用于运行TensorFlow操作的类。 '''
    # session可能拥有的资源,如:tf.Variable,tf.QueueBase和tf.ReaderBase。
    # 不再需要时释放这些资源是非常重要的。
    # 为此,请在session中调用tf.Session.close方法,或使用session作为上下文管理器
    sess = tf.Session()
    
    # 保存和恢复都需要实例化一个 tf.train.Saver。
    # saver = tf.train.Saver()
    # 在训练循环中,定期调用 saver.save() 方法,向文件夹中写入包含了当前模型中所有可训练变量的 checkpoint 文件。
    # saver.save(sess, FLAGS.train_dir, global_step=step)
    # 之后,就可以使用 saver.restore() 方法,重载模型的参数,继续训练或用于测试数据。
    # saver.restore(sess, FLAGS.train_dir)
    
    # restore trained data
    with tf.variable_scope("regression"):
        y1, variables = model.regression(x)
    saver = tf.train.Saver(variables)
    saver.restore(sess, "mnist/data/regression.ckpt")
    
    
    # tf.get_variable(<name>, <shape>, <initializer>) 创建或返回给定名称的变量
    # tf.variable_scope(<scope_name>) 管理传给get_variable()的变量名称的作用域
    with tf.variable_scope("convolutional"):
        keep_prob = tf.placeholder("float")
        y2, variables = model.convolutional(x, keep_prob)
    saver = tf.train.Saver(variables)
    saver.restore(sess, "mnist/data/convolutional.ckpt")
    
    
    def regression(input):
        # print('-------------------regression')
        # print('y2:' + str(y1))
        # print(input)
        return sess.run(y1, feed_dict={x: input}).flatten().tolist()
    
    # run(
    #     fetches,
    #     feed_dict=None,
    #     options=None,
    #     run_metadata=None
    # )
    def convolutional(input):
        # print('-------------------convolutional')
        # print('y2:' + str(y2))
        # print( input)
        return sess.run(y2, feed_dict={x: input, keep_prob: 1.0}).flatten().tolist()
    
    
    # im = Image.open(r'C:UsersadminDesktop无标题.png')
    # im2 = np.array(im)
    # print(im2)
    
    # img = imread(r'C:UsersadminDesktop无标题.png')  # 读入图像(设定合适的路径!)
    # plt.imshow(img)
    # plt.arr
    # plt.show()
    
    # 读取图片
    im = Image.open(r'C:UsersadminDesktop2.png')
    # 显示图片
    # im.show()
    im = im.convert("L")
    # im.show()
    data = im.getdata()
    data = np.matrix(data)
    
     #     print data
    # 变换成512*512
    data = np.reshape(data, (784, 1))
    # new_im = Image.fromarray(data)
    # # 显示图片
    # new_im.show()
    input = ((255 - np.array(data, dtype=np.uint8)) / 255.0).reshape(1, 784)
    
    # # print(input)
    output1 = regression(input)
    output2 = convolutional(input)
    print(output1)
    print(output2)
    手写数字识别

    运行后输出数据:其序号对应值为识别的数字,值为概率,有科学计数法显示数据。
    [0.002712834160774946, 0.37007448077201843, 0.38919582962989807, 0.04636502265930176, 2.2569240172742866e-05, 0.12520278990268707, 0.04699072241783142, 0.0002446999424137175, 0.01896093040704727, 0.00023008222342468798](Softmax回归模型)
    [0.0004617558151949197, 0.02070416323840618, 0.9636037349700928, 0.00868076179176569, 6.441913137678057e-05, 0.003921648487448692, 0.0009535282733850181, 0.0006638980703428388, 0.0006735732895322144, 0.0002723101933952421](卷积神经网络模型)

     0.38919582962989807 

     0.9636037349700928

    成功识别图片数字为2

    相关代码链接:https://download.csdn.net/download/qq_35554617/10883571

  • 相关阅读:
    Java Socket
    路由器和交换机的区别
    OSI七层协议
    traceroute命令
    DNS递归和迭代原理
    出栈入栈顺序问题
    A记录、CNAME记录、MX记录
    DNS解析原理
    RAID磁盘阵列0、1、5、10
    http状态码
  • 原文地址:https://www.cnblogs.com/motao9527/p/10190987.html
Copyright © 2020-2023  润新知