简体   繁体   English

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

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

I trained the CNN model and trying to test with a single image.我训练了 CNN model 并尝试使用单个图像进行测试。 I saved the.h5 file and tried to test with a single image.我保存了 .h5 文件并尝试使用单个图像进行测试。 But I got an error message as below.但我收到如下错误消息。

ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 48, 48, 1) ValueError:层序 1 的输入 0 与层不兼容:输入形状的预期轴 -1 具有值 3,但接收到的输入形状为 (None, 48, 48, 1)

Can anyone please help me with adjusting this input data to my model?谁能帮我调整这个输入数据到我的 model 吗?

Following is my model part:以下是我的 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))) ....

And I read the image and reshaped it as follow:我阅读了图像并将其重新塑造如下:

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

Finally, I put my reshaped image in my model like this:最后,我将重塑后的图像放入 model 中,如下所示:

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

How can I deal with this?我该如何处理?

You've trained a model with an RGB image (3 channel) but tried to do inference on Grayscale.您已经使用 RGB 图像(3 通道)训练了 model,但尝试在灰度上进行推理。 Try this尝试这个

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.

相关问题 如何使用经过训练的CNN张量流模型测试来自另一个.py文件的输入图像 - How can I use a trained CNN tensorflow model to test an input image from another .py file 如何预测 Keras CNN model 中的单个图像? - How can I predict single image in Keras CNN model? 如何使用现有CNN模型中的预训练权重在Keras中进行迁移学习? - How can I use pre-trained weights from an existing CNN model for transfer learning in Keras? 在不调整大小的情况下使用预训练的 CNN 时如何使用更大的输入图像? - How can I use larger input images when using a pre-trained CNN without resizing? 如何导入经过训练的模型来预测单个图像? - How to import a trained model to predict a single image? Tensorflow:如何从预先训练的CNN的特定层提取图像特征? - Tensorflow: How can I extract image features from a specific layer of a pre-trained CNN? 如何在Keras中使用训练模型预测输入图像? - How to predict input image with trained model in Keras? 我如何使用经过mnist训练的模型来预测图像 - How can i use my mnist trained model to predict an image 如何提高我的 cnn 训练的 model 的性能 - How to improve perfomance on my cnn trained model 如何使用经过训练的 CNN 对新图像进行分类? - How to classify a new image with a trained CNN?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM