首页 > 解决方案 > 使用我自己的自定义图像预测输出时出错

问题描述

因此,我正在尝试学习 Tensorflow,并且我正在使用从驱动器上传的自定义图像来预测模型的结果。这是预测结果的功能。

# Get prediction from the model based of the 'L' grayscale image
def get_pred(model, image_l):
    # Repeat the L value to match input shape
    image_l_R = np.repeat(image_l[..., np.newaxis], 3, -1)
    image_l_R = image_l_R.reshape((1, 224, 224, 3))
    # Normalize the input
    image_l_R = (image_l_R.astype('float32') - 127.5) / 127.5
    # Make prediction
    prediction = model.predict(image_l_R)
    # Normalize the output
    pred = (prediction[0].astype('float32') * 127.5) + 127.5
    
    return pred

这是我试图用来调用该函数的代码:

ig = Image.open('drive/MyDrive/alan_turing.jpg')
plt.imshow(ig)
print(ig.mode)
ign=np.asarray(ig)
print(ign.shape)
plt.imshow(get_pred(model, ign))

我得到的错误:

L
(919, 675)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-66-6f78fcc213bb> in <module>()
      7 #print(ign)
      8 print(ign.shape)
----> 9 plt.imshow(get_pred(model, ign))

<ipython-input-65-1176b84fb613> in get_pred(model, image_l)
      3     # Repeat the L value to match input shape
      4     image_l_R = np.repeat(image_l[..., np.newaxis], 3, -1)
----> 5     image_l_R = image_l_R.reshape((1, 224, 224, 3))
      6     # Normalize the input
      7     image_l_R = (image_l_R.astype('float32') - 127.5) / 127.5

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

我认为问题出在图像大小上,所以我尝试将图像大小转换为 244*244 并再次运行代码并得到相同的错误。当我尝试直接传递图像而不转换为 numpy 数组时。我得到JPEG不可迭代的错误。如果我听起来很愚蠢,我很抱歉,因为我是初学者。请帮我!!

标签: pythonnumpyopencvimage-processingneural-network

解决方案


推荐阅读