-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattack.py
84 lines (63 loc) · 2.79 KB
/
attack.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
import torch
class FGSM(object):
def __init__(self, model, epsilon, loss_fn, clip_min, clip_max):
super().__init__()
self.model = model
self.epsilon = epsilon
self.clip_min = clip_min
self.clip_max = clip_max
self.loss_fn = loss_fn
def attack(self, images, targets, targeted):
self.model.eval()
images_ = images.clone().detach()
images_.requires_grad_()
logits = self.model(images_)
self.model.zero_grad()
loss = self.loss_fn(logits, targets, reduction='sum')
loss.backward()
if targeted:
perturbed_images = images_ - self.epsilon * images_.grad.sign()
else:
perturbed_images = images_ + self.epsilon * images_.grad.sign()
if (self.clip_min is not None) or (self.clip_max is not None):
perturbed_images.clamp_(min=self.clip_min, max=self.clip_max)
return perturbed_images.detach()
class PGD(object):
def __init__(self, model, epsilon, loss_fn, clip_min, clip_max):
super().__init__()
self.model = model
self.epsilon = epsilon
self.clip_min = clip_min
self.clip_max = clip_max
self.loss_fn = loss_fn
self.device = torch.device(
'cuda:0' if torch.cuda.is_available() else 'cpu')
def attack(self, alpha, inputs, iterations, targets, targeted, num_restarts, random_start):
self.model.eval()
inputs_ = inputs.clone().detach()
inputs_min, inputs_max = inputs_ - self.epsilon, inputs_ + self.epsilon
fgsm = FGSM(self.model, alpha, self.loss_fn,
self.clip_min, self.clip_max)
if not random_start:
num_restarts = 1
for i in range(num_restarts):
if random_start:
inputs_ = inputs.clone().detach() + torch.mul(
self.epsilon,
torch.rand_like(inputs, device=self.device).uniform_(-1, 1)
)
for _ in range(iterations):
inputs_ = fgsm.attack(inputs_, targets, targeted)
# project onto epsilon-ball around original inputs
inputs_ = torch.max(inputs_min, inputs_)
inputs_ = torch.min(inputs_max, inputs_)
return inputs_.detach()
class SegmentPDG(PGD):
def __init__(self, model, epsilon, loss_fn, clip_min, clip_max, idx):
super().__init__(model, epsilon, loss_fn, clip_min, clip_max)
self.idx = idx
self.eps = epsilon
def attack(self, alpha, inputs, iterations, targets, targeted, num_restarts, random_start):
self.epsilon = torch.zeros_like(inputs[0:1])
self.epsilon[:, self.idx] = self.eps
return super().attack(alpha, inputs, iterations, targets, targeted, num_restarts, random_start)