Reputation: 33
I tried to test my trained model with an input image to predict but it was always inaccurate.
from __future__ import absolute_import, division, print_function
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(100, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.10),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=1)
model.evaluate(x_test, y_test)
import numpy as np
from keras.preprocessing import image
import PIL
test_image = image.load_img('Number 8_resized.jpg', target_size=(28, 28))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis=0)
result = model.predict(test_image.reshape(-1, 28, 28))
print("The predicited number is:")
print(np.argmax(result[0]))
Do I need to pre-process the above code? If yes how would I do it? Thanks.
Upvotes: 0
Views: 132
Reputation: 8709
Yes, your test image also needs to be pre-processed, same way as you preprocessed your training image data. You are getting wrong prediction because your model is very basic.
Try this code for prediction:
import numpy as np
from keras.preprocessing import image
import PIL
test_image = image.load_img('Number 8_resized.jpg', target_size=(28, 28))
test_image = image.img_to_array(test_image)
test_image /= 255.
result = model.predict(test_image.reshape(-1, 28, 28))
print("The predicited number is:")
print(np.argmax(result[0]))
Full Updated Code:
from __future__ import absolute_import, division, print_function
import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train),(x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(100, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.10),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(x_train, y_train, epochs=1)
model.evaluate(x_test, y_test)
import numpy as np
from keras.preprocessing import image
#import PIL
test_image = image.load_img('zero.png', target_size=(28, 28))
test_image = image.img_to_array(test_image)
# test_image = np.expand_dims(test_image, axis=0)
# print(test_image.shape)
test_image /= 255.
result = model.predict(test_image.reshape(-1, 28, 28))
print("The predicited number is:")
print(np.argmax(result[0]))
If you want to improve accuracy on test data, check out this tutorial and also don't forget to check this kaggle post on mnist digit recognizer accuracy.
Upvotes: 0
Reputation: 56377
Yes, you need to pre-process any test image in exactly the same way you pre-processed and normalized the training set. You are probably missing a division by 255.
Upvotes: 1