/
attack_generator.py
499 lines (415 loc) · 20.1 KB
/
attack_generator.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
import numpy as np
from models import *
import PIL
from PIL import Image
from attack_apgd import APGDAttack, APGDAttack_targeted
import datetime
from fab_pt import FABAttack_PT
from square import SquareAttack
from utils import Logger
import os
# Nomal CW loss
def cwloss(output, target,confidence=50, num_classes=10):
target = target.data
target_onehot = torch.zeros(target.size() + (num_classes,))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
real = (target_var * output).sum(1)
other = ((1. - target_var) * output - target_var * 10000.).max(1)[0]
loss = -torch.clamp(real - other + confidence, min=0.) # equiv to max(..., 0.)
loss = torch.sum(loss)
return loss
# loss for MM Attack
def mm_loss(output, target, target_choose, confidence=50, num_classes=10):
target = target.data
target_onehot = torch.zeros(target.size() + (num_classes,))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
real = (target_var * output).sum(1)
target_onehot = torch.zeros(target_choose.size() + (num_classes,))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target_choose.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
other = (target_var * output).sum(1)
loss = -torch.clamp(real - other + confidence, min=0.) # equiv to max(..., 0.)
loss = torch.sum(loss)
return loss
# loss for MM AT
def mm_loss_train(output, target, target_choose, num_classes=10):
target = target.data
target_onehot = torch.zeros(target.size() + (num_classes,))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
real = (target_var * output).sum(1)
target_onehot = torch.zeros(target_choose.size() + (num_classes,))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target_choose.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
other = (target_var * output).sum(1)
return other-real
# Nomal PGD
def pgd(model, data, target, epsilon, step_size, num_steps,loss_fn,category,rand_init,num_classes):
model.eval()
if category == "trades":
x_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda().detach() if rand_init else data.detach()
if category == "Madry":
x_adv = data.detach() + torch.from_numpy(np.random.uniform(-epsilon, epsilon, data.shape)).float().cuda() if rand_init else data.detach()
x_adv = torch.clamp(x_adv, 0.0, 1.0)
for k in range(num_steps):
x_adv.requires_grad_()
output = model(x_adv)
model.zero_grad()
with torch.enable_grad():
if loss_fn == "cent":
loss_adv = nn.CrossEntropyLoss(reduction="mean")(output, target)
if loss_fn == "cw":
loss_adv = cwloss(output,target,num_classes=num_classes)
loss_adv.backward()
eta = step_size * x_adv.grad.sign()
x_adv = x_adv.detach() + eta
x_adv = torch.min(torch.max(x_adv, data - epsilon), data + epsilon)
x_adv = torch.clamp(x_adv, 0.0, 1.0)
return x_adv
# tpgd
def tpgd(model, data, target, epsilon, step_size, step1_size, num_steps,loss_fn,category,rand_init,k,num_classes):
model.eval()
if category == "trades":
x_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda().detach() if rand_init else data.detach()
if category == "Madry":
x_adv = data.detach() + torch.from_numpy(np.random.uniform(-epsilon, epsilon, data.shape)).float().cuda() if rand_init else data.detach()
x_adv = torch.clamp(x_adv, 0.0, 1.0)
logits = model(data)
target_onehot = torch.zeros(target.size() + (len(logits[0]),))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
index = torch.argsort(logits-10000*target_var)[:,num_classes-k:]
x_adv_set = []
for i in range(k):
x_adv_0 = x_adv.clone().detach()
for j in range(num_steps):
x_adv_0.requires_grad_()
output1 = model(x_adv_0)
model.zero_grad()
with torch.enable_grad():
loss_adv0 = -1. * nn.CrossEntropyLoss(reduction="sum")(output1, index[:,i])
loss_adv0.backward()
eta = step_size * x_adv_0.grad.sign()
x_adv_0 = x_adv_0.detach() + eta
x_adv_0 = torch.min(torch.max(x_adv_0, data - epsilon), data + epsilon)
x_adv_0 = torch.clamp(x_adv_0, 0.0, 1.0)
x_adv_set.append(x_adv_0)
return x_adv_set
# evaluation for clean examples
def eval_clean(model, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
output = model(data)
test_loss += nn.CrossEntropyLoss(reduction='mean')(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
log = 'Natrual Test Result ==> Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset))
# print(log)
test_accuracy = correct / len(test_loader.dataset)
return test_loss, test_accuracy
# evaluation for normal PGD and CW attack
def eval_robust(model, test_loader, perturb_steps, epsilon, step_size, loss_fn, category, rand_init, num_classes):
starttime = datetime.datetime.now()
model.eval()
test_loss = 0
correct = 0
with torch.enable_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
x_adv = pgd(model,data,target,epsilon,step_size,perturb_steps,loss_fn,category,rand_init=rand_init, num_classes=num_classes)
output = model(x_adv)
test_loss += nn.CrossEntropyLoss(reduction='mean')(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
log = 'Attack Setting ==> Loss_fn:{}, Perturb steps:{}, Epsilon:{}, Step dize:{} \n Test Result ==> Average loss: {:.4f}, Accuracy: {}/{} ({:.2f}%)'.format(loss_fn,perturb_steps,epsilon,step_size,
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset))
# print(log)
test_accuracy = correct / len(test_loader.dataset)
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, test_accuracy, time
# evaluation for mmu
def eval_robust_mmu_sequential(model, test_loader, perturb_steps, epsilon, step_size, step1_size, loss_fn, category, rand_init,k,num_classes):
starttime = datetime.datetime.now()
model.eval()
test_loss = 0
bs = 128
with torch.no_grad():
for data, target in test_loader:
x_orig, y_orig = data, target
n_batches = int(np.ceil(len(test_loader.dataset)/bs))
robust_flags = torch.zeros(len(test_loader.dataset),dtype=torch.bool)
for batch_idx in range(n_batches):
start_idx = batch_idx * bs
end_idx = min((batch_idx + 1) * bs, len(test_loader.dataset))
x = x_orig[start_idx:end_idx, :].clone().cuda()
y = y_orig[start_idx:end_idx].clone().cuda()
output = model(x)
correct_batch = y.eq(output.max(dim=1)[1])
robust_flags[start_idx:end_idx] = correct_batch.detach()
robust_accuracy = torch.sum(robust_flags).item() / x_orig.shape[0]
x_adv = x_orig.clone().detach()
for i in range(k):
num_robust = torch.sum(robust_flags).item()
n_batches = int(np.ceil(num_robust / bs))
robust_lin_idcs = torch.nonzero(robust_flags, as_tuple=False)
if num_robust > 1:
robust_lin_idcs.squeeze_()
for batch_idx in range(n_batches):
start_idx = batch_idx * bs
end_idx = min((batch_idx + 1) * bs, num_robust)
batch_datapoint_idcs = robust_lin_idcs[start_idx:end_idx]
if len(batch_datapoint_idcs.shape) > 1:
batch_datapoint_idcs.squeeze_(-1)
x = x_orig[batch_datapoint_idcs, :].clone().cuda()
y = y_orig[batch_datapoint_idcs].clone().cuda()
logits = model(x)
target_onehot = torch.zeros(y.size() + (len(logits[0]),))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, y.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
index_i = torch.argsort(logits-10000*target_var)[:,-(i+1)]
# make sure that x is a 4d tensor even if there is only a single datapoint left
if len(x.shape) == 3:
x.unsqueeze_(dim=0)
x_adv0 = x.detach() + torch.from_numpy(np.random.uniform(-epsilon, epsilon, x.shape)).float().cuda() if rand_init else x.detach()
x_adv0 = torch.clamp(x_adv0, 0.0, 1.0)
for j in range(perturb_steps):
x_adv0.requires_grad_()
output = model(x_adv0)
model.zero_grad()
with torch.enable_grad():
loss_adv = mm_loss(output,y,index_i,num_classes=num_classes)
loss_adv.backward()
eta = step_size * x_adv0.grad.sign()
x_adv0 = x_adv0.detach() + eta
x_adv0 = torch.min(torch.max(x_adv0, x - epsilon), x + epsilon)
x_adv0 = torch.clamp(x_adv0, 0.0, 1.0)
output = model(x_adv0)
false_batch = ~y.eq(output.max(dim=1)[1]).cuda()
non_robust_lin_idcs = batch_datapoint_idcs[false_batch]
robust_flags[non_robust_lin_idcs] = False
# x_adv[non_robust_lin_idcs] = x_adv[false_batch].detach().cuda()
robust_accuracy = torch.sum(robust_flags).item() / x_orig.shape[0]
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, robust_accuracy, time
# evaluation for autoattack
def eval_robust_auto(model, test_loader, perturb_steps, epsilon, loss_fn):
starttime = datetime.datetime.now()
apgd = APGDAttack(model, n_restarts=1, n_iter=perturb_steps, verbose=False,
eps=epsilon, norm='Linf', eot_iter=1, rho=.75, seed=1, device='cuda')
# apgd = APGDAttack_targeted(model, n_restarts=1, n_iter=100, verbose=False,
# eps=epsilon, norm='Linf', eot_iter=1, rho=.75, seed=0, device='cuda')
model.eval()
test_loss = 0
correct = 0
with torch.enable_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
apgd.loss = loss_fn
x_adv = apgd.perturb(data, target)
output = model(x_adv)
test_loss += nn.CrossEntropyLoss(reduction='mean')(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, test_accuracy, time
# evaluation for fab
def eval_robust_auto_fab(model, test_loader, perturb_steps, epsilon):
starttime = datetime.datetime.now()
fab = FABAttack_PT(model, n_restarts=1, n_iter=perturb_steps, eps=epsilon, seed=1,
norm='Linf', verbose=False, device='cuda')
model.eval()
test_loss = 0
correct = 0
with torch.enable_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
fab.targeted = False
x_adv = fab.perturb(data, target)
output = model(x_adv)
test_loss += nn.CrossEntropyLoss(reduction='mean')(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, test_accuracy, time
# evaluation for square
def eval_robust_auto_square(model, test_loader, epsilon):
starttime = datetime.datetime.now()
square = SquareAttack(model, p_init=.8, n_queries=5000, eps=epsilon, norm='Linf',
n_restarts=1, seed=1, verbose=False, device='cuda', resc_schedule=False)
model.eval()
test_loss = 0
correct = 0
with torch.enable_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
x_adv = square.perturb(data, target)
output = model(x_adv)
test_loss += nn.CrossEntropyLoss(reduction='mean')(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, test_accuracy, time
def eval_robust_auto_topk_target(model, test_loader, perturb_steps, epsilon, step_size, loss_fn, category, rand_init, k, num_classes):
starttime = datetime.datetime.now()
apgd = APGDAttack_targeted(model, n_restarts=1, n_iter=perturb_steps, verbose=False,
eps=epsilon, norm='Linf', eot_iter=1, rho=.75, seed=1, device='cuda')
model.eval()
test_loss = 0
correct = 0
with torch.enable_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
apgd.loss = loss_fn
apgd.n_target_classes = k
x_adv = apgd.perturb(data, target)
output = model(x_adv)
test_loss += nn.CrossEntropyLoss(reduction='mean')(output, target).item()
pred = output.max(1, keepdim=True)[1]
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, test_accuracy, time
def eval_robust_mm_sequential(model, test_loader, perturb_steps, epsilon, loss_fn, k):
starttime = datetime.datetime.now()
model.eval()
test_loss = 0
bs = 128
apgd = APGDAttack_targeted(model, n_restarts=1, n_iter=perturb_steps, verbose=False,
eps=epsilon, norm='Linf', eot_iter=1, rho=.75, seed=1, device='cuda')
with torch.no_grad():
for data, target in test_loader:
x_orig, y_orig = data, target
n_batches = int(np.ceil(len(test_loader.dataset)/bs))
robust_flags = torch.zeros(len(test_loader.dataset),dtype=torch.bool)
for batch_idx in range(n_batches):
start_idx = batch_idx * bs
end_idx = min((batch_idx + 1) * bs, len(test_loader.dataset))
x = x_orig[start_idx:end_idx, :].clone().cuda()
y = y_orig[start_idx:end_idx].clone().cuda()
output = model(x)
correct_batch = y.eq(output.max(dim=1)[1])
robust_flags[start_idx:end_idx] = correct_batch.detach()
robust_accuracy = torch.sum(robust_flags).item() / x_orig.shape[0]
x_adv = x_orig.clone().detach()
for i in range(k):
num_robust = torch.sum(robust_flags).item()
n_batches = int(np.ceil(num_robust / bs))
robust_lin_idcs = torch.nonzero(robust_flags, as_tuple=False)
if num_robust > 1:
robust_lin_idcs.squeeze_()
for batch_idx in range(n_batches):
start_idx = batch_idx * bs
end_idx = min((batch_idx + 1) * bs, num_robust)
batch_datapoint_idcs = robust_lin_idcs[start_idx:end_idx]
if len(batch_datapoint_idcs.shape) > 1:
batch_datapoint_idcs.squeeze_(-1)
x = x_orig[batch_datapoint_idcs, :].clone().cuda()
y = y_orig[batch_datapoint_idcs].clone().cuda()
# make sure that x is a 4d tensor even if there is only a single datapoint left
if len(x.shape) == 3:
x.unsqueeze_(dim=0)
apgd.n_target_classes = 1
apgd.loss = loss_fn
x_adv0 = apgd.perturb(x, y, i=i)
output = model(x_adv0)
false_batch = ~y.eq(output.max(dim=1)[1]).cuda()
non_robust_lin_idcs = batch_datapoint_idcs[false_batch]
robust_flags[non_robust_lin_idcs] = False
# x_adv[non_robust_lin_idcs] = x_adv[false_batch].detach().cuda()
robust_accuracy = torch.sum(robust_flags).item() / x_orig.shape[0]
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, robust_accuracy, time
# evaluation for tpgd
def eval_robust_tpgd(model, test_loader, perturb_steps, epsilon, step_size, step1_size, loss_fn, category, rand_init,k,num_classes):
starttime = datetime.datetime.now()
model.eval()
test_loss = 0
correct = 0
with torch.enable_grad():
for data, target in test_loader:
data, target = data.cuda(), target.cuda()
x_adv_set = tpgd(model,data,target,epsilon,step_size,step1_size,perturb_steps,loss_fn,category,rand_init=rand_init,k=k,num_classes=num_classes)
d = torch.tensor([[True] for x in range(len(target))]).cuda()
for i in range(k):
output = model(x_adv_set[i])
pred = output.max(1, keepdim=True)[1]
a = pred.eq(target.view_as(pred))
d = d & a
correct += d.sum()
correct = correct.cpu().numpy()
test_loss /= len(test_loader.dataset)
test_accuracy = correct / len(test_loader.dataset)
endtime = datetime.datetime.now()
time = (endtime - starttime).seconds
return test_loss, test_accuracy, time
def mmu_at_train(model, data, target, epsilon, step_size, num_steps,loss_fn,category,rand_init,k,num_classes):
model.eval()
if category == "trades":
x_adv = data.detach() + 0.001 * torch.randn(data.shape).cuda().detach() if rand_init else data.detach()
if category == "Madry":
x_adv = data.detach() + torch.from_numpy(np.random.uniform(-epsilon, epsilon, data.shape)).float().cuda() if rand_init else data.detach()
x_adv = torch.clamp(x_adv, 0.0, 1.0)
logits = model(data)
target_onehot = torch.zeros(target.size() + (len(logits[0]),))
target_onehot = target_onehot.cuda()
target_onehot.scatter_(1, target.unsqueeze(1), 1.)
target_var = Variable(target_onehot, requires_grad=False)
index = torch.argsort(logits-10000*target_var)[:,num_classes-k:]
x_adv_set = []
loss_set = []
for i in range(k):
x_adv_0 = x_adv.clone().detach()
for j in range(num_steps):
x_adv_0.requires_grad_()
output1 = model(x_adv_0)
model.zero_grad()
with torch.enable_grad():
loss_adv0 = mm_loss(output1,target,index[:,i],num_classes=num_classes)
loss_adv0.backward()
eta = step_size * x_adv_0.grad.sign()
x_adv_0 = x_adv_0.detach() + eta
x_adv_0 = torch.min(torch.max(x_adv_0, data - epsilon), data + epsilon)
x_adv_0 = torch.clamp(x_adv_0, 0.0, 1.0)
pipy = mm_loss_train(model(x_adv_0),target,index[:,i],num_classes=num_classes)
loss_set.append(pipy.view(len(pipy),-1))
x_adv_set.append(x_adv_0)
loss_pipy = loss_set[0]
for i in range(k-1):
loss_pipy = torch.cat((loss_pipy, loss_set[i+1]),1)
index_choose = torch.argsort(loss_pipy)[:,-1]
adv_final = torch.zeros(x_adv.size()).cuda()
for i in range(len(index_choose)):
adv_final[i,:,:,:] = x_adv_set[index_choose[i]][i]
return adv_final