-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
carlini_wagner.py
223 lines (174 loc) · 8.01 KB
/
carlini_wagner.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
from typing import Union, Tuple, Any, Optional
from functools import partial
import numpy as np
import eagerpy as ep
from ..devutils import flatten
from ..devutils import atleast_kd
from ..types import Bounds
from ..models import Model
from ..distances import l2
from ..criteria import Misclassification
from ..criteria import TargetedMisclassification
from .base import MinimizationAttack
from .base import T
from .base import get_criterion
from .base import raise_if_kwargs
from .base import verify_input_bounds
from .gradient_descent_base import AdamOptimizer
class L2CarliniWagnerAttack(MinimizationAttack):
"""Implementation of the Carlini & Wagner L2 Attack. [#Carl16]_
Args:
binary_search_steps : Number of steps to perform in the binary search
over the const c.
steps : Number of optimization steps within each binary search step.
stepsize : Stepsize to update the examples.
confidence : Confidence required for an example to be marked as adversarial.
Controls the gap between example and decision boundary.
initial_const : Initial value of the const c with which the binary search starts.
abort_early : Stop inner search as soons as an adversarial example has been found.
Does not affect the binary search over the const c.
References:
.. [#Carl16] Nicholas Carlini, David Wagner, "Towards evaluating the robustness of
neural networks. In 2017 ieee symposium on security and privacy"
https://arxiv.org/abs/1608.04644
"""
distance = l2
def __init__(
self,
binary_search_steps: int = 9,
steps: int = 10000,
stepsize: float = 1e-2,
confidence: float = 0,
initial_const: float = 1e-3,
abort_early: bool = True,
):
self.binary_search_steps = binary_search_steps
self.steps = steps
self.stepsize = stepsize
self.confidence = confidence
self.initial_const = initial_const
self.abort_early = abort_early
def run(
self,
model: Model,
inputs: T,
criterion: Union[Misclassification, TargetedMisclassification, T],
*,
early_stop: Optional[float] = None,
**kwargs: Any,
) -> T:
raise_if_kwargs(kwargs)
x, restore_type = ep.astensor_(inputs)
criterion_ = get_criterion(criterion)
del inputs, criterion, kwargs
verify_input_bounds(x, model)
N = len(x)
if isinstance(criterion_, Misclassification):
targeted = False
classes = criterion_.labels
change_classes_logits = self.confidence
elif isinstance(criterion_, TargetedMisclassification):
targeted = True
classes = criterion_.target_classes
change_classes_logits = -self.confidence
else:
raise ValueError("unsupported criterion")
def is_adversarial(perturbed: ep.Tensor, logits: ep.Tensor) -> ep.Tensor:
if change_classes_logits != 0:
logits += ep.onehot_like(logits, classes, value=change_classes_logits)
return criterion_(perturbed, logits)
if classes.shape != (N,):
name = "target_classes" if targeted else "labels"
raise ValueError(
f"expected {name} to have shape ({N},), got {classes.shape}"
)
bounds = model.bounds
to_attack_space = partial(_to_attack_space, bounds=bounds)
to_model_space = partial(_to_model_space, bounds=bounds)
x_attack = to_attack_space(x)
reconstsructed_x = to_model_space(x_attack)
rows = range(N)
def loss_fun(
delta: ep.Tensor, consts: ep.Tensor
) -> Tuple[ep.Tensor, Tuple[ep.Tensor, ep.Tensor]]:
assert delta.shape == x_attack.shape
assert consts.shape == (N,)
x = to_model_space(x_attack + delta)
logits = model(x)
if targeted:
c_minimize = best_other_classes(logits, classes)
c_maximize = classes # target_classes
else:
c_minimize = classes # labels
c_maximize = best_other_classes(logits, classes)
is_adv_loss = logits[rows, c_minimize] - logits[rows, c_maximize]
assert is_adv_loss.shape == (N,)
is_adv_loss = is_adv_loss + self.confidence
is_adv_loss = ep.maximum(0, is_adv_loss)
is_adv_loss = is_adv_loss * consts
squared_norms = flatten(x - reconstsructed_x).square().sum(axis=-1)
loss = is_adv_loss.sum() + squared_norms.sum()
return loss, (x, logits)
loss_aux_and_grad = ep.value_and_grad_fn(x, loss_fun, has_aux=True)
consts = self.initial_const * np.ones((N,))
lower_bounds = np.zeros((N,))
upper_bounds = np.inf * np.ones((N,))
best_advs = ep.zeros_like(x)
best_advs_norms = ep.full(x, (N,), ep.inf)
# the binary search searches for the smallest consts that produce adversarials
for binary_search_step in range(self.binary_search_steps):
if (
binary_search_step == self.binary_search_steps - 1
and self.binary_search_steps >= 10
):
# in the last binary search step, repeat the search once
consts = np.minimum(upper_bounds, 1e10)
# create a new optimizer find the delta that minimizes the loss
delta = ep.zeros_like(x_attack)
optimizer = AdamOptimizer(delta, self.stepsize)
# tracks whether adv with the current consts was found
found_advs = np.full((N,), fill_value=False)
loss_at_previous_check = np.inf
consts_ = ep.from_numpy(x, consts.astype(np.float32))
for step in range(self.steps):
loss, (perturbed, logits), gradient = loss_aux_and_grad(delta, consts_)
delta -= optimizer(gradient)
if self.abort_early and step % (np.ceil(self.steps / 10)) == 0:
# after each tenth of the overall steps, check progress
if not (loss <= 0.9999 * loss_at_previous_check):
break # stop Adam if there has been no progress
loss_at_previous_check = loss.item()
found_advs_iter = is_adversarial(perturbed, logits)
found_advs = np.logical_or(found_advs, found_advs_iter.numpy())
norms = flatten(perturbed - x).norms.l2(axis=-1)
closer = norms < best_advs_norms
new_best = ep.logical_and(closer, found_advs_iter)
new_best_ = atleast_kd(new_best, best_advs.ndim)
best_advs = ep.where(new_best_, perturbed, best_advs)
best_advs_norms = ep.where(new_best, norms, best_advs_norms)
upper_bounds = np.where(found_advs, consts, upper_bounds)
lower_bounds = np.where(found_advs, lower_bounds, consts)
consts_exponential_search = consts * 10
consts_binary_search = (lower_bounds + upper_bounds) / 2
consts = np.where(
np.isinf(upper_bounds), consts_exponential_search, consts_binary_search
)
return restore_type(best_advs)
def best_other_classes(logits: ep.Tensor, exclude: ep.Tensor) -> ep.Tensor:
other_logits = logits - ep.onehot_like(logits, exclude, value=ep.inf)
return other_logits.argmax(axis=-1)
def _to_attack_space(x: ep.Tensor, *, bounds: Bounds) -> ep.Tensor:
min_, max_ = bounds
a = (min_ + max_) / 2
b = (max_ - min_) / 2
x = (x - a) / b # map from [min_, max_] to [-1, +1]
x = x * 0.999999 # from [-1, +1] to approx. (-1, +1)
x = x.arctanh() # from (-1, +1) to (-inf, +inf)
return x
def _to_model_space(x: ep.Tensor, *, bounds: Bounds) -> ep.Tensor:
min_, max_ = bounds
x = x.tanh() # from (-inf, +inf) to (-1, +1)
a = (min_ + max_) / 2
b = (max_ - min_) / 2
x = x * b + a # map from (-1, +1) to (min_, max_)
return x