Description
1. attach PDF for Homework Instruction2. Sample code Please check instruction pdf what I need
Unformatted Attachment Preview
CMSC 691 – Fall 2023
Homework Assignment 5
Announced: 11/2
Due: Thursday, 11/16, 5pm
The problem
• Fine-tune a pre-trained convolutional
network (e.g. ResNet18 or better)
for CIFAR10 dataset
• Create a set of adversarial examples
for your fine-tuned network
• Use PyTorch library
The problem – details
• Use pre-trained weights of the network as a
starting point (see Lecture 19)
• Use the Projected Gradient Method for
constructing adversarial examples
• Start from randomly selected 18 images from
CIFAR10 test set and alter them
• See plotCIFAR.py in Canvas for plotting code
that you can use
Returning the Assignment
n
n
Solution code should be written by you and you
only (no web/book/friend/etc. code)
Upload through Canvas
n
A jupyter notebook with code & results for:
n
Loading pre-trained weights into the model
n
Fine-tuning the model on 10-class CIFAR10 problem
n
Selecting 18 random test images and finding “adversarial”
modification to them
n
For the 18 images, in your notebook, show:
n
Original images with predicted classes
n
Modified images with predicted classes
n
The noise
Homework Assignment 5
Due: Thursday, 11/12, 5pm
The problem
• Fine-tune a pre-trained convolutional
network (e.g. ResNet18 or better)
for CIFAR10 dataset
• Create a set of adversarial examples
for your fine-tuned network
• Use PyTorch library
The problem – details
• Use pre-trained weights of the network as a
starting point (see Lecture 19)
• Use the Projected Gradient Method for
constructing adversarial examples
• Start from randomly selected 18 images from
CIFAR10 test set and alter them
• See plotCIFAR.py in Canvas for plotting code
that you can use
Returning the Assignment
◼
◼
Solution code should be written by you and you
only (no web/book/friend/etc. code)
Upload through Canvas
◼
A jupyter notebook with code & results for:
Loading pre-trained weights into the model
◼ Fine-tuning the model on 10-class CIFAR10 problem
◼ Selecting 18 random test images and finding “adversarial”
modification to them
◼
◼
For the 18 images, in your notebook, show:
◼
Original images with predicted classes
◼
Modified images with predicted classes
◼
The noise
@01993@Nlk!1231.ttach PDF for Instruction
2.In the bellow will be sample code-
import torchvision.datasets as datasets
from torchvision import transforms
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
num_classes=10;
transform = transforms.Compose([transforms.ToTensor(),])
full_train_dataset = datasets.CIFAR10(root=’./data’, train=True, download=True,
transform=transform )
full_test_dataset = datasets.CIFAR10(root=’./data’, train=False, download=True,
transform=transform )
batch_size=64;
test_batch_size=6*3; # for plot
trainloader = torch.utils.data.DataLoader(full_train_dataset,
batch_size=batch_size,shuffle=True)
testloader = torch.utils.data.DataLoader(full_test_dataset,
batch_size=test_batch_size,shuffle=False)
classNames = [
‘airplane’,
‘automobile’,
‘bird’,
‘cat’,
‘deer’,
‘dog’,
‘frog’,
‘horse’,
‘ship’,
‘truck’,];
def plot_images_RGB(X,y,yp,M,N):
f,ax = plt.subplots(M,N, sharex=True, sharey=True, figsize=(N,M*1.3))
for i in range(M):
for j in range(N):
img = np.moveaxis(X[i*N+j].cpu().numpy(),0,2);
ax[i][j].imshow(img)
yPredClass = yp[i*N+j].max(dim=0)[1]
yTrueClass = y[i*N+j];
title = ax[i][j].set_title(classNames[yPredClass])
plt.setp(title, color=(‘g’ if yPredClass == yTrueClass else ‘r’))
#!/usr/bin/env python3
# -*- coding: utf-8 -*import numpy as np
import matplotlib.pyplot as plt;
import torch;
import torchvision.datasets as datasets
from torchvision import transforms
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
device = torch.device(‘cuda’ if torch.cuda.is_available() else ‘cpu’)
num_classes=10;
transform = transforms.Compose([transforms.ToTensor(),])
full_train_dataset = datasets.CIFAR10(root=’./data’, train=True, download=True,
transform=transform )
full_test_dataset = datasets.CIFAR10(root=’./data’, train=False, download=True,
transform=transform )
batch_size=64;
test_batch_size=6*3; # for plot
trainloader = torch.utils.data.DataLoader(full_train_dataset,
batch_size=batch_size,shuffle=True)
testloader = torch.utils.data.DataLoader(full_test_dataset,
batch_size=test_batch_size,shuffle=False)
classNames = [
‘airplane’,
‘automobile’,
‘bird’,
‘cat’,
‘deer’,
‘dog’,
‘frog’,
‘horse’,
‘ship’,
‘truck’,];
def plot_images_RGB(X,y,yp,M,N):
f,ax = plt.subplots(M,N, sharex=True, sharey=True, figsize=(N,M*1.3))
for i in range(M):
for j in range(N):
img = np.moveaxis(X[i*N+j].cpu().numpy(),0,2);
ax[i][j].imshow(img)
yPredClass = yp[i*N+j].max(dim=0)[1]
yTrueClass = y[i*N+j];
title = ax[i][j].set_title(classNames[yPredClass])
plt.setp(title, color=(‘g’ if yPredClass == yTrueClass else ‘r’))
ax[i][j].set_axis_off()
plt.tight_layout()
def plot_images_BW(X,y,yp,M,N):
f,ax = plt.subplots(M,N, sharex=True, sharey=True, figsize=(N,M*1.3))
for i in range(M):
for j in range(N):
ax[i][j].imshow(1-X[i*N+j][0].cpu().numpy(), cmap=”gray”)
yPredClass = yp[i*N+j].max(dim=0)[1]
yTrueClass = y[i*N+j];
title = ax[i][j].set_title(classNames[yPredClass])
plt.setp(title, color=(‘g’ if yPredClass == yTrueClass else ‘r’))
ax[i][j].set_axis_off()
plt.tight_layout()
for X,y in testloader:
X,y = X.to(device), y.to(device)
break
ypredicted = torch.randn((y.shape[0],num_classes),dtype=torch.float32) # random
predictions, this should be replaced by the model!
plot_images_RGB(X, y, ypredicted, 3, 6)
Purchase answer to see full
attachment