[英]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.