【问题标题】:How can I input single Image in CNN trained model?如何在 CNN 训练模型中输入单个图像?
【发布时间】:2021-07-19 10:14:13
【问题描述】:

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

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

谁能帮我调整这个输入数据到我的模型?

以下是我的模型部分:

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

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

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

我该如何处理?

【问题讨论】:

  • 因为您的输入形状是 (48, 48, 3) 但是您输入的是 (48, 48, 1) 图像大小。

标签: python tensorflow image-processing deep-learning conv-neural-network


【解决方案1】:

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

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)

【讨论】:

  • 您遇到过什么问题吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多