python - 值错误 : cannot reshape array of size 50176 into shape (1, 224,224,3)

标签 python tensorflow keras resnet

我正在做图像分类,我训练了一个模型并保存了一个模型。当我尝试预测模型时,它显示输入错误。我正在使用 ResNet 架构构建分类器,最初将 input_size 声明为 224 x 224。现在我需要预测测试图像的类别。

我将图像转换为 224x224 numpy 数组。当我尝试下面的代码时

#plot the figure
fig = plt.figure()

for num,data in enumerate(test_data):

    img_num = data[1]
    img_data = data[0]

    y = fig.add_subplot(9,3,num+1)
    orig = img_data
    data = img_data.reshape(1,IMG_SIZ,IMG_SIZ,3)

    #predict the model
    model_out = model.predict_classes([orig])[0]

    if np.argmax(model_out) == 1: str_label='Dog'
    else: str_label='Cat'

    y.imshow(orig,cmap = 'gray')
    plt.title(str_label)
    y.axes.get_xaxis().set_visible(False)
    y.axes.get_yaxis().set_visible(False)


plt.show()
plt.savefig('test_labeled.jpg')

它向我显示以下错误

ValueError: cannot reshape array of size 50176 into shape (1,224,224,3)

我必须以多大的尺寸 reshape 正确的尺寸?

谢谢!

最佳答案

您的输入似乎是 [224, 224, 1] 而不是 [224, 224, 3]。看起来您在 process_test_data()

中将输入转换为 灰度

您可能需要更改:

img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img,(IMG_SIZ,IMG_SIZ))

到:

img = cv2.imread(path)
img = cv2.resize(img,(IMG_SIZ,IMG_SIZ),3)

关于python - 值错误 : cannot reshape array of size 50176 into shape (1, 224,224,3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306863/

相关文章:

machine-learning - NLP 模型在训练时的准确率停留在 0.5098

python - Django 表格 : need more than 1 value to unpack

python - 将 keras 模型的损失集成到 tensorflow 图中

python - 如何将提取的特征传递给keras模型?

tensorflow - tf.train.get_global_step() 在 TensorFlow 中做什么?

python - 使用 Tensorflow Serving 服务 Keras 模型

tensorflow - Tensorflow 2.0 如何连接远程集群?

python - Manage.py sqlall <myapp> 不创建列

python hug api返回自定义http代码

python - Python 中的列表比较是不必要的吗?