Multi-Class Image Classification with implementation in TensorFlow
By: Aditya Gupta
Tags: Multiclass Classification, Cnn, Deep Learning, TensorFlow, Keras

Classification is a problem that requires the data to be separated into different categories based on the features extracted from the independent variables. When there are only two categories to be considered for categorization, then the problem is called Binary Classification. And when there are more than two categories, it is referred to as a Multi-Class Classification.

Note: Do not confuse Multi-Class Classification with Multi-Label Classification. The basic and the most crucial difference between the two is that, in Multi-Class Classification, we can have only a single category pointing to a single observation. On the other hand, there can be multiple categories associated with a single observation in Multi-Label Classification. For example, an animal can be a cat or a dog, not both. But, a movie can be adventurous and sci-fi at the same time.

Now, I hope you have understood the basic concept of Multi-Class Classification. So, let us move forward and look at the dataset on which we will be working further.

Dataset

We will be working on a Kaggle competition named Digit Recognizer. The dataset provided in this competition is popularly known as the MNIST dataset. It is about classifying the images into the digits represented by them i.e. from 0 to 9. The dataset consists of 42000 grayscale images of dimensions (28,28) for training the model and another 28000 images for evaluating the model. So let’s get started with the implementation.

Implementation

All the code written below can be found here.

First, we import all the required libraries that we will need in the future.

Import the libraries

Then we import the data. It is in 2 files: train.csv and test.csv.

load the data

The next step will be to separate the dependant variable from the independent variables and check for any class imbalance.

Separating dependant and independent features

Hopefully, our data doesn’t seem to have any class imbalance. Now, as the pixel values in our data are from 0 to 255, it is good to normalize the values in the range of 0 to 1. This will allow our model to converge faster. Then we will reshape the dimensions of our data in a format of [number of observations, height, width, channels] which will help us in feeding the input to the model.

Normalize and reshape the data

In addition to that, we will also need to encode the labels, as they are categorical.

Encode the categories

Finally, we can split the images and labels into training and test sets.

Split into train and test sets

Now, we have our training and test sets and now, all we need to have is a model to train our data on. But before that, let’s apply some data augmentation techniques to our train set. This will help to introduce variability in our dataset and hence, our model can generalize well on the data.

Applying data augmentation to train set

Now, we can proceed to the crux of our implementation i.e. our model. We will be having a relatively less number of layers as our data itself has dimensions of (28,28) only.

Defining a model

We will use categorical_crossentropy as the loss function for multi-class classification, RMSProp as an optimizer, and a callback to reduce the learning rate when our validation accuracy becomes constant.

Compiling the model

Finally, we will train the model for 30 epochs and a batch_size of 64.

Training the model

Then, we will use some Matplotlib to plot the accuracy and loss graphs.

Training and Validation Graphs

As a final and the last step of the implementation, we can make predictions on the test set provided in the competition and submit those to get the final leaderboard score of our model.

Make predictions for submission

The above results gave a leaderboard score of 0.99067 which means more than 99% accuracy. Now, we have made our multi-class classification model with an accuracy of more than 99% !!

Note: We have to take extra care while handling datasets where classes are imbalanced. One of the simple ways to handle this is to use a weighted loss function or upsampling or downsampling.

This is it for this blog. Feel free to give any feedback or suggestions in the comments section.

Thanks.