Aller au contenu

Lab Session 4 - Deep Learning Tutorial

Note

Before you begin, make sure you have downloaded the latest update of the course slides from here, and keep them close while doing the lab.

Warning

We will work with pytorch. If you don't have a GPU (which is very likely to be the case if you use a laptop), we recommend installing pytorch in "cpu only" mode as it will be much smaller to download. See pytorch installation instructions and select "cpu only" as compute platform.

If you run this notebook on Google Colab, you'll have access to a GPU.

Objectives of the lab

At the end of this session, you will be able to : - Understand how PyTorch works. - Process data with PyTorch. - Train a Deep Learning model on your data. - Adapt this tutorial for the specificites of your modality.

0. What is this PyTorch?

This session is a Deep Learning tutorial in Pytorch.

PyTorch is a Python-based scientific computing package serving two broad purposes:

  • A replacement for NumPy to use the power of GPUs and other accelerators.
  • An automatic differentiation library that is useful to implement Deep Learning architectures.

Note

PyTorch is one of the standard librairies to define neural networks. This tutorial is loosely based on the 60 min blitz Deep Learning with Pytorch but with many original parts.

The tutorial is structured as follows:

  1. Tensors in Pytorch
  2. Understanding the training loop and automatic differentiation
  3. Defining a Deep Learning Architecture
  4. Training a Classifier on CIFAR10, a standard image classification dataset
  5. Study specificities of Text, Audio and Image modalities

Note

Copy / modify / playaround with the code snippets that we provide. In part 4, you are expected to complete some empty cells to successfully train and test your net.

1. Tensors

Tensors are a specialized data structure that are very similar to arrays and matrices. In PyTorch, we use tensors to encode the inputs and outputs of a model, as well as the model’s parameters.

Tensors are similar to NumPy’s ndarrays, except that tensors can run on GPUs or other specialized hardware to accelerate computing. If you’re familiar with ndarrays, you’ll be right at home with the Tensor API. If not, follow along in this quick API walkthrough.

1
2
import torch
import numpy as np

Tensor Initialization

Tensors can be initialized in various ways. Take a look at the following examples:

Directly from data

Tensors can be created directly from data. The data type is automatically inferred.

1
2
data = [[1, 2],[3, 4]]
x_data = torch.tensor(data)

From a NumPy array

Tensors can be created from NumPy arrays (and vice versa).

1
2
np_array = np.array(data)
x_np = torch.from_numpy(np_array)

From another tensor:

The new tensor retains the properties (shape, datatype) of the argument tensor, unless explicitly overridden.

1
2
3
4
5
x_ones = torch.ones_like(x_data) # retains the properties of x_data
print(f"Ones Tensor: \n {x_ones} \n")

x_rand = torch.rand_like(x_data, dtype=torch.float) # overrides the datatype of x_data
print(f"Random Tensor: \n {x_rand} \n")

With random or constant values:

shape is a tuple of tensor dimensions. In the functions below, it determines the dimensionality of the output tensor.

1
2
3
4
5
6
7
8
shape = (2,3,)
rand_tensor = torch.rand(shape)
ones_tensor = torch.ones(shape)
zeros_tensor = torch.zeros(shape)

print(f"Random Tensor: \n {rand_tensor} \n")
print(f"Ones Tensor: \n {ones_tensor} \n")
print(f"Zeros Tensor: \n {zeros_tensor}")

Tensor Attributes

Tensor attributes describe their shape, datatype, and the device on which they are stored.

1
2
3
4
5
tensor = torch.rand(3,4)

print(f"Shape of tensor: {tensor.shape}")
print(f"Datatype of tensor: {tensor.dtype}")
print(f"Device tensor is stored on: {tensor.device}")

Tensor Operations

Over 100 tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random sampling, and more are comprehensively described here.

Try out some of the operations from the list. If you're familiar with the NumPy API, you'll find the Tensor API a breeze to use.

Standard numpy-like indexing and slicing:

1
2
3
tensor = torch.ones(4, 4)
tensor[:,1] = 0
print(tensor)

Joining tensors You can use torch.cat to concatenate a sequence of tensors along a given dimension. See also torch.stack, another tensor joining operation that is subtly different from torch.cat.

1
2
t1 = torch.cat([tensor, tensor, tensor], dim=1)
print(t1)

Multiplying tensors

1
2
3
4
# This computes the element-wise product
print(f"tensor.mul(tensor) \n {tensor.mul(tensor)} \n")
# Alternative syntax:
print(f"tensor * tensor \n {tensor * tensor}")

This computes the matrix multiplication between two tensors

1
2
3
print(f"tensor.matmul(tensor.T) \n {tensor.matmul(tensor.T)} \n")
# Alternative syntax:
print(f"tensor @ tensor.T \n {tensor @ tensor.T}")

Bridge to NumPy

Tensor to NumPy array

1
2
3
4
t = torch.ones(5)
print(f"t: {t}")
n = t.numpy()
print(f"n: {n}")

NumPy array to Tensor

1
2
n = np.ones(5)
t = torch.from_numpy(n)

Changes in the NumPy array reflects in the tensor.

1
2
3
np.add(n, 1, out=n)
print(f"t: {t}")
print(f"n: {n}")

2. Defining a Deep Learning Model

A deep learning model takes the input, feeds it through several layers one after the other, and then finally gives the output.

A deep learning model can be constructed using the modules from the torch.nn package.

A typical training procedure for a deep learning model is as follows:

  • Define the model that has some learnable parameters ("weights")
  • Iterate over a dataset of inputs
  • Process input through the model ("forward pass")
  • Compute the loss (how far is the output from being correct)
  • Propagate gradients back into the model’s parameters ("backpropagation")
  • Update the weights of the model, typically using a simple update rule ("Gradient Descent"):
\[\begin{align}weight = weight - learningRate * gradient\end{align}\]

Define the model

Let’s define a simple deep learning model :

  • take as input a greyscale image (1 input channel),
  • processes it with 2 layers of 2D convolutional filters (Conv2d), each followed by ReLu and 2D max pooling,
  • followed by a 3 layer perceptron, which is composed of Linear units and ReLu.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import torch
import torch.nn as nn
import torch.nn.functional as F


class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        ## Convolutional layers, where weights represent conv kernels
        # 1 input image channel, 6 output channels, 3x3 square convolution
        self.conv1 = nn.Conv2d(in_channels=1, out_channels=6, kernel_size=3)
        # 6 input channels (the output of the last layer), 16 output channels, 3x3 square convolution
        self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size=3)

        ## Linear layer: MLP, i.e. fully-connected layer.
        self.fc1 = nn.Linear(in_features = 16 * 6 * 6, out_features = 120)  # 6*6 from the image dimension, and 16 for the number of channels
        self.fc2 = nn.Linear(in_features = 120, out_features = 84) # 120 is output of the previous layer.
        self.fc3 = nn.Linear(in_features = 84, out_features = 10) # 84 is the output of the previous layer, 10 is the number of classes.

    def forward(self, x):
        # Conv1, then max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # Conv2, then max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv2(x)), 2) # If the size is a square you can only specify a single number
        x = x.view(-1, self.num_flat_features(x)) # Reshape each image, processed by conv, into a vector (required for linear layers)
        # 1st Linear layer
        x = F.relu(self.fc1(x))
        # 2nd Linear layer
        x = F.relu(self.fc2(x))
        # 3rd Linear layer
        x = self.fc3(x)
        return x

    def num_flat_features(self, x): # Computes the number of flat (*"vectorized"*) features from a 2D conv.
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
print(net)

The forward function of the model class is the one that implements the forward pass, which is the sequence of operations from input to output. It is possible to use all operators from nn.functional and modules defined in nn, as well as operations on tensors.

The backward function (where gradients are computed) is automatically defined for you in PyTorch. Under the hood, it uses a module called autograd, and details about autograd can be found here.

The learnable parameters of a model are returned by net.parameters()

1
2
3
params = list(net.parameters())
print(len(params))
print(params[0].size())  # conv1's .weight

Let's try a random 32x32 input. (Note: expected input size of this net is 32x32.)

Warning

torch.nn only supports inputs in the form of batches, i.e. subsets of the datasets. In other words, the entire torch.nn package only supports inputs that are arrays of several samples ("batch"), and not a single sample.

For example, nn.Conv2d will take in a 4D Tensor of nSamples x nChannels x Height x Width.

If you have a single sample, just use input.unsqueeze(0) to add a fake batch dimension.

1
2
3
4
testinput = torch.randn(1, 1, 32, 32) # Batch dim, number of channels, height, width
out = net(testinput)
print(out)
print("Shape of the ouput: ", out.shape)

In order to better understand the inner operations of the model, let's break down the forward pass layer by layer, and print the successive shapes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# first layer of the model, as defined in the forward function, but we call the conv1 module from the model definition
x = testinput
print(f"Initial shape of the input : {x.shape}")
x = (F.relu(net.conv1(x)))
print(f"Shape after the first convolutional layer (conv1, relu) : {x.shape}")

x = F.max_pool2d(x, (2, 2))

print(f"Shape after max pooling (max_pool2d) with a 2x2 window: {x.shape}")
# Second layer 
x = F.max_pool2d(F.relu(net.conv2(x)), 2)

print(f"Shape after the second convolutional layer and 2x2 max pool (conv2, relu, max_pool2d) : {x.shape}")

x = x.view(-1, net.num_flat_features(x))
print(f"Shape after reshaping (flattening to a 1D vector) : {x.shape}")

x = F.relu(net.fc1(x))
print(f"Shape after FC1 : {x.shape}")

x = F.relu(net.fc2(x))
print(f"Shape after FC2 : {x.shape}")

x = net.fc3(x)
print(f"Shape after FC3, output of the model : {x.shape}")    

Try changing the size (32x32) of the image input and see what happens !

1
# TODO

Before proceeding further, let's recap all the classes you’ve seen so far.

Recap:

  • torch.Tensor - A multi-dimensional array with support for autograd operations like backward(). Also holds the gradient w.r.t. the tensor.
  • nn.Module - Neural network module, basic blocks for defining a model. Convenient way of encapsulating parameters, with helpers for moving them to GPU, exporting, loading, etc.
  • nn.Parameter - A kind of Tensor, that is automatically registered as a parameter of a model when assigned as an attribute to a Module.
  • These modules allowed us to define the constructor (init) and the forward pass for a model.

At this point, we covered:

  • Defining a model
  • Processing inputs (forward pass)

Still Left:

  • Computing the loss
  • Updating the weights of the network ("backpropgation")

Loss Function

A loss function takes the (output, target) pair of inputs, and computes a value that estimates how far away the output is from the target.

There are several different loss functions inside the nn package. A simple loss is: nn.MSELoss which computes the mean-squared error between the input and the target. This loss is adapted for regression problems when the targets are continuous.

For example:

1
2
3
4
5
6
7
output = net(testinput)
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
criterion = nn.MSELoss()

loss = criterion(output, target)
print(loss)

Note

The nn package contains various modules and loss functions that form the building blocks of deep neural networks. A full list with documentation can be found here. Don't hesitate to use the most appropriate functions for your model!

Backpropagation

To backpropagate, we first have to compute the gradients w.r.t. the error. This is done in PyTorch using loss.backward(). Under the hood, it uses autograd (see here for details about autograd).

Be careful though: you first need to clear the existing gradients! Otherwise, gradients will be accumulated to existing gradients (e.g., gradients computed at the previous iteration).

Let's call loss.backward() and have a look at conv1's bias gradients before and after the backward.

1
2
3
4
5
6
7
8
9
net.zero_grad() # zeroes the gradient buffers of all parameters

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad) # look at gradients for conv1

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)

In most applications, defining the loss and performing the backward propagation process using loss.backward() is sufficient. Hence, in practice, you will rarely need to have a look at the gradients when training a deep model, but it's good to know how to do it if you need it.

Now that the loss (error) is computed, we can update the weights of the model. The simplest update rule used in practice is the Stochastic Gradient Descent (SGD):

\[\begin{align}weight = weight - learningRate * gradient\end{align}\]

We could implement this using a (pseudo) Python code such as this one :

1
2
3
    learning_rate = 0.01
    for f in net.parameters():
        f.data.sub_(f.grad.data * learning_rate)

However, there are various update rules (e.g. SGD, Nesterov-SGD, Adam, RMSProp, etc) with implementation details in order to accelerate the update or make the convergence more rbosut. Going into their details is way out of the scope of this course. Hence, computing the gradient descent by hand is largely inefficient.

Instead, PyTorch natively implements updates rules in the torch.optim module ("optimizers"), and using this module is really simple:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import torch.optim as optim

# Create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01) 

# In your training loop:
optimizer.zero_grad()   # Zeroes the gradient buffers
output = net(testinput) # Makes the prediction
loss = criterion(output, target) # Computes the loss
loss.backward() # Computes the gradient
optimizer.step() # Does the update (Gradient Descent)

We have seen how to define a deep learning model, compute loss and make updates to the weights of the network.

The last important step, is to preprocess the data!

Input Data

Generally, when you have to deal with image, text, audio or video data, you can use standard python packages that load data into a numpy array. Then you can convert this array into a torch.Tensor.

  • For images, packages such as Pillow (PIL), OpenCV are useful
  • For audio, packages such as scipy and librosa or torchaudio
  • For text, either raw Python or Cython based loading, or NLTK and SpaCy are useful

Specifically for vision, pytorch has created a package called torchvision, that has data loaders for common datasets such as Imagenet, CIFAR10, MNIST, etc. and data transformers for images, torchvision.datasets and torch.utils.data.DataLoader.

There is a similar package for audio, which is called torchaudio.

This provides a huge convenience and avoids writing boilerplate code.

For this tutorial, we will use the CIFAR10 dataset. It has the classes: ‘airplane’, ‘automobile’, ‘bird’, ‘cat’, ‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’. The images in CIFAR-10 are of size 3x32x32, i.e. 3-channel color images of 32x32 pixels in size.

This is it. You are finally ready to:

3. Train a Classifier!

We will do the following steps in order:

a. Load and normalizing the CIFAR10 training and test datasets using torchvision

b. Define a Convolutional Neural Network

c. Define a loss function

d. Train the network on the training data

e. Test the network on the test data

(bonus). Use a GPU

a. Loading and normalizing CIFAR10

Using torchvision, it’s extremely easy to load CIFAR10.

1
2
3
import torch
import torchvision
import torchvision.transforms as transforms

The output of torchvision datasets are PILImage images of range [0, 1]. We transform them to Tensors of normalized range [-1, 1].

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=8,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=8,
                                         shuffle=False, num_workers=2)

classes = ('plane', 'car', 'bird', 'cat',
           'deer', 'dog', 'frog', 'horse', 'ship', 'truck')

Warning

If running on Windows you get a BrokenPipeError, try setting the num_worker of torch.utils.data.DataLoader() to 0.

Let us show some of the training images, for fun.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import matplotlib.pyplot as plt
import numpy as np

# Function to show an image
def imshow(img):
    img = img / 2 + 0.5     # unnormalize
    npimg = img.numpy()
    plt.imshow(np.transpose(npimg, (1, 2, 0)))
    plt.show()


# get some random training images and check the size
for images,labels in trainloader:
    print('batch size:', images.size(0))
    print('color channels :', images.size(1))
    print('Image size:'+ str(images.size(2))+ 'x'+ str(images.size(3)))
    break # we just want to fetch the first batch

# show images
imshow(torchvision.utils.make_grid(images))
# print labels
print(' '.join('%5s\t' % classes[labels[j]] for j in range(4)))

png

1
GroundTruth:    cat  ship  ship plane  frog  frog   car  frog

b. Define a Convolutional Neural Network

Copy the neural network from the Neural Networks section before and modify it to take 3-channel (color) images, instead of 1-channel (black and white) images as it was defined.

Note

Pay attention to the in/out features dimensions, especially at the transition between a Convolution (Conv) and Fully connected (fc) linear layer!

In each convolutional layer, the feature size may be reduced by the conv operation (see below), and is then divided by the 2D max pooling layer!

You can compute the size of the output of a convolution operation based on the following formula: Denote as h_out the output size of a 2D Conv layer, h_out depends on h_in (size features input), k (convolutional kernel size), p (zero padding), and s (stride) (more details in Conv2D documentation).

\[\begin{align}h_{out}=\frac{h_{in} - 2*p - k}{s} + 1 \end{align}\]
1
# TODO

c. Define a Loss function and optimizer

Let's use a Classification Cross-Entropy loss and SGD with momentum.

1
# TODO

d. Train the network

This is when things start to get interesting! We simply have to loop over our data iterator, and feed the inputs to the network and optimize.

In this tutorial we will consider a small number of iterations over the dataset n_epochs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
n_epochs=2

for epoch in range(n_epochs):  # loop over the dataset multiple times

    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data

        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()

        # print statistics
        running_loss += loss.item()
        if i % 2000 == 1999:    # print every 2000 mini-batches
            print('[epoch %d, batch %5d] loss: %.3f' %
                  (epoch + 1, i + 1, running_loss / 2000))
            running_loss = 0.0

print('Finished Training')

e. Test the network on the test data

We have trained the network for n_epochs passes over the training dataset. But we need to check if the network has learnt anything at all.

We will check this by predicting the class label that the neural network outputs, and checking it against the ground-truth. If the prediction is correct, we add the sample to the list of correct predictions.

First things first, let us display an image from the test set to get familiar.

1
2
3
4
5
for images,labels in testloader:
    # print images
    imshow(torchvision.utils.make_grid(images))
    print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(images.shape[0])))
    break # here again we just want to fetch the first batch

Now, we turn to the neural network, and observe how it predicted the above examples.

The outputs are energies for the 10 classes. The higher the energy for a class, the more the network thinks that the image is of the particular class. So, let's get the index of the highest energy:

1
2
3
4
5
6
7
outputs = net(images)

_, predicted = torch.max(outputs, 1)

print('Predicted: ', ' '.join('%5s' % classes[predicted[j]]
                              for j in range(images.shape[0])))
print('GroundTruth: ', ' '.join('%5s' % classes[labels[j]] for j in range(images.shape[0])))

Let us look at how the network performs on the whole dataset.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
correct = 0
total = 0
# torch.no_grad is important for TESTING.  
with torch.no_grad(): # This line actually disables the gradient computation.
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print('Accuracy of the network on the 10000 test images: %d %%' % (
    100 * correct / total))

That looks way better than chance, which is 10% accuracy (randomly picking a class out of 10 classes). Seems like the network learnt something.

Hmmm, what are the classes that performed well, and the classes that did not perform well:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class_correct = list(0. for i in range(10))
class_total = list(0. for i in range(10))
with torch.no_grad():
    for data in testloader:
        images, labels = data
        outputs = net(images)
        _, predicted = torch.max(outputs, 1)
        c = (predicted == labels).squeeze()
        for i in range(4):
            label = labels[i]
            class_correct[label] += c[i].item()
            class_total[label] += 1


for i in range(10):
    print('Accuracy of %5s : %2d %%' % (
        classes[i], 100 * class_correct[i] / class_total[i]))

In order to have a better intuition about what a 2D-convolutional layer is, we will feed a batch of images into the first convolutional layer, and visualize the result.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
## the first batch is the "images" tensor
print(f"Tensor of the first batch, shape : {images.shape} ")

## We use the first layer of the model to process the input
processed = net.conv1(images)

## We keep only one image of the batch to visualize it
index_img = 7 ## this is between 0 and batch_size - 1 

image = images[index_img]
image = image.unsqueeze(0) ## this is needed to add a singleton dimension to the tensor, so that we can visualize it with make_grid
# here we will add a singleton dimension as if we had a batch of size 1 to visualize, but we are keeping the three channels to keep the colors 


## same thing with the output of the first convolutional layer
processed = processed[index_img]
processed = processed.unsqueeze(1) ## this is needed to add a singleton dimension to the tensor, so that we can visualize it with make_grid
# here, remember that we want to visualize the output of the first convolutional layer, which has 6 channels. We need to add a singleton dimension to the tensor to visualize it with make_grid
# we keep the six feature maps as the "batch size" of the tensor, and we add a singleton dimension as a single channel

# visualize
print("Original image")
imshow(torchvision.utils.make_grid(image.detach()))

print("Outputs of the first convolutional layer")
imshow(torchvision.utils.make_grid(processed.detach(),scale_each=True, normalize=True))
1
2
Tensor of the first batch, shape : torch.Size([8, 3, 32, 32]) 
Original image

png

1
Outputs of the first convolutional layer

png

You can see that the output of the first convolutional layer corresponds to filtered versions of the input. Convolutions can enhance or reduce some local contrast changes, or edges / contours. A convolutional neural network model will generate many different "feature maps" such as this one.

Let's now see the effect of relu and max pooling

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
processed_relumaxpool = F.max_pool2d(F.relu(net.conv1(images)), (2, 2))

## same thing with the output of the first convolutional layer
processed_relumaxpool = processed_relumaxpool[index_img]
processed_relumaxpool = processed_relumaxpool.unsqueeze(1)
# visualize
print("Original image")
imshow(torchvision.utils.make_grid(image.detach()))

print("Outputs of the first convolutional layer")
imshow(torchvision.utils.make_grid(processed.detach(),normalize=True,value_range=(-1,1),padding=0))

## print ranges 
print(f"Range of the original image : [{torch.min(image).item()}, {torch.max(image).item()}]")
print(f"Range of the output of the first convolutional layer : [{torch.min(processed).item()}, {torch.max(processed).item()}]")
print(f"Range of the output of the first convolutional layer after Relu and Max Pool : [{torch.min(processed_relumaxpool).item()}, {torch.max(processed_relumaxpool).item()}]")


print("After Relu and Max Pool")
imshow(torchvision.utils.make_grid(processed_relumaxpool.detach(), normalize=True,value_range=(-1,1),padding=0))
1
Original image

png

1
Outputs of the first convolutional layer

png

1
2
3
4
Range of the original image : [-0.8039215803146362, 0.9764705896377563]
Range of the output of the first convolutional layer : [-2.5888426303863525, 2.6774139404296875]
Range of the output of the first convolutional layer after Relu and Max Pool : [0.0, 3.9081757068634033]
After Relu and Max Pool

png

By alternating 2D convolutional filter, which have the effect of amplifying / decreasing contrasts, edges, etc.. then ReLu which act as a threshold, and max pooling which reduces the resolution while keeping the largest values, many smaller "images" (also called "feature maps")are computed as the model gets deeper.

Saving/loading the model

It may be useful to save your model after training (for instance to share it with your binome, to compare different models, or to share it with the community). This can be done in the following way:

1
2
3
4
5
6
7
PATH = './cifar_net.pth'
# Save the model
torch.save(net.state_dict(), PATH)

# Load a saved model
net = Net()
net.load_state_dict(torch.load(PATH))

See [here] (https://pytorch.org/docs/stable/notes/serialization.html) for more details on saving PyTorch models.

(optional) Training on GPU

Using GPU is preferred over CPU for deep learning models, because GPUs are way more powerful at computing matrix products. How do we run these neural networks on the GPU?

Just like how you transfer a Tensor onto the GPU, you transfer the neural net onto the GPU.

Note

PyTorch uses CUDA to easily transfer evertyhing to GPUs.

Let's first define our device as the first visible cuda device if we have CUDA available:

1
2
3
4
5
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# Assuming that we are on a CUDA machine, this should print a CUDA device:

print(device)

The rest of this section assumes that device is a CUDA device. Otherwise, it means that you cannot run the code on a GPU (either you don't have a GPU, or the installation is broken).

The following method will recursively go over all modules and convert their parameters and buffers to CUDA tensors:

1
net.to(device)

Additionally, you will have to send the inputs and targets at every step to the GPU too:

1
inputs, labels = data[0].to(device), data[1].to(device)

Why don't I notice MASSIVE speedup compared to CPU? Because your network is really small.

4. Specificities of modalities

Let's turn to your modalities!

Here are links for specificities to deal with your modalities :

You must follow the tutorial corresponding to your modality to end the Lab.

Tutorial ended!

Goals achieved:

  • Understanding PyTorch's Tensor library and neural networks at a high level.
  • Train a small neural network to classify images
  • Learned the basic components to apply a neural network to your modality