Professional Documents
Culture Documents
Dog Breeds — pic source: German Shepherd Rescue Trust New Zealand
Get one more story in your member preview when you sign up. It’s free.
Imagine you are having your weekend jog/walk in the park, you see a really cute
dog. Have you ever wondered whichSign
breed theGoogle
up with dog belonged to? I have…
Overview
The aim of the project in the Data Scientist nanodegree was to create a web
application that is able to identify a breed of dog if given a photo or image as input.
If the photo or image contains a human face (or alien face), then the application
will return the breed of dog that most resembles this person.
. . .
The steps that were followed to work through the project were the following:
I have set myself a target test accuracy for the CNN of 90% i.e., the model identifies
the dog breed 9 times out of 10 correctly. We will be using the accuracy metric on
the testing dataset to measure the performance of our models.
To follow along with the steps you can download or clone the notebook from my
github repository. The repository features the ‘dog_breed_classifier.ipynb’ that runs
on the GPU provided for free at Google Colab.
Dog Images — The dog images provided are available in the repository within
the Images directory further organized into train, valid and test subfolders
Test Images — A folder with certain test images have been added to be able to
check the effectiveness of the algorithm
Load all the libraries and packages required through the notebook.
Sign up with Facebook
Utility libraries — random (for random seeding), timeit (to calculate execution
time),os, pathlib, glob(for folder and path operations), tqdm (for execution
progress), sklearn (for loading datasets), requests and io (load files from the
web)
Getprocessing
Image one more story in your(cv2),
— OpenCV memberPIL preview when you sign up. It’s free.
Get one more story in your member preview when you sign up. It’s free.
Dataset stats
Sign up with Google
The dog_names variable stores a list of the names for the classes to use in our
prediction model. Based on the pathSign up with
name, weFacebook
see a total of 8351 images of dogs
belonging to 133 different dog breeds which are then categorized into 6680, 835 and
Already have an account? Sign in
836 images in training, validation and testing.
Step 1: Detect Humans based on OpenCV Haar cascade
classifiers
When using TensorFlow as backend, Keras CNNs require a 4D array (which we’ll
also refer to as a 4D tensor) as input, with shape
(nb_samples,rows,columns,channels)
where nb_samples corresponds to the total number of images (or samples), and
rows , columns , and channels correspond to the number of rows, columns, and
channels for each image, respectively.
is the number of samples, or number of images, in the supplied array of image paths.
It is best to think of nb_samples as the number of 3D tensors (where each 3D tensor
corresponds to a different image).
The model is then used to extract the predictions. The predict method, returns an
array whose 𝑖-th entry is the model's predicted probability that the image belongs to
the 𝑖-th ImageNet category. This is implemented in the ResNet50_predict_labels
function below.
to 'Mexican hairless' . So, if the function returns any number between 151 to 268,
the supplied image is that of a dog.
Get one more story in your member preview when you sign up. It’s free.
This is then followed by a global average pooling layer which is then followed by a
dense layer to identify 133 breeds.
This takes a 4D-tensor with shape (1, 224, 224, 3) and provides an array of 133 with
probabilities. The optimizer used was ‘RMSProp’ and metric used was accuracy. The
model was run for 10 epochs and provided an accuracy of 6.69%
Get one more story in your member preview when you sign up. It’s free.
The bottleneck features are the last activation maps in the VGG16, (the fully-
connected layers for classifying has been cut off) thus making it now an effective
feature extractor. The bottleneck features were obtained from a website where its
stored as a .npz file using the BytesIO library along with requests for the url
extraction.
Get one more story in your member preview when you sign up. It’s free.
Get one more story in your member preview when you sign up. It’s free.
These models were then added with a global average pooling layer, a dropout layer
followed by a fully connected layer (with softmax) and then run for 20 epochs
Training the models took less than a minute in each of these cases.
Get one more story in your member preview when you sign up. It’s free.
I then explored options for increasing the accuracy. I used fastai to see if we could
leverage transfer learning and obtain a higher accuracy.
A cnn_learner was created with the resnet34 model and was run for two cycles. The
accuracy was upto 86%. An optimal learning rate seems to be between 1e-6 and 1e-4
Get one more story in your member preview when you sign up. It’s free.
Based on the analysis of various models that we have fit, the learn_resnet34 seems
to provide the most accuracy. This is also saved and exported as a pickle file for
classification.
Our algorithm accepts a file path to an image and first determines whether the
image contains a human, dog, or neither. Then,
if neit her is detected in the image, provide output that indicates an error.
Get one more story in your member preview when you sign up. It’s free.
The algorithm leverages the CNN built in Stage 5 and leverages the previous
Sign up with Google
functions created to come up with an output.
The provide_output outputs a greeting based on the predicted species and dog breed.
Get one more story in your member preview when you sign up. It’s free.
There are a few breeds that are virtually identical and are sub-breeds. There’s also a
possibility of some images being either blurred or having too much noise. There’s
also a possibility of enhancing the quality by additional image manipulation.
Following the above areas I’m sure we could increase the testing accuracy of the
model to above 90%.
A simple web application in Flask could be built to leverage the model to predict
breeds through user-input images.
Get one more story in your member preview when you sign up. It’s free.