Publicada con documentos de Google
dog-identification-app_report_200908
Actualizado automáticamente cada 5 minutos

Capstone Project

Seongjae Ryu

Machine Learning Engineer Nanodegree

August 20, 2021

CNN Project: Dog Breed Classifier

Project Report

I. Definition

Project Overview

Dog breed classifier is one of the popular CNN(convolutional neural network) projects [1]. The main problem is to identify a dog breed from an input image. It also needs to be identified whether it is an image of a dog or not. In case when a given input is identified as an image with a human face, a similar dog breed will be provided.

This idea is based on supervised machine learning with a multi-class classification problem. The intention of this model is to deploy APIs and build an application with the APIs.

Problem Statement

The problem is to build a dog breed classification model and deploy this model for an application.

Metrics

Only the dataset for the main problem of building a dog breed classification is balanced. ‘Accuracy’ is an optimal ​​means of performance measurement only when the dataset is balanced. Thus ‘accuracy’ is used only for measuring ‘dog breed classification’ performance.

  1. Human-face-image identifier

  1. Dog-image identifier

  1. Dog-breed classifier

II. Analysis

Data Exploration

Dog images and human-face images are included in the dataset, which is labeled, organized, and provided by Udacity [2]. The dog image dataset is balanced, but the human-face dataset is imbalanced.

  1. Dog image dataset [3]
  1. It contains 8,351 RGB images of dogs with each labeled file name. The images are organized in each directory; train, test, and valid. There are 6,680 train images, 836 test images, and 835 valid images. Each breed of 133 is grouped in each folder with label number and breed name; i.e. ‘/dog_images/train/103.Mastiff/Mastiff_06826.jpg’. Image sizes are varied.
  2. Balanced dataset, which is optimal to use ‘accuracy’.

  1. Human-face dataset [4]
  1. It contains 13,233 RGB human-face images with each labeled file name. Images are grouped by each person in each folder with label number and person’s name; i.e. ‘/Daniele_Bergamin/Daniele_Bergamin_0001.jpg’. There are 5,750 folders and the data is imbalanced, such as one has few images and the other has many images. The image size is 250 by 250 px.
  2. Imbalanced dataset, which is not optimal to use ‘accuracy’.

Exploratory Visualization

  1. Dog image dataset: It is suitable for modeling. Each label has enough data for modeling and the data is properly divided into each set of training, validation, and test.

  1. Human-face dataset: It is suitable for the purpose of testing whether it is a human face or not. But it is not suitable for creating a new model. Some labels have only 1 image. This is the reason for using a pre-trained face detection model.

Algorithms and Techniques

A convolutional neural network is used for multi-class classification, dog breed classifier. VGG16 is chosen for a pre-trained CNN model because it achieves 92.7% top-5 test accuracy in ImageNet, which is a dataset of over 14 million images belonging to 1000 classes [5]. One of the cons of using pre-trained CNN type models is that it only fits on specific situations. If the condition changes, such as parameter modification, the modified model needs to be trained a lot again to fit the very large amount of weights.

Haar Cascade Classifier is used for a face detection model. One of the primary benefits of using the classifier is the fastness and the lightness, which fit for the situation that the face detection is not a primary problem [6].

  1. Dog-image identifier: Pre-trained VGG-16 torchvision model [7].
  1. Load pre-trained VGG-16 model via torchvision.
  2. Load image and convert into tensor image.
  3. Infer the image and check whether the output is in 133 breeds or not.

  1. Dog-breed classifier: Fitted pre-trained VGG-16 torchvision model with custom classifier for 133 breeds.
  1. Load pre-trained VGG-16 model via torchvision.
  2. Define a new custom classifier to fit the model into the dataset with 133 breed categories.
  3. Load image and convert into tensor image with transforms.
  4. Train the model with train dataset and valid dataset.
  5. Test the model with a test dataset.
  6. Deploy the model and infer the most likely breed.

  1. Human-face-image identifier:  OpenCV with pre-trained face detector, haarcascade_frontalface_alt [8].
  1. Load pre-trained OpenCV model with haarcascade_frontalface_alt.xml.
  2. Load and convert RGB image into grayscale.
  3. Infer the image.

  1. Human-face-image Resemble-dog classifier: Combination of human-face-image identifier and dog-breed classifier.
  1. Infer the image via human-face-image identifier and if the inference tells it is a human face image, infer the breed via dog-breed classifier.

Benchmark

  1. Human-face-image identifier must have 70% or above true inference and vice versa.

  1. Dog-image identifier must have 70% or above true inference and vice versa.

  1. Dog-breed classifier must have 60% or above accuracy.

III. Methodology

Data Processing

Dog images and human-face images are included in the dataset, which is labeled, organized, and provided by Udacity [2]. Each data path is stored in a ‘numpy array’ and it is sliced for model tests. But further transformations are still needed for each model after this data processing.

  1. Data path list creation of dataset: The list of each dataset into each numpy array by using ‘glob’ and ‘numpy’ library.

  1. Data path list division for test: Slicing each numpy array

Implementation

  1. Import library: Python(3.6.3), PIL(5.2.0), cv2(3.3.1), matplotlib(2.1.0), numpy(1.12.1), pandas(0.23.3), seaborn(0.8.1), torch(0.4.0), torchvision(0.2.1), tqdm(4.11.2)

  1. Import dataset
  1. Load dataset
  1. Load dataset by ‘data directory’ and check each file and label number is correct.

  1. Check the number of dog data labels is correct.

  1. Explore the data
  1. Check the distribution of human images. If the number of labels is above 150, plot_human_data_distribution function will show a chart of top 50 and bottom 50. The number of the human labels is 5749, so the function shows the result of top 50 and bottom 50.

  1. Check the distribution of dog images.

  1. Detect humans
  1. Implement a face detector: With the implementation, the model will infer face from a human face image. Then, the detected area will be shown.

  1. Test the face detector with part of the whole dataset: to get the percentage of human face images inferred as a human face and the percentage of dog images inferred as not a human face.

  1. Dog Detector
  1. Implement a pre-trained classifier model through ‘torchvision’ library: VGG16 is used. The input data is transformed into a randomly cropped 224 by 224 tensor. If CUDA is available, the tensor object and the model object will work with GPU. The model returns predicted classes. An index of the maximum value among the classes will be used for the final output of VGG16_predict function.

  1. Use pre-trained classifier model as a dog detector: The labels of the dog dataset correspond to dictionary keys 151-268, inclusive, to include all categories from 'Chihuahua' to 'Mexican hairless' of model label text file(imagenet 1000 class idx to human readable labels) [9]. So if an input image is a dog, the index will be in the dictionary keys.

  1. Test dog detector: Test the performance of the dog_detector function on the images in human_files_short and dog_files_short.

  1. Compare torchvision models: Compare the performance of ‘torchvision’ models to decide whether to use VGG16 or not. For the comparison, define functions for multiple-model creation and tests [10].

Do test 5 times to compare the performance of each model: 'resnet18', 'alexnet', 'squeezenet1_0', 'vgg16', 'densenet161' [10].

Compare the result and select a model with high true result and low false result. Among the five models, VGG16 fits best for the dataset.

  1. Create a CNN to classify dog breeds (from scratch)
  1. Specify data loaders for the dog dataset: For the train set, an image is randomly 30 degree rotated and randomly resized into 224 by 224 tensor. It is also randomly horizontally flipped. For validation set and test set, an image is resized into 255 by 255 tensor and centerly cropped into 224 by 224 tensor. Both input tensors are 224 by 224. Reason of choosing the input size is that it is a common size choice for pre-trained CNN models.

  1. Implement Model Architecture: The initial input of the convolutional 2d layer is ‘3’ which is each channel of RGB. LeakyReLU with 0.2 angle negative slope is used for the activation function for each convolutional layer. MaxPool2d(2,2) is used for dimension reduction in half. After 4 sequential layer sets of Conv2d, LeakyReLU, and MaxPool2d, AvgPool2d(14) is used to apply a 2D average pooling over an input signal composed of several input planes, and a method, ‘view’, is used to allow a tensor to be a view of an existing tensor. Then, a classifier gets 128 inputs which is equal to the output number of AvgPool2d, and returns 133 outputs which is equal to the number of dog labels.

  1. Implement a loss function and an optimizer.

  1. Implement a model training function with validation. Then train the model and save and use the model with the best validation accuracy.

  1. Test the model: Keep training until the accuracy of the test result gets at least above 10%. If the accuracy is below 60%, the model will not be used.

  1. Create a CNN to classify dog breeds (using transfer learning)
  1. Specify Data Loaders for the Dog Dataset: Also delete previous data loaders to avoid memory issues.

  1. Model Architecture: In the comparison of torchvision models, VGG16 fits best for the dataset so it is used as the base model. After turning off the gradient option of the loaded base model, the last linear layer of the classifier is newly defined. Its output parameter is the only difference. This is to maintain the original performance as much as possible by minimizing the deformation from the original. It is changed from 1000 to 133, which is equal to the number of dog labels.

  1. Implement a loss function and an optimizer: Same loss function and optimizer is used. This is to maintain the original performance as much as possible by minimizing the deformation from the original.

  1. Implement a model training function with validation. Then train the model and save and use the model with the best validation accuracy.

  1. Test the model: Keep training until the accuracy of the test result gets at least above 10%. If the accuracy is below 60%, the model will not be used.

  1. Implement a dog breed predictor with the best accuracy model.

  1. Define an app function: It will display input file name and graphic. Then it will display a prediction output result. There are 3 cases for the result, which is human-face-case, dog-case and neither-case.

Refinement

Minimizing the deformation from the original results in the best performance. The minimization is changing only the output size of the last linear layer of the classifier. The better performance can be expected through more learning. But the difference in performance between the minimization case and the others is definitely too big. One example for ‘the others’ is changing 3 linear layers of the classifier. The total learning time is above 20 minutes, which is 5 times longer than the minimization case. But the accuracy of the example is only 1% by comparison with 74% accuracy of the minimization case.

IV. Result

Model Evaluation and Validation

  1. Human-face-image identifier must have 70% or above true inference and vice versa.

  1. Dog-image identifier must have 70% or above true inference and vice versa.

  1. Dog-breed classifier must have 60% or above accuracy.

  1. Human-face-image Resemble-dog classifier: 6 true result from 6 tests

Justification

All test results meet each criterion. But it is still possible to get better prediction results through the options below.

  1. Feed more new training images.
  2. Adjust learning rate and train for more epoches.
  3. Variate the transformation of training images.
  4. Try various pre-trained models and optimize hyper parameters appropriately.

IV. Reference

Reference

[1] Dog Breed Identification, Kaggle - Kaggle: https://www.kaggle.com/c/dog-breed-identification

[2] Dog Project, Udacity - Github: https://github.com/udacity/dog-project/blob/master/dog_app.ipynb

[3] Dog image dataset, Dog App - Udacity: https://s3-us-west-1.amazonaws.com/udacity-aind/dog-project/dogImages.zip

[4] Human image dataset, Dog App - Udacity: https://s3-us-west-1.amazonaws.com/udacity-aind/dog-project/lfw.zip

[5] VGG16 – Convolutional Network for Classification and Detection - Neurohive: https://neurohive.io/en/popular-networks/vgg16

[6] Cascade Classifier, OpenCV - OpenCV: https://docs.opencv.org/3.4/db/d28/tutorial_cascade_classifier.html

[7] torchvision.models - Pytorch: https://pytorch.org/vision/stable/models.html

[8] haarcascades, OpenCV - Github: https://github.com/opencv/opencv/tree/master/data/haarcascades

[9] imagenet 1000 class idx to human readable labels, yrevar, Gist - Github: https://gist.github.com/yrevar/942d3a0ac09ec9e5eb3a

[10] torchvision.models, Torchvision - PyTorch: https://pytorch.org/vision/stable/models.html