STAT3007/7007 Deep Learning, Prac 10

2022 Semester 1

Q1. Adversarial examples

Download the file lenet.pt, which contains a LeNet model trained on MNIST data with pixel values normalized to the range [0, 1] by dividing them by 255. The model can be loaded as follows.

import torch
import torch.nn as nn
import torch.nn.functional as F

class LeNet(nn.Module):
    def __init__(self, nchannels, width, height):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(nchannels, 6, 5)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16*int((width-12)/4)*int((height-12)/4), 120)
        self.fc2 = nn.Linear(120, 84) 
        self.fc3 = nn.Linear(84, 10) 

    def forward(self, X): 
        out = F.relu(self.conv1(X))
        out = F.max_pool2d(out, 2)
        out = F.relu(self.conv2(out))
        out = F.max_pool2d(out, 2)
        out = out.view(out.size(0), -1) 
        out = F.relu(self.fc1(out))
        out = F.relu(self.fc2(out))
        out = self.fc3(out)
        return out 

net = torch.load('lenet.pt')

Implement the fast gradient sign method, and use it to generate adversarial examples for the first 16 test images.