【问题标题】:ValueError: Input 0 of layer dense_2 is incompatible with the layerValueError: 层dense_2的输入0与层不兼容
【发布时间】:2020-12-03 05:48:46
【问题描述】:

我试图使用我训练的模型来检查传递给它的图像,但是它未能使用我传递的图像。

代码:

path_img = pathlib.Path("D:/Python Projects/Final Year Project/assets/resource/train_images/Acinetobacter.baumanii/edited/img0.png")
image = keras.preprocessing.image.load_img(path_img, target_size=(self.image_height, self.image_width))
image_array = keras.preprocessing.image.img_to_array(image)
image_array = tf.expand_dims(image_array, 0)
prediction = self.imgReg.model.predict(image_array )

我尝试运行检测图像预测的代码,但出现此错误:

ValueError: Input 0 of layer dense_2 is incompatible with the layer: expected axis -1 of input shape to have value 50176 but received input with shape [None, 57600]

我在 Google Colabs 中使用了相同的图像和模型,它可以在那里工作,但是在我的本地计算机上运行它时,它就不能正常工作,我该如何解决这个问题?

【问题讨论】:

  • 顺便说一句,我使用的是 Tensorflow 2.4.0-dev20200704,而 Google Colab 上的使用的是 2.3.0,这有关系吗?
  • Nvrmind,不是这样,已经测试过了

标签: python tensorflow machine-learning image-classification


【解决方案1】:

如报错所示,您需要在代码中进行以下更改:

image_array = tf.expand_dims(image_array, -1)

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 2020-11-28
    • 2021-05-31
    • 2021-09-13
    • 2022-07-22
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多