-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathattacks.py
326 lines (276 loc) · 12.5 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"""Functions and classes for adversarial training and for generating adversarial examples.
The way the attacks are instantiated is inspired by DeepMind's repository for adversarial robustness,
which is implemented in JAX, and can be found here:
https://github.com/deepmind/deepmind-research/tree/master/adversarial_robustness/.
The original license can be found here:
https://github.com/deepmind/deepmind-research/blob/master/LICENSE
"""
import functools
import math
from dataclasses import dataclass
from typing import Callable, Dict, List, Optional, Tuple, Union
import torch
import torch.nn.functional as F
from autoattack import AutoAttack
from timm.bits import DeviceEnv
from torch import nn
AttackFn = Callable[[nn.Module, torch.Tensor, torch.Tensor], torch.Tensor]
TrainAttackFn = Callable[[nn.Module, torch.Tensor, torch.Tensor, int], torch.Tensor]
Boundaries = Tuple[float, float]
ProjectFn = Callable[[torch.Tensor, torch.Tensor, float, Boundaries], torch.Tensor]
InitFn = Callable[[torch.Tensor, float, ProjectFn, Boundaries], torch.Tensor]
EpsSchedule = Callable[[int], float]
ScheduleMaker = Callable[[float, int, int], EpsSchedule]
Norm = str
def project_linf(x: torch.Tensor, x_adv: torch.Tensor, eps: float, boundaries: Boundaries) -> torch.Tensor:
clip_min, clip_max = boundaries
d_x = torch.clamp(x_adv - x.detach(), -eps, eps)
x_adv = torch.clamp(x + d_x, clip_min, clip_max)
return x_adv
def init_linf(x: torch.Tensor, eps: float, project_fn: ProjectFn, boundaries: Boundaries) -> torch.Tensor:
x_adv = x.detach() + torch.empty_like(x.detach(), device=x.device).uniform_(-eps, eps) + 1e-5
return project_fn(x, x_adv, eps, boundaries)
def init_l2(x: torch.Tensor, eps: float, project_fn: ProjectFn, boundaries: Boundaries) -> torch.Tensor:
x_adv = x.detach() + torch.empty_like(x.detach(), device=x.device).normal_(-eps, eps) + 1e-5
return project_fn(x, x_adv, eps, boundaries)
def project_l2(x: torch.Tensor, x_adv: torch.Tensor, eps: float, boundaries: Boundaries) -> torch.Tensor:
clip_min, clip_max = boundaries
d_x = x_adv - x.detach()
d_x_norm = d_x.renorm(p=2, dim=0, maxnorm=eps)
x_adv = torch.clamp(x + d_x_norm, clip_min, clip_max)
return x_adv
def pgd(model: nn.Module,
x: torch.Tensor,
y: torch.Tensor,
eps: float,
step_size: float,
steps: int,
boundaries: Tuple[float, float],
init_fn: InitFn,
project_fn: ProjectFn,
criterion: nn.Module,
targeted: bool = False,
num_classes: Optional[int] = None,
random_targets: bool = False,
logits_y: bool = False,
take_sign=True,
normalize=False,
dev_env: Optional[DeviceEnv] = None,
return_losses: bool = False) -> Union[torch.Tensor, Tuple[List[float], torch.Tensor]]:
losses = []
local_project_fn = functools.partial(project_fn, eps=eps, boundaries=boundaries)
x_adv = init_fn(x, eps, project_fn, boundaries)
if random_targets:
assert num_classes is not None
y = torch.randint_like(y, 0, num_classes, device=y.device)
if len(y.size()) > 1 and not logits_y:
y = y.argmax(dim=-1)
for _ in range(steps):
x_adv.requires_grad_()
loss = criterion(
F.log_softmax(model(x_adv), dim=-1),
y,
)
if return_losses:
losses.append(loss)
grad = torch.autograd.grad(loss, x_adv)[0]
# Differentiate between L2 and Linf, though this can be probably abstracted better
# Take sign for Linf
if take_sign:
d_x = torch.sign(grad)
# Or normalize for L2
elif normalize:
# from the robustness library
l = len(x.shape) - 1
grad_norm = torch.norm(grad.view(grad.shape[0], -1), dim=1).view(-1, *([1] * l))
d_x = grad / (grad_norm + 1e-10)
else:
d_x = grad
if targeted:
# Minimize the loss if the attack is targeted
x_adv = x_adv.detach() - step_size * d_x
else:
# Otherwise maximize
x_adv = x_adv.detach() + step_size * d_x
# Project into the allowed domain
x_adv = local_project_fn(x, x_adv)
if dev_env is not None:
# Mark step here to keep XLA program size small and speed-up compilation time
# It also seems to improve overall speed when `steps` > 1.
dev_env.mark_step()
if return_losses:
return x_adv, torch.as_tensor(losses)
return x_adv
_ATTACKS = {
"pgd": pgd,
"targeted_pgd": functools.partial(pgd, targeted=True, random_targets=True),
}
_INIT_PROJECT_FN: Dict[str, Tuple[InitFn, ProjectFn, bool, bool]] = {
"linf": (init_linf, project_linf, True, False),
"l2": (init_l2, project_l2, False, True)
}
def make_sine_schedule(final: float, warmup: int, zero_eps_epochs: int) -> Callable[[int], float]:
def sine_schedule(step: int) -> float:
if step < zero_eps_epochs:
return 0.0
if step < warmup:
return 0.5 * final * (1 + math.sin(math.pi * ((step - zero_eps_epochs) / warmup - 0.5)))
return final
return sine_schedule
def make_linear_schedule(final: float, warmup: int, zero_eps_epochs: int) -> Callable[[int], float]:
def linear_schedule(step: int) -> float:
if step < zero_eps_epochs:
return 0.0
if step < warmup:
return (step - zero_eps_epochs) / warmup * final
return final
return linear_schedule
_SCHEDULES: Dict[str, ScheduleMaker] = {
"linear": make_linear_schedule,
"sine": make_sine_schedule,
"constant": (lambda eps, _1, _2: (lambda _: eps))
}
def make_train_attack(attack_name: str, schedule: str, final_eps: float, period: int, zero_eps_epochs: int,
step_size: float, steps: int, norm: Norm, boundaries: Tuple[float, float],
criterion: nn.Module, num_classes: int, logits_y: bool, **kwargs) -> TrainAttackFn:
attack_fn = _ATTACKS[attack_name]
init_fn, project_fn, take_sign, normalize = _INIT_PROJECT_FN[norm]
schedule_fn = _SCHEDULES[schedule](final_eps, period, zero_eps_epochs)
def attack(model: nn.Module, x: torch.Tensor, y: torch.Tensor, step: int) -> torch.Tensor:
eps = schedule_fn(step)
return attack_fn(model,
x,
y,
eps,
step_size=step_size,
steps=steps,
boundaries=boundaries,
init_fn=init_fn,
project_fn=project_fn,
criterion=criterion,
num_classes=num_classes,
logits_y=logits_y,
take_sign=take_sign,
normalize=normalize,
**kwargs)
return attack
def make_attack(attack: str,
eps: float,
step_size: float,
steps: int,
norm: Norm,
boundaries: Tuple[float, float],
criterion: nn.Module,
device: Optional[torch.device] = None,
**attack_kwargs) -> AttackFn:
if attack not in {"autoattack", "apgd-ce"}:
attack_fn = _ATTACKS[attack]
init_fn, project_fn, take_sign, normalize = _INIT_PROJECT_FN[norm]
return functools.partial(attack_fn,
eps=eps,
step_size=step_size,
steps=steps,
boundaries=boundaries,
init_fn=init_fn,
project_fn=project_fn,
criterion=criterion,
take_sign=take_sign,
normalize=normalize,
**attack_kwargs)
if attack in {"apgd-ce"}:
attack_kwargs["version"] = "custom"
attack_kwargs["attacks_to_run"] = [attack]
if "dev_env" in attack_kwargs:
del attack_kwargs["dev_env"]
def autoattack_fn(model: nn.Module, x: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
assert isinstance(eps, float)
adversary = AutoAttack(model, norm.capitalize(), eps=eps, device=device, **attack_kwargs)
x_adv = adversary.run_standard_evaluation(x, y, bs=x.size(0))
return x_adv # type: ignore
return autoattack_fn
@dataclass
class AttackCfg:
name: str
eps: float
eps_schedule: str
eps_schedule_period: int
zero_eps_epochs: int
step_size: float
steps: int
norm: str
boundaries: Tuple[float, float]
class AdvTrainingLoss(nn.Module):
def __init__(self,
attack_cfg: AttackCfg,
natural_criterion: nn.Module,
dev_env: DeviceEnv,
num_classes: int,
eval_mode: bool = False):
super().__init__()
self.criterion = natural_criterion
self.attack = make_train_attack(attack_cfg.name,
attack_cfg.eps_schedule,
attack_cfg.eps,
attack_cfg.eps_schedule_period,
attack_cfg.zero_eps_epochs,
attack_cfg.step_size,
attack_cfg.steps,
attack_cfg.norm,
attack_cfg.boundaries,
criterion=nn.NLLLoss(reduction="sum"),
num_classes=num_classes,
logits_y=False,
dev_env=dev_env)
self.eval_mode = eval_mode
def forward(self, model: nn.Module, x: torch.Tensor, y: torch.Tensor,
epoch: int) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
if self.eval_mode:
model.eval()
x_adv = self.attack(model, x, y, epoch)
model.train()
logits, logits_adv = model(x), model(x_adv)
loss = self.criterion(logits_adv, y)
return loss, logits, logits_adv
class TRADESLoss(nn.Module):
"""Adapted from https://github.com/yaodongyu/TRADES/blob/master/trades.py#L17"""
def __init__(self,
attack_cfg: AttackCfg,
natural_criterion: nn.Module,
beta: float,
dev_env: DeviceEnv,
num_classes: int,
eval_mode: bool = False):
super().__init__()
self.attack = make_train_attack(attack_cfg.name,
attack_cfg.eps_schedule,
attack_cfg.eps,
attack_cfg.eps_schedule_period,
attack_cfg.zero_eps_epochs,
attack_cfg.step_size,
attack_cfg.steps,
attack_cfg.norm,
attack_cfg.boundaries,
criterion=nn.KLDivLoss(reduction="sum"),
num_classes=num_classes,
logits_y=True,
dev_env=dev_env)
self.natural_criterion = natural_criterion
self.kl_criterion = nn.KLDivLoss(reduction="sum")
self.beta = beta
self.eval_mode = eval_mode
def forward(self, model: nn.Module, x: torch.Tensor, y: torch.Tensor,
epoch: int) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
batch_size = x.size(0)
# Avoid setting the model in eval mode if on XLA (it crashes)
if self.eval_mode:
model.eval() # FIXME: understand why with eval the gradient
# of BatchNorm crashes
output_softmax = F.softmax(model(x.detach()), dim=-1)
x_adv = self.attack(model, x, output_softmax, epoch)
model.train()
logits, logits_adv = model(x), model(x_adv)
loss_natural = self.natural_criterion(logits, y)
loss_robust = (1.0 / batch_size) * self.kl_criterion(F.log_softmax(logits_adv, dim=1),
F.softmax(logits, dim=1))
loss = loss_natural + self.beta * loss_robust
return loss, logits, logits_adv