Implementing different CNN Architectures on Plant Seedlings Classification dataset — Part 2 (AlexNet)
Tags: Convolutional Network, Alexnet, Deep Learning

In my previous article, I had worked on the Plant Seedling dataset as it was a competition dataset where we could submit our predictions and check our scores. This was a good way to test our models. We implemented LeNet architecture but were only able to get a score of 29.77%. For a start, it was good, but we definitely need to improve our model performance. In order to achieve this, I want to implement some CNN architectures like Alexnet, VGG, Inceptionv3 and Xception. Since, it is very important that we understand the theory and the practical implementation as well. The aim of this series of articles is for us, as beginners in Computer Vision, to come up with the best strategy of working on Image Classification problems so we will gradually see the code become more complex as we progress.

I was barely able to train AlexNet from scratch but for the rest of architectures (VGG, Inception, Xception), I had to use pre-trained models. We will go through in detail, what pre-trained models mean and how it is beneficial for us.

I will not be going through an in-depth explanation of these architectures but rather, a simple introduction, followed by the practical code.

So, let’s start with AlexNet:

AlexNet: It was primarily designed by Alex Krizhevsky. It was published with Ilya Sutskever and Krizhevsky’s doctoral advisor Geoffrey Hinton, and is a Convolutional Neural Network or CNN.

After competing in ImageNet Large Scale Visual Recognition Challenge, AlexNet shot to fame. It achieved a top-5 error of 15.3%. This was 10.8% lower than that of runner up.

The primary result of the original paper was that the depth of the model was absolutely required for its high performance. This was quite expensive computationally but was made feasible due to GPUs or Graphical Processing Units, during training.

We will now try to implement the architecture from scratch. I will follow the same steps that I followed last time.

I first downloaded the data, which was a zip file and then extracted the content. I saw that there were three folders: ‘train’, ‘test’ and ‘sample submission.csv’.

Plant Seedling Data

I then checked the ‘train’ data folder and saw that there were 12 different classes.

Different Classes inside Plant Seedlings Data

I manually checked to see the different items in every folder and saw that some classes had more images as compared to the other.

This was alarming since I knew that the model would get biased to the images which are more in number.

In order to fix that, I created a new train folder where, I copied 200 images from every class.

I then created a validation set which had 20 images from every class. This way, I was preventing the unnecessary bias that would creep inside the model.

I first imported all the packages. I am using Tensorflow-gpu 1.15 and Keras 2.3.1

Import all the packages

I then use the Image Data Generator to create my train and validation set. This will take care of all the pre-processing steps required for the data to be trained and the labels. This includes normalization, adding some shear and zoom range to create certain level of data augmentation.

Using the Image data generator to create the train and validation set.

After the data has been setup, I then create the AlexNet Model:

It consists of five convolution layers with max pooling and batch normalization layers in between, followed by three fully connected layers.

Convolution layers of AlexNet

Fully Connected layers of AlexNet

I then use ‘ SGD ’ as my optimizer with a learning rate and a decay.

’ as my optimizer with a learning rate and a decay. Since, there are 12 classes, I use, ‘Categorical_Crossentropy’, as my loss.

Initializing the Learning rate, setting up the decay and optimizer

I then train the model for ‘15’ epochs with the training steps as ‘500’ and validation steps as ‘300’.

Model training with loss and accuracy

Based on the above image, we can confirm that our training accuracy goes above steadily increases to 81% and our validation accuracy also goes to 80%

This is good, this means that our model is not overfitting.

After saving the model, I use it on my test set and I generate a sample_submission.csv file which contains the ID and the appropriate class.

Saving the Model to Disk and loading the model for predictions

Loading the model and using it for prediction

Based on the above image, I use the model to start making predictions on the test set.

I use the glob library to get a list of all the files and then iterate over them, I append the name of the images to an empty list called ‘IDs’.

I then load the image, convert it to an array and then use ‘expand_dims’ to flatten the array.

I then use my model to do the predictions, which comes in the form of an array. For ex: [[0,0,0,1,0,0,0,0,0,0,0,0]]

I use the ‘If-Else’ conditions to get the various classes and append them to an empty list called ‘Preds’.

I now use the ‘Ids’ and ‘Preds’ to create my ‘sample_submission.csv’ file.

We now need to upload this file to the ‘Plant Seedlings Classification’ site to make the predictions.

In order to do this, go to the Plant Seedlings Classification and click on ‘Late Submission’.

Late Submission tab

When you click on ‘Late Submission’, you will get an option to upload your file.

Uploading the file to get a score

Once the file is uploaded, click on ‘Make Submission’, to get the score.

Uploading the file and making the submissions

So, I got a score of around 12.8%, which means, I was only able to predict 12 images correctly out of a 100.

Classification Score for the Predictions