-
Notifications
You must be signed in to change notification settings - Fork 0
/
attacks.py
206 lines (175 loc) · 8.34 KB
/
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
from __future__ import print_function
import os
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from torch.autograd import Variable
import torch.optim as optim
from torchvision import datasets, transforms
import numpy as np
def torch_accuracy(output, target, topk=(1,)):
topn = max(topk)
batch_size = output.size(0)
_, pred = output.topk(topn, 1, True, True)
pred = pred.t()
if len(target.size()) == 1:
is_correct = pred.eq(target.view(1, -1).expand_as(pred))
elif len(target.size()) == 2:
is_correct = pred.eq(target.max(1)[1].expand_as(pred))
ans = []
for i in topk:
is_correct_i = is_correct[:i].view(-1).float().sum(0, keepdim=True)
ans.append(is_correct_i.mul_(100.0 / batch_size))
return ans
class AttackerPolymer:
def __init__(self, epsilon, num_steps, step_size, num_classes, device):
self.epsilon = epsilon
self.num_steps = num_steps
self.step_size = step_size
self.num_classes = num_classes
self.device = device
self.attacker_name = ['NAT', 'PGD_20', 'PGD_100', 'MIM', 'CW', 'AA']
def run_all(self, model, img, gt, return_acc=True):
adv_acc_dict = {}
adv_acc_dict['NAT'] = self.NAT(model, img, gt, return_acc=return_acc)
adv_acc_dict['PGD_20'] = self.PGD(model, img, gt, num_steps=20, category='Madry', return_acc=return_acc)
# adv_acc_dict['PGD_100'] = self.PGD(model, img, gt, num_steps=100, category='Madry', return_acc=return_acc)
# adv_acc_dict['MIM'] = self.MIM(model, img, gt, return_acc=return_acc)
# adv_acc_dict['CW'] = self.CW(model, img, gt, return_acc=return_acc)
# adv_acc_dict['APGD_ce'] = self.AA(model, img, gt, attacks_to_run=['apgd-ce'], return_acc=return_acc)
# adv_acc_dict['APGD_dlr'] = self.AA(model, img, gt, attacks_to_run=['apgd-dlr'], return_acc=return_acc)
# adv_acc_dict['APGD_t'] = self.AA(model, img, gt, attacks_to_run=['apgd-t'], return_acc=return_acc)
# adv_acc_dict['FAB_t'] = self.AA(model, img, gt, attacks_to_run=['fab-t'], return_acc=return_acc)
# adv_acc_dict['Square'] = self.AA(model, img, gt, attacks_to_run=['square'], return_acc=return_acc)
adv_acc_dict['AA'] = self.AA(model, img, gt, return_acc=return_acc)
return adv_acc_dict
def run_specified(self, name, model, img, gt, step_count=None, category='Madry', return_acc=False):
name = name.upper()
if 'PGD' in name:
num_steps = int(name.split('_')[-1])
return self.PGD(model, img, gt, num_steps=num_steps, category=category, step_count=step_count,
return_acc=return_acc)
elif name == 'MIM':
return self.MIM(model, img, gt, return_acc=return_acc)
elif name == 'CW':
return self.CW(model, img, gt, return_acc=return_acc)
elif name == 'AA':
return self.AA(model, img, gt, return_acc=return_acc)
elif name == 'NAT':
return self.NAT(model, img, gt, return_acc=return_acc)
else:
raise NotImplementedError
def NAT(self, model, img, gt, return_acc=False):
model.eval()
if return_acc:
pred = model(img)
acc = torch_accuracy(pred, gt, (1,))
return acc
return img
def PGD(self, model, img, gt, num_steps, category='Madry', rand_init=True, step_count=None, return_acc=False):
model.eval()
if category == "trades":
x_adv = img.detach() + 0.001 * torch.randn(img.shape).to(
self.device).detach() if rand_init else img.detach()
nat_output = model(img)
elif category == "Madry":
x_adv = img.detach() + torch.from_numpy(
np.random.uniform(-self.epsilon, self.epsilon, img.shape)).float().to(
self.device) if rand_init else img.detach()
x_adv = torch.clamp(x_adv, 0.0, 1.0)
else:
raise NotImplementedError
for k in range(self.num_steps):
x_adv.requires_grad_()
output = model(x_adv)
model.zero_grad()
with torch.enable_grad():
loss_adv = nn.CrossEntropyLoss()(output, gt)
loss_adv.backward()
if step_count is not None:
step_count += torch.eq(output.max(1)[1], gt).int()
eta = self.step_size * x_adv.grad.sign()
# Update adversarial img
x_adv = x_adv.detach() + eta
x_adv = torch.min(torch.max(x_adv, img - self.epsilon), img + self.epsilon)
x_adv = torch.clamp(x_adv, 0.0, 1.0)
x_adv = Variable(x_adv, requires_grad=False)
if return_acc:
adv_pred = model(x_adv)
adv_acc = torch_accuracy(adv_pred, gt, (1,))
return adv_acc
if step_count:
return x_adv, step_count
else:
return x_adv
def MIM(self, model, img, gt, decay_factor=1.0, return_acc=False):
model.eval()
x_adv = img.detach() + torch.from_numpy(np.random.uniform(-self.epsilon, self.epsilon, img.shape)).float().to(
self.device)
x_adv = torch.clamp(x_adv, 0.0, 1.0)
previous_grad = torch.zeros_like(img.data)
for k in range(self.num_steps):
x_adv.requires_grad_()
output = model(x_adv)
model.zero_grad()
with torch.enable_grad():
loss_adv = nn.CrossEntropyLoss()(output, gt)
loss_adv.backward()
grad = x_adv.grad.data / torch.mean(torch.abs(x_adv.grad.data), [1, 2, 3], keepdim=True)
previous_grad = decay_factor * previous_grad + grad
eta = self.step_size * previous_grad.sign()
# Update adversarial img
x_adv = x_adv.detach() + eta
x_adv = torch.min(torch.max(x_adv, img - self.epsilon), img + self.epsilon)
x_adv = torch.clamp(x_adv, 0.0, 1.0)
x_adv = Variable(x_adv, requires_grad=False)
if return_acc:
adv_pred = model(x_adv)
adv_acc = torch_accuracy(adv_pred, gt, (1,))
return adv_acc
return x_adv
def CW(self, model, img, gt, margin=50, return_acc=False):
model.eval()
x_adv = Variable(img.data, requires_grad=True)
random_noise = torch.FloatTensor(*x_adv.shape).uniform_(-self.epsilon, self.epsilon).to(self.device)
x_adv = Variable(x_adv.data + random_noise, requires_grad=True)
onehot_targets = torch.eye(self.num_classes)[gt].to(self.device)
for _ in range(self.num_steps):
opt = optim.SGD([x_adv], lr=1e-3)
opt.zero_grad()
with torch.enable_grad():
logits = model(x_adv)
self_loss = torch.sum(onehot_targets * logits, dim=1)
other_loss = torch.max((1 - onehot_targets) * logits - onehot_targets * 1000, dim=1)[0]
loss = -torch.sum(torch.clamp(self_loss - other_loss + margin, 0))
loss = loss / onehot_targets.shape[0]
loss.backward()
eta = self.step_size * x_adv.grad.data.sign()
x_adv = Variable(x_adv.data + eta, requires_grad=True)
eta = torch.clamp(x_adv.data - img.data, -self.epsilon, self.epsilon)
x_adv = Variable(img.data + eta, requires_grad=True)
x_adv = Variable(torch.clamp(x_adv, 0, 1.0), requires_grad=True)
if return_acc:
adv_pred = model(x_adv)
adv_acc = torch_accuracy(adv_pred, gt, (1,))
return adv_acc
return x_adv
def AA(self, model, img, gt, attacks_to_run=None, return_acc=False):
try:
from autoattack import AutoAttack
except:
os.system('pip3 install git+https://github.com/fra31/auto-attack')
from autoattack import AutoAttack
adversary = AutoAttack(model, norm='Linf', eps=self.epsilon, version='standard', verbose=False)
if attacks_to_run:
adversary.attacks_to_run = attacks_to_run
x_adv = adversary.run_standard_evaluation_individual(img, gt, bs=len(img))[attacks_to_run[0]]
else:
x_adv = adversary.run_standard_evaluation(img, gt, bs=len(img))
if return_acc:
adv_pred = model(x_adv)
adv_acc = torch_accuracy(adv_pred, gt, (1,))
return adv_acc
return x_adv