logo

PYTORCH-CLASSIFIER

Gallery details

This project builds a deep learning network to identify 102 different types of flowers. The dataset was obtained from the 102 category flowers dataset. While this specific example is used on this data, this model can be trained on any set of labeled images. Below are a few examples of the variability between classes and within the classes themselves.

CLASS VARIABILITY BETWEEN CLASSES-

The 3 images below are: (Spear Thistle) (Fire Lily) (Cantenbury Bells)

CLASS VARIABILITY WITHIN CLASSES-

Each of the 3 images below is a Toad Lily

ARCHITECTURE-

Click for Github – Project Source Code

Deep Learning Network Testing Snippets

1.DEVELOPING THE APPLICATION –

The project is broken down into multiple steps:

  • Load and preprocess the image dataset
  • Train the image classifier on the dataset
  • Use the trained classifier to predict image content
Data | Network | Utility Classes & Functions
Data LoadNetwork ClassNet UtilityLoad NetworkImage ProcessData MappingPredictPlot Results
[snippet slug=classifier-data lang=python]
[snippet slug=classifier-network lang=python]
[snippet slug=classifier-net-utils lang=python]
[snippet slug=classifier-load lang=python]
[snippet slug=classifier-imageprocess lang=python]
[snippet slug=classifier-mapping lang=python]
[snippet slug=classfier-predict lang=python]
[snippet slug=classfier-plotimages-predictions lang=python]

.Build and train network –

Building and training the classifier:

  • Load a pre-trained network
  • Define a new, untrained feed-forward network as a classifier, choose activation functions and dropouts
  • Train the classifier layer using backpropagation using the pre-trained network to get the features
  • Track the loss and accuracy on the validation set to determine the best hyperparameters
[snippet slug=classifier-build-train lang=python]

 

.Test the network-

[snippet slug=classifier-test lang=python]

 

  • acc on test is 85.90214940217825 %
  • loss is 0.5098767114373354

.Save the checkpoint-

[snippet slug=classifier-save lang=python]

 

.Load the checkpoint-

[snippet slug=classifier-load-model lang=python]

 

.Inference for classification-

  • probs, classes = predict(image_path, model)
  • print(probs)
  • print(classes)
  • > [ 0.01558163 0.01541934 0.01452626 0.01443549 0.01407339]
  • > [’70’, ‘3’, ’45’, ’62’, ’55’]

.Image Processing & Class Predictions

[snippet slug=classifier-get-results lang=python]

 

.Plot Results-

To visualize more than 1 result at a time I added this function, displays a grid of n results with image and prediction

 

2.COMMAND LINE APPLICATION SPECIFICATIONS –

The project submission must include at least two files train.py and predict.py. The first file, train.py, will train a new network on a dataset and save the model as a checkpoint. The second file, predict.py, uses a trained network to predict the class for an input image.

    • Train a new network on a data set with train.py :
      • Basic usage: python train.py data_directory
      • Prints out training loss, validation loss, and validation accuracy as the network trains
      • Options:
        • Set directory to save checkpoints:
          • python train.py data_dir –save_dir save_directory
        • Choose architecture: python train.py data_dir --arch "vgg13"
        • Set hyperparameters:
          • python train.py data_dir –learning_rate 0.01 –hidden_units 512 –epochs 20
        • Use GPU for training: python train.py data_dir --gpu
    • Predict flower name from an image with predict.py along with the probability of that name. That is, you’ll pass in a single image /path/to/image and return the flower name and class probability.
      • Basic usage: python predict.py /path/to/image checkpoint
      • Options:
        • Return top K most likely classes:
          • python predict.py input checkpoint –top_k 3
        • Use a mapping of categories to real names:
          • python predict.py input checkpoint –category_names cat_to_name.json
        • Use GPU for inference: python predict.py input checkpoint --gpu
Command Line Application
Predict.pyTrain.py
[snippet slug=classifier-command-predict lang=python]
[snippet slug=classifier-commandline-train lang=python]

.Test command line application-

  • Share

Leave a reply

Your email address will not be published. Required fields are marked *