I'm trying to use keras neural network of tensorflow to recognize the handwriting digit number. But idk why when i call predict()
, it returns same results for all of input images.
Here is code:
### Train dataset ###
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train/255
x_test = x_test/255
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(units=128,activation=tf.nn.relu))
model.add(tf.keras.layers.Dense(units=10,activation=tf.nn.softmax))
model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
model.fit(x_train, y_train, epochs=5)
The result looks like this:
Epoch 1/5
1875/1875 [==============================] - 2s 672us/step - loss: 0.2620 - accuracy: 0.9248
Epoch 2/5
1875/1875 [==============================] - 1s 567us/step - loss: 0.1148 - accuracy: 0.9658
Epoch 3/5
1875/1875 [==============================] - 1s 559us/step - loss: 0.0784 - accuracy: 0.9764
Epoch 4/5
1875/1875 [==============================] - 1s 564us/step - loss: 0.0596 - accuracy: 0.9817
Epoch 5/5
1875/1875 [==============================] - 1s 567us/step - loss: 0.0462 - accuracy: 0.9859
Then the code to use image to test is below:
img = cv.imread('path/to/1.png')
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img = cv.resize(img,(28,28))
img = np.array([img])
if cv.countNonZero((255-image)) == 0:
print('')
img = np.invert(img)
plt.imshow(img[0])
plt.show()
prediction = model.predict(img)
result = np.argmax(prediction)
print(prediction)
print(f'Result: {result}')
The result is:
[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]
Result: 3
[[0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]]
Result: 3