Search code examples
pythontensorflowkerasprediction

Error while predicting classes of an trained DNN model


I used the following program to predict classes for my image.

from tensorflow.keras.preprocessing.image import load_img, img_to_array

x = load_img("8-SignLanguageMNIST/test1.jpg", target_size = (28, 28))

x = img_to_array(x)

x = np.expand_dims(x, axis = 0)

x = np.vstack([x])

classes = model.predict(x)
print(classes[0])

The Images I used for training are of shape (28, 28, 1).

Here I am uploading an RGB image which is of shape (28, 28, 3), I tried converting that image to grayscale and then predicting but kept getting the following error.

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 1 but received input with shape [None, 28, 28, 3]

Can anyone tell me what I am doing wrong, and help me out with that.


Solution

  • you need to apply a conversion to grayscale like below:

    load_img(path, color_mode='grayscale')