-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
220 lines (177 loc) · 6.47 KB
/
Copy pathutils.py
File metadata and controls
220 lines (177 loc) · 6.47 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import numpy as np
import matplotlib.pyplot as plt
import json
import torch
import torchvision
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torchattacks import *
import robustbench
from robustbench.utils import load_model, clean_accuracy
def generate_adv(model, attack):
"""
generate adversarial attacks
model: the model to be attacked
attack: the name of the attack
"""
if attack == "pgd":
atk = PGD(model, eps=8 / 255, alpha=2 / 225, steps=10, random_start=True)
elif attack == "fgsm":
atk = FGSM(model)
elif attack == "pixle":
atk = Pixle(model)
elif attack == "nifgsm":
atk = NIFGSM(model)
elif attack == "autoattack":
atk = AutoAttack(model)
elif attack == "vnifgsm":
atk = VNIFGSM(model)
elif attack =="vmifgsm":
atk = VMIFGSM(model)
return atk
def get_pred(model, images, device):
"""
Get the predictions from the model
model: the model
images: the images as the input
device: the device to use cuda / cpu
"""
logits = model(images.to(device))
_, pres = logits.max(dim=1)
return pres.cpu()
def imshow(img, title):
"""
Show the image
image: the image
title: the title of the image
"""
img = torchvision.utils.make_grid(img.cpu().data, normalize=True)
npimg = img.numpy()
fig = plt.figure(figsize = (5, 15))
plt.imshow(np.transpose(npimg,(1,2,0)))
plt.title(title)
plt.show()
def load_dataset(n_examples, loader, batch_size = 100) :
"""
Load the dataset for generating adversarial samples
n_examples: the number of examples
loader: the data loader
batch_size: the batch size
"""
x_list, y_list = [], []
for i, (x, y) in enumerate(loader):
x_list.append(x)
#print(x.shape)
y_list.append(y)
if n_examples is not None and batch_size * i >= n_examples:
break
x_list_tensor = torch.cat(x_list)
y_list_tensor = torch.cat(y_list)
if n_examples is not None:
x_list_tensor = x_list_tensor[:n_examples]
y_list_tensor = y_list_tensor[:n_examples]
return x_list_tensor, y_list_tensor
def CIFAR10(batch_size=128, finetune = False, input_size = 224, test_batch_size=128):
"""
The cifar 10 dataset
batch_size: the batch size default is 128
finetune: whether to transfomer the dataset
input_size: the input size
test_batch_size: the batch size of test dataset
"""
if finetune:
transformer = {
'train': transforms.Compose([
transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
else:
transformer = {
'train': transforms.Compose([
transforms.ToTensor()
]),
'val': transforms.Compose([
transforms.ToTensor()
]),
}
train_dataset = datasets.CIFAR10('./datasets/CIFAR-10', train=True,
download=True, transform=transformer['train'])
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True)
val_dataset = datasets.CIFAR10('./datasets/CIFAR-10', train=False, download=True,
transform=transformer['val'])
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=test_batch_size, shuffle=True)
return train_dataset, val_dataset, train_loader, val_loader
def CIFAR100(batch_size=128, finetune = False, input_size = 224, test_batch_size=128):
"""
The cifar 100 dataset
batch_size: the batch size default is 128
finetune: whether to transfomer the dataset
input_size: the input size
test_batch_size: the batch size of test dataset
"""
if finetune:
transformer = {
'train': transforms.Compose([
transforms.RandomResizedCrop(input_size),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
'val': transforms.Compose([
transforms.Resize(input_size),
transforms.CenterCrop(input_size),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}
else:
transformer = {
'train': transforms.Compose([
transforms.ToTensor()
]),
'val': transforms.Compose([
transforms.ToTensor()
]),
}
train_dataset = datasets.CIFAR100('./datasets/CIFAR-100', train=True,
download=True, transform=transformer['train'])
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=batch_size, shuffle=True)
val_dataset = datasets.CIFAR100('./datasets/CIFAR-100', train=False, download=True,
transform=transformer['val'])
val_loader = torch.utils.data.DataLoader(
val_dataset, batch_size=test_batch_size, shuffle=True)
return train_dataset, val_dataset, train_loader, val_loader
def test_attack(val_loader, attack, model, device = "cuda"):
"""
test the accuracy with the attacked testloader
val_loader: the validation loader
atttack: the attack used for generating adversarial samples
model: the model of to be tested
device: the device for testing
"""
step_counter = 0
acc_counter = 0
for i, (images, labels) in enumerate(val_loader):
adv_images_adv = attack(images, labels)
adv_images_adv= adv_images_adv.to(device)
model = model.to(device)
labels = labels.to(device)
acc = clean_accuracy(model, adv_images_adv, labels)
acc_counter += acc*len(images)
step_counter += len(images)
print('[Model loaded]')
print('Acc: %2.2f %%'%(acc_counter/step_counter*100))
print("adv model")
return acc_counter / step_counter