【发布时间】:2020-07-07 12:55:05
【问题描述】:
我在包含属于两个不同类别的图像的数据集上训练了一个模型,现在我试图从这个模型中获得一些关于新图像的预测。我使用 saved_model 格式进行保存,并尝试在我的模型上加载和预测一张图像。我的代码如下
loaded = tf.keras.models.load_model('/Library/...')
loaded.compile(loss=tf.keras.losses.CategoricalCrossentropy(label_smoothing=0.1),
optimizer=tf.keras.optimizers.SGD(lr=0.005, momentum=0.9), metrics=['accuracy'])
test_image = image.load_img(img_path, target_size=(img_width, img_height))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
test_image = test_image.reshape(img_width, img_height)
result = loaded.predict(test_image)
print(loaded.predict(test_image))
print(result)
我得到了错误:
Traceback (most recent call last):
File "/Users/...", line 73, in <module>
test_image = test_image.reshape(img_width, img_height)
ValueError: cannot reshape array of size 268203 into shape (299,299)
我认为这是图像文件的问题,但它与我以前训练的图像来自同一来源,我在那里没有遇到任何问题。所有文件都是 RGB png 图像(我认为问题在于它们是 RGBA,但事实并非如此)。任何帮助将不胜感激!
【问题讨论】:
标签: python tensorflow machine-learning keras