-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattacks.py
More file actions
1002 lines (823 loc) · 41.4 KB
/
attacks.py
File metadata and controls
1002 lines (823 loc) · 41.4 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
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import torchattacks
import torch
import torchvision.datasets as dset
import utils
import torch.nn.functional as F
from tqdm.auto import tqdm, trange
from torchvision import transforms
from torchattacks.attack import Attack
from torchattacks.attacks._differential_evolution import differential_evolution
import numpy as np
import torch.nn as nn
class OnePixel(Attack):
r"""
Attack in the paper 'One pixel attack for fooling deep neural networks'
[https://arxiv.org/abs/1710.08864]
Modified from "https://github.com/DebangLi/one-pixel-attack-pytorch/" and
"https://github.com/sarathknv/adversarial-examples-pytorch/blob/master/one_pixel_attack/"
Distance Measure : L0
Arguments:
model (nn.Module): model to attack.
pixels (int): number of pixels to change (Default: 1)
steps (int): number of steps. (Default: 10)
popsize (int): population size, i.e. the number of candidate agents or "parents" in differential evolution (Default: 10)
inf_batch (int): maximum batch size during inference (Default: 128)
Shape:
- images: :math:`(N, C, H, W)` where `N = number of batches`, `C = number of channels`, `H = height` and `W = width`. It must have a range [0, 1].
- labels: :math:`(N)` where each value :math:`y_i` is :math:`0 \leq y_i \leq` `number of labels`.
- output: :math:`(N, C, H, W)`.
Examples::
>>> attack = torchattacks.OnePixel(model, pixels=1, steps=10, popsize=10, inf_batch=128)
>>> adv_images = attack(images, labels)
"""
def __init__(self, model, pixels=1, steps=10, popsize=10, inf_batch=128):
super().__init__("OnePixel", model)
self.pixels = pixels
self.steps = steps
self.popsize = popsize
self.inf_batch = inf_batch
self.supported_mode = ["default", "targeted"]
def forward(self, images, labels):
r"""
Overridden.
"""
images = images.clone().detach().to(self.device)
labels = labels.clone().detach().to(self.device)
if self.targeted:
target_labels = self.get_target_label(images, labels)
batch_size, channel, height, width = images.shape
bounds = [(0, height), (0, width)] + [(0, 1)] * channel
bounds = bounds * self.pixels
popmul = max(1, int(self.popsize / len(bounds)))
adv_images = []
for idx in range(batch_size):
image, label = images[idx : idx + 1], labels[idx : idx + 1]
if self.targeted:
target_label = target_labels[idx : idx + 1]
def func(delta):
print("self loss: ", self._loss)
return self._loss(image, target_label, delta)
def callback(delta, convergence):
return self._attack_success(image, target_label, delta)
else:
def func(delta):
return self._loss(image, label, delta)
def callback(delta, convergence):
return self._attack_success(image, label, delta)
delta = differential_evolution(
func=func,
bounds=bounds,
callback=callback,
maxiter=self.steps,
popsize=popmul,
init="random",
recombination=1,
atol=-1,
polish=False,
).x
delta = np.split(delta, len(delta) / len(bounds))
adv_image = self._perturb(image, delta)
adv_images.append(adv_image)
adv_images = torch.cat(adv_images)
return adv_images
def _loss(self, image, label, delta):
adv_images = self._perturb(image, delta) # Mutiple delta
prob = self._get_prob(adv_images)[:, label]
if self.targeted:
return 1 - prob # If targeted, increase prob
else:
return prob # If non-targeted, decrease prob
def _attack_success(self, image, label, delta):
adv_image = self._perturb(image, delta) # Single delta
prob = self._get_prob(adv_image)
pre = np.argmax(prob)
if self.targeted and (pre == label):
return True
elif (not self.targeted) and (pre != label):
return True
return False
def _get_prob(self, images):
with torch.no_grad():
batches = torch.split(images, self.inf_batch)
outs = []
for batch in batches:
out, _ = self.get_logits(batch)
outs.append(out)
outs = torch.cat(outs)
prob = F.softmax(outs, dim=1)
return prob.detach().cpu().numpy()
def _perturb(self, image, delta):
delta = np.array(delta)
if len(delta.shape) < 2:
delta = np.array([delta])
num_delta = len(delta)
adv_image = image.clone().detach().to(self.device)
adv_images = torch.cat([adv_image] * num_delta, dim=0)
for idx in range(num_delta):
pixel_info = delta[idx].reshape(self.pixels, -1)
for pixel in pixel_info:
pos_x, pos_y = pixel[:2]
channel_v = pixel[2:]
for channel, v in enumerate(channel_v):
adv_images[idx, channel, int(pos_x), int(pos_y)] = v
return adv_images
def apply_attack_on_limited_dataset(model, dataloader, attack, device, verbose=True, n=1):
robust_accuracy = []
top1 = utils.AvgrageMeter()
top5 = utils.AvgrageMeter()
for k, (images, labels) in enumerate(dataloader):
if k >= n:
break
images, labels = images.to(device), labels.to(device)
logits, logits_aux = model(images)
prec1, prec5 = utils.accuracy(logits, labels, topk=(1, 5))
n = images.size(0)
top1.update(prec1.item(), n)
top5.update(prec5.item(), n)
clean_accuracy = top1
print('Clean accuracy: %f', clean_accuracy.avg)
for k, (images, labels) in enumerate(dataloader):
if k >= n:
break
images, labels = images.to(device), labels.to(device)
adv_images = attack(images, labels)
logits, logits_aux = model(adv_images)
prec1, prec5 = utils.accuracy(logits, labels, topk=(1, 5))
n = images.size(0)
top1.update(prec1.item(), n)
top5.update(prec5.item(), n)
robust_acc = top1
if verbose:
print('Robust accuracy: %f', robust_acc.avg)
robust_accuracy.append(robust_acc.avg)
return clean_accuracy, robust_accuracy
class FGSM(Attack):
r"""
FGSM in the paper 'Explaining and harnessing adversarial examples'
[https://arxiv.org/abs/1412.6572]
Distance Measure : Linf
Arguments:
model (nn.Module): model to attack.
eps (float): maximum perturbation. (Default: 0.007)
Shape:
- images: :math:`(N, C, H, W)` where `N = number of batches`, `C = number of channels`, `H = height` and `W = width`. It must have a range [0, 1].
- labels: :math:`(N)` where each value :math:`y_i` is :math:`0 \leq y_i \leq` `number of labels`.
- output: :math:`(N, C, H, W)`.
Examples::
>>> attack = torchattacks.FGSM(model, eps=0.007)
>>> adv_images = attack(images, labels)
"""
def __init__(self, model, eps=0.35, mode='bp'):
super().__init__("FGSM", model)
self.eps = eps
self._supported_mode = ['default', 'targeted']
self.mode=mode
def forward(self, images, labels):
r"""
Overridden.
"""
images = images.clone().detach().to(self.device)
labels = labels.clone().detach().to(self.device)
loss_function = nn.CrossEntropyLoss()
images.requires_grad = True
outputs, _ = self.model(images)
prec1, prec5 = utils.accuracy(outputs, labels, topk=(1, 5))
n = images.size(0)
# Calculate loss
cost = loss_function(outputs, labels)
if self.mode == 'DFA':
# Zero gradients
self.model.zero_grad()
loss_gradient = torch.autograd.grad(cost, outputs, retain_graph=True)[0]
# Broadcast gradient of the loss to every layer
for layer in self.model[1].module.modules():
layer.loss_gradient = loss_gradient
cost.backward()
grad = images.grad
else:
# Update adversarial images
grad = torch.autograd.grad(cost, images, retain_graph=False, create_graph=False)[0]
# save the gradient w.r.t. the input for further inspection
self.grad = grad
adv_images = images + self.eps*grad.sign()
adv_images = torch.clamp(adv_images, min=0, max=1).detach()
return adv_images
class PGD(Attack):
r"""
PGD in the paper 'Towards Deep Learning Models Resistant to Adversarial Attacks'
[https://arxiv.org/abs/1706.06083]
Distance Measure : Linf
Arguments:
model (nn.Module): model to attack.
eps (float): maximum perturbation. (Default: 0.3)
alpha (float): step size. (Default: 2/255)
steps (int): number of steps. (Default: 40)
random_start (bool): using random initialization of delta. (Default: True)
Shape:
- images: :math:`(N, C, H, W)` where `N = number of batches`, `C = number of channels`, `H = height` and `W = width`. It must have a range [0, 1].
- labels: :math:`(N)` where each value :math:`y_i` is :math:`0 \leq y_i \leq` `number of labels`.
- output: :math:`(N, C, H, W)`.
Examples::
>>> attack = torchattacks.PGD(model, eps=8/255, alpha=1/255, steps=40, random_start=True)
>>> adv_images = attack(images, labels)
"""
def __init__(self, model, eps=0.35, mode='bp',
alpha=2/255, steps=10, random_start=True):
super().__init__("PGD", model)
self.eps = eps
self.alpha = alpha
self.steps = steps
self.random_start = random_start
self._supported_mode = ['default', 'targeted']
self.mode = mode
def forward(self, images, labels):
images = images.clone().detach().to(self.device)
labels = labels.clone().detach().to(self.device)
loss = nn.CrossEntropyLoss()
adv_images = images.clone().detach()
if self.random_start:
adv_images = adv_images + torch.empty_like(adv_images).uniform_(-self.eps, self.eps)
adv_images = torch.clamp(adv_images, min=0, max=1).detach()
for _ in range(self.steps):
adv_images.requires_grad = True
outputs, _ = self.model(adv_images)
cost = loss(outputs, labels)
if self.mode == 'DFA':
self.model.zero_grad()
loss_gradient = torch.autograd.grad(cost, outputs, retain_graph=True)[0]
for layer in self.model.modules():
if hasattr(layer, 'loss_gradient'):
layer.loss_gradient = loss_gradient
cost.backward()
grad = adv_images.grad
else:
grad = torch.autograd.grad(cost, adv_images, retain_graph=False, create_graph=False)[0]
adv_images = adv_images.detach() + self.alpha * grad.sign()
delta = torch.clamp(adv_images - images, min=-self.eps, max=self.eps)
adv_images = torch.clamp(images + delta, min=0, max=1).detach()
return adv_images
class TPGD(Attack):
r"""
PGD based on KL-Divergence loss in the paper 'Theoretically Principled Trade-off between Robustness and Accuracy'
[https://arxiv.org/abs/1901.08573]
Distance Measure : Linf
Arguments:
model (nn.Module): model to attack.
eps (float): strength of the attack or maximum perturbation. (Default: 8/255)
alpha (float): step size. (Default: 2/255)
steps (int): number of steps. (Default: 7)
Shape:
- images: :math:`(N, C, H, W)` where `N = number of batches`, `C = number of channels`, `H = height` and `W = width`. It must have a range [0, 1].
- output: :math:`(N, C, H, W)`.
Examples::
>>> attack = torchattacks.TPGD(model, eps=8/255, alpha=2/255, steps=7)
>>> adv_images = attack(images)
"""
def __init__(self, model, mode='bp', eps=8/255, alpha=2/255, steps=7):
super().__init__("TPGD", model)
self.eps = eps
self.alpha = alpha
self.steps = steps
self._supported_mode = ['default']
self.mode = mode
def forward(self, images, labels=None):
images = images.clone().detach().to(self.device)
logit_ori, _ = self.model(images)
logit_ori.detach()
labels = F.softmax(logit_ori, dim=1)
adv_images = images + 0.001 * torch.randn_like(images)
adv_images = torch.clamp(adv_images, min=0, max=1).detach()
loss = nn.KLDivLoss(reduction='sum')
for _ in range(self.steps):
adv_images.requires_grad = True
logit_adv, _ = self.model(adv_images)
outputs = F.log_softmax(logit_adv, dim=1)
cost = loss(outputs, labels)
if self.mode == 'DFA':
self.model.zero_grad()
loss_gradient = torch.autograd.grad(cost, outputs, retain_graph=True)[0]
for layer in self.model.modules():
if hasattr(layer, 'loss_gradient'):
layer.loss_gradient = loss_gradient
cost.backward()
grad = adv_images.grad
else:
grad = torch.autograd.grad(cost, adv_images, retain_graph=False, create_graph=False)[0]
adv_images = adv_images.detach() + self.alpha * grad.sign()
delta = torch.clamp(adv_images - images, min=-self.eps, max=self.eps)
adv_images = torch.clamp(images + delta, min=0, max=1).detach()
return adv_images
import time
import math
import time
import math
class Square(Attack):
def __init__(self, model, norm='Linf', eps=None, n_queries=50, n_restarts=1,
p_init=.8, loss='margin', resc_schedule=True,
seed=0, verbose=False, targeted=False):
super().__init__("Square", model)
self.norm = norm
self.n_queries = n_queries
self.eps = eps
self.p_init = p_init
self.n_restarts = n_restarts
self.seed = seed
self.verbose = verbose
self.loss = loss
self.rescale_schedule = resc_schedule
self._supported_mode = ['default', 'targeted']
self._targeted = targeted
def forward(self, images, labels):
images = images.clone().detach().to(self.device)
labels = labels.clone().detach().to(self.device)
adv_images = self.perturb(images, labels)
return adv_images
def margin_and_loss(self, x, y):
logits, _ = self.model(x) # Adjusted to match your forward loop
xent = F.cross_entropy(logits, y, reduction='none')
u = torch.arange(x.shape[0])
y_corr = logits[u, y].clone()
logits[u, y] = -float('inf')
y_others = logits.max(dim=-1)[0]
if not self._targeted:
if self.loss == 'ce':
return y_corr - y_others, -1. * xent
elif self.loss == 'margin':
return y_corr - y_others, y_corr - y_others
else:
if self.loss == 'ce':
return y_others - y_corr, xent
elif self.loss == 'margin':
return y_others - y_corr, y_others - y_corr
def attack_single_run(self, x, y):
with torch.no_grad():
adv = x.clone()
c, h, w = x.shape[1:]
n_features = c * h * w
n_ex_total = x.shape[0]
if self.norm == 'Linf':
x_best = torch.clamp(x + self.eps * self.random_choice([x.shape[0], c, 1, w]), 0., 1.)
margin_min, loss_min = self.margin_and_loss(x_best, y)
n_queries = torch.ones(x.shape[0]).to(self.device)
s_init = int(math.sqrt(self.p_init * n_features / c))
for i_iter in range(self.n_queries):
idx_to_fool = (margin_min > 0.0).nonzero().flatten()
if len(idx_to_fool) == 0:
break
x_curr = self.check_shape(x[idx_to_fool])
x_best_curr = self.check_shape(x_best[idx_to_fool])
y_curr = y[idx_to_fool]
if len(y_curr.shape) == 0:
y_curr = y_curr.unsqueeze(0)
margin_min_curr = margin_min[idx_to_fool]
loss_min_curr = loss_min[idx_to_fool]
p = self.p_selection(i_iter)
s = max(int(round(math.sqrt(p * n_features / c))), 1)
vh = self.random_int(0, h - s)
vw = self.random_int(0, w - s)
new_deltas = torch.zeros([c, h, w]).to(self.device)
new_deltas[:, vh:vh + s, vw:vw + s] = 2. * self.eps * self.random_choice([c, 1, 1])
x_new = x_best_curr + new_deltas
x_new = torch.min(torch.max(x_new, x_curr - self.eps), x_curr + self.eps)
x_new = torch.clamp(x_new, 0., 1.)
x_new = self.check_shape(x_new)
margin, loss = self.margin_and_loss(x_new, y_curr)
# update loss if new loss is better
idx_improved = (loss < loss_min_curr).float()
loss_min[idx_to_fool] = idx_improved * loss + (1. - idx_improved) * loss_min_curr
# update margin and x_best if new loss is better or misclassification
idx_miscl = (margin <= 0.).float()
idx_improved = torch.max(idx_improved, idx_miscl)
margin_min[idx_to_fool] = idx_improved * margin + (1. - idx_improved) * margin_min_curr
idx_improved = idx_improved.reshape([-1, *[1] * len(x.shape[:-1])])
x_best[idx_to_fool] = idx_improved * x_new + (1. - idx_improved) * x_best_curr
n_queries[idx_to_fool] += 1.
ind_succ = (margin_min <= 0.).nonzero().squeeze()
if self.verbose and ind_succ.numel() != 0:
print(f'{i_iter + 1} - success rate={ind_succ.numel()}/{n_ex_total} '
f'({float(ind_succ.numel()) / n_ex_total:.2%}) - avg # queries={n_queries[ind_succ].mean().item():.1f} '
f'- med # queries={n_queries[ind_succ].median().item():.1f} - loss={loss_min.mean():.3f}')
if ind_succ.numel() == n_ex_total:
break
elif self.norm == 'L2':
delta_init = torch.zeros_like(x)
s = h // 5
sp_init = (h - s * 5) // 2
vh = sp_init + 0
for _ in range(h // s):
vw = sp_init + 0
for _ in range(w // s):
delta_init[:, :, vh:vh + s, vw:vw + s] += self.eta(s).view(1, 1, s, s) * self.random_choice([x.shape[0], c, 1, 1])
vw += s
vh += s
x_best = torch.clamp(x + self.normalize(delta_init) * self.eps, 0., 1.)
margin_min, loss_min = self.margin_and_loss(x_best, y)
n_queries = torch.ones(x.shape[0]).to(self.device)
s_init = int(math.sqrt(self.p_init * n_features / c))
for i_iter in range(self.n_queries):
idx_to_fool = (margin_min > 0.0).nonzero().flatten()
if len(idx_to_fool) == 0:
break
x_curr = self.check_shape(x[idx_to_fool])
x_best_curr = self.check_shape(x_best[idx_to_fool])
y_curr = y[idx_to_fool]
if len(y_curr.shape) == 0:
y_curr = y_curr.unsqueeze(0)
margin_min_curr = margin_min[idx_to_fool]
loss_min_curr = loss_min[idx_to_fool]
delta_curr = x_best_curr - x_curr
p = self.p_selection(i_iter)
s = max(int(round(math.sqrt(p * n_features / c))), 3)
if s % 2 == 0:
s += 1
vh = self.random_int(0, h - s)
vw = self.random_int(0, w - s)
new_deltas_mask = torch.zeros_like(x_curr)
new_deltas_mask[:, :, vh:vh + s, vw:vw + s] = 1.0
norms_window_1 = (delta_curr[:, :, vh:vh + s, vw:vw + s] ** 2).sum(dim=(-2, -1), keepdim=True).sqrt()
vh2 = self.random_int(0, h - s)
vw2 = self.random_int(0, w - s)
new_deltas_mask_2 = torch.zeros_like(x_curr)
new_deltas_mask_2[:, :, vh2:vh2 + s, vw2:vw2 + s] = 1.
norms_image = self.lp_norm(x_best_curr - x_curr)
mask_image = torch.max(new_deltas_mask, new_deltas_mask_2)
norms_windows = self.lp_norm(delta_curr * mask_image)
new_deltas = torch.ones([x_curr.shape[0], c, s, s]).to(self.device)
new_deltas *= (self.eta(s).view(1, 1, s, s) * self.random_choice([x_curr.shape[0], c, 1, 1]))
old_deltas = delta_curr[:, :, vh:vh + s, vw:vw + s] / (1e-12 + norms_window_1)
new_deltas += old_deltas
new_deltas = new_deltas / (1e-12 + (new_deltas ** 2).sum(dim=(-2, -1), keepdim=True).sqrt()) * (torch.max(
(self.eps * torch.ones_like(new_deltas)) ** 2 - norms_image ** 2, torch.zeros_like(new_deltas)) / c + norms_windows ** 2).sqrt()
delta_curr[:, :, vh2:vh2 + s, vw2:vw2 + s] = 0.
delta_curr[:, :, vh:vh + s, vw:vw + s] = new_deltas + 0
x_new = torch.clamp(x_curr + self.normalize(delta_curr) * self.eps, 0., 1.)
x_new = self.check_shape(x_new)
norms_image = self.lp_norm(x_new - x_curr)
margin, loss = self.margin_and_loss(x_new, y_curr)
# update loss if new loss is better
idx_improved = (loss < loss_min_curr).float()
loss_min[idx_to_fool] = idx_improved * loss + (1. - idx_improved) * loss_min_curr
# update margin and x_best if new loss is better or misclassification
idx_miscl = (margin <= 0.).float()
idx_improved = torch.max(idx_improved, idx_miscl)
margin_min[idx_to_fool] = idx_improved * margin + (1. - idx_improved) * margin_min_curr
idx_improved = idx_improved.reshape([-1, *[1] * len(x.shape[:-1])])
x_best[idx_to_fool] = idx_improved * x_new + (1. - idx_improved) * x_best_curr
n_queries[idx_to_fool] += 1.
ind_succ = (margin_min <= 0.).nonzero().squeeze()
if self.verbose and ind_succ.numel() != 0:
print(f'{i_iter + 1} - success rate={ind_succ.numel()}/{n_ex_total} '
f'({float(ind_succ.numel()) / n_ex_total:.2%}) - avg # queries={n_queries[ind_succ].mean().item():.1f} '
f'- med # queries={n_queries[ind_succ].median().item():.1f} - loss={loss_min.mean():.3f}')
if ind_succ.numel() == n_ex_total:
break
return n_queries, x_best
def perturb(self, x, y=None):
self.init_hyperparam(x)
adv = x.clone()
if y is None:
if not self._targeted:
with torch.no_grad():
output, _ = self.model(x) # Adjusted to match your forward loop
y_pred = output.max(1)[1]
y = y_pred.detach().clone().long().to(self.device)
else:
with torch.no_grad():
y = self._get_target_label(x, None)
else:
if not self._targeted:
y = y.detach().clone().long().to(self.device)
else:
y = self._get_target_label(x, y)
if not self._targeted:
logits, _ = self.model(x) # Unpack the tuple returned by the model
acc = logits.max(1)[1] == y
else:
logits, _ = self.model(x) # Unpack the tuple returned by the model
acc = logits.max(1)[1] != y
startt = time.time()
torch.random.manual_seed(self.seed)
torch.cuda.random.manual_seed(self.seed)
for counter in range(self.n_restarts):
ind_to_fool = acc.nonzero().squeeze()
if len(ind_to_fool.shape) == 0:
ind_to_fool = ind_to_fool.unsqueeze(0)
if ind_to_fool.numel() != 0:
x_to_fool = x[ind_to_fool].clone()
y_to_fool = y[ind_to_fool].clone()
adv_curr = self.attack_single_run(x_to_fool, y_to_fool)
output_curr, _ = self.model(adv_curr) # Unpack the tuple here
if not self._targeted:
acc_curr = output_curr.max(1)[1] == y_to_fool
else:
acc_curr = output_curr.max(1)[1] != y_to_fool
ind_curr = (acc_curr == 0).nonzero().squeeze()
acc[ind_to_fool[ind_curr]] = 0
adv[ind_to_fool[ind_curr]] = adv_curr[ind_curr].clone()
if self.verbose:
print('restart {} - robust accuracy: {:.2%}'.format(
counter, acc.float().mean()),
'- cum. time: {:.1f} s'.format(
time.time() - startt))
return adv
def init_hyperparam(self, x):
assert self.norm in ['Linf', 'L2']
assert not self.eps is None
assert self.loss in ['ce', 'margin']
if self.device is None:
self.device = x.device
self.orig_dim = list(x.shape[1:])
self.ndims = len(self.orig_dim)
if self.seed is None:
self.seed = time.time()
def check_shape(self, x):
return x if len(x.shape) == (self.ndims + 1) else x.unsqueeze(0)
def random_choice(self, shape):
t = 2 * torch.rand(shape).to(self.device) - 1
return torch.sign(t)
def random_int(self, low=0, high=1, shape=[1]):
t = low + (high - low) * torch.rand(shape).to(self.device)
return t.long()
def normalize(self, x):
if self.norm == 'Linf':
t = x.abs().view(x.shape[0], -1).max(1)[0]
return x / (t.view(-1, *([1] * self.ndims)) + 1e-12)
elif self.norm == 'L2':
t = (x ** 2).view(x.shape[0], -1).sum(-1).sqrt()
return x / (t.view(-1, *([1] * self.ndims)) + 1e-12)
def lp_norm(self, x):
if self.norm == 'L2':
t = (x ** 2).view(x.shape[0], -1).sum(-1).sqrt()
return t.view(-1, *([1] * self.ndims))
def eta_rectangles(self, x, y):
delta = torch.zeros([x, y]).to(self.device)
x_c, y_c = x // 2 + 1, y // 2 + 1
counter2 = [x_c - 1, y_c - 1]
for counter in range(0, max(x_c, y_c)):
delta[max(counter2[0], 0):min(counter2[0] + (2*counter + 1), x),
max(0, counter2[1]):min(counter2[1] + (2*counter + 1), y)
] += 1.0/(torch.Tensor([counter + 1]).view(1, 1).to(
self.device) ** 2)
counter2[0] -= 1
counter2[1] -= 1
delta /= (delta ** 2).sum(dim=(0,1), keepdim=True).sqrt()
return delta
def eta(self, s):
delta = torch.zeros([s, s]).to(self.device)
delta[:s // 2] = self.eta_rectangles(s // 2, s)
delta[s // 2:] = -1. * self.eta_rectangles(s - s // 2, s)
delta /= (delta ** 2).sum(dim=(0, 1), keepdim=True).sqrt()
if torch.rand([1]) > 0.5:
delta = delta.permute([1, 0])
return delta
def p_selection(self, it):
""" schedule to decrease the parameter p """
if self.rescale_schedule:
it = int(it / self.n_queries * 10000)
if 10 < it <= 50:
p = self.p_init / 2
elif 50 < it <= 200:
p = self.p_init / 4
elif 200 < it <= 500:
p = self.p_init / 8
elif 500 < it <= 1000:
p = self.p_init / 16
elif 1000 < it <= 2000:
p = self.p_init / 32
elif 2000 < it <= 4000:
p = self.p_init / 64
elif 4000 < it <= 6000:
p = self.p_init / 128
elif 6000 < it <= 8000:
p = self.p_init / 256
elif 8000 < it:
p = self.p_init / 512
else:
p = self.p_init
return p
class APGD(Attack):
r"""
APGD in the paper 'Reliable evaluation of adversarial robustness with an ensemble of diverse parameter-free attacks'
[https://arxiv.org/abs/2003.01690]
[https://github.com/fra31/auto-attack]
Distance Measure : Linf, L2
Arguments:
model (nn.Module): model to attack.
norm (str): Lp-norm of the attack. ['Linf', 'L2'] (Default: 'Linf')
eps (float): maximum perturbation. (Default: None)
steps (int): number of steps. (Default: 100)
n_restarts (int): number of random restarts. (Default: 1)
seed (int): random seed for the starting point. (Default: 0)
loss (str): loss function optimized. ['ce', 'dlr'] (Default: 'ce')
eot_iter (int): number of iteration for EOT. (Default: 1)
rho (float): parameter for step-size update (Default: 0.75)
verbose (bool): print progress. (Default: False)
Shape:
- images: :math:`(N, C, H, W)` where `N = number of batches`, `C = number of channels`, `H = height` and `W = width`. It must have a range [0, 1].
- labels: :math:`(N)` where each value :math:`y_i` is :math:`0 \leq y_i \leq` `number of labels`.
- output: :math:`(N, C, H, W)`.
Examples::
>>> attack = torchattacks.APGD(model, norm='Linf', eps=8/255, steps=100, n_restarts=1, seed=0, loss='ce', eot_iter=1, rho=.75, verbose=False)
>>> adv_images = attack(images, labels)
"""
def __init__(self, model, mode='bp', norm='Linf', eps=8/255, steps=50, n_restarts=1,
seed=0, loss='ce', eot_iter=1, rho=.75, verbose=False):
super().__init__("APGD", model)
self.eps = eps
self.steps = steps
self.norm = norm
self.n_restarts = n_restarts
self.seed = seed
self.loss = loss
self.eot_iter = eot_iter
self.thr_decr = rho
self.verbose = verbose
self._supported_mode = ['default']
self.mode = mode
def forward(self, images, labels):
r"""
Overridden.
"""
images = images.clone().detach().to(self.device)
labels = labels.clone().detach().to(self.device)
_, adv_images = self.perturb(images, labels, cheap=True)
return adv_images
def check_oscillation(self, x, j, k, y5, k3=0.75):
t = np.zeros(x.shape[1])
for counter5 in range(k):
t += x[j - counter5] > x[j - counter5 - 1]
return t <= k*k3*np.ones(t.shape)
def check_shape(self, x):
return x if len(x.shape) > 0 else np.expand_dims(x, 0)
def dlr_loss(self, x, y):
x_sorted, ind_sorted = x.sort(dim=1)
ind = (ind_sorted[:, -1] == y).float()
return -(x[np.arange(x.shape[0]), y] - x_sorted[:, -2] * ind - x_sorted[:, -1] * (1. - ind)) / (x_sorted[:, -1] - x_sorted[:, -3] + 1e-12)
def attack_single_run(self, x_in, y_in):
x = x_in.clone() if len(x_in.shape) == 4 else x_in.clone().unsqueeze(0)
y = y_in.clone() if len(y_in.shape) == 1 else y_in.clone().unsqueeze(0)
self.steps_2, self.steps_min, self.size_decr = max(int(0.22 * self.steps), 1), max(int(0.06 * self.steps), 1), max(int(0.03 * self.steps), 1)
if self.verbose:
print('parameters: ', self.steps, self.steps_2, self.steps_min, self.size_decr)
if self.norm == 'Linf':
t = 2 * torch.rand(x.shape).to(self.device).detach() - 1
x_adv = x.detach() + self.eps * torch.ones([x.shape[0], 1, 1, 1]).to(self.device).detach() * t / (t.reshape([t.shape[0], -1]).abs().max(dim=1, keepdim=True)[0].reshape([-1, 1, 1, 1]))
elif self.norm == 'L2':
t = torch.randn(x.shape).to(self.device).detach()
x_adv = x.detach() + self.eps * torch.ones([x.shape[0], 1, 1, 1]).to(self.device).detach() * t / ((t ** 2).sum(dim=(1, 2, 3), keepdim=True).sqrt() + 1e-12)
x_adv = x_adv.clamp(0., 1.)
x_best = x_adv.clone()
x_best_adv = x_adv.clone()
loss_steps = torch.zeros([self.steps, x.shape[0]])
loss_best_steps = torch.zeros([self.steps + 1, x.shape[0]])
acc_steps = torch.zeros_like(loss_best_steps)
if self.loss == 'ce':
criterion_indiv = nn.CrossEntropyLoss(reduction='none')
elif self.loss == 'dlr':
criterion_indiv = self.dlr_loss
else:
raise ValueError('unknowkn loss')
x_adv.requires_grad_()
grad = torch.zeros_like(x)
for _ in range(self.eot_iter):
with torch.enable_grad():
logits, _ = self.model(x_adv) # 1 forward pass (eot_iter = 1)
loss_indiv = criterion_indiv(logits, y)
loss = loss_indiv.sum()
if self.mode == 'DFA':
# Zero gradients
self.model.zero_grad()
loss_gradient = torch.autograd.grad(loss, logits, retain_graph=True)[0]
# Broadcast gradient of the loss to every layer
for layer in self.model[1].module.modules():
layer.loss_gradient = loss_gradient
loss.backward()
grad = x_adv.grad
else:
grad += torch.autograd.grad(loss, [x_adv])[0].detach() # 1 backward pass (eot_iter = 1)
grad /= float(self.eot_iter)
grad_best = grad.clone()
acc = logits.detach().max(1)[1] == y
acc_steps[0] = acc + 0
loss_best = loss_indiv.detach().clone()
step_size = self.eps * torch.ones([x.shape[0], 1, 1, 1]).to(self.device).detach() * torch.Tensor([2.0]).to(self.device).detach().reshape([1, 1, 1, 1])
x_adv_old = x_adv.clone()
counter = 0
k = self.steps_2 + 0
u = np.arange(x.shape[0])
counter3 = 0
loss_best_last_check = loss_best.clone()
reduced_last_check = np.zeros(loss_best.shape) == np.zeros(loss_best.shape)
n_reduced = 0
for i in range(self.steps):
### gradient step
with torch.no_grad():
x_adv = x_adv.detach()
grad2 = x_adv - x_adv_old
x_adv_old = x_adv.clone()
a = 0.75 if i > 0 else 1.0
if self.norm == 'Linf':
x_adv_1 = x_adv + step_size * torch.sign(grad)
x_adv_1 = torch.clamp(torch.min(torch.max(x_adv_1, x - self.eps), x + self.eps), 0.0, 1.0)
x_adv_1 = torch.clamp(torch.min(torch.max(x_adv + (x_adv_1 - x_adv) * a + grad2 * (1 - a), x - self.eps), x + self.eps), 0.0, 1.0)
elif self.norm == 'L2':
x_adv_1 = x_adv + step_size * grad / ((grad ** 2).sum(dim=(1, 2, 3), keepdim=True).sqrt() + 1e-12)
x_adv_1 = torch.clamp(x + (x_adv_1 - x) / (((x_adv_1 - x) ** 2).sum(dim=(1, 2, 3), keepdim=True).sqrt() + 1e-12) * torch.min(
self.eps * torch.ones(x.shape).to(self.device).detach(), ((x_adv_1 - x) ** 2).sum(dim=(1, 2, 3), keepdim=True).sqrt()), 0.0, 1.0)
x_adv_1 = x_adv + (x_adv_1 - x_adv) * a + grad2 * (1 - a)
x_adv_1 = torch.clamp(x + (x_adv_1 - x) / (((x_adv_1 - x) ** 2).sum(dim=(1, 2, 3), keepdim=True).sqrt() + 1e-12) * torch.min(
self.eps * torch.ones(x.shape).to(self.device).detach(), ((x_adv_1 - x) ** 2).sum(dim=(1, 2, 3), keepdim=True).sqrt() + 1e-12), 0.0, 1.0)
x_adv = x_adv_1 + 0.
### get gradient
x_adv.requires_grad_()
grad = torch.zeros_like(x)
for _ in range(self.eot_iter):
with torch.enable_grad():
logits, _ = self.model(x_adv) # 1 forward pass (eot_iter = 1)
loss_indiv = criterion_indiv(logits, y)
loss = loss_indiv.sum()
if self.mode == 'DFA':
# Zero gradients
self.model.zero_grad()
loss_gradient = torch.autograd.grad(loss, logits, retain_graph=True)[0]
# Broadcast gradient of the loss to every layer
for layer in self.model[1].module.modules():
layer.loss_gradient = loss_gradient
loss.backward()
grad = x_adv.grad
else:
grad += torch.autograd.grad(loss, [x_adv])[0].detach() # 1 backward pass (eot_iter = 1)
grad /= float(self.eot_iter)
pred = logits.detach().max(1)[1] == y
acc = torch.min(acc, pred)
acc_steps[i + 1] = acc + 0
x_best_adv[(pred == 0).nonzero().squeeze()] = x_adv[(pred == 0).nonzero().squeeze()] + 0.
if self.verbose:
print('iteration: {} - Best loss: {:.6f}'.format(i, loss_best.sum()))
### check step size
with torch.no_grad():
y1 = loss_indiv.detach().clone()
loss_steps[i] = y1.cpu() + 0
ind = (y1 > loss_best).nonzero().squeeze()
x_best[ind] = x_adv[ind].clone()
grad_best[ind] = grad[ind].clone()
loss_best[ind] = y1[ind] + 0
loss_best_steps[i + 1] = loss_best + 0
counter3 += 1
if counter3 == k:
fl_oscillation = self.check_oscillation(loss_steps.detach().cpu().numpy(), i, k, loss_best.detach().cpu().numpy(), k3=self.thr_decr)
fl_reduce_no_impr = (~reduced_last_check) * (loss_best_last_check.cpu().numpy() >= loss_best.cpu().numpy())
fl_oscillation = ~(~fl_oscillation * ~fl_reduce_no_impr)
reduced_last_check = np.copy(fl_oscillation)
loss_best_last_check = loss_best.clone()
if np.sum(fl_oscillation) > 0:
step_size[u[fl_oscillation]] /= 2.0
n_reduced = fl_oscillation.astype(float).sum()
fl_oscillation = np.where(fl_oscillation)
x_adv[fl_oscillation] = x_best[fl_oscillation].clone()
grad[fl_oscillation] = grad_best[fl_oscillation].clone()
counter3 = 0
k = np.maximum(k - self.size_decr, self.steps_min)
return x_best, acc, loss_best, x_best_adv
def perturb(self, x_in, y_in, best_loss=False, cheap=True):
assert self.norm in ['Linf', 'L2']
x = x_in.clone() if len(x_in.shape) == 4 else x_in.clone().unsqueeze(0)
y = y_in.clone() if len(y_in.shape) == 1 else y_in.clone().unsqueeze(0)
adv = x.clone()
acc = self.model(x)[0].max(1)[1] == y
loss = -1e10 * torch.ones_like(acc).float()
if self.verbose:
print('-------------------------- running {}-attack with epsilon {:.4f} --------------------------'.format(self.norm, self.eps))
print('initial accuracy: {:.2%}'.format(acc.float().mean()))
startt = time.time()
if not best_loss:
torch.random.manual_seed(self.seed)
torch.cuda.random.manual_seed(self.seed)
if not cheap:
raise ValueError('not implemented yet')
else:
for counter in range(self.n_restarts):
ind_to_fool = acc.nonzero().squeeze()
if len(ind_to_fool.shape) == 0: ind_to_fool = ind_to_fool.unsqueeze(0)
if ind_to_fool.numel() != 0:
x_to_fool, y_to_fool = x[ind_to_fool].clone(), y[ind_to_fool].clone()
best_curr, acc_curr, loss_curr, adv_curr = self.attack_single_run(x_to_fool, y_to_fool)
ind_curr = (acc_curr == 0).nonzero().squeeze()
#
acc[ind_to_fool[ind_curr]] = 0
adv[ind_to_fool[ind_curr]] = adv_curr[ind_curr].clone()
if self.verbose:
print('restart {} - robust accuracy: {:.2%} - cum. time: {:.1f} s'.format(
counter, acc.float().mean(), time.time() - startt))
return acc, adv
else:
adv_best = x.detach().clone()
loss_best = torch.ones([x.shape[0]]).to(self.device) * (-float('inf'))
for counter in range(self.n_restarts):
best_curr, _, loss_curr, _ = self.attack_single_run(x, y)
ind_curr = (loss_curr > loss_best).nonzero().squeeze()
adv_best[ind_curr] = best_curr[ind_curr] + 0.
loss_best[ind_curr] = loss_curr[ind_curr] + 0.
if self.verbose:
print('restart {} - loss: {:.5f}'.format(counter, loss_best.sum()))