- Project Motivation
- Dependencies
- Datasets
- Content
- Analysis
- Instructions
- Licensing
- Acknowledgements
- Demo
This project is part of Udacity Data Scientist Nanodegree program and I'll develop an CNN algorithm that will accept any dog image and return an estimate of the dog's breed. If human is detected, it will provide an estimate of the most resembling dog breed. If neither dog or human face is detected the algorithm will return an error.
The project consitutes of 2 parts:
- The Jupyter notebook which includes all of the implemented algorithms and approaches
- Flask we app which uses the saved model created through the jupyter notebook to perform the dog breed identification on uploaded photos
The following libraries were used in building the dog breed identification algorithm:
- Keras (Tensorflow high level API)
- OpenCV (Computer vision library)
- face_detection library
refer to requirements.txt
- Dog dataset: https://s3-us-west-1.amazonaws.com/udacity-aind/dog-project/dogImages.zip
- Human dataset: https://s3-us-west-1.amazonaws.com/udacity-aind/dog-project/lfw.zip
- dog_app.ipynb - A jupyter notebook file that contains the god breed identification algorithm, here will find the source code of the algorithms
- model directory: - model_Resnet50_final.h5: The saved model that contains the model architecture and weight, created using the code in the jupyter notebook
- static - contains static images used by the Flask web app
- templates - the html templates used for the Flask web application(master.html and predict.html)
- helper.py - A python file contains the dog or human identification algorithm, it loads the saved model from the model folder
- app.py - this is the main flask python file that is responsible for routing and rendering html templates
- requirements.txt - lists all the dependecies
- test.py - a test file to test the algorithm locally that will instantiate the model and try to predict the breed of a supplied image
-
Face detection:
To detect whether the supplied image is a human face I've compared OpenCV's implementation of Haar feature-based cascade classifiers accuracy with that of face_recognition. On a sample data of 100 human face and 100 dog, OpenCV was able to dectect 100% of the human faces however 11% of the dog images were classified as human faces. on the other hand, face_recognition library provides 2 methods for face dectection, HOG-based and using deep learning.
The HOG-based model identified 100 faces of the supplied human faces sample and 10 faces of the dog sample dataset. 1% better than OpenCV. The deep learning model of the face_recognition library was able to detect 100 faces of the supplied human sample dataset and 1 face of the dog data set which is a remarkable accuracy compared to the other methods.
Eventually we would use the latter model on a GPU enabled system, and OPenCV or HOG-based model where GPU is not available for the sake of performance. In the notebook I'm going to use the HOG-based model.
For fun, I've also implemented another version of the face detector function that overlays a dog filter if human face is detected.
-
Dog detection:
To detect whether a dog is present in the supplied image ReseNet-50 pre-trained model was used which can identify 1000 categories. Trying his model on a sample dog dataset gave an excellent result, it was able to detect dogs in all of the sample set and also didn't mistakenly identify dogs in any of the human sample dataset.
-
Dog breed classification:
In this part, after a dog is detected in an image we want to be able to identify it's breed.
The following models were applied to find out the best approach:
-
Create a CNN to classify dog breed from scratch Training a CNN model that's built from scratch gave us a test accuracy of 8.9% which is better than a random guess but still no an acceptable result.
-
Use a CNN to classify Dog Breeds Here we are going to train a CNN using transfer learning using the pre-trained VGG-16 model where the last convolutional output of the model is fed as input to our model. So we only need to train the fully connected layer that we added to the VGG-16 model and this resulted in reduced training time without sacrificing accuracy.
The achieved test accuracy was ~43%, a big mprovement compared to the model we built from scratch.
-
Create a CNN to classify dog breeds using Transfer Learning Similar to the above step we will use transfer learning to a CNN model but we'll try to achieve at least 60% accuracy on the test data. For this purpose we are going to use Resnet50 pre-trained model and modify it's architecture by adding our custom fully connected layer.
After several test I was able to reach accuracy of ~81% which is a huge improvement and acceptable metric for his project.
-
-
Algorithm
This section will build on the algorithm built previously to determine whether the supplied image contains a human, dog, or neither. If a dog is detected, return the predicted breed, if a human is detected, return the resembling dog and if neither is detected, provide output that indicates an error.
-
Testing
Testing the algorthim seems to work as expected, not all dog breeds were detected however, also it can't detect multiple breeds in a single image.
The output is acceptable in a demo but not in production, to improve our results, the below points can be considered:
- Increase the training set size
- Apply data augmentation
- Use Regional-CNN or YOLO algorithms to detect multiple objects in a single image
- For the dog filter to better fit the human face, consider using Facial keypoints dataset available here
- Clone the repository: git clone https://github.com/m0hamdan/Dog_Breed_Identification_App.git
- Uncomment lines 54 and 55 in app.py, this is only required if running locally
- Run the following command in the repo root directory: python -m flask run
- Go to http://localhost:8000
None
- Udacity for providing the dogs images and jupyter notebook walkthrough