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)