Handwritten digit recognition: obtain recognition probability according to 28 * 28 pictures on the trained data (based on Tensorflow,Python)


Handwritten digit recognition -- a detailed explanation of the official case of convolutional neural network model (based on Tensorflow,Python)

Handwritten digit recognition -- a detailed explanation of the official case of Softmax regression model (based on Tensorflow,Python)

After running the programFour documents, again by handImage judgment recognition probability


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)
Handwritten digit recognition

Output data after operation: the corresponding value of the serial number is the identified number, the value is probability, and there is scientific counting method to display the data.
[0.002712834160774946, 0.37007448077201843, 0.38919582962989807, 0.04636502265930176, 2.2569240172742866e-05, 0.12520278990268707, 0.04699072241783142, 0.0002446999424137175, 0.01896093040704727, 0.000230082232468798] (Softmax regression model)
[0.0004617558151949197, 0.02070416323840618, 0.9636037349700928, 0.00868076179176569, 6.441913137678057e-05, 0.00392164848748692, 0.000953528733850181, 0.00066389807034288388, 0.0006735732895322144, 0.000272310933952421] (convolutional neural network model)



Successfully identified picture number as 2

Related code link: https://download.csdn.net/download/qq_/10883571

Keywords: Python Session network

Added by Operandi on Tue, 10 Dec 2019 08:26:39 +0200