This project was done as a part of the AI Programming with Python Nanodegree by Udacity. Attached below are the project details with reference to the rubric provided.
This project involves training a deep learning model to classify images of flowers. The implementation consists of two main parts:
Ensure that all required files are included in the submission. Model checkpoints are not required for submission.
The development notebook should start with a cell that imports all necessary packages and modules.
# Example of package imports
import torch
from torchvision import transforms, models
from torch.utils.data import DataLoader
# Add other necessary imports
Utilize torchvision transforms to augment the training data with random scaling, rotations, mirroring, and/or cropping.
# Example of training data augmentation
train_transform = transforms.Compose([
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
# Add other augmentation techniques
])
Appropriately crop and normalize the training, validation, and testing data.
# Example of data normalization
normalize_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
])
Load data for each set (train, validation, test) using torchvision's DataLoader.
# Example of data batching
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)
# Add loaders for validation and test sets
Load data for each set using torchvision's ImageFolder.
# Example of data loading
train_dataset = torchvision.datasets.ImageFolder(train_dir, transform=train_transform)
# Add datasets for validation and test sets
Load a pretrained network (e.g., VGG16) from torchvision.models and freeze its parameters.
# Example of loading a pretrained network
pretrained_model = models.vgg16(pretrained=True)
for param in pretrained_model.parameters():
param.requires_grad = False
Define a new feedforward network for use as a classifier using the features as input.
# Example of defining a feedforward classifier
classifier = torch.nn.Sequential(
torch.nn.Linear(25088, 4096),
torch.nn.ReLU(),
# Add other layers
)
Train the feedforward classifier while keeping the feature network parameters static.
# Example of training the network
criterion = torch.nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.classifier.parameters(), lr=0.001)
# Add a training loop
Measure the network's accuracy on the test data.
# Example of testing accuracy
# Evaluate the model on the test set
Display validation loss and accuracy during training.
# Example of displaying validation loss and accuracy
# Add a validation loop with loss and accuracy calculation
Implement a function that successfully loads a checkpoint and rebuilds the model.
# Example of loading checkpoints
# Implement a function to load checkpoints
Save the trained model as a checkpoint with associated hyperparameters and the class_to_idx dictionary.
# Example of saving the model
# Save model checkpoint with hyperparameters and class_to_idx
Implement the process_image
function that converts a PIL image into an object usable as input to a trained model.
# Example of image processing
# Implement the process_image function
Implement the predict
function that takes the path to an image and a checkpoint, then returns the top K most probable classes.
# Example of class prediction
# Implement the predict function
Create a matplotlib figure displaying an image and its top 5 most probable classes with actual flower names.
# Example of sanity checking with matplotlib
# Create a matplotlib figure for sanity checking
Ensure that all required files are included in the submission. Model checkpoints are not required for submission.
Ensure that train.py
successfully trains a new network on a dataset of images and saves the model to a checkpoint.
python train.py --data_dir 'path/to/data' --arch 'vgg16' --learning_rate 0.001 --hidden_units 4096 --epochs 10 --gpu
Print out the training loss, validation loss, and validation accuracy as the network trains.
# Example of printing training/validation log
# Print out training/validation loss and accuracy during training
Allow users to choose from at least two different architectures available from torchvision.models.
python train.py --data_dir 'path/to/data' --arch 'resnet50' --learning_rate 0.001 --hidden_units 4096 --epochs 10 --gpu
Allow users to set hyperparameters for learning rate, the number of hidden units, and training epochs.
python train.py --data_dir 'path/to/data' --arch 'vgg16' --learning_rate 0.01 --hidden_units 512 --epochs 15 --gpu
Allow users to choose training the model on a GPU.
python train.py --data_dir 'path/to/data' --arch 'vgg16' --learning_rate 0.001 --hidden_units 4096 --epochs 10 --gpu
Ensure that predict.py
successfully reads in an image and a checkpoint, then prints the most likely image class and its associated probability.
python predict.py --image_path 'path/to/image.jpg' --checkpoint 'path/to/checkpoint.pth'
Allow users to print out the top K classes along with associated probabilities.
python predict.py --image_path 'path/to/image.jpg' --checkpoint 'path/to/checkpoint.pth' --top_k 3
Allow users to load a JSON file that maps class values to other category names.
python predict.py --image_path 'path/to/image.jpg' --checkpoint 'path/to/checkpoint.pth' --category_names 'path/to/cat_to_name.json'
Allow users to use the GPU to calculate predictions.
python predict.py --image_path 'path/to/image.jpg' --checkpoint 'path/to/checkpoint.pth' --gpu