Project code for Udacity's AI Programming with Python Nanodegree program. In this project, students first develop code for an image classifier built with PyTorch, then convert it into a command line application.
The image classifier aims to recognize different species of flowers that commonly occur in the United Kingdom. The model has been trained using the 102 Category Flower Dataset from the Visual Geometry Group at the University of Oxford.
As part of the overall application architecture, pre-trained deep learning models from the torchvision models subpackage were used (vgg16 or densenet121). The classifier portions of the models were replaced with the student's work using beginner knowledge of fully connected network architecture.
The model was trained over 20 epochs with a testing accuracy of 92%.
A sample output of the classification model within a Jupyter Notebook is shown below. The top image shows the actual species and the bottom horizontal bar graph plots the model's predictions with associated probabilities.
- All code is written in Python 3
- torch
- torchvision
- PIL
- matplotlib
- numpy
- json
I used the pre-trained densenet121
model and froze the features portion of the neural network that contains the convolution and pooling layers. I replaced the classifier portion, the fully connected layers, with the following code:
classifier = nn.Sequential(nn.Linear(1024, 512),
nn.ReLU(),
nn.Dropout(p = 0.5),
nn.Linear(512, 102),
nn.LogSoftmax(dim = 1))
model.classifier = classifier
Python script files:
filename | description |
---|---|
Image Classifier Project.ipynb | Jupyter Notebook of project code with explanatory text |
get_input_args.py | Two functions to retrieve user command line inputs for training and prediction scripts |
load_checkpoint.py | Function to load parameters of a trained network from checkpoint.pth file |
load_data.py | Two functions, one that loads data for training and one that loads a single file to be fed into classifier model. Contains image transform code |
predict.py | Calls the prediction_model.py function using user command line inputs from get_input_args.py |
prediction_model.py | Function that feeds an image into classifier neural network and return prediction with associated probability |
save_checkpoint.py | Function that saves parameters of a trained network for later use |
train.py | Calls the train_model.py function using user command line inputs from get_input_args.py and generates checkpoint.pth output file using save_checkpoint.py function |
train_model.py | Function that builds classifier neural network using PyTorch torchvision models subpackage and user defined layers. Training and validation functionality |
workspace_utils.py | Keeps Udacity workspace active during training |
Input files:
filename | description |
---|---|
cat_to_name.json | Dictionary of integers as keys and actual flower names as values |
Output files:
filename | description |
---|---|
checkpoint.pth | PyTorch model checkpoint file, used to rebuild trained model |
- Add model architecture section to README
- Continue training model, try to get higher accuracy
Student: Joshua Cayanan
Flower icon in readme header made by Freepik from www.flaticon.com
Deep Learning icon in readme header made by Becris from www.flaticon.com
This project is licensed under the terms of the MIT license and protected by the Udacity Honor Code and Community Code of Conduct.