繁体   English   中英

如何在 CNN 训练的 model 中输入单个图像?

[英]How can I input single Image in CNN trained model?

我训练了 CNN model 并尝试使用单个图像进行测试。 我保存了 .h5 文件并尝试使用单个图像进行测试。 但我收到如下错误消息。

ValueError:层序 1 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入形状为 (None, 48, 48, 1)

谁能帮我调整这个输入数据到我的 model 吗?

以下是我的 model 零件:

def create_model(x=None):
    # we initialize the model
    model = Sequential()

    # Conv Block 1
    model.add(Conv2D(64, (3, 3), input_shape=(48,48,3),  padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3),   padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(Conv2D(64, (3, 3),  padding='same'))
    model.add(BatchNormalization())
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) ....

我阅读了图像并将其重新塑造如下:

face_image = cv2.resize(face_image, (48,48))
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2GRAY)
face_image = np.reshape(face_image, [1, face_image.shape[0], face_image.shape[1], 1])

最后,我将重塑后的图像放入 model 中,如下所示:

predicted_class = np.argmax(model.predict(face_image))

我该如何处理?

您已经使用 RGB 图像(3 通道)训练了 model,但尝试在灰度上进行推理。 尝试这个

face_image = cv2.resize(face_image, (48,48))
face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
face_image = np.reshape(face_image, [1, face_image.shape[0], face_image.shape[1], 3])
predicted_class = np.argmax(model.predict(face_image), -1)

暂无
暂无

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

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