-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbpdaattack.py
More file actions
54 lines (41 loc) · 1.73 KB
/
bpdaattack.py
File metadata and controls
54 lines (41 loc) · 1.73 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
import torch
import torch.nn as nn
import numpy as np
class BPDAattack(object):
def __init__(self, model=None, defense=None, device=None, epsilon=None, learning_rate=0.5,
max_iterations=100, clip_min=0, clip_max=1):
self.model = model
self.epsilon = epsilon
self.loss_fn = nn.CrossEntropyLoss(reduction='sum')
self.defense = defense
self.clip_min = clip_min
self.clip_max = clip_max
self.LEARNING_RATE = learning_rate
self.MAX_ITERATIONS = max_iterations
self.device = device
def generate(self, x, y):
"""
Given examples (X_nat, y), returns their adversarial
counterparts with an attack length of epsilon.
"""
adv = x.detach().clone()
lower = np.clip(x.detach().cpu().numpy() - self.epsilon, self.clip_min, self.clip_max)
upper = np.clip(x.detach().cpu().numpy() + self.epsilon, self.clip_min, self.clip_max)
for i in range(self.MAX_ITERATIONS):
adv_purified = self.defense(adv.to(torch.float16))
adv_purified.requires_grad_()
adv_purified.retain_grad()
print(adv_purified.shape)
scores = self.model(adv_purified.to(torch.float16))
print(scores.shape)
loss = self.loss_fn(scores, y)
loss.backward()
grad_sign = adv_purified.grad.data.sign()
# early stop, only for batch_size = 1
# p = torch.argmax(F.softmax(scores), 1)
# if y != p:
# break
adv += self.LEARNING_RATE * grad_sign
adv_img = np.clip(adv.detach().cpu().numpy(), lower, upper)
adv = torch.Tensor(adv_img).to(self.device)
return adv