DOG BREED CLASSIFIER

Original article was published on Deep Learning on Medium

Introduction

In 21st century we have seen many applications of deep learning around us which makes our life sophisticated in some way like face detection , Image Classification etc. I have also tried my best use the techniques and come up with my model . here i have build a dog breed classifier .

Dataset

Two data sets have been used in this project.

  1. Dog Image
  2. Human Images

Approach

Loading dataset in the local machine.

from sklearn.datasets import load_files 
from keras.utils import np_utils
import numpy as np
from glob import glob
# define function to load train, test, and validation datasets
def load_dataset(path):
data = load_files(path)
dog_files = np.array(data['filenames'])
dog_targets = np_utils.to_categorical(np.array(data['target']), 133)
return dog_files, dog_targets
# load train, test, and validation datasets
train_files, train_targets = load_dataset('dogImages/train')
valid_files, valid_targets = load_dataset('dogImages/valid')
test_files, test_targets = load_dataset('dogImages/test')
# load list of dog names
dog_names = [item[20:-1] for item in sorted(glob("dogImages/train/*/"))]
# print statistics about the dataset
print('There are %d total dog categories.' % len(dog_names))
print('There are %s total dog images.\n' % len(np.hstack([train_files, valid_files, test_files])))
print('There are %d training dog images.' % len(train_files))
print('There are %d validation dog images.' % len(valid_files))
print('There are %d test dog images.'% len(test_files))

Now After loading all the data set , we have to tell whether an image is dog or a human , so i first tried harcasscade from cv2 and the output is here on a small set of sample images.

Output :
% of human faces deteced : 99
% of faces dectected in dogs Image 12

This algorithm is good for human face but not for dogs. So we will first try to build our own model from scratch and test the accuracy.

Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 224, 224, 64) 1792
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 74, 74, 64) 0
_________________________________________________________________
batch_normalization_1 (Batch (None, 74, 74, 64) 256
_________________________________________________________________
conv2d_2 (Conv2D) (None, 74, 74, 128) 73856
_________________________________________________________________
max_pooling2d_3 (MaxPooling2 (None, 24, 24, 128) 0
_________________________________________________________________
batch_normalization_2 (Batch (None, 24, 24, 128) 512
_________________________________________________________________
conv2d_3 (Conv2D) (None, 24, 24, 256) 295168
_________________________________________________________________
max_pooling2d_4 (MaxPooling2 (None, 8, 8, 256) 0
_________________________________________________________________
batch_normalization_3 (Batch (None, 8, 8, 256) 1024
_________________________________________________________________
dropout_1 (Dropout) (None, 8, 8, 256) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 16384) 0
_________________________________________________________________
dense_1 (Dense) (None, 512) 8389120
_________________________________________________________________
dropout_2 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 133) 68229
=================================================================
Total params: 8,829,957
Trainable params: 8,829,061
Non-trainable params: 896

For the code visit my Github you will get better understanding.

Result

This model give the accuracy of 5–6 % which is very poor as the model is very small if we compare this with the complexity of the data set.

Transfer learning

Now , we have to be smart. we know we can’t process this data set on large model to find best weights so we will use bottleneck features of inception model and then we can make our own model and train on that.

Model: "sequential_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
global_average_pooling2d_1 ( (None, 512) 0
_________________________________________________________________
dense_3 (Dense) (None, 133) 68229
=================================================================
Total params: 68,229
Trainable params: 68,229
Non-trainable params: 0
_____________________
Inception_predictions=[np.argmax(Inception_model.predict(
np.expand_dims(feature, axis=0))) for feature in test_Inception]
test_accuracy = 100*np.sum(np.array(
Inception_predictions)==np.argmax(test_targets, axis=1))/len(Inception_predictions)
print('Test accuracy: %.4f%%' % test_accuracy)Output :
Test accuracy: 82.1770%

This is my second model which is very small as compared to previous one . But you will be shocked to know the results , The accuracy comes out to be 82%. What a great leap from 5 to 82%. That is the magic of transfer learning.

Conclusion

  1. Model is able to distinguish between human , dog and other things .
  2. Model is predicting name of Dogs
  3. Model is suggesting a name to all human images which are resembling the dogs.