The purpose of this project is to share how to develop an image recognition application in Pytorch. You can learn how to:
- load and preprocess the image dataset
- train the image classifier on dataset using transfer learning
- use the trained classifier to predict image content
The objective is to train an image classifier to recognize different species of flowers. Classifier is trained on Dataset of 102 flower categories.
There are two pre-trained models selected VGG19_with batch normalization and Resent50. Both are subjected to transfer learning and performance is tested regarding model accuracy, loss and training time.
Model resenet50 learns slower than model 'vgg19_bn' till epoch 6, but reaches in average higher accuracy from epoch 6 onward. It also reached the highest validation accuracy and in average lower prediction time, which can be beneficial in the production environment. This model will be further used for tuning using the whole training and validation dataset.
The number of hidden layers in classifier and learning rate were further tunned with following grid:
Run | Model | Hidden Layers | Learning Rate |
---|---|---|---|
1 | TBD | None | 0.001 |
2 | TBD | None | StepLR |
3 | TBD | [1024, 512] | 0.001 |
4 | TBD | [1024, 512] | StepLR |
The number of layers and learning rate play a significant role in network learning. The model with classifier with none hidden layers is performing best, it learns fastest with learning rate 0.001 and reached the highest validation accuracy 93.3%.
There are several necessary 3rd party libraries beyond the Anaconda distribution of Python which needs to be installed and imported to run code. These are:
torch
Pytorch library for designing, training and testing neural networks in python- torchvision package consists of popular datasets, model architectures, and common image transformations for computer vision. It comes pre-installed with Pytroch.
- tqdm progress bar to visualize ANN learning progress.
There is 1 notebook available here to showcase work related to the above questions. Markdown cells were used to assist in walking through the thought process for individual steps.
There are additional files:
train.py
will train a new network on a dataset and save the model as a checkpoint
train.py --data_dir "/data/directory" --save_dir "/save/directory", --arch "resenet50"
predict.py
uses a trained network to predict the class for an input image
prdict.py --image_dir "/image/directory" --load_dir "/model/directory" --top_k 5 --category_names "Mapping/to/category/names.json"
model.py
Module for classes and functions related to neural network design, initialization and training
Must give credit to Udacity for task design and Maria-Elena Nilsback and Andrew Zisserman for the dataset.