-
Notifications
You must be signed in to change notification settings - Fork 7
/
attack_tester.py
212 lines (127 loc) · 6.51 KB
/
attack_tester.py
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
import numpy as np
import torch
from torch.autograd import Variable
# from torchvision import transforms
import sys, os
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import config as cf
class AttacksTester:
def __init__(self, model, raw_data_loader, N, eps, parent_folder=None, folder=None):
self.print_freq = 10
self.parent_folder = parent_folder
self.folder = folder
self.count = 0
# Save the advesarial example in tensor format
self.x_adv_arr = np.zeros((N,3,32,32))
self.y_label_arr = np.zeros(N)
self.eps = eps
self.model = model
self.data_loader = raw_data_loader
self.run_model()
# Maybe I can create an external class to handle this...
self.save_arr(loc=self.parent_folder)
def run_model(self):
# Perform this if there is no folder created in desired path and
# the number of images does not match that in the folder.
arr = []
acc_org = cf.AverageMeter()
acc_adv = cf.AverageMeter()
for i, (x, y) in enumerate(self.data_loader):
x = x.cuda().requires_grad_()
y = y.cuda()
# Generate Adversarial
# x_adv = self.pgd(x, y, max_iter=20)
x_adv = self.fast_pgd(x, y, max_iter=100)
# Compute output of adversarial and input
logits = self.model.forward(x)
logits_adv = self.model.forward(x_adv)
prec_org = cf.accuracy(logits, y)
prec_adv = cf.accuracy(logits_adv, y)
acc_org.update(prec_org.item(), x.size(0))
acc_adv.update(prec_adv.item(), x_adv.size(0))
# Test: Display Image
# self.test(x, torch.sign(noise), self.eps)
if i % self.print_freq == 0:
print('Epoch: [{0}/{1}]\t'
'Acc_org: {acc_org.val:.3f} ({acc_org.avg:.3f})\t'
'Acc_adv: {acc_adv.val:.3f} ({acc_adv.avg:.3f})'.format(
i, len(self.data_loader),
acc_org=acc_org,
acc_adv=acc_adv))
# Print Accuracy Results
print(' * Acc Org: {acc_org.avg:.3f}'.format(acc_org=acc_org))
print(' * Acc Adv: {acc_adv.avg:.3f}'.format(acc_adv=acc_adv))
def fast_pgd(self, x_batch, y, max_iter):
x = x_batch.clone().detach().requires_grad_(True).cuda()
# Reshape to [1,C,1,1] to enable broadcasintg
alpha = (self.eps * cf.eps_size / max_iter)[np.newaxis,:,np.newaxis,np.newaxis]
alpha = torch.FloatTensor(alpha).cuda()
for _ in range(max_iter):
logits = self.model(x)
loss = self.model.module.loss(logits, y)
loss.backward()
# Get gradient
x_grad = x.grad.data
# Broad cast alpha to shape [N,C,H,W]
x.data = x.data + alpha * torch.sign(x_grad)
# Clamp data between valid ranges
x.data[:,0,:,:].clamp_(min=cf.min_val[0], max=cf.max_val[0])
x.data[:,1,:,:].clamp_(min=cf.min_val[1], max=cf.max_val[1])
x.data[:,2,:,:].clamp_(min=cf.min_val[2], max=cf.max_val[2])
x.grad.zero_()
# Store adversarial example & label in all array
self.x_adv_arr[self.count:(self.count + len(y))] = x.cpu().data.numpy()
self.y_label_arr[self.count:(self.count + len(y))] = y.cpu().data.numpy()
self.count = self.count + len(y)
return x
# ============================= SUPPORT FUNCTIONS =================================
def compute_scaled_update(self, x, x_grad, alpha):
x_scaled = self.scale_batch(x)
output = x_scaled + alpha * torch.sign(x_grad)
output = torch.clamp(output, min=0.0, max=1.0)
out = self.gaussian_normalize(output)
return out
def scale_batch(self, x):
x_copy = x.clone().detach()
x_copy[:,0,:,:] = x_copy[:,0,:,:] * cf.std[0] + cf.mean[0]
x_copy[:,1,:,:] = x_copy[:,1,:,:] * cf.std[1] + cf.mean[1]
x_copy[:,2,:,:] = x_copy[:,2,:,:] * cf.std[2] + cf.mean[2]
x_copy = torch.clamp(x_copy, min=0.0, max=1.0)
return x_copy
def gaussian_normalize(self, x):
x_copy = x.clone().detach()
x_copy[:,0,:,:] = (x_copy[:,0,:,:] - cf.mean[0]) / cf.std[0]
x_copy[:,1,:,:] = (x_copy[:,1,:,:] - cf.mean[1]) / cf.std[1]
x_copy[:,2,:,:] = (x_copy[:,2,:,:] - cf.mean[2]) / cf.std[2]
return x_copy
def save_arr(self, root='datasets/', loc='cifar-10-fgsm'):
path = root + loc
if not os.path.exists(path):
os.makedirs(path)
np.save(os.path.join(path, 'x_adv_'+self.folder+'.npy'), self.x_adv_arr)
np.save(os.path.join(path,'y_'+self.folder+'.npy'), self.y_label_arr)
# =========================== SLOW METHODS: TESTING ============================
def fgsm(self, x, y, max_iter):
eps = self.eps * cf.eps_size
alpha = eps
for _ in range(max_iter):
logits = self.model(x)
loss = self.model.module.loss(logits, y)
loss.backward()
x_grad = x.grad.data
# Three ways to compute eps
# x.data = self.compute_scaled_update(x, x_grad, alpha)
x.data[:,0,:,:] = x.data[:,0,:,:] + alpha[0] * torch.sign(x_grad[:,0,:,:])
x.data[:,1,:,:] = x.data[:,1,:,:] + alpha[1] * torch.sign(x_grad[:,1,:,:])
x.data[:,2,:,:] = x.data[:,2,:,:] + alpha[2] * torch.sign(x_grad[:,2,:,:])
x.grad.zero_()
return x
def pgd(self, x_batch, y, max_iter=20):
res = torch.zeros((x_batch.size()))
for i, x_init in enumerate(x_batch):
x_img = x_init.unsqueeze(0).clone().detach().requires_grad_(True).cuda()
y_t = y[i].unsqueeze(0)
res[i] = self.fgsm(x_img, y_t, max_iter)
return res