Skip to content

AyushAniket/ImageClassification

Repository files navigation

Project - Image Classification using Neural Network

Final Project of the Udacity AI Programming with Python Nanodegree. An image classifier built with pytorch that predicts the names of flowers. The classifier currently has training, validation, and test data for 102 flowers and uses transfer learning with either VGG19 or Densenet121 to train and infer with.This project includes a jupyter notebook file and python files for command line application.

Files:

  1. Image Classifier Project.ipynb - Jupyter notebook file
  2. Function.py - Consists of additional function required in command line application.
  3. Train.py - For training the neural network
  4. Predict.py - For predicting name of any flower image within the specified dataset.
  5. cat_to_name.json - Dictionary containing index and name of various flowers.

Downloads and Installations:

Install the following packages if you dont already have them:

  • Python 3.7: conda install python==3.7
  • Numpy 1.16: conda install numpy==1.16
  • Pytorch 1.1: conda install pytorch=1.1
  • Torchvision 0.3: conda install torchvision=0.3
  • Matplotlib 3.0: conda install matplotlib==3.0

How to use

  1. First, you will need to train a model on the training and validtion data and then load the trained model to make predictions using the test data.

  2. Once you are in the working directory of the repository to train a model (either VGG19 or Densenet121) you can run the train.py file like so:

  • python train.py './flowers'
  1. The ./flowers file path is the data directory that includes the train and valid folders that contain the training and validation images. By default the Densenet121 model will be used along with other pre-set hyperparameters. Here is a list of all of the arguments you can use in training along with some examples:
  • --data_directory: The relative path to the image files to train on. It should include two folders: 'train' and 'valid' for training.
  • --save_dir: The relative path to the directory you wish to save the trained model's checkpoint to. This file path must exist prior to training. Default is the current directory '/'
  • --arch: The architacture you wish to use for the training. This can either be 'vgg19' or 'densenet121'. Default is densenet121
  • --learning_rate: The learning rate for the training process. Default is 0.001
  • --hidden_units: The number of units used in the hidden layer. NOTE: There is only one hidden layer used in this project and thus only one hidden_unit required for training. Default is 512
  • --epochs: The amount of epochs to train for. Default is 10
  • --gpu: A boolean value for if you would like to use the GPU for training. Default is false
  1. Example use of all arguments:
  • python train.py './flowers' --save_dir './checkpoints' --arch 'densenet' --learning_rate 0.01 --hidden_units 500 --epochs 10 --gpu
  1. Using a trained model for inference Once you have a trained model we can use it to infer the flower names in the test folder. To do so use the predict.py file like so:
  • python predict.py './flowers/test/1/image_06743.jpg' './checkpoint.pth'
  1. The ./flowers/test/1/image_06743.jpg is the file path and name of the image we wish to infer on. The file extention is required. By default this will return the top 1 prediction. Here is a list of arguments you can use to get the top n predictions or use the GPU for inference
  • data_directory: The relative path to the image file that you want to infer on. The file name and extention are required.
  • checkpoint: The relative path to the models checkpoint pth file. The file name and extention are required.
  • --top_k: The amount of most likley classes to return for the predictions. Default is 1
  • --category_names: The json file, including file path, to load category names. Default is './cat_to_name.json'
  • --gpu: Boolean value to infer with the GPU. Default is False

Licence

MIT

About

Image Classification using Neural Network

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors