前一段时间忙于csgo,也roll了几个小皮肤,沉迷于csgo中,没有学习,有点颓。
今天呢,又跟老师抬杠,抬杠的时候着实很爽,很舒服。害,到头来还是面对现实。
言归正传,我们想要将数字图片喂给神经网络,让神经网络去识别图片上的数字,以验证模型训练的准确性
我们准备了10张数字图片,如下图所示
由前面训练模型,可知我们训练的图片都是28*28像素的黑底白字图片,所以我们得对图片进行预处理
def pre_pic(picName): #对图片进行处理
img = Image.open(picName)
reIm = img.resize((28,28), Image.ANTIALIAS)#消除图片的一种锯齿差
im_arr = np.array(reIm.convert('L'))#把图片变成灰度图,以满足程序的需求
threshold = 50
for i in range(28):#因为输入的图片是黑底白字,要求的是白底黑字,所以对每一个像素进行反转
for j in range(28):
im_arr[i][j] = 255 - im_arr[i][j]
if (im_arr[i][j] < threshold):
im_arr[i][j] = 0
else: im_arr[i][j] = 255
nm_arr = im_arr.reshape([1, 784])
nm_arr = nm_arr.astype(np.float32)
img_ready = np.multiply(nm_arr, 1.0/255.0)
return img_ready
处理完喂给训练好的神经网络模型
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})
return preValue
else:
print("No checkpoint file found")
return -1
然后输出识别结果,全部代码如下
#coding:utf-8
import tensorflow as tf
import numpy as np
from PIL import Image
import mnist_backward
import mnist_forward
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"]='1' # 这是默认的显示等级,显示所有信息
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error
os.environ["TF_CPP_MIN_LOG_LEVEL"]='3' # 只显示 Error
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})
return preValue
else:
print("No checkpoint file found")
return -1
def pre_pic(picName): #对图片进行处理
img = Image.open(picName)
reIm = img.resize((28,28), Image.ANTIALIAS)#消除图片的一种锯齿差
im_arr = np.array(reIm.convert('L'))#把图片变成灰度图,以满足程序的需求
threshold = 50
for i in range(28):#因为输入的图片是黑底白字,要求的是白底黑字,所以对每一个像素进行反转
for j in range(28):
im_arr[i][j] = 255 - im_arr[i][j]
if (im_arr[i][j] < threshold):
im_arr[i][j] = 0
else: im_arr[i][j] = 255
nm_arr = im_arr.reshape([1, 784])
nm_arr = nm_arr.astype(np.float32)
img_ready = np.multiply(nm_arr, 1.0/255.0)
return img_ready
def application():
testNum = input("input the number of test pictures:")
for i in range(int(testNum)):
testPic = input("the path of test picture:")
testPicArr = pre_pic(testPic)
preValue = restore_model(testPicArr)
print( "The prediction number is:", preValue)
def main():
application()
if __name__ == '__main__':
main()
为了实验的方便,图片直接以数字大小命名