Dog Breed Classifier Using Deep Learning

A Convolutional Neural Network project

Alex Yu
4 min readMay 9, 2020
Breed classification for the Rock

Do you ever wonder what kind of dog Dwayne “the Rock” Johnson resembles? Thanks to convolutional neural network, now you can find out! (My model says a silky terrier.)

Overview

Expected Output

This is a technical write-up for a machine learning project in the Udacity Data Scientist Nanodegree program. The goal of this project is to implement an algorithm that identifies the canine breed when given an image of a dog, and identifies the resembling dog breed when supplied with an image of a human. The accompanying IPython notebook can be found here. This write-up presumes the reader’s knowledge in implementing CNN models using Python’s Keras library.

To implement this algorithm, three classifiers are required: a dog detector, a human detector and a dog breed classifier. With the first two already provided in the template (dog detector using Keras’ Resnet50 model, human detector using Open CV’s cascade classifier), this project focuses on training a dog breed classifier model. A test on 100 images shows that the provided dog detector has a 100% sensitivity and a 100% specificity, and the human detector has a 100% sensitivity and a 89% specificity.

A total of 8351 dog images are provided with 133 breed categories. The images are split into 6680 test images, 835 validation images and 836 test images.

Model Training

Approach #1: Train from Scratch

The initial approach is to train the CNN model from scratch. The pipeline used can be seen here, and it is implemented with Keras with a Tensorflow backend. The loss function used is categorical cross-entropy.

CNN model for Approach #1

After 10 epochs, the accuracy reaches roughly 2.75%, better than random guessing (1/133) but far from ideal. Another run with 100 epochs (~30-minute runtime) returns a model with 15% accuracy.

Approach #2: Transfer Learning

To save time, a CNN model is trained using transfer learning. There are a couple of pre-trained image recognition models to choose from (e.g. VGG-19, Inception…). For my implementation, I chose Resnet-50, considering its proven ability to classify 118 dog breeds.

A file containing the pre-computed bottleneck features of Resnet-50 is prepared by Udacity. To setup the transfer learning model, I only have to add a final softmax layer with 133 outputs to the pre-trained model (see Step 5 in accompanying notebook).

After 20 epochs (1-minute runtime), the transfer learning model returns an 80% accuracy on the test set.

Result

Pseudo-code for the final algorithm can be seen below. The algorithm determines whether a human or a dog is in the image, and feeds the image to the breed predictor.

image_path = "image/sample.jpg"if dog_detector(image_path) == True:
print("This dog is of breed" + breed_predictor(image_path))
elif human_detector(image_path) == True:
print("This person resembles" + breed_predictor(image_path))
else:
print("Dog or human not found!")

The algorithm is tested with a series of test images. As the dog breed predictor achieved a 80% accuracy for 133 breeds, the model can often identify between two very similar breeds.

For the human face input, the algorithm outputs a dog breed that the person is most similar to according to the trained model.

However, since the human detector and dog detector are not perfect, the algorithm sometimes mistakes a cat for a human, or does not identify the dog in the image.

As the algorithm shows not just the most likely breed, but the probability of every bog breed based on the image, the user can see, for example, the top 5 most likely breeds predicted by the model, and decide if it’s justified. A brown Labrador, for instance, does look like a Chesapeake bay retriever.

Reflection and Future Improvement

We see that transfer learning dramatically improved the accuracy of the model and reduced runtime. In the accompany notebook, VGG-16 is also tested for transfer learning (see Step 4), and gives a lower accuracy of 38% after 20 epochs. Other models such as VGG-19 and Xception could also be tested to find the optimal model.

Additionally, techniques such as Augmentation can be implemented to improve the accuracy of the dog breed predictor to beyond 80%.

The template notebook I started with can be found here with instructions. Props to Udacity for this interesting project.

--

--