Going forward, AI algorithms will be incorporated into more and more everyday applications. For example, you might want to include an image classifier in a smart phone app. To do this, you'd use a deep learning model trained on hundreds of thousands of images as part of the overall application architecture. A large part of software development in the future will be using these types of models as common parts of applications.
In this project, you'll train an image classifier to recognize different species of flowers. You can imagine using something like this in a phone app that tells you the name of the flower your camera is looking at. In practice you'd train this classifier, then export it for use in your application. We'll be using this dataset of 102 flower categories, you can see a few examples below.
The project is broken down into multiple steps:
We'll lead you through each part which you'll implement in Python.
When you've completed this project, you'll have an application that can be trained on any set of labeled images. Here your network will be learning about flowers and end up as a command line application. But, what you do with your new skills depends on your imagination and effort in building a dataset. For example, imagine an app where you take a picture of a car, it tells you what the make and model is, then looks up information about it. Go build your own dataset and make something new.
First up is importing the packages you'll need. It's good practice to keep all the imports at the beginning of your code. As you work through this notebook and find you need to import a package, make sure to add the import up here.
!mv workspace-utils.py workspace_utils.py
mv: cannot stat 'workspace-utils.py': No such file or directory
# Imports here
import os
import numpy as np
import torch
from torch import nn, optim
from torchvision import datasets, transforms, models
from torch.utils.data.dataloader import DataLoader
from PIL import Image
Here you'll use torchvision
to load the data (documentation). The data should be included alongside this notebook, otherwise you can download it here. The dataset is split into three parts, training, validation, and testing. For the training, you'll want to apply transformations such as random scaling, cropping, and flipping. This will help the network generalize leading to better performance. You'll also need to make sure the input data is resized to 224x224 pixels as required by the pre-trained networks.
The validation and testing sets are used to measure the model's performance on data it hasn't seen yet. For this you don't want any scaling or rotation transformations, but you'll need to resize then crop the images to the appropriate size.
The pre-trained networks you'll use were trained on the ImageNet dataset where each color channel was normalized separately. For all three sets you'll need to normalize the means and standard deviations of the images to what the network expects. For the means, it's [0.485, 0.456, 0.406]
and for the standard deviations [0.229, 0.224, 0.225]
, calculated from the ImageNet images. These values will shift each color channel to be centered at 0 and range from -1 to 1.
data_dir = '/content'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
# TODO: Define your transforms for the training, validation, and testing sets
train_transforms = transforms.Compose([
transforms.Resize(225),
transforms.ColorJitter(brightness=2),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(30),
transforms.RandomResizedCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
test_transforms = transforms.Compose([
transforms.Resize(225),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
])
# TODO: Load the datasets with ImageFolder
train_dataset = datasets.ImageFolder(train_dir, transform=train_transforms)
valid_dataset = datasets.ImageFolder(valid_dir, transform=test_transforms)
test_dataset = datasets.ImageFolder(test_dir, transform=test_transforms)
# TODO: Using the image datasets and the trainforms, define the dataloaders
trainloader = DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)
validloader = DataLoader(dataset=valid_dataset, batch_size=64, shuffle=True)
testloader = DataLoader(dataset=test_dataset, batch_size=64, shuffle=True)
You'll also need to load in a mapping from category label to category name. You can find this in the file cat_to_name.json
. It's a JSON object which you can read in with the json
module. This will give you a dictionary mapping the integer encoded categories to the actual names of the flowers.
import json
with open('cat_to_name.json', 'r') as f:
cat_to_name = json.load(f)
Now that the data is ready, it's time to build and train the classifier. As usual, you should use one of the pretrained models from torchvision.models
to get the image features. Build and train a new feed-forward classifier using those features.
We're going to leave this part up to you. Refer to the rubric for guidance on successfully completing this section. Things you'll need to do:
We've left a cell open for you below, but use as many as you need. Our advice is to break the problem up into smaller parts you can run separately. Check that each part is doing what you expect, then move on to the next. You'll likely find that as you work through each part, you'll need to go back and modify your previous code. This is totally normal!
When training make sure you're updating only the weights of the feed-forward network. You should be able to get the validation accuracy above 70% if you build everything right. Make sure to try different hyperparameters (learning rate, units in the classifier, epochs, etc) to find the best model. Save those hyperparameters to use as default values in the next part of the project.
One last important tip if you're using the workspace to run your code: To avoid having your workspace disconnect during the long-running tasks in this notebook, please read in the earlier page in this lesson called Intro to GPU Workspaces about Keeping Your Session Active. You'll want to include code from the workspace_utils.py module.
Note for Workspace users: If your network is over 1 GB when saved as a checkpoint, there might be issues with saving backups in your workspace. Typically this happens with wide dense layers after the convolutional layers. If your saved checkpoint is larger than 1 GB (you can open a terminal and check with ls -lh
), you should reduce the size of your hidden layers and train again.
# TODO: Build and train your network
import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(16,10))
j = 0
for lr in (1e-3, 2e-3, 8e-3):
n_epoch = 15
train_losses = []
valid_losses = []
model = models.resnet34(pretrained=True)
for param in model.parameters():
param.requires_grad = False
from collections import OrderedDict
fc = nn.Sequential(OrderedDict([
("fc1", nn.Linear(512, 128)),
("relu1", nn.ReLU()),
("fc2", nn.Linear(128, 102)),
("output", nn.LogSoftmax(dim=1)),
]))
model.fc = fc
criterion = nn.NLLLoss()
optimizer = optim.Adam(params=model.fc.parameters(), lr=lr)
model.cuda()
for n in range(n_epoch):
if n % 5 == 0:
print("==================================")
print(f"Current lr:{lr}, n_epoch:{n}")
train_loss = 0
valid_loss = 0
accuracy = 0
model.train()
for images, labels in trainloader:
images, labels = images.to("cuda"), labels.to("cuda")
output = model(images)
loss = criterion(output, labels)
train_loss += loss.item()
optimizer.zero_grad()
loss.backward()
optimizer.step()
model.eval()
with torch.no_grad():
for images, labels in validloader:
images, labels = images.to("cuda"), labels.to("cuda")
output = model(images)
loss = criterion(output, labels)
valid_loss += loss.item()
ps = torch.exp(output)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
train_losses.append(train_loss)
valid_losses.append(valid_loss)
print("------------------------------------------------")
print(f"Train loss: {train_loss}, Valid loss: {valid_loss}, Accuracy on valid: {accuracy/len(validloader)}")
ax[j].plot(train_losses)
ax[j].plot(valid_losses)
j += 1
plt.show()
================================== Current lr:0.001, n_epoch:0 ------------------------------------------------ Train loss: 418.950665473938, Valid loss: 40.685494899749756, Accuracy on valid: 0.311346173286438 ------------------------------------------------ Train loss: 298.29711389541626, Valid loss: 25.75662636756897, Accuracy on valid: 0.5188942551612854 ------------------------------------------------ Train loss: 225.12093782424927, Valid loss: 19.166014671325684, Accuracy on valid: 0.6213461756706238 ------------------------------------------------ Train loss: 195.53204464912415, Valid loss: 16.174536883831024, Accuracy on valid: 0.6673557758331299 ------------------------------------------------ Train loss: 174.79864609241486, Valid loss: 14.773742079734802, Accuracy on valid: 0.6908653974533081 ================================== Current lr:0.001, n_epoch:5 ------------------------------------------------ Train loss: 163.4851371049881, Valid loss: 13.235257685184479, Accuracy on valid: 0.7260577082633972 ------------------------------------------------ Train loss: 153.12056577205658, Valid loss: 11.594764471054077, Accuracy on valid: 0.7614423036575317 ------------------------------------------------ Train loss: 144.46040737628937, Valid loss: 10.503775954246521, Accuracy on valid: 0.7785577178001404 ------------------------------------------------ Train loss: 141.63204181194305, Valid loss: 10.03523999452591, Accuracy on valid: 0.792644202709198 ------------------------------------------------ Train loss: 139.77888357639313, Valid loss: 9.740492969751358, Accuracy on valid: 0.8005288243293762 ================================== Current lr:0.001, n_epoch:10 ------------------------------------------------ Train loss: 132.28707921504974, Valid loss: 8.870001316070557, Accuracy on valid: 0.813076913356781 ------------------------------------------------ Train loss: 129.00571089982986, Valid loss: 8.92706897854805, Accuracy on valid: 0.8199999928474426 ------------------------------------------------ Train loss: 128.02008360624313, Valid loss: 9.511207640171051, Accuracy on valid: 0.7840865254402161 ------------------------------------------------ Train loss: 123.60395073890686, Valid loss: 8.496912211179733, Accuracy on valid: 0.8187500238418579 ------------------------------------------------ Train loss: 125.8921599984169, Valid loss: 8.197636038064957, Accuracy on valid: 0.8250961303710938 ================================== Current lr:0.002, n_epoch:0 ------------------------------------------------ Train loss: 410.631160736084, Valid loss: 37.74876618385315, Accuracy on valid: 0.31822115182876587 ------------------------------------------------ Train loss: 277.9698621034622, Valid loss: 22.310233235359192, Accuracy on valid: 0.564182698726654 ------------------------------------------------ Train loss: 215.07868921756744, Valid loss: 18.462525486946106, Accuracy on valid: 0.6213461756706238 ------------------------------------------------ Train loss: 189.6777387857437, Valid loss: 15.210036873817444, Accuracy on valid: 0.671298086643219 ------------------------------------------------ Train loss: 172.59169578552246, Valid loss: 14.223050475120544, Accuracy on valid: 0.6939423084259033 ================================== Current lr:0.002, n_epoch:5 ------------------------------------------------ Train loss: 164.86340242624283, Valid loss: 12.605363190174103, Accuracy on valid: 0.7267307639122009 ------------------------------------------------ Train loss: 159.9641396999359, Valid loss: 12.810842335224152, Accuracy on valid: 0.7226442098617554 ------------------------------------------------ Train loss: 150.854336977005, Valid loss: 10.977665543556213, Accuracy on valid: 0.7649999856948853 ------------------------------------------------ Train loss: 144.65757554769516, Valid loss: 10.968836069107056, Accuracy on valid: 0.7653846144676208 ------------------------------------------------ Train loss: 141.68359035253525, Valid loss: 10.170245230197906, Accuracy on valid: 0.7742788791656494 ================================== Current lr:0.002, n_epoch:10 ------------------------------------------------ Train loss: 137.4058312177658, Valid loss: 9.615502715110779, Accuracy on valid: 0.7960577011108398 ------------------------------------------------ Train loss: 139.1810044646263, Valid loss: 9.723607510328293, Accuracy on valid: 0.776490330696106 ------------------------------------------------ Train loss: 134.91813379526138, Valid loss: 9.427866518497467, Accuracy on valid: 0.8113461136817932 ------------------------------------------------ Train loss: 133.05849087238312, Valid loss: 9.184632748365402, Accuracy on valid: 0.8038461208343506 ------------------------------------------------ Train loss: 130.4970781803131, Valid loss: 8.565993070602417, Accuracy on valid: 0.8118749856948853 ================================== Current lr:0.008, n_epoch:0 ------------------------------------------------ Train loss: 400.09451246261597, Valid loss: 35.787134885787964, Accuracy on valid: 0.27600961923599243 ------------------------------------------------ Train loss: 292.4679379463196, Valid loss: 30.81237244606018, Accuracy on valid: 0.3460577130317688 ------------------------------------------------ Train loss: 264.6471927165985, Valid loss: 25.195542097091675, Accuracy on valid: 0.4392307698726654 ------------------------------------------------ Train loss: 249.93690299987793, Valid loss: 25.612703800201416, Accuracy on valid: 0.42326924204826355 ------------------------------------------------ Train loss: 244.70455527305603, Valid loss: 24.73864245414734, Accuracy on valid: 0.4838942289352417 ================================== Current lr:0.008, n_epoch:5 ------------------------------------------------ Train loss: 232.93597400188446, Valid loss: 24.096962809562683, Accuracy on valid: 0.4704807698726654 ------------------------------------------------ Train loss: 237.75001800060272, Valid loss: 26.645978212356567, Accuracy on valid: 0.4395192265510559 ------------------------------------------------ Train loss: 238.83601796627045, Valid loss: 25.291427969932556, Accuracy on valid: 0.46379807591438293 ------------------------------------------------ Train loss: 237.7499564886093, Valid loss: 23.217052698135376, Accuracy on valid: 0.5052884817123413 ------------------------------------------------ Train loss: 236.45088756084442, Valid loss: 23.012110352516174, Accuracy on valid: 0.5018749833106995 ================================== Current lr:0.008, n_epoch:10 ------------------------------------------------ Train loss: 231.9217437505722, Valid loss: 22.291985750198364, Accuracy on valid: 0.5024519562721252 ------------------------------------------------ Train loss: 233.25756752490997, Valid loss: 22.555623292922974, Accuracy on valid: 0.5036057829856873 ------------------------------------------------ Train loss: 225.2420276403427, Valid loss: 21.973045468330383, Accuracy on valid: 0.502548098564148 ------------------------------------------------ Train loss: 231.1224936246872, Valid loss: 21.36587083339691, Accuracy on valid: 0.5170192122459412 ------------------------------------------------ Train loss: 226.86410331726074, Valid loss: 23.74612319469452, Accuracy on valid: 0.4956730604171753
## Real model
lr = 2e-3
n_epoch =10
model = models.resnet34(pretrained=True)
for param in model.parameters():
param.requires_grad = False
from collections import OrderedDict
fc = nn.Sequential(OrderedDict([
("fc1", nn.Linear(512, 128)),
("relu1", nn.ReLU()),
("fc2", nn.Linear(128, 102)),
("output", nn.LogSoftmax(dim=1)),
]))
model.fc = fc
criterion = nn.NLLLoss()
optimizer = optim.Adam(params=model.fc.parameters(), lr=lr)
model.cuda()
for n in range(n_epoch):
model.train()
for images, labels in trainloader:
images, labels = images.to("cuda"), labels.to("cuda")
output = model(images)
loss = criterion(output, labels)
model.fc.zero_grad()
loss.backward()
optimizer.step()
It's good practice to test your trained network on test data, images the network has never seen either in training or validation. This will give you a good estimate for the model's performance on completely new images. Run the test images through the network and measure the accuracy, the same way you did validation. You should be able to reach around 70% accuracy on the test set if the model has been trained well.
# TODO: Do validation on the test set
model.eval()
accuracy = 0
with torch.no_grad():
for images, labels in testloader:
images, labels = images.to("cuda"), labels.to("cuda")
output = model(images)
ps = torch.exp(output)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
print(accuracy/len(testloader))
tensor(0.7605)
Now that your network is trained, save the model so you can load it later for making predictions. You probably want to save other things such as the mapping of classes to indices which you get from one of the image datasets: image_datasets['train'].class_to_idx
. You can attach this to the model as an attribute which makes inference easier later on.
model.class_to_idx = image_datasets['train'].class_to_idx
Remember that you'll want to completely rebuild the model later so you can use it for inference. Make sure to include any information you need in the checkpoint. If you want to load the model and keep training, you'll want to save the number of epochs as well as the optimizer state, optimizer.state_dict
. You'll likely want to use this trained model in the next part of the project, so best to save it now.
# TODO: Save the checkpoint
model.class_to_idx = train_dataset.class_to_idx
checkpoint = {
"class_to_idx": model.class_to_idx,
"input_size": 224,
"output_size": 102,
"n_epoch": 10,
"lr": 2e-3,
"state_dict": model.state_dict()
}
torch.save(checkpoint, "checkpoint.pth")
At this point it's good to write a function that can load a checkpoint and rebuild the model. That way you can come back to this project and keep working on it without having to retrain the network.
from collections import OrderedDict
def load_checkpoint(checkpoint_pth):
# Declare model type
model = models.resnet34(pretrained=True)
for param in model.parameters():
param.requires_grad = False
fc = nn.Sequential(OrderedDict([
("fc1", nn.Linear(512, 128)),
("relu1", nn.ReLU()),
("fc2", nn.Linear(128, 102)),
("output", nn.LogSoftmax(dim=1)),
]))
model.fc = fc
checkpoint = torch.load(checkpoint_pth)
model.class_to_idx = checkpoint["class_to_idx"]
model.load_state_dict(state_dict=checkpoint["state_dict"])
return model
model2 = load_checkpoint("checkpoint.pth")
/usr/local/lib/python3.7/dist-packages/torchvision/models/_utils.py:209: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and will be removed in 0.15, please use 'weights' instead. f"The parameter '{pretrained_param}' is deprecated since 0.13 and will be removed in 0.15, " /usr/local/lib/python3.7/dist-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and will be removed in 0.15. The current behavior is equivalent to passing `weights=ResNet34_Weights.IMAGENET1K_V1`. You can also use `weights=ResNet34_Weights.DEFAULT` to get the most up-to-date weights. warnings.warn(msg)
model2.eval()
model2.cuda()
accuracy = 0
with torch.no_grad():
for images, labels in testloader:
images, labels = images.to("cuda"), labels.to("cuda")
output = model2(images)
ps = torch.exp(output)
top_p, top_class = ps.topk(1, dim=1)
equals = top_class == labels.view(top_class.shape)
accuracy += torch.mean(equals.type(torch.FloatTensor))
print(accuracy/len(testloader))
tensor(0.7599)
!ls -sh
total 84M 4.0K assets 4.0K LICENSE 4.0K cat_to_name.json 0 predict.py 82M checkpoint.pth 4.0K __pycache__ 0 flowers 4.0K README.md 1.6M Image Classifier Project.ipynb 0 train.py 16K Image Classifier Project-zh.ipynb 4.0K workspace_utils.py
Now you'll write a function to use a trained network for inference. That is, you'll pass an image into the network and predict the class of the flower in the image. Write a function called predict
that takes an image and a model, then returns the top $K$ most likely classes along with the probabilities. It should look like
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']
First you'll need to handle processing the input image such that it can be used in your network.
You'll want to use PIL
to load the image (documentation). It's best to write a function that preprocesses the image so it can be used as input for the model. This function should process the images in the same manner used for training.
First, resize the images where the shortest side is 256 pixels, keeping the aspect ratio. This can be done with the thumbnail
or resize
methods. Then you'll need to crop out the center 224x224 portion of the image.
Color channels of images are typically encoded as integers 0-255, but the model expected floats 0-1. You'll need to convert the values. It's easiest with a Numpy array, which you can get from a PIL image like so np_image = np.array(pil_image)
.
As before, the network expects the images to be normalized in a specific way. For the means, it's [0.485, 0.456, 0.406]
and for the standard deviations [0.229, 0.224, 0.225]
. You'll want to subtract the means from each color channel, then divide by the standard deviation.
And finally, PyTorch expects the color channel to be the first dimension but it's the third dimension in the PIL image and Numpy array. You can reorder dimensions using ndarray.transpose
. The color channel needs to be first and retain the order of the other two dimensions.
def process_image(image):
''' Scales, crops, and normalizes a PIL image for a PyTorch model,
returns an Numpy array
'''
np_img = None
with Image.open(image) as im:
w,h = im.size
min_s = min(w,h)
if min_s == w:
w = 256
h = h * 256 // w
else:
h = 256
w = w * 256 // h
im.thumbnail((w, h))
w, h = im.size
(left, upper, right, lower) = w//2-224//2, h//2-224/2, w//2+224//2, h//2+224//2
im_cropped = im.crop((left, upper, right, lower))
np_img = np.array(im_cropped) / 255
arr = (np_img - np.array([0.485, 0.456, 0.406])) / np.array([0.229, 0.224, 0.225])
return torch.from_numpy(arr.transpose(2,0,1))
To check your work, the function below converts a PyTorch tensor and displays it in the notebook. If your process_image
function works, running the output through this function should return the original image (except for the cropped out portions).
def imshow(image, ax=None, title=None):
"""Imshow for Tensor."""
if ax is None:
fig, ax = plt.subplots()
# PyTorch tensors assume the color channel is the first dimension
# but matplotlib assumes is the third dimension
image = image.numpy().transpose((1, 2, 0))
# Undo preprocessing
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image = std * image + mean
# Image needs to be clipped between 0 and 1 or it looks like noise when displayed
image = np.clip(image, 0, 1)
ax.imshow(image)
return ax
imshow(process_image("train/1/image_06734.jpg"))
<matplotlib.axes._subplots.AxesSubplot at 0x7f90510cea10>
Once you can get images in the correct format, it's time to write a function for making predictions with your model. A common practice is to predict the top 5 or so (usually called top-$K$) most probable classes. You'll want to calculate the class probabilities then find the $K$ largest values.
To get the top $K$ largest values in a tensor use x.topk(k)
. This method returns both the highest k
probabilities and the indices of those probabilities corresponding to the classes. You need to convert from these indices to the actual class labels using class_to_idx
which hopefully you added to the model or from an ImageFolder
you used to load the data (see here). Make sure to invert the dictionary so you get a mapping from index to class as well.
Again, this method should take a path to an image and a model checkpoint, then return the probabilities and classes.
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']
cat_to_name["1"]
'pink primrose'
def predict(image_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
probs, classes = None, None
image = process_image(image_path)
image = image.view((1,3,224,224)).type(torch.FloatTensor)
model.eval()
model.cuda()
with torch.no_grad():
image = image.to("cuda")
output = model(image)
ps = torch.exp(output)
top_p, top_class = ps.topk(topk, dim=1)
probs, classes = top_p.tolist()[0], top_class.tolist()[0]
return probs, classes
# TODO: Implement the code to predict the class from an image file
probs, classes = predict("train/1/image_06734.jpg", model=model2)
Now that you can use a trained model for predictions, check to make sure it makes sense. Even if the testing accuracy is high, it's always good to check that there aren't obvious bugs. Use matplotlib
to plot the probabilities for the top 5 classes as a bar graph, along with the input image. It should look like this:
You can convert from the class integer encoding to actual flower names with the cat_to_name.json
file (should have been loaded earlier in the notebook). To show a PyTorch tensor as an image, use the imshow
function defined above.
res = dict((v,k) for k,v in model2.class_to_idx.items())
types = [cat_to_name[res[i]] for i in classes]
plt.barh(types, probs)
<BarContainer object of 5 artists>
def show_sol(img_pth, model, topk=5):
fig, ax = plt.subplots(nrows=2, figsize=(10,8))
imshow(process_image(img_pth), ax=ax[0])
probs, classes = predict(img_pth, model)
types = [cat_to_name[res[i]] for i in classes]
ax[1].barh(types, probs)
show_sol("train/1/image_06734.jpg", model=model2)
Reminder for Workspace users: If your network becomes very large when saved as a checkpoint, there might be issues with saving backups in your workspace. You should reduce the size of your hidden layers and train again.
We strongly encourage you to delete these large interim files and directories before navigating to another page or closing the browser tab.
# TODO remove .pth files or move it to a temporary `~/opt` directory in this Workspace