Skip to content
Advertisement

MSINT – Image classification – value error incompatible shape

I am beginning with image classification using keras. Tried a simple minst dataset for detecting numbers in images. Ran the model. However I wanted to test the model on my own dataset and facing some problem.

    import tensorflow as tf
    import matplotlib.pyplot as plt
    
    msint = tf.keras.datasets.mnist #28x28 images of hand written digits 0-9
    (x_train, y_train), (x_test,y_test) = msint.load_data()
    
    
    x_train = tf.keras.utils.normalize(x_train,axis=1)
    x_test = tf.keras.utils.normalize(x_test,axis=1)
    
    model = tf.keras.models.Sequential()
    model.add(tf.keras.layers.Flatten())
    model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(128, activation=tf.nn.relu))
    model.add(tf.keras.layers.Dense(10, activation=tf.nn.softmax))
    model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
    model.fit(x_train,y_train, epochs=3)

#Testing on my own image data
img2 = cv2.imread("981_cropped.jpg",cv2.IMREAD_GRAYSCALE)
(thresh, blackAndWhiteImage) = cv2.threshold(img2, 128, 255, cv2.THRESH_BINARY)

z_predict = tf.keras.utils.normalize(blackAndWhiteImage,axis=1)
predictions = new_model.predict([z_predict])

Error:

WARNING:tensorflow:Model was constructed with shape (None, 28, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28, 28), dtype=tf.float32, name=’flatten_input’), name=’flatten_input’, description=”created by layer ‘flatten_input'”), but it was called on an input with incompatible shape (None, 28).

ValueError: Input 0 of layer dense is incompatible with the layer: expected axis -1 of input shape to have value 784 but received input with shape (None, 28)

Advertisement

Answer

You should resize the image before feeding it to your network. The model expects an image with shape (28,28).

  • Resize the image like this: img2 = cv2.resize(img2, (28, 28 ))

  • As the model expects a batch dimension, you should add another dimension to your image like this: z_predict = tf.expand_dims(z_predict,axis=0)

  • Notice that predictions will be probabilities of each class. If you want to get the class number of prediction, you can use of np.argmax(prediction).

The modified code should be like this:

import cv2
img2 = cv2.imread("981_cropped.jpg",cv2.IMREAD_GRAYSCALE)
img2 = cv2.resize(img2, (28, 28 ))      #resize image
(thresh, blackAndWhiteImage) = cv2.threshold(img2, 128, 255, cv2.THRESH_BINARY)

z_predict = tf.keras.utils.normalize(blackAndWhiteImage,axis=1)

z_predict = tf.expand_dims(z_predict,axis=0)  # add batch dimension
predictions = model.predict(z_predict)
np.argmax(predictions)  # get predicted class
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement