【问题标题】:Image Prediction Issue图像预测问题
【发布时间】:2021-04-16 04:45:56
【问题描述】:

我使用 CNN 制作了一个模型来预测给定的 X 射线是否属于 Covid 患者。

我面临的问题

  1. 有些图像可以正常工作,而有些则引发错误,我无法确定问题所在

这是错误

ValueError: in user code:

/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1478 predict_function *
return step_function(self, iterator)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1468 step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:1259 run
return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica
return self._call_for_each_replica(fn, args, kwargs)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica
return fn(*args, **kwargs)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1461 run_step **
outputs = model.predict_step(data)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/training.py:1434 predict_step
return self(x, training=False)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/base_layer.py:998 __call__
input_spec.assert_input_compatibility(self.input_spec, inputs, self.name)
/home/khushwant/.local/lib/python3.8/site-packages/tensorflow/python/keras/engine/input_spec.py:255 assert_input_compatibility
raise ValueError(

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 64, 64, 1)

如何重新创建问题?

  1. 克隆回购Repo link
  2. 进入文件夹并安装requirements.txt
  3. 运行 cdclass.py
  4. 尝试上传我放在试用文件夹中的图片

您会看到 corona 文件夹中的图像可以正常工作,但是一旦我从普通文件夹上传内容,它就会引发错误,如上所述。

PS:-我尝试了模型中的预测(在我创建它时在 google colab 中)相同的图像工作得很好,但在部署中,它会抛出错误)你会发现一个 . Repo 中的 ipynb 文件,如果您愿意,可以随意查看。

【问题讨论】:

  • 你能添加一个指向 repo 的链接吗?
  • 哦,对不起,我忘了这样做
  • @RaJa 抱歉,我刚刚添加了链接

标签: python tensorflow keras python-imaging-library


【解决方案1】:

您的某些图像似乎不是RGB图像,而是灰度图像

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 64, 64, 1)

错误告诉您,您的模型希望输入具有 3 个颜色通道,但错误图像只有一个(由 (None, 64, 64, 1) 中的 1 给出)。

这是与此处描述的类似问题:is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 256, 256, 3] 但在这种情况下,模型似乎缺少正确的输入层。

【讨论】:

  • 我尝试使用axis = -1修复该问题,但随后模型开始预测错误在Google Colab中创建和预测时是否有任何转变,它工作得很好
  • 我认为你应该先检查输入图像。模型设置正确。但有时灰色图像保存为 RGB,有时保存为灰色(1 通道)。特别是在使用来自不同来源的图像时
  • 我不认为图像在这里造成了问题我认为图书馆枕头正在制造问题。我尝试以另一种方式部署此模型(而不是以字节格式读取图像),我提供了存储图像的路径,当时它工作得非常好,如果你愿意,我可以分享该代码。
  • @RaJa 在这里是正确的,当它期望 3 channels 时,您正在使用 1 channel 传递图像。使用axis = -1 不会改变任何东西,错误意味着最后一个索引应该是3 而不是1
猜你喜欢
  • 2019-04-09
  • 2020-08-29
  • 2020-09-12
  • 2011-02-05
  • 1970-01-01
  • 1970-01-01
  • 2015-03-30
  • 2022-09-23
  • 1970-01-01
相关资源
最近更新 更多