Python 图像预测问题
我用CNN做了一个模型来预测给定的X光片是否为新冠病毒患者 我面临的问题Python 图像预测问题,python,tensorflow,keras,python-imaging-library,Python,Tensorflow,Keras,Python Imaging Library,我用CNN做了一个模型来预测给定的X光片是否为新冠病毒患者 我面临的问题 有些图像效果很好,但有些图像出现了错误,我无法确定问题出在哪里 这里是错误 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, ite
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)
如何重新创建问题?
PS:-我在模型中尝试了预测(我创建它时在google colab中),相同的图像工作正常,但在部署中,它抛出了错误)你会在Repo中找到一个.ipynb文件,如果你愿意,你可以随意检查它。看起来你的一些图像不是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给出)
这是一个类似的问题,如下所述:
但在这种情况下,,模型似乎缺少正确的输入层。您能添加一个到repo的链接吗?哦,对不起,我忘了添加链接that@RaJa抱歉,我刚刚添加了link,我尝试使用axis=-1来修复这个问题,但是后来模型开始预测错误了,在谷歌Colab中创建和预测时有什么转机吗?它工作得很好,我想你应该先检查输入图像。模型设置正确。但有时灰色图像保存为RGB,有时保存为灰色(单通道)。特别是,当使用来自不同来源的图像时,我不认为图像在这里造成了问题,我认为是库枕造成了问题。我尝试以另一种方式部署此模型(不是读取字节格式的图像),我给出了存储图像的路径,当时它工作得非常好,如果您愿意,我可以共享该代码。@RaJa是正确的,您在这里传递的图像带有
1个通道
,而它需要3个通道
。使用axis=-1
不会改变任何内容,错误意味着最后一个索引应该是3
而不是1
。