I trained a Dense Neural Network with MNIST dataset in order to classify 28x28 images of numbers. Now I was trying to make it work with my own samples (I draw the image of a "7" in paint and I transformed it into an array) but the results are really poor.
from tensorflow.keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
from tensorflow.keras import models
from tensorflow.keras import layers
network = models.Sequential()
network.add(layers.Dense(512, activation='relu', input_shape=(28*28,)))
network.add(layers.Dense(10, activation='softmax'))
network.compile(optimizer='rmsprop',
loss='categorical_crossentropy',
metrics=['accuracy'])
train_images = train_images.reshape((60000,28*28))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28*28))
test_images = test_images.astype('float32') / 255
from tensorflow.keras.utils import to_categorical
train_labels = to_categorical(train_labels)
test_labels = to_categorical(test_labels)
network.fit(train_images,train_labels,epochs=20,batch_size=512,validation_split=0.2)
print(network.evaluate(test_images,test_labels))
#-DEMO-----------------------------------------------------------------
from PIL import Image
import PIL.ImageOps
import os
direccio = 'C:/Users/marcc/OneDrive/Escritorio'
os.chdir(direccio)
myImage = Image.open("Image.PNG").convert('L')
myImage = PIL.ImageOps.invert(myImage)
myImage = myImage.resize((28,28))
myImage.show()
#transforming my image into an array (THE PROBLEM MUST BE HERE)
import numpy as np
myImage_array = np.array(myImage)
myImage_array = myImage_array.reshape((28*28))
myImage_array = myImage_array.astype('float32') / 255
myImage_array=myImage_array.reshape(1,784)
print(myImage_array.shape)
print(network.predict(myImage_array))
The code until DEMO is made by François Chollet. I only made the last part which is the implementation of my own image.
The results that I get after testing it with the image of a seven are:
[[6.9165975e-03 3.0256975e-03 4.9591944e-01 4.8350231e-03 5.6093242e-03
8.6059235e-03 4.5295963e-01 8.3720963e-04 2.1008164e-02 2.8301307e-04]]
As you can see the results are really bad (the seventh position should have the highest probability)
If I plot an image of MNIST using the code:
digit = train_images[4]
import matplotlib.pyplot as plt
plt.imshow(digit, cmap=plt.cm.binary)
plt.show()
It looks like: MNIST image of a 9
If I do the same with my image: My Image of a 7 (after being transformed to an array)
CodePudding user response:
The result you are getting is the distribution on the probability your sample to belong in each class. if you see the result
[[6.9165975e-03 3.0256975e-03 4.9591944e-01 4.8350231e-03 5.6093242e-03
8.6059235e-03 4.5295963e-01 8.3720963e-04 2.1008164e-02 2.8301307e-04]]
you see that there are 10 probabilities your sample to belong in the first class (no 1) in the second class (no 2) etc etc
if you look carefully your output, you will see that the highest propability is in the 7th place so the model classifies your sample as a number 7
if you want your output to be the number of the class you can try something like this
CATEGORIES = ["1","2","3","4","5","6","7","8","9","0"]
prediction = model.predict('your_sample')
max = (prediction.max(0))
result = (np.where(prediction == max))
print(CATEGORIES[result])
CodePudding user response:
Everything is OK, the problem that I had is that in my region seven is normally written using another line (I really thought it was more extended). Because I am using a Dense Network, it doesn't interpretate the shapes of numbers but the disposition of the pixels so a little modification on how the number is written may have really bad consequences in this simplistic model.
As Αpostolos-Valiakos said I really had to try different numbers. But I really thought that it was a problem of how my image is converted to an array. Thanks to everyone for their help