【问题标题】:Predicting self draw number with keras用 keras 预测自我抽奖号码
【发布时间】:2018-07-07 14:07:25
【问题描述】:

我使用 keras 来训练 MNIST 数据集。 训练后,我在 MNIST 数据集上测试了数据。 现在我在纸上画了一张零、一、二和三的图片,并将其上传到我的 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

【问题讨论】:

    标签: neural-network keras mnist


    【解决方案1】:

    您的图像具有三个通道 (RGB),而 MNIST 图像只有一个通道(灰度)。您的图像需要以灰度模式打开。如果您使用的是 PIL,可以在 How can I convert an RGB image into grayscale in Python? 找到更多信息。

    【讨论】:

      猜你喜欢
      • 2019-06-13
      • 2017-02-28
      • 2016-05-29
      • 1970-01-01
      • 2018-09-16
      • 2020-10-12
      • 2021-06-01
      • 2021-10-06
      • 1970-01-01
      相关资源
      最近更新 更多