Skip to content

lmasello/Dog-breed-classifier

Repository files navigation

CNN Dog Breed Classifier Project

The project consists of building a classifier that determines the canine's breed given an image of a dog. If the input image corresponds to a human, the model yields the resembling dog breed. The project is composed of the following steps:

  • Detect human faces using OpenCV's implementation of Haar feature-based cascade classifiers.
  • Detect dogs using a VGG-16 model pre-trained on the ImageNet dataset.
  • Create a CNN to classify dog breeds (from scratch)
  • Create a CNN to classify dog breeds (using transfer learning)
  • Combine the human and dog detectors to provide estimates of canine breeds of input images:
    • If the image contains a dog, it returns the predicted breed.
    • If the image contains a human face, it returns the resembling dog breed.
    • If the image contains neither a dog nor human, it returns an error.

The following images show examples of the resulting algorithm:

drawing drawing drawing drawing

Model performance

The Dog Breed classifier achieved a test accuracy of 84% (705/836).

Project structure

  • dog_app.ipynb: This file contains the main project notebook with the whole processing pipeline. The notebook trains a classifier and saves the model into a model_transfer.pt file.
  • dog_breed_classifier.py: A Python file contains the main functions to run the model's prediction on the web app.
  • web_app: A streamlit file with the web application code to get a input image and return the model's prediction.

How to run the dog_app notebook and get a dog breed classifier model

  1. Clone the repository and navigate to the downloaded folder.

    	git clone https://github.com/lmasello/Dog-breed-classifier.git
    	cd Dog-breed-classifier
    

NOTE: if you are using the Udacity workspace, you DO NOT need to re-download the datasets in steps 2 and 3 - they can be found in the /data folder as noted within the workspace Jupyter notebook.

  1. Download the dog dataset. Unzip the folder and place it in the repo, at location path/to/dog-project/dogImages. The dogImages/ folder should contain 133 folders, each corresponding to a different dog breed.

  2. Download the human dataset. Unzip the folder and place it in the repo, at location path/to/dog-project/lfw. If you are using a Windows machine, you are encouraged to use 7zip to extract the folder.

  3. Make sure you have already installed the necessary Python packages according to the requirements.txt file.

  4. Open a terminal window and navigate to the project folder, and open the notebook.

    	jupyter notebook dog_app.ipynb
    
  5. If you just want to get a model, you can skip the notebook's Step 3: Create a CNN to Classify Dog Breeds (from Scratch) and, instead, execute Step 4: Create a CNN to Classify Dog Breeds (using Transfer Learning).

NOTE: In the notebook, you will need to train CNNs in PyTorch. If your code is taking too long to run, you will need to either reduce the complexity of your chosen CNN architecture or switch to running your code on a GPU.

How to run the web application

  1. Run the dog_app.ipynb notebook and save the resulting model into a model_transfer.pt file at the root of the project.
  2. Make sure you have already installed the necessary Python packages according to the requirements.txt file.
  3. Open a terminal window and run the streamlit application:
cd Dog-breed-classifier
streamlit run web_app.py

Model architecture

The model uses Transfer Learning based on the VGG16 architecture due to its reported performance. It only performs one modification on the last Linear layer to adapt the number of output features to the Dog Breed classifier case.

Acknowledgements

This project contains my implementation of the "Dog breed classifier" project for the Udacity's Deep Learning program and Data Scientist program. The baseline code and datasets have been taken from the Udacity's Deep Learning repository.