-
Notifications
You must be signed in to change notification settings - Fork 0
/
pgd.py
142 lines (135 loc) · 6.03 KB
/
pgd.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
from utils import *
import torch.nn.functional as F
import numpy as np
def attack_pgd(model, X, y, epsilon, alpha, attack_iters, restarts, lower_limit, upper_limit, opt=None):
max_loss = torch.zeros(y.shape[0]).cuda()
max_delta = torch.zeros_like(X).cuda()
for zz in range(restarts):
delta = torch.zeros_like(X).cuda()
for i in range(len(epsilon)):
delta[:, i, :, :].uniform_(-epsilon[i][0][0].item(), epsilon[i][0][0].item())
delta.data = clamp(delta, lower_limit - X, upper_limit - X)
delta.requires_grad = True
for _ in range(attack_iters):
output = model(X + delta)
index = torch.where(output.max(1)[1] == y)
if len(index[0]) == 0:
break
loss = F.cross_entropy(output, y)
loss.backward()
grad = delta.grad.detach()
d = delta[index[0], :, :, :]
g = grad[index[0], :, :, :]
d = clamp(d + alpha * torch.sign(g), -epsilon, epsilon)
d = clamp(d, lower_limit - X[index[0], :, :, :], upper_limit - X[index[0], :, :, :])
delta.data[index[0], :, :, :] = d
delta.grad.zero_()
all_loss = F.cross_entropy(model(X+delta), y, reduction='none').detach()
max_delta[all_loss >= max_loss] = delta.detach()[all_loss >= max_loss]
max_loss = torch.max(max_loss, all_loss)
return max_delta
def attack_cw(model, X, y, epsilon, alpha, attack_iters, restarts, lower_limit, upper_limit, opt=None):
max_loss = torch.zeros(y.shape[0]).cuda()
max_delta = torch.zeros_like(X).cuda()
for zz in range(restarts):
delta = torch.zeros_like(X).cuda()
for i in range(len(epsilon)):
delta[:, i, :, :].uniform_(-epsilon[i][0][0].item(), epsilon[i][0][0].item())
delta.data = clamp(delta, lower_limit - X, upper_limit - X)
delta.requires_grad = True
for _ in range(attack_iters):
output = model(X + delta)
index = torch.where(output.max(1)[1] == y)
if len(index[0]) == 0:
break
loss = CW_loss(output, y)
loss.backward()
grad = delta.grad.detach()
d = delta[index[0], :, :, :]
g = grad[index[0], :, :, :]
d = clamp(d + alpha * torch.sign(g), -epsilon, epsilon)
d = clamp(d, lower_limit - X[index[0], :, :, :], upper_limit - X[index[0], :, :, :])
delta.data[index[0], :, :, :] = d
delta.grad.zero_()
all_loss = CW_loss(model(X+delta), y, reduction=False).detach()
max_delta[all_loss >= max_loss] = delta.detach()[all_loss >= max_loss]
max_loss = torch.max(max_loss, all_loss)
return max_delta
def evaluate_pgd(args, model, test_loader, eval_steps=None):
attack_iters = args.eval_iters # 50
restarts = args.eval_restarts # 10
pgd_loss = 0
pgd_acc = 0
n = 0
model.eval()
print('Evaluating with PGD {} steps and {} restarts'.format(attack_iters, restarts))
if args.dataset=="cifar":
mu = torch.tensor(cifar10_mean).view(3,1,1).cuda()
std = torch.tensor(cifar10_std).view(3,1,1).cuda()
if args.dataset=="imagenette" or args.dataset=="imagenet" :
mu = torch.tensor(imagenet_mean).view(3,1,1).cuda()
std = torch.tensor(imagenet_std).view(3,1,1).cuda()
upper_limit = ((1 - mu)/ std)
lower_limit = ((0 - mu)/ std)
epsilon = (args.epsilon / 255.) / std
alpha = (args.alpha / 255.) / std
for step, (X, y) in enumerate(test_loader):
X, y = X.cuda(), y.cuda()
pgd_delta = attack_pgd(model, X, y, epsilon, alpha, attack_iters, restarts, lower_limit, upper_limit)
with torch.no_grad():
output = model(X + pgd_delta)
loss = F.cross_entropy(output, y)
pgd_loss += loss.item() * y.size(0)
pgd_acc += (output.max(1)[1] == y).sum().item()
n += y.size(0)
if step + 1 == eval_steps:
break
if (step + 1) % 10 == 0 or step + 1 == len(test_loader):
print('{}/{}'.format(step+1, len(test_loader)),
pgd_loss/n, pgd_acc/n)
return pgd_loss/n, pgd_acc/n
def evaluate_CW(args, model, test_loader, eval_steps=None):
attack_iters = args.eval_iters # 50
restarts = args.eval_restarts # 10
cw_loss = 0
cw_acc = 0
n = 0
model.eval()
print('Evaluating with CW {} steps and {} restarts'.format(attack_iters, restarts))
if args.dataset=="cifar":
mu = torch.tensor(cifar10_mean).view(3,1,1).cuda()
std = torch.tensor(cifar10_std).view(3,1,1).cuda()
if args.dataset=="imagenette" or args.dataset=="imagenet":
mu = torch.tensor(imagenet_mean).view(3,1,1).cuda()
std = torch.tensor(imagenet_std).view(3,1,1).cuda()
upper_limit = ((1 - mu)/ std)
lower_limit = ((0 - mu)/ std)
epsilon = (args.epsilon / 255.) / std
alpha = (args.alpha / 255.) / std
for step, (X, y) in enumerate(test_loader):
X, y = X.cuda(), y.cuda()
pgd_delta = attack_cw(model, X, y, epsilon, alpha, attack_iters, restarts, lower_limit, upper_limit)
with torch.no_grad():
output = model(X + pgd_delta)
loss = CW_loss(output, y)
cw_loss += loss.item() * y.size(0)
cw_acc += (output.max(1)[1] == y).sum().item()
n += y.size(0)
if step + 1 == eval_steps:
break
if (step + 1) % 10 == 0 or step + 1 == len(test_loader):
print('{}/{}'.format(step+1, len(test_loader)),
cw_loss/n, cw_acc/n)
return cw_loss/n, cw_acc/n
def CW_loss(x, y, reduction=True, num_cls=10, threshold=10,):
batch_size = x.shape[0]
x_sorted, ind_sorted = x.sort(dim=1)
ind = (ind_sorted[:, -1] == y).float()
logit_mc = x_sorted[:, -2] * ind + x_sorted[:, -1] * (1. - ind)
logit_gt = x[np.arange(batch_size), y]
loss_value_ori = -(logit_gt - logit_mc)
loss_value = torch.maximum(loss_value_ori, torch.tensor(-threshold).cuda())
if reduction:
return loss_value.mean()
else:
return loss_value