python - 使用我自己的自定义图像预测输出时出错
问题描述
因此,我正在尝试学习 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不可迭代的错误。如果我听起来很愚蠢,我很抱歉,因为我是初学者。请帮我!!
解决方案
推荐阅读
- xaml - 如何在 DirectX UWP 应用中呈现 XAML 页面
- wordpress - 如何从 Wordpress 搜索结果中排除特定页面
- android - 我的 JobService 在 Android 10 中无法运行,但在 Android 9 及更低版本中运行良好
- reactjs - 如何在 Reactjs 中创建多步表单
- java - 为 Spring Cloud 配置 maven 依赖项
- r - 在 R 4.0.0 之前安装了包“stringr”:请重新安装 BiocManager 安装路径不可写,无法更新包
- node.js - nodejs multer image upload:保留文件名和扩展名
- tensorflow - TensorFlow 和 Cuda 不兼容
- javascript - 如何将opencv输出显示到画布?
- python-3.x - python3如何创建一个检查空值并取相邻值的平均值来替换空值的列表?