繁体   English   中英

CNN不预测图像类别

[英]CNN not predicting class of image

我使用CNN制作了一个脑肿瘤检测模型,当我尝试通过预测它的类别来测试样本图像时,会发生错误。

根据错误,模型的输入应该有 1 个额外的维度。 我如何预测图像的类别。 给出错误的代码片段是:

best_model.predict(image)

错误如下

ValueError: 层 zero_padding2d 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=3。 收到完整形状:[无, 240, 3]

可以在警告中看到预期的形状:

WARNING:tensorflow:Model was constructed with shape (None, 240, 240, 3) for input Tensor("input_1_1:0", shape=(None, 240, 240, 3), dtype=float32), but it was called on an input with incompatible shape (None, 240, 3).

我尝试了解决方案,但它仍然给出相同的错误:这是图像

所以问题是模型假设第一个维度是批次数。 在您的情况下,它“认为”您有 240 个批次,其中每个图像的大小为(240,3)

您需要做的是在传递给模型之前扩展图像的尺寸。 你可以使用expand_dims

一个例子:

image = tf.zeros([240, 240, 3])
tf.expand_dims(image, axis=0)

这将为图像添加批处理维度,并且模型可以正确操作它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM