Pass:
After running the programFour documents, again by hand
Image judgment recognition probability
Code:
Handwritten digit recognition![]()
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) # This function can be understood as a parameter, which is used to define the process and assign specific # dtype: Data type. What is commonly used is tf.float32,tf.float64 Equal value type # shape: Data shape. The default is None,It can be a one-dimensional value or a multi-dimensional value, such as[2,3], [None, 3]Indicates that the column is 3 and the row is indefinite # name: Name. # Return: Tensor type x = tf.placeholder("float", [None, 784]) '''Used for running TensorFlow The class of the operation. ''' # session Possible resources, such as: tf.Variable,tf.QueueBase and tf.ReaderBase. # It is very important to release these resources when they are no longer needed. # To do this, please session Call in tf.Session.close Method, or use session As context manager sess = tf.Session() # Both save and restore need to instantiate a tf.train.Saver. # saver = tf.train.Saver() # In the training cycle, call regularly saver.save() Method, write to the folder the checkpoint Papers. # saver.save(sess, FLAGS.train_dir, global_step=step) # After that, you can use saver.restore() Method, overloads the parameters of the model, continues training or is used to test the data. # 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>) Creates or returns a variable of the given name # tf.variable_scope(<scope_name>) Management to get_variable()The scope of the variable name of 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\No title.png') # im2 = np.array(im) # print(im2) # img = imread(r'C:\Users\admin\Desktop\No title.png') # Read in the image (set the appropriate path!) # plt.imshow(img) # plt.arr # plt.show() # Read pictures im = Image.open(r'C:\Users\admin\Desktop\2.png') # display picture # im.show() im = im.convert("L") # im.show() data = im.getdata() data = np.matrix(data) # print data # Transform to 512*512 data = np.reshape(data, (784, 1)) # new_im = Image.fromarray(data) # # display picture # 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)