Dog Breed Classifier using CNN.

Original article was published on Deep Learning on Medium

Dog Breed Classifier using CNN.

Introduction

The problem of Image Classification rests as follows: Given a set of images that are all labeled with a particular class we are required to predict these classes for a new set of test images and measure the accuracy of the predictions. There is a diversity of complications correlated with this task, including angle variation, lighting conditions, image deformation, etc.
With these image classification difficulties associated, deep learning is able to make great strides on this task.

We’ll drive through creating a Convolutional Neural Network classifier using dog breed datasets and describe the steps that we developed to identify whether a picture has a human or a dog in it. In extension, in the case of identifying dogs in the image, it will tell us the breed of that dog with 81 % accuracy.

Methodology

The tracks that we developed to create our classifier:

  • 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 your Algorithm
  • Step 7: Test Your Algorithm

Import Datasets

The datasets contain 8351 dog images. I split them into train, validation, and test data. We are going to classify dogs into 133 distinct dog breeds by train the model using 6680 images, validate and fine-tuning the parameters using 835 images, and then test the model accuracy on 836 test images that model has not seen before.

Humans Detector

I used Haar cascade feature-based object classifier to detect human faces present in OpenCV. Initially, the algorithm needs a lot of positive images (images of faces) and negative images (images without faces) to train the classifier. Then we need to extract features from it. Haar features are just like our convolutional kernel. Each feature is a single value obtained by subtracting the sum of pixels under the white rectangle from the sum of pixels under the black rectangle.

Dog detector

I used a pre-trained ResNet-50 model to detect dogs in images. Our first line of code downloads the ResNet-50 model, along with weights that have been trained on ImageNet, a very large, very popular dataset used for image classification and other vision tasks. ImageNet contains over 10 million URLs, each linking to an image containing an object from one of 1000 categories. Given an image, this pre-trained ResNet-50 model returns a prediction for the object that is contained in the image.

Keras wants a 4D tensor as input, with shape ( batch_size, rows, columns, channels). We can Do this with the following code.

While looking at the dictionary, you will notice that the categories corresponding to dogs appear in an uninterrupted sequence and correspond to dictionary keys 151–268, inclusive, to include all categories from ‘Chihuahua’ to ‘Mexican hairless’. Thus, to check to see if an image is predicted to contain a dog by the pre-trained ResNet-50 model, we need only check if the ResNet50_predict_labels function above returns a value between 151 and 268 (inclusive).

CNN from Scratch

Now let’s build our own CNN model without any transfer learning in Keras.

Images are first passed to a sequence of Convolutional, Dropout, and max-pooling layers. Then I used the GlobalAveragePooling layer. It outputs 2D tensor with shape (batch_size, channels).Finally, I fed it to the Dense layer with a softmax activation function.

After the model is created I compiled and trained and saved the best model using ModelCheckpoint.

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

To reduce training time without sacrificing accuracy, we show you how to train a CNN using transfer learning.

Obtain Bottleneck Features

Model Architecture

The model uses the pre-trained VGG-16 model as a fixed feature extractor, where the last convolutional output of VGG-16 is fed as input to our model. We only add a global average pooling layer and a fully connected layer, where the latter contains one node for each dog category and is equipped with a softmax.

Compile and train the model.

We got an accuracy of 36.72. It is not good. Right? Let’s try to improve it.

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

Same as above we build our model. This time lets use a bigger model. This time we are choosing Resnet50.

YeY! we got an accuracy of 81%. It looks good, isn’t it for this small dataset?

Write your Algorithm

  • if a dog is detected in the image, return the predicted breed.
  • if a human is detected in the image, return the resembling dog breed.
  • if neither is detected in the image, provide an output that indicates an error.

Results obtained on Real-world images