-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.py
339 lines (265 loc) · 11.1 KB
/
utils.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
327
328
329
330
331
332
333
334
335
336
337
338
339
# Copyright 2022 The Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import random
from typing import Dict
from typing import Optional
from typing import Tuple
from typing import Union
import numpy as np
import torch
import torch.utils.data
from typing_extensions import Literal
NormType = Union[Literal["linf"], Literal["l2"], Literal["l1"]]
LabelRandomization = Tuple[Literal["random"], Literal["systematically"], Literal[None]]
def clipping_aware_rescaling_l2_torch(
x0: torch.Tensor, delta: torch.Tensor, target_l2: Union[float, torch.Tensor]
):
"""Rescale delta such that it exactly lies target_l2 away in l2 from x0 after clipping.
Adapted from https://github.com/jonasrauber/clipping-aware-rescaling/.
Args:
x0: Tensor containing the base samples.
delta: Tensor containing the perturbations to add to x0.
target_l2: Target l2 distance.
Returns:
Tensor containing required rescaling factors.
"""
N = x0.shape[0]
assert delta.shape[0] == N
delta2 = delta.pow(2).reshape((N, -1))
space = torch.where(delta >= 0, 1 - x0, x0).reshape((N, -1)).type(delta.dtype)
f2 = space.pow(2) / torch.max(delta2, 1e-20 * torch.ones_like(delta2))
f2_sorted, ks = torch.sort(f2, dim=-1)
m = torch.cumsum(delta2.gather(dim=-1, index=ks.flip(dims=(1,))), dim=-1).flip(
dims=(1,)
)
dx = f2_sorted[:, 1:] - f2_sorted[:, :-1]
dx = torch.cat((f2_sorted[:, :1], dx), dim=-1)
dy = m * dx
y = torch.cumsum(dy, dim=-1)
if not issubclass(type(target_l2), torch.Tensor):
target_l2 = torch.ones(len(x0)).to(x0.device) * target_l2
assert len(target_l2) == len(
x0
), f"Inconsistent length of `target_l2`. Must have length {len(x0)}."
assert len(target_l2.shape) == 1, "Inconsistent shape of `target_l2` (must be 1D)."
target_l2 = target_l2.view((-1, 1, 1, 1)).expand(*x0.shape)
target_l2 = target_l2.view(len(target_l2), -1)
target_l2 = target_l2.type(delta.dtype)
c = y >= target_l2**2
# work-around to get first nonzero element in each row
f = torch.arange(c.shape[-1], 0, -1, device=c.device)
v, j = torch.max(c.long() * f, dim=-1)
rows = torch.arange(0, N)
eps2 = f2_sorted[rows, j] - (y[rows, j] - target_l2[rows, j] ** 2) / m[rows, j]
# it can happen that for certain rows even the largest j is not large enough
# (i.e. v == 0), then we will just use it (without any correction) as it's
# the best we can do (this should also be the only cases where m[j] can be
# 0 and they are thus not a problem)
eps2 = torch.where(v == 0, f2_sorted[:, -1], eps2)
eps = torch.sqrt(eps2)
eps = eps.reshape((-1,) + (1,) * (len(x0.shape) - 1))
return eps
def clipping_aware_rescaling_l1_torch(
x0: torch.Tensor, delta: torch.Tensor, target_l1: Union[float, torch.Tensor]
):
"""Rescale delta such that it exactly lies target_l1 away in l1 from x0 after clipping.
Adapted from https://github.com/jonasrauber/clipping-aware-rescaling/.
Args:
x0: Tensor containing the base samples.
delta: Tensor containing the perturbations to add to x0.
target_l1: Target l1 distance.
Returns:
Tensor containing required rescaling factors.
"""
N = x0.shape[0]
assert delta.shape[0] == N
delta2 = delta.abs().reshape((N, -1))
space = torch.where(delta >= 0, 1 - x0, x0).reshape((N, -1)).type(delta.dtype)
f2 = space.abs() / torch.max(delta2, 1e-20 * torch.ones_like(delta2))
f2_sorted, ks = torch.sort(f2, dim=-1)
m = torch.cumsum(delta2.gather(dim=-1, index=ks.flip(dims=(1,))), dim=-1).flip(
dims=(1,)
)
dx = f2_sorted[:, 1:] - f2_sorted[:, :-1]
dx = torch.cat((f2_sorted[:, :1], dx), dim=-1)
dy = m * dx
y = torch.cumsum(dy, dim=-1)
# c = y >= target_l2
if not issubclass(type(target_l1), torch.Tensor):
target_l1 = torch.ones(len(x0)).to(x0.device) * target_l1
assert len(target_l1) == len(
x0
), f"Inconsistent length of `target_l2`. Must have length {len(x0)}."
assert len(target_l1.shape) == 1, "Inconsistent shape of `target_l2` (must be 1D)."
target_l1 = target_l1.view((-1, 1, 1, 1)).expand(*x0.shape)
target_l1 = target_l1.view(len(target_l1), -1)
target_l1 = target_l1.type(delta.dtype)
c = y >= target_l1
# Work-around to get first nonzero element in each row.
f = torch.arange(c.shape[-1], 0, -1, device=c.device)
v, j = torch.max(c.long() * f, dim=-1)
rows = torch.arange(0, N)
eps2 = f2_sorted[rows, j] - (y[rows, j] - target_l1[rows, j]) / m[rows, j]
# It can happen that for certain rows even the largest j is not large enough
# (i.e. v == 0), then we will just use it (without any correction) as it's
# the best we can do (this should also be the only cases where m[j] can be
# 0 and they are thus not a problem).
eps = torch.where(v == 0, f2_sorted[:, -1], eps2)
eps = eps.reshape((-1,) + (1,) * (len(x0.shape) - 1))
return eps
def clipping_aware_rescaling_linf_torch(
x0: torch.Tensor, delta: torch.Tensor, target_linf: Union[float, torch.Tensor]
):
"""Rescale delta such that it exactly lies target_linf away in l2inf from x0 after clipping.
Adapted from https://github.com/jonasrauber/clipping-aware-rescaling/.
Args:
x0: Tensor containing the base samples.
delta: Tensor containing the perturbations to add to x0.
target_linf: Target l2 distance.
Returns:
Tensor containing required rescaling factors.
"""
N = x0.shape[0]
assert delta.shape[0] == N
if not issubclass(type(target_linf), torch.Tensor):
target_linf = torch.ones(len(x0)).to(x0.device) * target_linf
assert len(target_linf) == len(
x0
), f"Inconsistent length of `target_linf`. Must have length {len(x0)}."
assert (
len(target_linf.shape) == 1
), "Inconsistent shape of `target_linf` (must be 1D)."
target_linf = target_linf.view((-1, 1, 1, 1)).expand(*x0.shape)
target_linf = target_linf.view(len(target_linf), -1)
target_linf = target_linf.type(delta.dtype)
delta2 = delta.abs().reshape((N, -1))
space = torch.where(delta >= 0, 1 - x0, x0).reshape((N, -1)).type(delta.dtype)
space_mask = space < target_linf
if torch.any(torch.all(space_mask, dim=-1)):
print("Not possible to rescale delta yield set Linf distance")
delta2[space_mask] = 0
delta2_sorted, _ = torch.sort(delta2, dim=-1, descending=True)
eps = target_linf[:, 0] / delta2_sorted[:, 0]
eps = eps.view(-1, 1, 1, 1)
return eps
def clipping_aware_rescaling(
x0: torch.Tensor,
delta: torch.Tensor,
target_distance: Union[float, torch.Tensor],
norm: NormType,
growing: bool = True,
shrinking: bool = True,
return_delta: bool = False,
):
"""Rescale delta such that it exactly lies target_distance away from x0 after clipping.
Adapted from https://github.com/jonasrauber/clipping-aware-rescaling/.
Args:
x0: Tensor containing the base samples.
delta: Tensor containing the perturbations to add to x0.
target_distance: Target distance.
norm: Norm for measuring the distance between x0 and delta.
growing: If True, delta is allowed to grow.
shrinking: If True, delta is allowed to shrink.
return_delta: Return rescaled delta in addition to x0
plus rescaled delta.
Returns:
If return_delta, Tuple of (x0 plus rescaled delta, rescaled delta), otherwise
only x0 plus rescaled delta.
"""
if norm == "linf":
eps = clipping_aware_rescaling_linf_torch(x0, delta, target_distance)
elif norm == "l2":
eps = clipping_aware_rescaling_l2_torch(x0, delta, target_distance)
elif norm == "l1":
eps = clipping_aware_rescaling_l1_torch(x0, delta, target_distance)
else:
raise ValueError("Invalid norm")
if not shrinking:
eps = torch.clamp_min(eps, 1.0)
if not growing:
eps = torch.clamp_max(eps, 1.0)
x = x0 + eps * delta
x = torch.clamp(x, 0, 1)
if return_delta:
return x, eps * delta
else:
return x
def normalize(x: torch.Tensor, norm: NormType):
"""Normalize data to have unit norm.
Args:
x: Data to normalize.
norm: Norm to use.
Returns:
Normalized x0.
"""
if norm == "linf":
x = torch.sign(x)
elif norm in ("l2", "l1"):
x /= torch.norm(x, p=1 if norm == "l1" else 2, keepdim=True, dim=(1, 2, 3))
else:
raise ValueError("Invalid norm:", norm)
return x
class RandomizeLabelsDataset(torch.utils.data.Dataset):
def __init__(
self,
base: torch.utils.data.Dataset,
mode: LabelRandomization,
label_map: Optional[Dict[int, int]] = None,
n_classes: int = 10,
):
if not n_classes > 0:
raise ValueError("n_classes must be > 0.")
if mode is None and label_map is None:
raise ValueError("If mode is None, label_map must not be None.")
if not mode in (None, "random", "systematically"):
raise ValueError("mode must be one of None, random, systematically.")
self.base = base
self.mode = mode
if label_map is None:
if mode == "random":
labels = np.random.randint(low=0, high=n_classes, size=len(base))
elif mode == "systematically":
labels = [
(a + b) % n_classes for a, b in enumerate(list(range(n_classes)))
]
random.shuffle(labels)
label_map = {i: labels[i] for i in range(len(labels))}
self.label_map = label_map
def __getitem__(self, item):
x, y = self.base[item]
if self.mode == "random":
y = self.label_map[item]
elif self.mode == "systematically":
y = self.label_map[y]
else:
raise ValueError()
return x, y
def __len__(self):
return len(self.base)
def __repr__(self):
return f"RandomizeLabelsDataset(base_dataset: {repr(self.base)}, mode: {self.mode})"
def build_dataloader_from_arrays(x: np.ndarray, y: np.ndarray, batch_size: int = 1):
"""Wrap two arrays in a dataset and data loader.
Args:
x: Array containing input data.
y: Array containing target data.
batch_size: Batch size of the newly created data loader.
Returns:
Dataloader based on x,y.
"""
x_tensor = torch.tensor(x, device="cpu", dtype=torch.float32)
y_tensor = torch.tensor(y, device="cpu", dtype=torch.long)
dataset = torch.utils.data.TensorDataset(x_tensor, y_tensor)
dataloader = torch.utils.data.DataLoader(dataset, batch_size)
return dataloader