【发布时间】: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)
【问题讨论】:
-
那么输入形状不包含批处理形状。
标签: tensorflow flask conv-neural-network reshape valueerror