ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。收到的完整形状:(256、256、256)

Posted

技术标签:

【中文标题】ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。收到的完整形状:(256、256、256)【英文标题】:ValueError: Input 0 of layer conv2d is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: (256, 256, 256) 【发布时间】:2021-11-13 15:59:46 【问题描述】:

所以我正在使用 CNN 进行脑肿瘤检测项目。我有一个模型,它需要 4 个参数作为输入(BATCH_SIZE、IMAGE_SIZE、IMAGE_SIZE、CHANNELS)来进行预测。我的模型适用于大多数图像,但对于某些图像,它会给出错误 ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。收到的完整形状:(256, 256, 256)

我已经完成了图像大小调整(256,256)和整形(1,256,256,3),但仍然出现错误。我在谷歌上寻找答案,但找不到任何合理的答案。可能是什么问题呢?我将不胜感激任何帮助。附上我的模型和错误的屏幕截图以供参考。 谢谢!!

# Model
input_shape = (BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, CHANNELS)
n_classes = 2

model = models.Sequential([
    resize_and_rescale,
    
    layers.Conv2D(32, kernel_size = (3,3), activation='relu', input_shape=input_shape),
    layers.MaxPooling2D((2, 2)),
    
    layers.Conv2D(64,  kernel_size = (3,3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    
    layers.Conv2D(64,  kernel_size = (3,3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    
    layers.Conv2D(64, (3, 3), activation='relu'),
    layers.MaxPooling2D((2, 2)),
    
    layers.Flatten(),
    
    layers.Dense(64, activation='relu'),
    layers.Dense(n_classes, activation='softmax'),
])

model.build(input_shape=input_shape)
# Function used in flask for deploying
def prediction():
    img = request.files['img']
    img.save("img.jpg")
    image = Image.open("img.jpg")
    x = np.array(image.resize((256,256)))
    # x = x.reshape(1,256,256,3)
    x = np.expand_dims(x,axis=0)
    res = (model.predict_on_batch(x))
    classification = np.where(res == np.amax(res))[1][0]
    # a=names(classification)
    a=str(res[0][classification]*100) + '% Confidence ' + names(classification)

    return render_template("prediction.html", data=a)

【问题讨论】:

那么输入形状不包含批处理形状。 【参考方案1】:

您应该将input_shape = (BATCH_SIZE, IMAGE_SIZE, IMAGE_SIZE, CHANNELS) 更改为input_shape = (IMAGE_SIZE, IMAGE_SIZE, CHANNELS),因为批量大小参数是自动推断的。

【讨论】:

如果我按照您所说的那样更改代码,那么我是否需要执行 np.expand_dims(IMG,axis=0) 。因为当我删除 Batch_size 时,input_shape 只有 3 个参数。但是您说批处理参数是自动推断的。那么这是否意味着 input_shape 仍然会有 4 个参数? 是的,推断的大小将是(无、高度、宽度、通道数)。是的,当你预测单张照片时,你需要 np.expand_dims(img,axis=0)

以上是关于ValueError:conv2d 层的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3。收到的完整形状:(256、256、256)的主要内容,如果未能解决你的问题,请参考以下文章

conv2d 层的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 1,但接收到形状为 [None, 64, 64, 3] 的输入

model.fit 给出 ValueError :检查输入时出错:预期的 conv2d 得到了形状为 () 的数组

ValueError:“顺序”层的输入 0 与该层不兼容:预期形状 =(无,33714,12),找到形状 =(无,12)

ValueError:lstm_45 层的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。收到的完整形状:(无,无,无,128)

Keras ValueError: Input 0 is in compatible with layer conv2d_1: expected ndim=4, found ndim=5

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array w