前面学习了关于使用MNIST数据集中的数据进行训练和测试。现在要用自己的手写数字进行识别
使用自己的手写数字进行识别
主要部分如下
def application():
testNum = int(input("input the number of test pictures:"))
for i in range(testNum):
testPic = input("the path of test picture:")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print("prediction num is",preValue)
整体代码如下
1、先是导包
import tensorflow as tf
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import mnist_backward
import mnist_forward
2、模型加载
def restore_model(testPicArr):
with tf.Graph().as_default() as tg:
x = tf.placeholder(tf.float32,[None,mnist_forward.INPUT_NODE])
y = mnist_forward.forward(x,None)
preValue = tf.argmax(y,1)
variable_averages = tf.train.ExponentialMovingAverage(mnist_backward.MOVING_AVERAGE_DECAY)
variables_to_restore = variable_averages.variables_to_restore()
saver = tf.train.Saver(variables_to_restore)
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(mnist_backward.MODEL_SAVE_PATH)
if (ckpt and ckpt.model_checkpoint_path):
saver.restore(sess,ckpt.model_checkpoint_path)
preValue = sess.run(preValue, feed_dict={x:testPicArr})