我用keras训练MNIST数据集。 训练后,我测试了MNIST数据集上的数据。 现在我在纸上画了一张ZERO,ONE,TWO和THREE的图片,然后把它上传到我的jupiternotebook中,并想预测我画的数字。 我试图对这些数字进行预处理,但在预测时仍然会出现错误。
以下是代码,以及我绘制的图片之一。
img = np.random.rand(224,224,3)
img_path = "0_a.jpg"
img = image.load_img(img_path, target_size=(224, 224))
print(type(img))
x = image.img_to_array(img)
print(type(x))
print(x.shape)
plt.imshow(x/255.)
img = np.random.rand(224,224,3)
img_path = "0_a.jpg"
img = image.load_img(img_path, target_size=(224, 224))
print(type(img))
x = image.img_to_array(img)
print(type(x))
print(x.shape)
plt.imshow(x/255.)
model.predict(x)
这是我得到的错误,我不知道该怎么做。
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-166-2648d9cfd8aa> in <module>()
----> 1 model.predict(x)
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/models.py in predict(self, x, batch_size, verbose, steps)
1025 self.build()
1026 return self.model.predict(x, batch_size=batch_size, verbose=verbose,
-> 1027 steps=steps)
1028
1029 def predict_on_batch(self, x):
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py in predict(self, x, batch_size, verbose, steps)
1780 x = _standardize_input_data(x, self._feed_input_names,
1781 self._feed_input_shapes,
-> 1782 check_batch_axis=False)
1783 if self.stateful:
1784 if x[0].shape[0] > batch_size and x[0].shape[0] % batch_size != 0:
/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix)
108 ': expected ' + names[i] + ' to have ' +
109 str(len(shape)) + ' dimensions, but got array '
--> 110 'with shape ' + str(data_shape))
111 if not check_batch_axis:
112 data_shape = data_shape[1:]
ValueError: Error when checking : expected dense_13_input to have 2 dimensions, but got array with shape (224, 224, 3)
[1]: https://i.stack.imgur.com/SvB
ta.jpg
答案 0 :(得分:0)
您的图像有三个通道(RGB),而MNIST图像只有一个通道(灰度)。您的图像需要以灰度模式打开。如果您正在使用PIL,可以在How can I convert an RGB image into grayscale in Python?找到更多信息。