-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathadv_gen.py
51 lines (42 loc) · 1.49 KB
/
adv_gen.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
import torch
import numpy as np
import torch.nn as nn
def fgsm_attack(model, x, y, T=1, epsilon=None, start=0.001, gradient=False):
# freeze parameters for fast forward
model.eval()
for param in model.parameters():
param.requires_grad = False
x = x.detach()
if epsilon is None:
epsilon = np.random.choice(np.linspace(start, 0.08, num=20), size=1)[0]
x.requires_grad_(True)
pre = model(x)
loss = nn.CrossEntropyLoss()(pre / T, y)
model.zero_grad() # empty grad
loss.backward()
x_adv = x.data + epsilon * x.grad.data.sign() # gradient ascend
x_adv = torch.clamp(x_adv, 0, 1)
if not gradient:
return x_adv
else:
return x_adv, x.grad.data.sign()
def target_fgsm_attack(model, x, T=1, num_classes=20, epsilon=None, start=0.001, gradient=False):
# freeze parameters for fast forward
model.eval()
for param in model.parameters():
param.requires_grad = False
x = x.detach() # batch, 3, w, h
y = torch.randint(0, num_classes, size=(x.shape[0],)).to(x.device)
if epsilon is None:
epsilon = np.random.choice(np.linspace(start, 0.08, num=20), size=1)[0]
x.requires_grad_(True)
pre = model(x)
loss = nn.CrossEntropyLoss()(pre / T, y)
model.zero_grad() # empty grad
loss.backward()
x_adv = x.data - epsilon * x.grad.data.sign() # gradient descent
x_adv = torch.clamp(x_adv, 0, 1)
if not gradient:
return x_adv
else:
return x_adv, x.grad.data.sign()