Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/main/beginner_source/blitz/cifar10_tutorial.py
Views: 713
# -*- coding: utf-8 -*-1"""2Training a Classifier3=====================45This is it. You have seen how to define neural networks, compute loss and make6updates to the weights of the network.78Now you might be thinking,910What about data?11----------------1213Generally, when you have to deal with image, text, audio or video data,14you can use standard python packages that load data into a numpy array.15Then you can convert this array into a ``torch.*Tensor``.1617- For images, packages such as Pillow, OpenCV are useful18- For audio, packages such as scipy and librosa19- For text, either raw Python or Cython based loading, or NLTK and20SpaCy are useful2122Specifically for vision, we have created a package called23``torchvision``, that has data loaders for common datasets such as24ImageNet, CIFAR10, MNIST, etc. and data transformers for images, viz.,25``torchvision.datasets`` and ``torch.utils.data.DataLoader``.2627This provides a huge convenience and avoids writing boilerplate code.2829For this tutorial, we will use the CIFAR10 dataset.30It has the classes: ‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’,31‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. The images in CIFAR-10 are of32size 3x32x32, i.e. 3-channel color images of 32x32 pixels in size.3334.. figure:: /_static/img/cifar10.png35:alt: cifar103637cifar10383940Training an image classifier41----------------------------4243We will do the following steps in order:44451. Load and normalize the CIFAR10 training and test datasets using46``torchvision``472. Define a Convolutional Neural Network483. Define a loss function494. Train the network on the training data505. Test the network on the test data51521. Load and normalize CIFAR1053^^^^^^^^^^^^^^^^^^^^^^^^^^^^^5455Using ``torchvision``, it’s extremely easy to load CIFAR10.56"""57import torch58import torchvision59import torchvision.transforms as transforms6061########################################################################62# The output of torchvision datasets are PILImage images of range [0, 1].63# We transform them to Tensors of normalized range [-1, 1].6465########################################################################66# .. note::67# If running on Windows and you get a BrokenPipeError, try setting68# the num_worker of torch.utils.data.DataLoader() to 0.6970transform = transforms.Compose(71[transforms.ToTensor(),72transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])7374batch_size = 47576trainset = torchvision.datasets.CIFAR10(root='./data', train=True,77download=True, transform=transform)78trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,79shuffle=True, num_workers=2)8081testset = torchvision.datasets.CIFAR10(root='./data', train=False,82download=True, transform=transform)83testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,84shuffle=False, num_workers=2)8586classes = ('plane', 'car', 'bird', 'cat',87'deer', 'dog', 'frog', 'horse', 'ship', 'truck')8889########################################################################90# Let us show some of the training images, for fun.9192import matplotlib.pyplot as plt93import numpy as np9495# functions to show an image969798def imshow(img):99img = img / 2 + 0.5 # unnormalize100npimg = img.numpy()101plt.imshow(np.transpose(npimg, (1, 2, 0)))102plt.show()103104105# get some random training images106dataiter = iter(trainloader)107images, labels = next(dataiter)108109# show images110imshow(torchvision.utils.make_grid(images))111# print labels112print(' '.join(f'{classes[labels[j]]:5s}' for j in range(batch_size)))113114115########################################################################116# 2. Define a Convolutional Neural Network117# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^118# Copy the neural network from the Neural Networks section before and modify it to119# take 3-channel images (instead of 1-channel images as it was defined).120121import torch.nn as nn122import torch.nn.functional as F123124125class Net(nn.Module):126def __init__(self):127super().__init__()128self.conv1 = nn.Conv2d(3, 6, 5)129self.pool = nn.MaxPool2d(2, 2)130self.conv2 = nn.Conv2d(6, 16, 5)131self.fc1 = nn.Linear(16 * 5 * 5, 120)132self.fc2 = nn.Linear(120, 84)133self.fc3 = nn.Linear(84, 10)134135def forward(self, x):136x = self.pool(F.relu(self.conv1(x)))137x = self.pool(F.relu(self.conv2(x)))138x = torch.flatten(x, 1) # flatten all dimensions except batch139x = F.relu(self.fc1(x))140x = F.relu(self.fc2(x))141x = self.fc3(x)142return x143144145net = Net()146147########################################################################148# 3. Define a Loss function and optimizer149# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^150# Let's use a Classification Cross-Entropy loss and SGD with momentum.151152import torch.optim as optim153154criterion = nn.CrossEntropyLoss()155optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)156157########################################################################158# 4. Train the network159# ^^^^^^^^^^^^^^^^^^^^160#161# This is when things start to get interesting.162# We simply have to loop over our data iterator, and feed the inputs to the163# network and optimize.164165for epoch in range(2): # loop over the dataset multiple times166167running_loss = 0.0168for i, data in enumerate(trainloader, 0):169# get the inputs; data is a list of [inputs, labels]170inputs, labels = data171172# zero the parameter gradients173optimizer.zero_grad()174175# forward + backward + optimize176outputs = net(inputs)177loss = criterion(outputs, labels)178loss.backward()179optimizer.step()180181# print statistics182running_loss += loss.item()183if i % 2000 == 1999: # print every 2000 mini-batches184print(f'[{epoch + 1}, {i + 1:5d}] loss: {running_loss / 2000:.3f}')185running_loss = 0.0186187print('Finished Training')188189########################################################################190# Let's quickly save our trained model:191192PATH = './cifar_net.pth'193torch.save(net.state_dict(), PATH)194195########################################################################196# See `here <https://pytorch.org/docs/stable/notes/serialization.html>`_197# for more details on saving PyTorch models.198#199# 5. Test the network on the test data200# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^201#202# We have trained the network for 2 passes over the training dataset.203# But we need to check if the network has learnt anything at all.204#205# We will check this by predicting the class label that the neural network206# outputs, and checking it against the ground-truth. If the prediction is207# correct, we add the sample to the list of correct predictions.208#209# Okay, first step. Let us display an image from the test set to get familiar.210211dataiter = iter(testloader)212images, labels = next(dataiter)213214# print images215imshow(torchvision.utils.make_grid(images))216print('GroundTruth: ', ' '.join(f'{classes[labels[j]]:5s}' for j in range(4)))217218########################################################################219# Next, let's load back in our saved model (note: saving and re-loading the model220# wasn't necessary here, we only did it to illustrate how to do so):221222net = Net()223net.load_state_dict(torch.load(PATH, weights_only=True))224225########################################################################226# Okay, now let us see what the neural network thinks these examples above are:227228outputs = net(images)229230########################################################################231# The outputs are energies for the 10 classes.232# The higher the energy for a class, the more the network233# thinks that the image is of the particular class.234# So, let's get the index of the highest energy:235_, predicted = torch.max(outputs, 1)236237print('Predicted: ', ' '.join(f'{classes[predicted[j]]:5s}'238for j in range(4)))239240########################################################################241# The results seem pretty good.242#243# Let us look at how the network performs on the whole dataset.244245correct = 0246total = 0247# since we're not training, we don't need to calculate the gradients for our outputs248with torch.no_grad():249for data in testloader:250images, labels = data251# calculate outputs by running images through the network252outputs = net(images)253# the class with the highest energy is what we choose as prediction254_, predicted = torch.max(outputs.data, 1)255total += labels.size(0)256correct += (predicted == labels).sum().item()257258print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')259260########################################################################261# That looks way better than chance, which is 10% accuracy (randomly picking262# a class out of 10 classes).263# Seems like the network learnt something.264#265# Hmmm, what are the classes that performed well, and the classes that did266# not perform well:267268# prepare to count predictions for each class269correct_pred = {classname: 0 for classname in classes}270total_pred = {classname: 0 for classname in classes}271272# again no gradients needed273with torch.no_grad():274for data in testloader:275images, labels = data276outputs = net(images)277_, predictions = torch.max(outputs, 1)278# collect the correct predictions for each class279for label, prediction in zip(labels, predictions):280if label == prediction:281correct_pred[classes[label]] += 1282total_pred[classes[label]] += 1283284285# print accuracy for each class286for classname, correct_count in correct_pred.items():287accuracy = 100 * float(correct_count) / total_pred[classname]288print(f'Accuracy for class: {classname:5s} is {accuracy:.1f} %')289290########################################################################291# Okay, so what next?292#293# How do we run these neural networks on the GPU?294#295# Training on GPU296# ----------------297# Just like how you transfer a Tensor onto the GPU, you transfer the neural298# net onto the GPU.299#300# Let's first define our device as the first visible cuda device if we have301# CUDA available:302303device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')304305# Assuming that we are on a CUDA machine, this should print a CUDA device:306307print(device)308309########################################################################310# The rest of this section assumes that ``device`` is a CUDA device.311#312# Then these methods will recursively go over all modules and convert their313# parameters and buffers to CUDA tensors:314#315# .. code:: python316#317# net.to(device)318#319#320# Remember that you will have to send the inputs and targets at every step321# to the GPU too:322#323# .. code:: python324#325# inputs, labels = data[0].to(device), data[1].to(device)326#327# Why don't I notice MASSIVE speedup compared to CPU? Because your network328# is really small.329#330# **Exercise:** Try increasing the width of your network (argument 2 of331# the first ``nn.Conv2d``, and argument 1 of the second ``nn.Conv2d`` –332# they need to be the same number), see what kind of speedup you get.333#334# **Goals achieved**:335#336# - Understanding PyTorch's Tensor library and neural networks at a high level.337# - Train a small neural network to classify images338#339# Training on multiple GPUs340# -------------------------341# If you want to see even more MASSIVE speedup using all of your GPUs,342# please check out :doc:`data_parallel_tutorial`.343#344# Where do I go next?345# -------------------346#347# - :doc:`Train neural nets to play video games </intermediate/reinforcement_q_learning>`348# - `Train a state-of-the-art ResNet network on imagenet`_349# - `Train a face generator using Generative Adversarial Networks`_350# - `Train a word-level language model using Recurrent LSTM networks`_351# - `More examples`_352# - `More tutorials`_353# - `Discuss PyTorch on the Forums`_354# - `Chat with other users on Slack`_355#356# .. _Train a state-of-the-art ResNet network on imagenet: https://github.com/pytorch/examples/tree/master/imagenet357# .. _Train a face generator using Generative Adversarial Networks: https://github.com/pytorch/examples/tree/master/dcgan358# .. _Train a word-level language model using Recurrent LSTM networks: https://github.com/pytorch/examples/tree/master/word_language_model359# .. _More examples: https://github.com/pytorch/examples360# .. _More tutorials: https://github.com/pytorch/tutorials361# .. _Discuss PyTorch on the Forums: https://discuss.pytorch.org/362# .. _Chat with other users on Slack: https://pytorch.slack.com/messages/beginner/363364# %%%%%%INVISIBLE_CODE_BLOCK%%%%%%365del dataiter366# %%%%%%INVISIBLE_CODE_BLOCK%%%%%%367368369