【问题标题】:CNN not predicting class of imageCNN 不预测图像类别
【发布时间】:2021-02-01 05:57:19
【问题描述】:

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

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

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).

我尝试了解决方案,但仍然出现相同的错误: Here is the image

【问题讨论】:

  • image的形状是什么?这似乎不是模型所期望的有效尺寸
  • @David S 该图像取自用于训练模型的同一数据集。图片的形状为:(240,240,3)

标签: python python-3.x tensorflow conv-neural-network


【解决方案1】:

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

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

一个例子:

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

这会给图片添加一个batch维度,模型可以正常操作。

【讨论】:

  • @nanan 我查看了您添加到问题中的图像,您应该这样做 image = tf.expand_dims(image, axis=0) 看文档,它返回一个新的扩展张量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-30
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多