【问题标题】:Issue with predictions using an existing Tensorflow Model使用现有 TensorFlow 模型进行预测的问题
【发布时间】: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


    【解决方案1】:

    您必须检查图像尺寸。数组元素的数量必须是 .reshape 的参数的乘积。 299*299 不等于 268203。

    一个例子是

    a = np.arange(6)
    

    这些是有效的重塑:

    a.reshape(1,6)
    a.reshape(2,3)
    a.reshape(3,2)
    a.reshape(6,1)
    

    因为参数的乘积是 6,也就是数组的长度。

    【讨论】:

      猜你喜欢
      • 2016-02-16
      • 2018-06-12
      • 2020-06-24
      • 1970-01-01
      • 2018-08-03
      • 1970-01-01
      • 2021-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多