【问题标题】:cannot reshape array of size 2352 into shape (1,28,28,1)无法将大小为 2352 的数组重塑为形状 (1,28,28,1)
【发布时间】:2020-01-28 04:56:18
【问题描述】:

在 MNIST 数据分类期间,我无法从自己上传自定义图像并给出 ValueError 无法将大小为 2352 的数组重新整形为 (1,28,28,1)

import numpy as np
from google.colab import files
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt

uploaded = files.upload()

for fn in uploaded.keys():
   path = '/content/' + fn
   img = image.load_img(path, target_size =(28, 28))
   x = image.img_to_array(img)
   x = np.expand_dims(x, axis = 0)
   images = np.vstack([x])
   print(images.shape)
   images = images.reshape(1, 28, 28, 1)
   print(images.shape)
   classes = model.predict(images, batch_size = 10)



Capture.PNG(image/png) - 8252 bytes, last modified: 1/26/2020 - 100% done
Saving Capture.PNG to Capture (1).PNG
(1, 28, 28, 3)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-156-810149b1d6fd> in <module>()
     13   images = np.vstack([x])
     14   print(images.shape)
---> 15   images = images.reshape(1, 28, 28, 1)
     16   print(images.shape)
     17   classes = model.predict(images, batch_size = 10)

 ValueError: cannot reshape array of size 2352 into shape (1,28,28,1)

【问题讨论】:

    标签: python deep-learning


    【解决方案1】:

    您可能正在尝试在 RGB 图像上进行预测,而模型需要灰度图像。如果你这样做会起作用

    img = img[:,:,0]
    

    在您加载图像后立即执行剩余的过程。

    【讨论】:

    • 我在图像加载后添加了这个 - 正如你所建议的 - 但我得到一个'TypeError:图像对象不可下标'
    • 好的,我知道了,img = img[:,:,0] 需要在图像表示为数组之后放置。
    猜你喜欢
    • 1970-01-01
    • 2020-10-15
    • 2019-08-23
    • 2020-02-18
    • 2020-04-14
    • 2020-10-05
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多