我正在尝试在Flask上使用Keras进行图像识别。在进行预测时,我遇到此错误
ValueError: Input 0 of layer sequential_16 is incompatible with the layer: expected axis -1 of input shape to have value 24 but received input with shape [None, 150, 150, 3]
我有点理解问题,但是我不确定如何指定形状。这是在Flask服务器上,我在这里不做任何培训。我使用以前在Jupyter笔记本电脑上训练过的模型。
这是代码
def predict(img):
# Preprocess input image
img_width, img_height = 150, 150
x = load_img(img, target_size=(img_width, img_height))
x = img_to_array(x)
x = np.expand_dims(x, axis=0)
# Load model
dependencies = {
'precision': Precision,
'recall': Recall
}
model = load_model('model.h5', custom_objects=dependencies)
# Predict
result = model.predict(x)[0]
label = np.argmax(result)
return label
回溯表明它发生在result = model.predict(x)[0]
上。有人知道如何解决此错误吗?尝试使用谷歌搜索,但我没有发现任何类似的错误。
编辑-模型摘要
Model: "sequential_16"
Layer (type) Output Shape Param #
=================================================================
dense_96 (Dense) (None, 32) 800
_________________________________________________________________
dense_97 (Dense) (None, 1024) 33792
_________________________________________________________________
dense_98 (Dense) (None, 512) 524800
_________________________________________________________________
dense_99 (Dense) (None, 256) 131328
_________________________________________________________________
dense_100 (Dense) (None, 128) 32896
_________________________________________________________________
dense_101 (Dense) (None, 3) 387
_________________________________________________________________
activation_16 (Activation) (None, 3) 0
=================================================================
Total params: 724,003
Trainable params: 724,003
Non-trainable params: 0
答案 0 :(得分:0)
好,所以您输入的形状错误。输入层未在模型摘要中显示,但表示第一层具有800个参数。
这告诉我输入层的尺寸为[None,24],因为24 * 32(权重)+ 32(偏置)= 800。
当您添加第一个致密层时,应该是
model.add(密集(32,input_shape =(150,150,3))