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.
Please make sure if you are running this notebook in the workspace that you have chosen GPU rather than CPU mode.
# Imports here
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
import matplotlib.pyplot as plt
import torch
import numpy as np
from torch import nn
from torch import optim
import torch.nn.functional as F
from torchvision import datasets, transforms, models
from workspace_utils import active_session
from PIL import Image
from collections import OrderedDict
import json
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 = 'flowers'
train_dir = data_dir + '/train'
valid_dir = data_dir + '/valid'
test_dir = data_dir + '/test'
using_gpu = torch.cuda.is_available()
# TODO: Define your transforms for the training, validation, and testing sets
train_transforms = transforms.Compose([transforms.RandomRotation(30),
transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
testval_transforms = transforms.Compose([transforms.Resize(256),
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
image_trainset = datasets.ImageFolder(train_dir, transform=train_transforms)
image_testset = datasets.ImageFolder(test_dir, transform=testval_transforms)
image_valset = datasets.ImageFolder(valid_dir, transform=testval_transforms)
# TODO: Using the image datasets and the trainforms, define the dataloaders
image_trainloader = torch.utils.data.DataLoader(image_trainset, batch_size=64, shuffle=True)
image_testloader = torch.utils.data.DataLoader(image_testset, batch_size=64, shuffle=True)
image_valloader = torch.utils.data.DataLoader(image_valset, 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.
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.
# TODO: Build and train your network
epochs = 4
lr = 0.001
print_every = 10
# Freeze parameters so we don't backprop through them
hidden_layers = [10240, 1024]
def make_model(structure, hidden_layers, lr):
if structure=="densenet161":
model = models.densenet161(pretrained=True)
input_size = 2208
else:
model = models.vgg16(pretrained=True)
input_size = 25088
output_size = 102
for param in model.parameters():
param.requires_grad = False
classifier = nn.Sequential(OrderedDict([
('dropout',nn.Dropout(0.5)),
('fc1', nn.Linear(input_size, hidden_layers[0])),
('relu1', nn.ReLU()),
('fc2', nn.Linear(hidden_layers[0], hidden_layers[1])),
('relu2', nn.ReLU()),
('fc3', nn.Linear(hidden_layers[1], output_size)),
('output', nn.LogSoftmax(dim=1))
]))
model.classifier = classifier
return model
model = make_model('vgg16', hidden_layers, lr)
criterion = nn.NLLLoss()
optimizer = optim.Adam(model.classifier.parameters(), lr=lr)
def cal_accuracy(model, dataloader):
validation_loss = 0
accuracy = 0
for i, (inputs,labels) in enumerate(dataloader):
optimizer.zero_grad()
inputs, labels = inputs.to('cuda') , labels.to('cuda')
model.to('cuda')
with torch.no_grad():
outputs = model.forward(inputs)
validation_loss = criterion(outputs,labels)
ps = torch.exp(outputs).data
equality = (labels.data == ps.max(1)[1])
accuracy += equality.type_as(torch.FloatTensor()).mean()
validation_loss = validation_loss / len(dataloader)
accuracy = accuracy /len(dataloader)
return validation_loss, accuracy
with active_session():
def my_DLM(model, image_trainloader, image_valloader, epochs, print_every, criterion, optimizer, device='gpu'):
epochs = epochs
print_every = print_every
steps = 0
# change to cuda
model.to('cuda')
for e in range(epochs):
running_loss = 0
for ii, (inputs, labels) in enumerate(image_trainloader):
steps += 1
inputs, labels = inputs.to('cuda'), labels.to('cuda')
optimizer.zero_grad()
# Forward and backward passes
outputs = model.forward(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if steps % print_every == 0:
model.eval()
val_loss, train_ac = cal_accuracy(model, image_valloader)
print("Epoch: {}/{}... | ".format(e+1, epochs),
"Loss: {:.4f} | ".format(running_loss/print_every),
"Validation Loss {:.4f} | ".format(val_loss),
"Accuracy {:.4f}".format(train_ac))
running_loss = 0
my_DLM(model, image_trainloader, image_valloader, epochs, print_every, criterion, optimizer, 'gpu')
Epoch: 1/4... | Loss: 6.4524 | Validation Loss 0.3465 | Accuracy 0.0295 Epoch: 1/4... | Loss: 4.4584 | Validation Loss 0.3212 | Accuracy 0.1000 Epoch: 1/4... | Loss: 4.0043 | Validation Loss 0.2779 | Accuracy 0.2329 Epoch: 1/4... | Loss: 3.4299 | Validation Loss 0.2149 | Accuracy 0.3459 Epoch: 1/4... | Loss: 2.8110 | Validation Loss 0.1520 | Accuracy 0.4691 Epoch: 1/4... | Loss: 2.3905 | Validation Loss 0.1409 | Accuracy 0.5067 Epoch: 1/4... | Loss: 2.1229 | Validation Loss 0.1574 | Accuracy 0.5879 Epoch: 1/4... | Loss: 1.8654 | Validation Loss 0.0935 | Accuracy 0.6126 Epoch: 1/4... | Loss: 1.7904 | Validation Loss 0.0804 | Accuracy 0.6531 Epoch: 1/4... | Loss: 1.7925 | Validation Loss 0.0588 | Accuracy 0.6823 Epoch: 2/4... | Loss: 0.9521 | Validation Loss 0.0989 | Accuracy 0.6787 Epoch: 2/4... | Loss: 1.4292 | Validation Loss 0.0681 | Accuracy 0.7240 Epoch: 2/4... | Loss: 1.4400 | Validation Loss 0.0685 | Accuracy 0.7422 Epoch: 2/4... | Loss: 1.1558 | Validation Loss 0.0631 | Accuracy 0.7508 Epoch: 2/4... | Loss: 1.1908 | Validation Loss 0.0793 | Accuracy 0.7537 Epoch: 2/4... | Loss: 1.1530 | Validation Loss 0.0374 | Accuracy 0.8083 Epoch: 2/4... | Loss: 1.0959 | Validation Loss 0.0445 | Accuracy 0.7878 Epoch: 2/4... | Loss: 1.0612 | Validation Loss 0.1145 | Accuracy 0.7927 Epoch: 2/4... | Loss: 1.1214 | Validation Loss 0.0712 | Accuracy 0.7605 Epoch: 2/4... | Loss: 1.0777 | Validation Loss 0.0675 | Accuracy 0.7990 Epoch: 3/4... | Loss: 0.4120 | Validation Loss 0.0518 | Accuracy 0.7844 Epoch: 3/4... | Loss: 0.9345 | Validation Loss 0.0469 | Accuracy 0.8107 Epoch: 3/4... | Loss: 0.9003 | Validation Loss 0.0599 | Accuracy 0.7949 Epoch: 3/4... | Loss: 0.8103 | Validation Loss 0.0423 | Accuracy 0.8115 Epoch: 3/4... | Loss: 0.8979 | Validation Loss 0.0524 | Accuracy 0.8002 Epoch: 3/4... | Loss: 0.8262 | Validation Loss 0.0339 | Accuracy 0.8498 Epoch: 3/4... | Loss: 0.8887 | Validation Loss 0.0295 | Accuracy 0.8441 Epoch: 3/4... | Loss: 0.8642 | Validation Loss 0.0449 | Accuracy 0.8248 Epoch: 3/4... | Loss: 0.8362 | Validation Loss 0.0366 | Accuracy 0.8551 Epoch: 3/4... | Loss: 0.8648 | Validation Loss 0.0401 | Accuracy 0.8534 Epoch: 4/4... | Loss: 0.0714 | Validation Loss 0.0595 | Accuracy 0.8376 Epoch: 4/4... | Loss: 0.9117 | Validation Loss 0.0505 | Accuracy 0.8198 Epoch: 4/4... | Loss: 0.8286 | Validation Loss 0.0579 | Accuracy 0.8262 Epoch: 4/4... | Loss: 0.7527 | Validation Loss 0.0454 | Accuracy 0.8586 Epoch: 4/4... | Loss: 0.8232 | Validation Loss 0.0177 | Accuracy 0.8680 Epoch: 4/4... | Loss: 0.8381 | Validation Loss 0.0441 | Accuracy 0.8471 Epoch: 4/4... | Loss: 0.6254 | Validation Loss 0.0303 | Accuracy 0.8397 Epoch: 4/4... | Loss: 0.7069 | Validation Loss 0.0434 | Accuracy 0.8546 Epoch: 4/4... | Loss: 0.8144 | Validation Loss 0.0592 | Accuracy 0.8371 Epoch: 4/4... | Loss: 0.7457 | Validation Loss 0.0560 | Accuracy 0.8632 Epoch: 4/4... | Loss: 0.7427 | Validation Loss 0.0354 | Accuracy 0.8579
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
def testing(dataloader):
model.eval()
model.to('cuda')
correct = 0
total = 0
with torch.no_grad():
for inputs, labels in image_testloader:
inputs, labels = inputs.to('cuda'), labels.to('cuda')
outputs = model(inputs)
_ , prediction = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (prediction == labels.data).sum().item()
print('Accuracy on the test set: %d %%' % (100 * correct / total))
testing(image_testloader)
Accuracy on the test set: 85 %
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.
model.class_to_idx = image_trainset.class_to_idx
# TODO: Save the checkpoint
state = {
'structure' :'vgg16',
'learning_rate': lr,
'epochs': epochs,
'hidden_layers':hidden_layers,
'state_dict':model.state_dict(),
'class_to_idx':model.class_to_idx
}
torch.save(state, '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.
# TODO: Write a function that loads a checkpoint and rebuilds the model
def loading_checkpoint(path):
# Loading the parameters
state = torch.load(path)
lr = state['learning_rate']
structure = state['structure']
hidden_layers = state['hidden_layers']
epochs = state['epochs']
# Building the model from checkpoints
model = make_model(structure, hidden_layers, lr)
class_to_idx = state['class_to_idx']
model.load_state_dict(state['state_dict'])
loading_checkpoint('checkpoint.pth')
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
'''
# TODO: Process a PIL image for use in a PyTorch model
pil_image = Image.open(image)
image_transforms = transforms.Compose([
transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
img = image_transforms(pil_image)
return img
# Demo
image_path = (test_dir + '/100/' + 'image_07939.jpg')
processed_image = process_image(image_path)
processed_image
tensor([[[ 0.8789, 0.9132, 0.9474, ..., 0.9474, 1.0673, 1.1529], [ 0.8789, 0.9132, 0.9474, ..., 0.9988, 1.1358, 1.2557], [ 0.8789, 0.9132, 0.9303, ..., 1.0502, 1.1872, 1.3070], ..., [-1.6727, -1.5870, -1.4500, ..., -0.5596, -0.7650, -1.0390], [-1.6898, -1.6213, -1.5357, ..., -0.3027, -0.5253, -0.8164], [-1.6727, -1.6555, -1.6213, ..., -0.1657, -0.3541, -0.6109]], [[ 0.2927, 0.2752, 0.2577, ..., 0.5728, 0.6954, 0.8179], [ 0.3102, 0.2927, 0.2927, ..., 0.5553, 0.6779, 0.7829], [ 0.2927, 0.2752, 0.2752, ..., 0.5028, 0.6429, 0.7654], ..., [-1.5980, -1.5630, -1.4755, ..., -0.0049, -0.2850, -0.6001], [-1.6856, -1.6506, -1.5980, ..., 0.1702, -0.0749, -0.3901], [-1.6331, -1.6331, -1.6506, ..., 0.3102, 0.1001, -0.1800]], [[-0.5844, -0.5670, -0.5495, ..., -0.2184, -0.0964, 0.0082], [-0.5495, -0.5321, -0.5147, ..., -0.2184, -0.0964, -0.0092], [-0.5321, -0.5321, -0.5147, ..., -0.2010, -0.0790, 0.0082], ..., [-1.4907, -1.4733, -1.4384, ..., -0.6890, -0.9156, -1.1596], [-1.5256, -1.5256, -1.5081, ..., -0.4973, -0.7587, -1.0550], [-1.4559, -1.4733, -1.5081, ..., -0.3578, -0.5844, -0.8458]]])
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):
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.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
image_path = (test_dir + '/100/' + 'image_07939.jpg')
imshow(processed_image.numpy())
<matplotlib.axes._subplots.AxesSubplot at 0x7fb4be6abf28>
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']
def predict(image_path, model, topk=5):
''' Predict the class (or classes) of an image using a trained deep learning model.
'''
# TODO: Implement the code to predict the class from an image file
model.eval()
model.cpu()
img = process_image(image_path)
img = img.unsqueeze_(0)
img = img.float()
with torch.no_grad():
output = model.forward(img)
probs, classes = torch.topk(input=output, k=topk)
top_prob = probs.exp()
# Convert indices to classes
idx_to_class = {val: key for key, val in model.class_to_idx.items()}
top_classes = [idx_to_class[each] for each in classes.cpu().numpy()[0]]
print('Top Classes: ', top_classes)
print('Top Probs: ', top_prob)
return top_prob, top_classes
#return top_prob.numpy()[0], mapped_classes
image_path = (test_dir + '/29/' + 'image_04095.jpg')
probs, classes = predict(image_path, model)
# Converting from tensor to numpy-array
print(probs)
print(classes)
Top Classes: ['29', '14', '13', '10', '30'] Top Probs: tensor([[ 0.7722, 0.1411, 0.0865, 0.0002, 0.0000]]) tensor([[ 0.7722, 0.1411, 0.0865, 0.0002, 0.0000]]) ['29', '14', '13', '10', '30']
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.
# TODO: Display an image along with the top 5 classes
def sanity_checking():
plt.rcParams["figure.figsize"] = (3,3)
plt.rcParams.update({'font.size': 12})
# Showing actual image
image_path = (test_dir + '/29/' + 'image_04095.jpg')
probs, classes = predict(image_path, model)
#classes = classes.cpu().numpy()
image_to_show = process_image(image_path)
image = imshow(image_to_show.numpy(), ax = plt)
image.axis('off')
image.title(cat_to_name[str(classes[0])])
image.show()
# Showing Top Classes
labels = []
for class_index in classes:
labels.append(cat_to_name[str(class_index)])
y_pos = np.arange(len(labels))
probs = probs[0]
plt.barh(y_pos, probs, align='center', color='green')
plt.yticks(y_pos, labels)
plt.xlabel('Probability')
plt.title('Top Classes')
plt.show()
sanity_checking()
Top Classes: ['29', '14', '13', '10', '30'] Top Probs: tensor([[ 0.7722, 0.1411, 0.0865, 0.0002, 0.0000]])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。