Dog Breed Classification using CNNsDeniz Doruk NuhogluBlockedUnblockFollowFollowingFeb 9In today’s post, I will be showing you how to be make an exceptionally FUN application that detects whether a picture has a human or a dog in it.
Moreover, in the case a dog is detected in the image, it will be telling us the breed of the dog with a high level of accuracy.
The relevant repository where you can find the instructions on downloading datasets and prerequisites can be accessed via this github link.
In building our classifier we will be following below steps:Step 0: Import DatasetsStep 1: Detect HumansStep 2: Detect DogsStep 3: Create a CNN to Classify Dog BreedsStep 4: Train a CNN to Classify Dog Breeds (from scratch)Step 5: Train a CNN to Classify Dog Breeds (via transfer learning)Step 0: Import DatasetsOur first step will be to load the datasets that are divided into train, validation and test folders.
We would like to classify dogs into 133 different breeds that are found in our training dataset.
As we can see in below code snippet, we will be using 6680 dog images to train the models that we will be using.
We will be using 835 validation images to fine tune our parameters, and testing the final model’s accuracy on 836 test images.
The test dataset will, of course, be images which the model has not seen before.
Step 1: Detect HumansTo detect humans, we first convert the tri-channel RGB image (Red-Green-Blue) into grayscale.
An OpenCV function called “detectMultiScale” works on grayscale images and gives us the coordinates of a “box” of where the human face is detected.
Later on, we can use this to draw a blue rectangle on the image we are plotting as shown below.
Snippet 2 : Detecting humansStep 2: Detecting DogsWe will be using resnet50 pre-trained model in Keras to detect dogs in images.
The first step here is to convert RGB coding of .
jpg images into BGR and then normalize the three channels based on means and standard deviation obtained from the huge Imagenet database.
Luckily, this is done by the preprocess_input in the applications.
resnet50 module in Keras.
Snippet 3: Resnet50Using the ResNet50 model we imported from keras.
applications library, we can classify the images into labels.
In the resnet library any label coded as from 151 to 268 is in effect a “dog”.
The function below will give us a True boolean value if this is the case and False otherwise.
Snippet 4: Dog DetectorStep 3: Create a CNN to Classify Dog BreedsWe will be using below the base Sequential model and designing our CNN networks.
I have used three Convolutional Layers, each followed by a MaxPooling2D to reduce complexity of the stacked model.
At the end, we will be using global_average_pooling to convert each feature map in the Max Pooling Layer into a scalar.
Snippet 5: Building the CNN ModelStep 4: Train a CNN to Classify Dog Breeds (from scratch)We start training the model we created, and we see that our validation loss constantly lowers, and that our accuracy lowers for 5 epochs, signaling our model is learning.
Snippet 6: Training the Model with 5 EpochsAt 20 epochs, is it possible to get around %4.
When we run 250 epochs on this dataset, we are able to reach %40+ accuracy, which does take quite a long time even with strong GPU support which is why we will be using transfer learning as a next step.
Step 5: Train a CNN to Classify Dog Breeds (via transfer learning)Next, we will be using pre-extracted “bottleneck features” which are an output of a pre-trained library applied on our train, test and validation datasets.
We will be using Resnet50 library output as an input to our model and train our model using these bottleneck features.
Snippet 7: Loading bottleneck featuresWe make a simple model by adding a Global Average Pooling layer that summarizes each of the previous feature maps into a scalar.
The dense layer creates 133 different outputs, one for each needed label.
Then, a softmax activation converts each of these into a probability.
Snippet 8: Define, compile and train the modelWe then test our algorithm live to see whether it correctly predicts the breed of the dog in the loaded image path.
We get a Welsh Springer Spanel image loaded into the model and as you can see below, the neural network correctly classifies the sample dog image that we used.
Snippet 9: Predict using the modelThe final part connects all the functions we have previously built and will help determine whether an image that was provided is of a human, a dog or neither.
Snippet 10: Final algorithm.