Build an Image Classification Model using Convolutional Neural Networks in PyTorch

  Implementing CNNs using PyTorch We will use a very simple CNN architecture with just 2 convolutional layers to extract features from the images.

We’ll then use a fully connected dense layer to classify those features into their respective categories.

Let’s define the architecture: View the code on Gist.

Let’s now call this model, and define the optimizer and the loss function for the model: View the code on Gist.

This is the architecture of the model.

We have two Conv2d layers and a Linear layer.

Next, we will define a function to train the model: View the code on Gist.

Finally, we will train the model for 25 epochs and store the training and validation losses: View the code on Gist.

We can see that the validation loss is decreasing as the epochs are increasing.

Let’s visualize the training and validation losses by plotting them: View the code on Gist.

Ah, I love the power of visualization.

We can clearly see that the training and validation losses are in sync.

It is a good sign as the model is generalizing well on the validation set.

Let’s check the accuracy of the model on the training and validation set: View the code on Gist.

An accuracy of ~72% accuracy on the training set is pretty good.

Let’s check the accuracy for the validation set as well: View the code on Gist.

As we saw with the losses, the accuracy is also in sync here – we got ~72% on the validation set as well.

  Generating predictions for the test set It’s finally time to generate predictions for the test set.

We will load all the images in the test set, do the same pre-processing steps as we did for the training set and finally generate predictions.

So, let’s start by loading the test images: View the code on Gist.

Now, we will do the pre-processing steps on these images similar to what we did for the training images earlier: View the code on Gist.

Finally, we will generate predictions for the test set: View the code on Gist.

Replace the labels in the sample submission file with the predictions and finally save the file and submit it on the leaderboard: View the code on Gist.

View the code on Gist.

You will see a file named submission.

csv in your current directory.

You just have to upload it on the solution checker of the problem page which will generate the score.

Our CNN model gave us an accuracy of around 71% on the test set.

That is quite an improvement on the 65% we got using a simple neural network in our previous article.

  End Notes In this article, we looked at how CNNs can be useful for extracting features from images.

They helped us to improve the accuracy of our previous neural network model from 65% to 71% – a significant upgrade.

You can play around with the hyperparameters of the CNN model and try to improve accuracy even further.

Some of the hyperparameters to tune can be the number of convolutional layers, number of filters in each convolutional layer, number of epochs, number of dense layers, number of hidden units in each dense layer, etc.

In the next article of this series, we will learn how to use pre-trained models like VGG-16 and model checkpointing steps in PyTorch.

And as always, if you have any doubts related to this article, feel free to post them in the comments section below!.You can also read this article on Analytics Vidhyas Android APP Share this:Click to share on LinkedIn (Opens in new window)Click to share on Facebook (Opens in new window)Click to share on Twitter (Opens in new window)Click to share on Pocket (Opens in new window)Click to share on Reddit (Opens in new window) Related Articles (adsbygoogle = window.

adsbygoogle || []).

push({});.. More details

Leave a Reply