Seonghun Son
Seonghun Son

Reputation: 13

How can I input single Image in CNN trained model?

I trained the CNN model and trying to test with a single image. I saved the .h5 file and tried to test with a single image. 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)

Can anyone please help me with adjusting this input data to my model?

Following is my model part:

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 :

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

How can I deal with this?

Upvotes: 0

Views: 789

Answers (1)

Innat
Innat

Reputation: 17219

You've trained a model with an RGB image (3 channel) but tried to do inference on Grayscale. 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)

Upvotes: 1

Related Questions