用户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.fl