form1.cn
Make a little progress every day

mnist手写数字识别与图片预处理

28th of March 2020 Linux tensorflow 2059


用户mnist训练了一个模型,想用自己的图片进行测试,需要将自己的图片进行预处理为mnist需要的1*784的二维张量。


因为MNIST数据是28*28的黑底白字图像,而且输入时要将其拉直,也就是可以看成1*784的二维张量(张量的值在0~1之间),所以我们要对图片进行预处理操作,是图片能被网络识别。


引入的库

import tensorflow as tf
import pylab 
from PIL import Image
import numpy as np


以下为图片预处理过程

img = Image.open('./mytestcode/666.jpg').convert('L') # 读取图片转成灰度格式 
img = img.resize((28, 28)) # 更改图片大小  我的图片原始大小为:(366, 368)
npimg1 = np.array(img) # 转为numpy矩阵
flatten_img = npimg1.reshape(1, 784) # 转为mnist1*784二维张量

# 以下意思是把白色背景转为黑色背景,因为训练的都是黑色背景
new_flatten_img = (255-flatten_img)/255.0
new_flatten_img = new_flatten_img.reshape(1, 784) #      --------  该数据可直接被模型识别

print(new_flatten_img.shape)


以下为用训练好的模型识别过程

tf.reset_default_graph()
# tf Graph Input
x = tf.placeholder(tf.float32, [None, 784]) # mnist data维度 28*28=784
y = tf.placeholder(tf.float32, [None, 10]) # 0-9 数字=> 10 classes
# Set model weights
W = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.zeros([10]))
# 构建模型
pred = tf.nn.softmax(tf.matmul(x, W) + b) # Softmax分类
saver = tf.train.Saver()
model_path = "log/521model.ckpt"  # 这是我训练好模型的保存位置
with tf.Session() as sess:
# 初始化
sess.run(tf.global_variables_initializer())
# 从先前保存的模型恢复模型权重
saver.restore(sess, model_path)
output = tf.argmax(pred, 1)
outputval = sess.run(output, feed_dict={x: new_flatten_img})
print(outputval) # 为预测结果
#print(batch_xs.shape)
# 打印自己的图片
im = flatten_img
im = im.reshape(-1,28)
pylab.imshow(im)
pylab.show()