Simple Image Classifier with keras

T Hansda
3 min readJun 17, 2019

A classifier, as the name suggests it classifies things, let it be pictures or any other type of data. In this article I’ll develop a image classifier step by step that will classify images of handwritten digits(0–9). I’ll use keras (with tensorflow backend)

Import the dataset

Keras comes with few datasets. we are going to use mnist dataset, it has 60000 training images and 10000 test images or handwritten digits(0–9). First we will load the dataset and normalize the images array values to be feed into the keras model.

The images are actually 28x28 arrays. we need to turn the array elements into floats and normalise the values from 0 to 1 because if we don’t do that the activation values will get too big and the weights may diverge destroying the whole model.

Visualise

Lets see some of the images from the dataset

If you want to visualise data from test images then just replace x_train and y_train with x_test and y_test respectively.

The model

Now we need to build our model. There are two ways to build a model, one is to use Sequential function and other is Functional API. I don’t like to use sequential because after a while when we start to build complex model then we can’t use Sequential function. So it’s a good idea to start with Functional API at the first place.

Here the numbers are arbitrary, you can add as much neurons and layers as you want. The more layers or neurons you add the model becomes more capable but more neurons need more processing power. More neurons introduce the problem of overfitting, that means the model starts to memories things rather than understanding, so it will perform very good on the training set but will fail in tests. On the other hand taking very little number of neurons introduces the problem of uderfitting which means the model is not capable enough to understand the given dataset.

I used adam optimizer with default learning rate and categorical_crossentropy as the loss function.

Note: The last layer must have 10 neurons as every neuron represents a number from 0 to 9.

Training the model

Before we can actually train our model we need to process out data a little. We will turn that 28x28 image into a 1D array with 784 elements. We get the output labels as numbers from the dataset, we need to convert the numbers into 10 digit binary class

0 becomes [1,0,0,0,0,0,0,0,0,0]
1 becomes [0,1,0,0,0,0,0,0,0,0]

9 becomes [0,0,0,0,0,0,0,0,0,1]
Now you get to know why we needed 10 neurons on the output layer.

Now we can train our classifier.

I was able to get 98% accuracy with training of 50 epochs.

Testing

let’s see, our model got an accuracy of 98% and there is 10000 test images, so there are around 200 picture that the model could not classify correctly. First we will show the first 10 images of the test images, then will see the first 10 wrongly predicted images.

First 10 pictures from the test images

Now visualise the first 10 wrongly predicted pictures.

10 pictures that the classifier could not predict correctly

Let’s be honest, even humans will predict some of the pictures wrong here. Take the 2nd last picture, is that even a digit ?

Conclusion

Here it is, a fully functional and quite good image classifier. I’ll write an article about another classifier, where we will build out own dataset and train the model on it, so stay tuned.

I’m not an AI expert, I’m just an engineering student who is interested in AI stuff. This article is to help some other people and to reinforce my own understanding. If you have any question or suggestion , feel free to ask.

--

--

T Hansda

An engineering student, Interested about machine learning, electronics and computers