I've created a model (LeNet-5), which is giving pretty good accuracy(98%).
Then I tried to see how will it perform on my hand written data(Obviously from different distribution, just curious). So I took the picture of a 5
and converted it to gray-scale using PIL
and saw what it predicts. It didn't perform well.
Code to convert to gray-scale:
# Open the file
im = Image.open(path)
# Resize the image
im_resized = im.resize(size)
# Convert to grayscale
gr = ImageOps.grayscale(im_resized)
And It also didn't perform well on some other images from Internet. Then I got suspicious from the numbers.
MNIST: background is black and number in white
my image: background is white and number in black
So I wanted to see the images of MNIST. But all I get is some white dots. No meaningful image at all.
Here is the code snippet to see the image:
from mnist import MNIST
mndata = MNIST(mndir)
train_images, train_labels = mndata.load_training()
test_images, test_labels = mndata.load_testing()
ar = np.array(test_images[10], np.int32)
ar = np.resize(ar, (28, 28))
im = Image.fromarray(ar, 'L')
im.show()
For this I get something like:
Edit:
When I tested it again, I came to know the PIL
library requires uint8
type to show the image. For more info: PIL-produces-black-image.
To make the above code work, use
ar = np.resize(ar, (28, 28)).astype(np.uint8)