Don’t know the breed of your dog? ML can help!

Learning how to build a production-ready software that can classify breeds of dog images

Have you ever wondered the breed of your dog or any other dog you may see on the street? You probably would have to ask a specialist about that. Well, no more! In this post, I will teach you to build your own web application that will accept the image of your dog and output its breed with more than 80% accuracy!

What’s this dog’s breed? Photo by Alex Dolle from Pexels

You might be wondering how’s that possible? Well, we will be using Machine learning, specifically Deep Learning to train a model on a dataset of dog images with their breeds in order to learn the features that distinguish each breed. Let’s start then

Data Analysis

The dataset can be downloaded from here. After successfully loading and exploring the dataset, the following were some of the insights about the data:

  1. Total categories of Dog Breeds: 133
  2. Total Dog Images: 8351 (Train: 6680, Valid: 835, Test: 836)
  3. Most popular breeds: Alaskan_malamute:96 , Border_collie: 93

The top 30 breeds ordered by the number of images are shown here:

We can also see some of the dog images with their breeds here:

Data Pre-Processing

After analysis, comes the part to get the data ready for machine learning algorithms. We will load each image as a numpy array and resize them to be of size 224x224 as it is the default size that most conventional neural networks accept image in. We will also add another dimension for the number of images

Preprocessing Images for CNNs

Lastly, we will use ImageDataGenerator to apply re-scaling and augmentation on the fly to the images

CNN from Scratch

We will start by training a Convolutional Neural Network (CNN) from scratch on the pre-processed dataset as follows:

We are using a callback ModelCheckpoint to save just the model based on validation score. Testing this model, we get an accuracy of just around 1%

Using Transfer Learning

Now we will see how using pre-trained features can make a huge difference. Download the ResNet-50 bottleneck features. You can extract the respective train, test and validation sets by running the code cell below:

We will define the model again now and use GlobalAveragePooling2D on the extracted features which average an array of features into a single value. Lastly, we use additional callbacks for reducing learning rate, ReduceLROnPlateau , if the validation loss isn’t increasing in 2 successive epochs and also to stop the training early, EarlyStopping , if the validation loss doesn’t increase for 5 successive epochs.

The accuracy now on the test set is 82.65%. That’s a huge improvement from the model we trained from scratch.

Building the web app

For the web app, we will first write a helper function which accepts the image path and returns the breed. label_to_cat dictionary maps each numerical label to its dog breed.

For the web app, we will use Flask web framework which helps us create web applications with minimal code. We will define a route which accepts the image and renders an output template with the dog’s breed

predict.html is the template which shows the image and its dog breed respectively.


Congratulations! You have successfully implemented a Dog breed classifier and can tell a dog’s breed with confidence. Let’s conclude what we learned here:

  1. We analyzed and pre-processed the dataset. Separate train, test and valid sets are necessary for a machine learning algorithm to predict with confidence.
  2. We used a CNN from scratch that performed poorly as it failed to extract the features.
  3. We then used transfer learning and the accuracy increased a lot
  4. Finally, we built a Flask web app to make our project production ready

We surely learned a lot but there are a lot of other things that you can try. You can deploy the web app on heroku or try to improve the accuracy using different layers such as Dropout layers.

For more information and detailed analysis, make sure to check out the code available on my GitHub here.

Software Engineer who loves Data Science and building products related to data. Connect with me on LinkedIn here:

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store