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

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

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

运行程序后得的

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

四个文件,再通过手写的

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

图片判断识别概率

代码:

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

手写数字识别 ----在已经训练好的数据上根据28*28的图片获取识别概率(基于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:\Users\admin\Desktop\无标题.png\') # im2 = np.array(im) # print(im2) # img = imread(r\'C:\Users\admin\Desktop\无标题.png\') # 读入图像(设定合适的路径!) # plt.imshow(img) # plt.arr # plt.show() # 读取图片 im = Image.open(r\'C:\Users\admin\Desktop\2.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)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zgxjjs.html