Classifying Dog Breed using PyTorch

Ankit Vashisht
4 min readJun 28, 2020

--

dog images
Photo by Alecsander Alves on Unsplash

In this article, We will be building a dog breed classifier using various pre-trained models in PyTorch library which will predict dog breed from the given image.

About PyTorch

PyTorch is an open source machine learning library based on the Torch library, used for applications such as computer vision and natural language processing, primarily developed by Facebook’s AI Research lab. It is free and open-source software released under the Modified BSD license.

Dataset Exploration

Dataset Description

For this project, I have chosen Stanford Dog Breed dataset which contains images of 120 breeds of dogs from around the world. The dataset contains 20,580 images in total.The dataset has been built using images and annotation from ImageNet for the task of fine-grained image categorization. It was originally collected for fine-grain image categorization, a challenging problem as certain dog breeds have near identical features or differ in colour and age.

Necessary Imports

First we’ll make necessary imports which we’ll going to use in this project.

In order to work with PyTorch, the data should be converted in PyTorch dataset format. As all the images of different dog breeds are in their respective folders, we’ll use ImageFolder to load the data to PyTorch Dataset Format.

There are total 120 classes in our dataset. I will use the following code to rename the classes name in little bit nice format.

Training , Validation and Testing dataset

Now we’ll split our dataset into Training, Validation and Testing dataset.

Creating Custom Dataset Class

As the images in our dataset are still in PIL format and we need to apply more transformations for data augmentation ( to prevent overfitting ), we’ll create our custom Dataset class which inherits PyTorch Dataset.

To create custom Dataset Class, we need to implement 3 methods inside our Custom Dataset Class:

  1. __init__()
  2. __len__()
  3. __getitem__()

Quick Look at data

Let’s have a quick look at our data

Creating DataLoaders

In PyTorch, DataLoaders are the easiest way to create batches of data. We’ll create Training, validation and testing dataloaders with their respective batch sizes.

Now we’ll use the make_grid function that will visualize data in one batch.

Different Network Architectures

Now we’ll define different network architectures including one custom and many pre-trained models. In total we’ll define 7 models and analyze their performance. Following are the models we’ll define :

  1. Custom CNN
  2. ResNet34
  3. ResNet152
  4. VGG16
  5. GoogleNet
  6. DenseNet
  7. Wide ResNet

For more details about the implementation of models, check out here : https://jovian.ml/ankitvashisht12/dog-breed-classifier-final

Training

The following step is to train the model for it to learn the proper weights for the added layer and also, the pretrained weights can be adjusted to our task. The code chunk below shows how to implement training and evaluation. I have used train_one_cycle instead of using a fixed learning rate, uses a learning rate scheduler, which will change the learning rate after every batch of training.

We can fire up the training using the following code :

Let’s look at loss and accuracy curves

Plot 1 : Loss V/s No. of Epochs
Plot 2 : Accuracy v/s No. of Epochs
Plot 3 : Learning rate v/s No. of epochs

Prediction

Some predictions on images from test dataset :

Let’s call evaluate on test dataloader

As we can see, the model is giving 85.56% on test dataset which is quite nice.

Results

Below is the comparison table of different implemented models.

Comparison Table

From the table above, we can conclude that the approaches based on transfer learning reached higher performances than simpler ones. This demonstrates, once more, the importance of building models on top of pretrained architectures in visual recognition, especially when our dataset is relatively small.

For more details refer : https://jovian.ml/ankitvashisht12/dog-breed-classifier-final

As we can see that nowadays it’s very easy to build end to end deep learning algorithms with the help of pre-trained models using tecniques like Transfer learning and libraries like PyTorch.

References

--

--