Reputation: 163
I have done this basic project with Anaconda notebook. Everything runs fine, but every prediction with my own digit picture is wrong. I am using the MNIST Set for digital numbers and I am trying to paint my own digit with black background and white painting. But every prediction is wrong. Could one see what´s missing in the code?
enter code here
# Install TensorFlow
import tensorflow as tf
# Import matplotlib library
import matplotlib.pyplot as plt
#Import numpy
import numpy as np
#import cv
import cv2
#Dataset
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(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10, activation='softmax')])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train, epochs=5)
print("Evaluierung");
model.evaluate(x_test, y_test)
plt.imshow(x_train[1], cmap="gray") # Import the image
plt.show() # Plot the image
predictions = model.predict([x_train]) # Make prediction, works perfect
print(np.argmax(predictions[1])) # Print out the number, works perfect
# my own picture, black background, white color, 28 px * 28 px in size
img = cv2.imread("bild1.png", cv2.IMREAD_GRAYSCALE)
plt.imshow(img) # Import the image
plt.show() # Plot the image
cv2.imshow("image",img)
cv2.waitKey(2000)
img = img/255.0
img = img.reshape(1,28,28)
pred = model.predict_classes(img)
print("Prediction: ", pred)
Every prediction from the training and test data are correct, my own pictures are ALL wrong with NO error code! It would be great if you could help me
Upvotes: 1
Views: 292
Reputation: 163
Thanks very much for your answer
I changed the code for normalizing like you told me, but it didn´t change anything.
Then I looked in Google for some handwritten digits (outside the mnist database) with black background, I resized it and took them for predict -> to my surprise every pic is recognized correctly!! So the problem I think is not my code but the way I paint my own handwritten digits. I make it with photoshop, new gray-scale template, black background, white painting und resizing it to 28 * 28 pixels. But my own painting is not recognized correcty - no error code, but simply a wrong prediction. Do you have an idea what is wrong with my painting ... ?? Thanks again!!
Upvotes: 0
Reputation: 11333
There's nothing particularly wrong with the way you are doing this. However I can give few pointers to why you are getting these results.
Sometimes when you load the images using image loading libraries, they will have values clipped as opposed to being between exactly 0-255. Therefore, when normalizing, instead use something like,
img = img - np.min(img)
img = img/np.max(img)
Training model on the raw 60000 samples and expecting it to work on real-world data ... well that's not going to work. For example, you will have the following differences compared to the data your model was trained on.
So, if you want the model to perform better, use data augmentation
Upvotes: 1