Dog Breed Classifier that does not “Bark Up The Wrong Tree”

Jayalaxmi Ganihar
6 min readMar 12, 2021

Project Overview

The aim of this project is to use a picture to establish the dog’s breed. The machine would first determine if the picture is of a dog or a human face. It will predict the dog’s breed in the case of a dog. In the case of a human, it will predict the dog breed that most closely resembles the human. This project was created as a capstone project for the Udacity Data Scientist Nanodegree.

Solution Strategy

The steps that were followed to work through the project were the following:

  • Step 0: Import Datasets
  • Step 1: Detect Humans
  • Step 2: Detect Dogs
  • Step 3: Create a CNN to classify Dog Breeds (from scratch)
  • Step 4: Use a CNN to classify Dog Breeds (using Transfer Learning)
  • Step 5: Create a CNN to classify Dog Breeds (using Transfer Learning)
  • Step 6: Write an algorithm
  • Step 7: Test algorithm

Import Datasets

In this project, Convolutional Neural Networks and OpenCV were used. The model was trained using Udacity’s dataset, which includes 133 dog breed categories and 8351 images. Udacity also provided the Humans dataset, which included 13233 images. There are 133 different dog breed groups in the dataset, with a total of 8351 dog photos. There were 6680 images used for preparation, 835 images used for validation, and 836 images used for research.

As you can see, the dataset is well-balanced, with 50 photos per dog breed class on average. I chose accuracy as an assessment metric because this is a classification issue. Precision would have been an acceptable option if the dataset was excessively unbalanced.

Brittany(LHS) and Labrador_retriever (RHS)
Labrador_retriever(LHS) and Welsh_springer_spanieL (RHS)

The individual images were of different sizes and orientations, but most of them were in the range of 300–500 pixels in height and width. As a part of the pre-processing step, these images were resized to 224x224 pixels to fit the network architecture. The distribution of height and width is shown below.

Detect Humans

The Haar feature-based cascade classifier from OpenCV was used to determine whether or not the image was a human face. The first 100 photographs from the human and dog datasets were used to test this detector. On human images, it was 100 percent correct, but it misidentified 11 percent of dog images as human faces.

ResNet-50 model was trained on the well-known ImageNet dataset to detect dogs. The dog detector was tested on the same set of images, and it detected all dogs in the dog dataset with 100% accuracy, but none in the human face dataset.

Create a CNN to classify Dog Breeds (from scratch)

The first step is to build a CNN model. Training a CNN model that achieves good accuracy would take a long time and a lot of computing power. Since there are 133 output groups, a random selection is less than 1% accurate. So, something that is better than random is a win. In creating the CNN architecture, a few convolutional filters of increasing size (8, 16, 32), as is common in CNN construction were used.GlobalAveragePooling was used to reduce spatial dimensions

Use a CNN to classify Dog Breeds (using Transfer Learning)

The model can be improved by adding more layers and training for a greater number of epochs but alternatively transfer learning can be used. Information learned from one problem can be used to solve a separate but related problem through transfer learning. The layers from the pre-trained models that lead to feature extraction are retained and only the final classification layers are trained after the extraction of features.

Create a CNN to classify Dog Breeds (using Transfer Learning)

First, use the VGG16 model’s bottleneck functionality, then add just the global average pooling and dense layers at the end. After only 20 epochs of training and only a few seconds per epoch, test accuracy of 40 percent was achieved. The validation accuracy improved by increasing epochs.

Write an algorithm

Bottleneck features from the InceptionV3 model were used to train another classifier. inception bottleneck features are inherently designed for image classification. The InceptionV3 is trained on ImageNet dataset consisting of 15 million images, but there are about 6000 images in our dataset. This difference in dataset sizes would make the model to overfit, so add a Dropout layer to reduce overfitting.

Test algorithm

The classifier had a test accuracy of 79%. The evaluation results are as follows

I think this is a human resembling the dog breed Dachshund.
I found a dog. The breed is Brittany.
I found a dog. The breed is Labrador_retriever.
I think this is a human resembling the dog breed Akita.
I think this is a human resembling the dog breed Norwegian_lundehund.
I think this is a human resembling the dog breed Bearded_collie.
I think this is a human resembling the dog breed Mastiff.
I found a dog. The breed is Pomeranian
I found a dog. The breed is Bulldog.
I think this is a human resembling the dog breed Canaan_dog.
I found a dog. The breed is Poodle.
I found a dog. The breed is Afghan_hound.

Conclusion

CNN model had a Test accuracy: 11.6029%

CNN with Transfer Learning had a Test accuracy: 40.0718%

InceptionV3 model had a Test accuracy: 79.6651%

The results are good, as planned after achieving a 79% accuracy for the proposed network.

Although the accuracy appears to be limited, our issue is not sensitive enough to be worried about. Using the transfer learning and data augmentation methods, we can achieve above 90% accuracy in this problem given enough time and effort.

Further, the algorithm can be enhanced by taking into account the following factors:

Improvements

By using a larger dataset to train the network, the chosen network would be able to relate more characteristics to the breeds. More epochs for network training. More epochs will most likely allow the network to enhance its ability to recognize dog breeds in pictures. Most likely by making the network’s architecture larger and more complex, but this would necessitate additional resources such as RAM memory and GPU processing.

Link to GitHub Repo:https://github.com/Jayalaxmi25/Dog-App

--

--