-
Notifications
You must be signed in to change notification settings - Fork 1
/
adversarial_attacks.py
769 lines (609 loc) · 31.7 KB
/
adversarial_attacks.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
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
'''
This class implements adversarial attacks
'''
# Imports
import torch
from torch.autograd import Variable
import torchvision as tv
import numpy as np
import matplotlib.pyplot as plt
from tqdm import tqdm
# Class
class Attacker:
def __init__(self, net,
data,
gpu = False):
"""This class will use data to generate attacks on network
Args:
net (pytorch model): Neural Network to be attacked
data (pytorch data loader): Data in a dataloader
gpu (bool, optional): Wheather or not to use GPU. Defaults to False.
"""
super(Attacker,self).__init__()
# Move inputs to CPU or GPU
self.gpu = gpu
if isinstance(self.gpu, bool):
self.net = net if self.gpu == False else net.cuda()
else:
self.net = net.to(self.gpu)
self.data = data
# Evaluation Tools
self.criterion = torch.nn.CrossEntropyLoss()
self.indv_criterion = torch.nn.CrossEntropyLoss(reduction = 'none')
self.soft_max = torch.nn.Softmax(dim = 1)
def normalize(self, input_tensor, p, dim):
"""Normalizes a batch of vectors along diminesion with L-p norms
Args:
input_tensor (Tensor): batch of vectors
p (int, np.inf or float('inf)): type of norm to use
dim (int): dimension of vectors
Returns:
Tensor: normalized batch of vectors
"""
# Orginal Size
dim1_size = input_tensor.size(1)
dim2_size = input_tensor.size(2)
# Find norm of vectors
norms = torch.linalg.norm(input_tensor, ord=p, dim=dim).view(-1, 1, 1)
# Divide all elements in vector by norm
return torch.bmm(1 / norms, input_tensor.view(-1, 1, max(dim1_size, dim2_size))).view(-1, dim1_size, dim2_size)
def get_FIM(self, images, labels):
"""Calculate the Fisher Information Matrix for all images
Args:
images : Images to be used
labels : Correct labels of images
Returns:
FIM, Loss for each Image, Predicted Class for each image
"""
# Push to gpu
if isinstance(self.gpu, bool):
images = Variable(images, requires_grad = True) if self.gpu == False else Variable(images.cuda(), requires_grad = True)
labels = labels if self.gpu == False else labels.cuda()
else:
images = Variable(images.to(self.gpu), requires_grad = True)
labels = labels.to(self.gpu)
# Make images require gradients
images.requires_grad_(True)
#Forward pass
outputs = self.net(images)
soft_max_output = self.soft_max(outputs)
losses = self.indv_criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
# Find size parameters
batch_size = outputs.size(0)
num_classes = outputs.size(1)
# Calculate FIMs
fisher = 0
for i in range(num_classes):
# Clear Gradients
self.net.zero_grad()
images.grad = None
# Cycle through lables (y)
temp_labels = torch.tensor([i]).repeat(batch_size)
if isinstance(self.gpu, bool):
temp_labels= temp_labels if self.gpu == False else temp_labels.cuda()
else:
temp_labels= temp_labels.to(self.gpu)
# Calculate losses
temp_loss = self.criterion(outputs, temp_labels)
temp_loss.backward(retain_graph = True)
# Calculate expectation
p = soft_max_output[:,i].view(batch_size, 1, 1, 1)
grad = images.grad.data.view(batch_size, images.size(2)* images.size(2), 1)
fisher += p * torch.bmm(grad, torch.transpose(grad, 1, 2)).view(batch_size, 1, images.size(2)* images.size(2), images.size(2)* images.size(2))
return fisher, losses, predicted
def get_eigensystem(self, tensor, max_only = False):
"""Given a tensor find the eigensystem
Args:
tensor (Tensor): A tensor object
max_only (bool, optional): Wheater to just return the maximum or all. Defaults to False.
Returns:
Eigenvalues, Eigenvectors
"""
# Find eigen system
tensor = tensor.cpu()
eig_values, eig_vectors = torch.symeig(tensor, eigenvectors = True, upper = True)
if isinstance(self.gpu, bool):
if self.gpu:
eig_values, eig_vectors = eig_values.cuda(), eig_vectors.cuda()
else:
eig_values = eig_values.to(self.gpu)
eig_vectors = eig_vectors.to(self.gpu)
if max_only == True:
eig_val_max = eig_values[:, :, -1]
eig_vec_max = eig_vectors[:, :, :, -1]
return eig_val_max, eig_vec_max
else:
return eig_values, eig_vectors
def get_attack_accuracy(self, attack = "OSSA",
epsilons = [1],
transfer_network = None,
return_attacks_only = False,
return_perturbations_only = False,
attack_images = None,
attack_labels = None,
prog_bar = True):
# Push transfer_network to GPU
if isinstance(self.gpu, bool):
if self.gpu and transfer_network is not None:
transfer_network = transfer_network.cuda()
else:
if transfer_network is not None:
transfer_network = transfer_network.to(self.gpu)
# Load CW
if attack == "CW":
from pytorch_cw2.cw import L2Adversary
if isinstance(self.gpu, bool):
if self.gpu:
device_num = "cuda:0"
device_type = "gpu"
else:
device_num = "cpu"
device_type = "cpu"
else:
device_num = "cuda:" + str(self.gpu)
device_type = "gpu"
self.cw_attack = L2Adversary(targeted=False, confidence=0.0, c_range=(1e-3, 1e10),
search_steps=5, max_steps=1000, abort_early=True,
box=(self.data.test_pixel_min, self.data.test_pixel_max),
optimizer_lr=1e-2, device_num=device_num)
elif attack == "CW2":
# Imports
import sys
sys.path.insert(1, '../adversarial-robustness-toolbox/')
from art.estimators.classification import PyTorchClassifier
from art.attacks.evasion import CarliniL2Method
if isinstance(self.gpu, bool):
if self.gpu:
device_num = "cuda:0"
device_type = "gpu"
else:
device_num = "cpu"
device_type = "cpu"
else:
device_num = "cuda:" + str(self.gpu)
device_type = "gpu"
classifier = PyTorchClassifier( model = self.net,
nb_classes = self.data.num_classes,
loss = self.criterion,
# clip_values = (float(self.data.test_pixel_min), float(self.data.test_pixel_max)),
input_shape = (self.data.num_channels, self.data.image_size, self.data.image_size),
device_type = device_type,
device_num = device_num)
# Hyperparameters from "Towards Deep Learning Models Resistant to Adversarial Attacks"
if self.data.set_name == "MNIST":
max_iter = 25
elif self.data.set_name == "CIFAR10":
max_iter = 20
else:
print("Eneter a valid data_set name for PGD")
exit()
attack_cw2 = CarliniL2Method(classifier=classifier,
max_iter = max_iter,
batch_size = self.data.test_batch_size,
verbose = False)
# Load PGD
elif attack == "PGD":
# Imports
import sys
sys.path.insert(1, '../adversarial-robustness-toolbox/')
from art.estimators.classification import PyTorchClassifier
from art.attacks.evasion import ProjectedGradientDescent
if isinstance(self.gpu, bool):
if self.gpu:
device_num = "cuda:0"
device_type = "gpu"
else:
device_num = "cpu"
device_type = "cpu"
else:
device_num = "cuda:" + str(self.gpu)
device_type = "gpu"
classifier = PyTorchClassifier( model = self.net,
nb_classes = self.data.num_classes,
loss = self.criterion,
clip_values = (float(self.data.test_pixel_min), float(self.data.test_pixel_max)),
input_shape = (self.data.num_channels, self.data.image_size, self.data.image_size),
device_type = device_type,
device_num = device_num)
# Hyperparameters from "Towards Deep Learning Models Resistant to Adversarial Attacks"
if self.data.set_name == "MNIST":
norm = "inf"
eps = 0.3
max_iter = 40
eps_step = 0.01
elif self.data.set_name == "CIFAR10":
norm = "inf"
eps = 0.3 * 1.6
max_iter = 40
eps_step = 0.01 * 1.6
else:
print("Eneter a valid data_set name for PGD")
exit()
attack_pgd = ProjectedGradientDescent( estimator = classifier,
norm = norm,
eps = eps,
max_iter = max_iter,
eps_step = eps_step,
batch_size = self.data.test_batch_size,
targeted = True,
verbose = False)
# Load EOT
elif attack == "EOT":
# Imports
import sys
sys.path.insert(1, '../adversarial-robustness-toolbox/')
from models.classes.EoT_Unitary import UniEoT
from art.estimators.classification import PyTorchClassifier
from art.attacks.evasion import ProjectedGradientDescent
eot_unitary_rotation = UniEoT(data = self.data,
gpu = self.gpu,
model_name = self.net.model_name,
nb_samples = int(1e2),
clip_values = (float(self.data.test_pixel_min), float(self.data.test_pixel_max)),
apply_predict = True)
if isinstance(self.gpu, bool):
device_type = "gpu" if self.gpu else "Cpu"
else:
device_type = "gpu"
classifier = PyTorchClassifier(model=self.net,
nb_classes=10,
loss=self.criterion,
preprocessing_defences=[eot_unitary_rotation],
# clip_values=(float(self.data.test_pixel_min), float(self.data.test_pixel_max)),
input_shape=(3, 32, 32),
device_type=device_type)
# Hyperparameters from "Towards Deep Learning Models Resistant to Adversarial Attacks"
if self.data.set_name == "MNIST":
norm = "inf"
eps = 0.3
max_iter = 40
eps_step = 0.01
elif self.data.set_name == "CIFAR10":
norm = "inf"
eps = 0.3 * 1.6
max_iter = 40
eps_step = 0.01 * 1.6
else:
print("Eneter a valid data_set name for PGD")
exit()
attack_eot = ProjectedGradientDescent(estimator=classifier,
norm = norm,
eps = eps, # Max perturbation Size
max_iter = max_iter,
eps_step = eps_step, # Step size for PGD,
batch_size = self.data.test_batch_size,
targeted=True,
verbose = False)
# Test images in test loader
attack_accuracies = np.zeros(len(epsilons))
for inputs, labels in tqdm (self.data.test_loader, desc="Batches Done...", disable=not prog_bar):
# Optionally use custom images
if attack_images is not None:
inputs = attack_images
labels = attack_labels
# Get Batch Size
batch_size = np.shape(inputs)[0]
# Push to gpu
if isinstance(self.gpu, bool):
if self.gpu:
inputs, labels = inputs.cuda(), labels.cuda()
else:
inputs, labels = inputs.to(self.gpu), labels.to(self.gpu)
if attack == "OSSA":
# Highest Eigenvalue and vector
eig_vec_max, losses = self.get_max_eigenpair(inputs, labels)
normed_attacks = self.normalize(eig_vec_max, p = None, dim = 2)
elif attack == "Gaussian_Noise":
# Get losses
outputs = self.net(inputs)
losses = self.indv_criterion(outputs, labels)
# Generate attack
normed_attacks = self.normalize(torch.rand_like(inputs.view(inputs.size(0), 1, -1)), p = None, dim = 2)
elif attack == "FGSM":
# Calculate Gradients
gradients, batch_size, losses, predicted = self.get_gradients(inputs, labels)
normed_attacks = self.normalize(torch.sign(gradients), p = None, dim = 2)
elif attack == "PGD":
# Get random targets
import random
targets = torch.empty_like(labels)
for i, label in enumerate(labels):
possible_targets = list(range(10))
del possible_targets[label]
targets[i] = random.choice(possible_targets)
# Generate adversarial examples
attacks = attack_pgd.generate(x=inputs.detach().cpu().numpy(),
y=targets.detach().cpu().numpy())
attacks = torch.from_numpy(attacks)
if isinstance(self.gpu, bool):
if self.gpu:
attacks = attacks.cuda()
else:
attacks = attacks.to(self.gpu)
# Get losses
outputs = self.net(inputs)
losses = self.indv_criterion(outputs, labels)
# Reduce the attacks to only the perturbations
attacks = attacks - inputs
# Norm the attack
normed_attacks = self.normalize(attacks.view(batch_size, 1, -1), p = None, dim = 2)
elif attack == "EOT":
# Get random targets
import random
targets = torch.empty_like(labels)
for i, label in enumerate(labels):
possible_targets = list(range(10))
del possible_targets[label]
targets[i] = random.choice(possible_targets)
# Generate adversarial examples
# print("Generating Attacks")
attacks = attack_eot.generate(x=inputs.detach().cpu().numpy(),
y=targets.detach().cpu().numpy())
attacks = torch.from_numpy(attacks)
if isinstance(self.gpu, bool):
if self.gpu:
attacks = attacks.cuda()
else:
attacks = attacks.to(self.gpu)
# Get losses
outputs = self.net(inputs)
losses = self.indv_criterion(outputs, labels)
# Reduce the attacks to only the perturbations
attacks = attacks - inputs
# Norm the attack
normed_attacks = self.normalize(attacks.view(batch_size, 1, -1), p = None, dim = 2)
elif attack == "CW2":
# Generate adversarial examples
attacks = attack_cw2.generate(x=inputs.detach().cpu().numpy())
attacks = torch.from_numpy(attacks)
if isinstance(self.gpu, bool):
if self.gpu:
attacks = attacks.cuda()
else:
attacks = attacks.to(self.gpu)
# Get losses
outputs = self.net(inputs)
losses = self.indv_criterion(outputs, labels)
# Reduce the attacks to only the perturbations
attacks = attacks - inputs
# Norm the attack
normed_attacks = self.normalize(attacks.view(batch_size, 1, -1), p = None, dim = 2)
elif attack == "CW":
# Use other labs code to produce full attack images
import torch.distributed as dist
attacks = self.cw_attack(self.net.to(self.gpu), inputs.to(self.gpu), labels.to(self.gpu), to_numpy=False)
dist.barrier()
if isinstance(self.gpu, bool):
attacks = attacks.cuda() if self.gpu else attacks
else:
attacks = attacks.to(self.gpu)
# Get losses
outputs = self.net(inputs)
losses = self.indv_criterion(outputs, labels)
# Reduce the attacks to only the perturbations
attacks = attacks - inputs
# Norm the attack
normed_attacks = self.normalize(attacks.view(batch_size, 1, -1), p = None, dim = 2)
else:
print("Invalid Attack Type")
exit()
# Return just perturbation
if return_perturbations_only:
return normed_attacks
# Cycle over all espiplons
for i in range(len(epsilons)):
# Set the unit norm of the highest eigenvector to epsilon
input_norms = torch.linalg.norm(inputs.view(batch_size, 1, -1), ord=None, dim=2).view(-1, 1, 1)
perturbations = float(epsilons[i]) * input_norms * normed_attacks
# perturbations = float(epsilons[i]) * normed_attacks
# Declare attacks as the perturbation added to the image
attacks = (inputs.view(batch_size, 1, -1) + perturbations).view(batch_size, self.data.num_channels, self.data.image_size, self.data.image_size)
# Check if loss has increased
adv_outputs = self.net(attacks)
adv_losses = self.indv_criterion(adv_outputs, labels)
# If losses has not increased flip direction
signs = (losses < adv_losses).type(torch.float)
signs[signs == 0] = -1
perturbations = signs.view(-1, 1, 1) * perturbations
# Compute attack and models prediction of it
attacks = (inputs.view(batch_size, 1, -1) + perturbations).view(batch_size, self.data.num_channels, self.data.image_size, self.data.image_size)
# Return Only Attacks
if return_attacks_only:
return attacks
# Calculate Adverstial output
adv_outputs = self.net(attacks) if transfer_network is None else transfer_network(attacks)
_, adv_predicted = torch.max(adv_outputs.data, 1)
# Save Attack Accuracy
attack_accuracies[i] = torch.sum(adv_predicted == labels).item() + attack_accuracies[i]
# Divide by total
attack_accuracies = attack_accuracies / (len(self.data.test_loader.dataset))
return attack_accuracies
def get_max_eigenpair(self, images, labels, max_iter = int(1e2)):
"""Use Lanczos Algorthmn to generate eigenvector associated with the highest eigenvalue
Args:
tensor (Tensor): matrix with which eigenvector is desired from
"""
# Declare Similarity Metric
cos_sim = torch.nn.CosineSimilarity()
# Make images require gradients
images.requires_grad_(True)
#Forward pass
outputs = self.net(images)
soft_max_output = self.soft_max(outputs)
losses = self.indv_criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
# Find size parameters
batch_size = images.size(0)
channel_num = images.size(1)
image_size = images.size(2)
num_classes = outputs.size(1)
# Iterate until convergence
# print("Begin Lancoz")
for j in range(max_iter):
# Initilize Eigenvector
eigenvector0 = torch.rand(batch_size, channel_num * image_size**2, 1)
eigenvector = torch.zeros(batch_size, channel_num * image_size**2, 1)
# Push to gpus
if isinstance(self.gpu, bool):
if self.gpu:
eigenvector0 = eigenvector0.cuda()
eigenvector = eigenvector.cuda()
else:
eigenvector0 = eigenvector0.to(self.gpu)
eigenvector = eigenvector.to(self.gpu)
# Normalize eigenvector
norms = torch.linalg.norm(eigenvector0, ord=2, dim=1).view(-1, 1, 1)
eigenvector0 = torch.bmm(1 / norms, eigenvector0.view(batch_size, 1, -1)).view(-1, channel_num * image_size**2, 1)
# If it does not converge in max_iter tries try again with new random vector
for k in range(max_iter):
# Calculate expectation
for i in range(num_classes):
# Clear Gradients
self.net.zero_grad()
images.grad = None
# Cycle through lables (y)
temp_labels = torch.tensor([i]).repeat(batch_size)
if isinstance(self.gpu, bool):
temp_labels = temp_labels if self.gpu == False else temp_labels.cuda()
else:
temp_labels = temp_labels.to(self.gpu)
# Calculate losses
temp_loss = self.criterion(outputs, temp_labels)
temp_loss.backward(retain_graph = True)
# Accumulate expectation
p = soft_max_output[:,i].view(batch_size, 1, 1)
grad = images.grad.data.view(batch_size, channel_num * (image_size**2), 1)
# p * (gT * eta) * g
eigenvector += p * (torch.bmm(torch.transpose(grad, 1, 2), eigenvector0) * grad)
# Normalize
norms = torch.linalg.norm(eigenvector, ord=2, dim=1).view(-1, 1, 1)
eigenvector = torch.bmm(1 / norms, eigenvector.view(batch_size, 1, -1)).view(-1, channel_num * image_size**2, 1)
# Check Convegence
similarity = torch.mean(cos_sim(eigenvector0.view(-1, 1),
eigenvector.view(-1, 1))).item()
# print("Iteration: ", j*max_iter + k ,"\tSimilarity: ", similarity)
if similarity > 0.94:
# Return vector
return eigenvector.view(batch_size, 1, -1), losses
else:
# Restart Cycle
eigenvector0 = eigenvector
eigenvector = torch.zeros(batch_size, channel_num * image_size**2, 1)
if isinstance(self.gpu, bool):
if self.gpu:
eigenvector = eigenvector.cuda()
else:
eigenvector = eigenvector.to(self.gpu)
print("Lanczos did not converge, final similarity is", similarity)
exit()
def get_gradients(self, images, labels):
"""Calculate the gradients of an image
Args:
images: Images to be tested
labels: Correct lables of images
Returns:
gradients, batch_size, num_classes, losses, predicted
"""
# Push to gpu
if isinstance(self.gpu, bool):
images = Variable(images, requires_grad = True) if self.gpu == False else Variable(images.cuda(), requires_grad = True)
labels = labels if self.gpu == False else labels.cuda()
else:
images = Variable(images.to(self.gpu), requires_grad = True)
labels = labels.to(self.gpu)
# Make images require gradients
images.requires_grad_(True)
# Clear Gradients
self.net.zero_grad()
images.grad = None
#Forward pass
outputs = self.net(images)
loss = self.criterion(outputs, labels)
losses = self.indv_criterion(outputs, labels)
_, predicted = torch.max(outputs.data, 1)
# Find size parameters
batch_size = outputs.size(0)
# Find gradients
loss.cpu().backward(retain_graph = True)
gradients = images.grad.data.view(batch_size, 1, -1)
return gradients, batch_size, losses, predicted
def get_fool_ratio(self, test_acc, attack_accs):
"""Calculate the fooling ratio of attacks
Args:
test_acc (float): orginal network accuracy
attack_accs (list of floats): list of accuracies after an attack
Returns:
list of floats: list of fooling ratios
"""
return [round(100*((test_acc - attack_acc) / test_acc), 2) for attack_acc in attack_accs]
def check_attack_perception(self, attack, epsilons = [1], save_only = False):
# Initalize images and labels for one of each number
images = torch.zeros((self.data.num_classes, self.data.num_channels, self.data.image_size, self.data.image_size))
labels = torch.zeros((self.data.num_classes)).type(torch.LongTensor)
if self.data.set_name == "MNIST":
label_names = list(range(10))
else:
label_names = ["Plane", "Car", "Bird", "Cat", "Deer", "Dog", "Frog", "Horse", "Ship", "Truck"]
# Find one of each number
found = False
number = 0
index = 0
while found is not True:
for test_inputs, test_labels in self.data.test_loader:
image, label, = test_inputs[0], test_labels[0]
if label.item() == number:
images[number, :, :, :] = image
labels[number] = label
number += 1
if number > 9:
found = True
index += 1
fig, axes2d = plt.subplots(nrows=len(epsilons),
ncols=10,
sharex=True, sharey=True)
# plt.suptitle(attack + " on " + self.data.set_name, fontsize=20)
# fig.text(0.03, 0.5, 'Noise to Signal L2 Norm Ratio', va='center', ha='center', rotation='vertical', fontsize=18)
for i, row in enumerate(tqdm(axes2d, desc="Epsilons Done...")):
# Push to gpus
if isinstance(self.gpu, bool):
if self.gpu:
images = images.cuda()
labels = labels.cuda()
else:
images = images.to(self.gpu)
labels = labels.to(self.gpu)
attacks = self.get_attack_accuracy(attack = attack,
attack_images = images,
attack_labels = labels,
epsilons = [epsilons[i]],
return_attacks_only = True,
prog_bar = False)
# UNnormalize
attacks = attacks.view(attacks.size(0), attacks.size(1), -1)
batch_means = torch.tensor(self.data.mean).repeat(attacks.size(0), 1).view(attacks.size(0), attacks.size(1), 1)
batch_stds = torch.tensor(self.data.std).repeat(attacks.size(0), 1).view(attacks.size(0), attacks.size(1), 1)
if isinstance(self.gpu, bool):
if self.gpu:
batch_means = batch_means.cuda()
batch_stds = batch_stds.cuda()
else:
batch_means = batch_means.to(self.gpu)
batch_stds = batch_stds.to(self.gpu)
attacks = attacks.mul_(batch_stds).add_(batch_means)
attacks = attacks.sub_(torch.min(attacks)).div_(torch.max(attacks) - torch.min(attacks)).view(attacks.size(0), attacks.size(1), self.data.image_size, self.data.image_size)
for j, cell in enumerate(row):
# Plot in cell
img = tv.utils.make_grid(attacks[j,:,:,:])
cell.imshow(np.transpose(img.detach().cpu().numpy(), (1, 2, 0)))
cell.set_xticks([])
cell.set_yticks([])
if i == 0:
cell.set_title(label_names[j])
if j == 0:
cell.set_ylabel(epsilons[i])
fig.subplots_adjust(hspace = -0.4, wspace=0)
if save_only:
plt.savefig('results/' + self.data.set_name + "/attacks/" + attack + '_attacks.png')
else:
plt.show()