预测不准
上一组代码感觉预测不准,今天想到是不是送进去的数组有问题,于是增加几个函数来看一看数组情况,用到了matplot
增加及修改的函数
imgShow pyplot显示数组图像
loadData打印shape
imgTool增加了squeeze,由于img转换完之后的shape是(28,28,1),去掉最后1的那个维度,再在0的位置增加一个维度,后面predict函数在squeeze后面增加了expand_dim
def imgShow(image):
plt.figure()
plt.imshow(image)
plt.colorbar()
plt.grid(False)
plt.show()
def loadData():
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
print(x_train.shape)
print(x_test.shape)
def imgTool():
img = cv2.imread("D:/workspace/MNIST_data/1.jpg")
img = cv2.resize(img,(28,28))
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img = img_to_array(img)
print(img.shape)
img = np.squeeze(img)
img = img / 255.0
imgShow(img)
print(img.shape)
#cv2.imshow('img',img)
#key = cv2.waitKey(0)
原因分析:
经测试,图片转数组没有问题,shape有点问题,送进去的shape应该是(1,28,28),需要用np来操作,总之就是送进去的数据shape要与训练数据完全一致