-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
401 lines (292 loc) · 12.8 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import gzip
import itertools
import json
import logging
from pathlib import Path
import pickle
import random
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.cuda
logger = logging.getLogger(__name__)
def save_zip(obj, path, protocol=4):
"""
Saves a compressed object to disk.
"""
# Create the folder, if necessary
Path(path).parent.mkdir(parents=True, exist_ok=True)
file = gzip.GzipFile(path, 'wb')
pickle.dump(obj, file, protocol)
file.close()
def load_zip(path):
"""
Loads a compressed object from disk.
"""
file = gzip.GzipFile(path, 'rb')
obj = pickle.load(file)
file.close()
return obj
class AttackConfig:
def __init__(self, config_dict):
self.config_dict = config_dict
def get_arguments(self, attack_name, domain, p, attack_type):
kwargs = {}
def load_kwargs(new_kwargs):
for key, value in new_kwargs.items():
if key in kwargs.keys():
logger.debug(
'Overriding key %s by replacing %s with %s.',
key, kwargs[key], value)
else:
logger.debug('Registering %s=%s.', key, value)
kwargs[key] = value
def loop_across_dict(current_dict, selectors):
if 'params' in current_dict:
logger.debug('Found params.')
load_kwargs(current_dict['params'])
if len(selectors) == 0:
return
general_selector, specific_selector = selectors[0]
if general_selector in current_dict and specific_selector in current_dict:
raise RuntimeError('Both selectors available: cannot choose.')
if specific_selector in current_dict:
logger.debug('Going into %s.', specific_selector)
loop_across_dict(current_dict[specific_selector], selectors[1:])
elif general_selector in current_dict:
assert len(current_dict.keys()) <= 2
logger.debug('Going into %s.', general_selector)
loop_across_dict(current_dict[general_selector], selectors[1:])
# The specific value overrides the general one, from outermost to innermost
loop_across_dict(self.config_dict,
[
('all_attacks', attack_name),
('all_domains', domain),
('all_distances', p),
('all_types', attack_type)
]
)
return kwargs
def read_attack_config_file(path):
with open(path, 'r') as f:
config_dict = json.load(f)
return AttackConfig(config_dict)
def adversarial_distance(genuines, adversarials, p):
assert genuines.shape == adversarials.shape
if len(genuines) == 0:
return torch.zeros([0], device=genuines.device)
else:
# pairwise_distance only accepts 2D tensors
genuines = genuines.flatten(1)
adversarials = adversarials.flatten(1)
distances = torch.nn.functional.pairwise_distance(
genuines, adversarials, p)
assert distances.shape == (len(genuines),)
return distances
def one_many_adversarial_distance(one, many, p):
assert one.shape == many.shape[1:]
# Add a batch dimension that matches many's batch size
one = one.unsqueeze(0).expand(len(many), -1, -1, -1)
assert one.shape == many.shape
return adversarial_distance(one, many, p)
def apply_misclassification_policy(model, images, true_labels, policy):
if policy == 'ignore':
return images, true_labels, true_labels.clone()
else:
predicted_labels = get_labels(model, images).detach()
assert predicted_labels.shape == true_labels.shape
if policy == 'remove':
correct_label = torch.eq(predicted_labels, true_labels)
return images[correct_label], true_labels[correct_label], true_labels[correct_label].clone()
elif policy == 'use_predicted':
return images, true_labels, predicted_labels
else:
raise NotImplementedError(f'Unsupported policy "{policy}".')
def check_successful(model, adversarials, labels, targeted):
assert len(adversarials) == len(labels)
adversarial_outputs = model(adversarials)
adversarial_labels = torch.argmax(adversarial_outputs, axis=1)
assert adversarial_labels.shape == labels.shape
if targeted:
return torch.eq(adversarial_labels, labels)
else:
return ~torch.eq(adversarial_labels, labels)
def misclassified(model, adversarials, labels, has_detector):
assert len(adversarials) == len(labels)
adversarial_predictions = model(adversarials)
assert len(adversarial_predictions) == len(adversarials)
assert len(adversarial_predictions.shape) == 2
return misclassified_outputs(adversarial_predictions, labels, has_detector)
def misclassified_outputs(outputs, labels, has_detector):
assert len(outputs) == len(labels)
assert len(outputs.shape) == 2
adversarial_labels = torch.argmax(outputs, axis=1)
assert adversarial_labels.shape == labels.shape
successful = ~torch.eq(adversarial_labels, labels)
if has_detector:
num_classes = outputs.shape[1]
rejected = torch.eq(adversarial_labels, num_classes - 1)
successful = successful & ~rejected
return successful
def remove_failed(model, images, labels, adversarials, has_detector):
assert len(images) == len(labels)
assert images.shape == adversarials.shape
successful = misclassified(model, adversarials, labels, has_detector)
assert successful.shape == (len(images),)
adversarials = list(adversarials)
for i in range(len(images)):
if not successful[i]:
adversarials[i] = None
return adversarials
# Returns b if filter_ is True, else a
def fast_boolean_choice(a, b, filter_, reshape=True):
assert len(a) == len(b) == len(filter_)
if reshape:
assert len(filter_.shape) == 1
pre_expansion_shape = [len(filter_)] + ([1] * (len(a.shape) - 1))
filter_ = filter_.reshape(*pre_expansion_shape)
post_expansion_shape = [len(filter_)] + list(a.shape[1:])
filter_ = filter_.expand(*post_expansion_shape)
assert a.shape == b.shape == filter_.shape
filter_ = filter_.float()
return filter_ * b + (1 - filter_) * a
def get_labels(model, images):
model_device = next(model.parameters()).device
outputs = model(images.to(model_device))
assert len(outputs) == len(images)
assert len(outputs.shape) == 2
return torch.argmax(outputs, axis=1).to(images.device)
# Implements:
# to[active][filter] = from[filter]
def replace_active(from_, to, active, filter_):
assert len(to) == len(active)
assert len(from_) == len(filter_)
replace_to = active.clone()
replace_to[active] = filter_
assert to[replace_to].shape == from_[filter_].shape
to[replace_to] = from_[filter_]
def show_images(images, adversarials, limit=None, model=None):
try:
assert len(images) == len(adversarials)
successful_images = []
successful_adversarials = []
for image, adversarial in zip(images, adversarials):
if adversarial is not None:
successful_images.append(image)
successful_adversarials.append(adversarial)
if len(successful_adversarials) == 0:
logger.warning('No successful adversarials. Skipping show_images.')
return
successful_images = torch.stack(successful_images)
successful_adversarials = torch.stack(successful_adversarials)
if limit is not None:
successful_images = successful_images[:limit]
successful_adversarials = successful_adversarials[:limit]
assert successful_images.shape == successful_adversarials.shape
if model is None:
labels = [None] * len(successful_images)
adversarial_labels = [None] * len(successful_images)
elif len(successful_images) > 0:
labels = get_labels(model, successful_images).detach()
adversarial_labels = get_labels(model, successful_adversarials)
else:
labels = []
adversarial_labels = []
assert len(successful_images) == len(labels) == len(adversarial_labels)
for image, label, adversarial, adversarial_label in zip(successful_images, labels, successful_adversarials, adversarial_labels):
image_title = 'Original'
adversarial_title = 'Adversarial'
if model is not None:
image_title += f' (label: {label})'
adversarial_title += f' (label: {adversarial_label})'
if image.shape[0] == 1:
# Use grayscale for images with only one channel
plt.style.use('grayscale')
normalisation = plt.Normalize(vmin=0, vmax=1)
image = np.moveaxis(image.cpu().numpy(), 0, 2).squeeze()
adversarial = np.moveaxis(adversarial.cpu().numpy(), 0, 2).squeeze()
difference = np.abs(image - adversarial)
_, axes = plt.subplots(1, 3, squeeze=False)
axes[0, 0].title.set_text(image_title)
axes[0, 0].imshow(image, norm=normalisation)
axes[0, 1].title.set_text(adversarial_title)
axes[0, 1].imshow(adversarial, norm=normalisation)
axes[0, 2].title.set_text('Difference')
axes[0, 2].imshow(difference, norm=normalisation)
print(f'L2 norm: {np.linalg.norm(difference.flatten())}')
print(f'LInf norm: {np.max(difference)}')
plt.show()
except Exception as e:
# Never let a visualization error cause an exception
logger.error('Failed to show images: %s.', e)
def maybe_stack(tensors, fallback_shape, dtype=torch.float, device='cpu'):
if len(tensors) > 0:
return torch.stack(tensors)
else:
if fallback_shape is None:
shape = (0, )
else:
shape = [0] + list(fallback_shape)
return torch.zeros(shape, dtype=dtype, device=device)
def clip_adversarial(adversarial, genuine, epsilon, input_min=0, input_max=1):
# Note: Supports both single and batch modes
assert adversarial.shape == genuine.shape
clipped_lower = torch.clip(genuine - epsilon, min=input_min, max=input_max)
clipped_upper = torch.clip(genuine + epsilon, min=input_min, max=input_max)
replace_lower = adversarial < clipped_lower
replace_upper = adversarial > clipped_upper
# Clip to [clipped_lower, clipped_upper]
adversarial = fast_boolean_choice(adversarial, clipped_lower, replace_lower, reshape=False)
adversarial = fast_boolean_choice(adversarial, clipped_upper, replace_upper, reshape=False)
# Note: Technically the additional clip is unnecessary and is only used as a safety measure
return torch.clip(adversarial, min=input_min, max=input_max)
def create_label_dataset(model, images, batch_size):
# TODO: Debuggare quando si passa a questo stack
image_dataset = torch.utils.data.TensorDataset(images)
dataloader = torch.utils.data.DataLoader(
image_dataset, batch_size=batch_size)
labels = []
for image_batch in dataloader:
# Convert to tensor
image_batch = torch.stack(image_batch).squeeze(0)
label_batch = get_labels(model, image_batch).detach()
labels += list(label_batch)
labels = torch.stack(labels)
return torch.utils.data.TensorDataset(images, labels)
def powerset(iterable, allow_empty):
if allow_empty:
start = 0
else:
start = 1
s = list(iterable)
return list(itertools.chain.from_iterable(itertools.combinations(s, r) for r in range(start, len(s)+1)))
def set_seed(seed):
logger.info('Setting seed.')
# Note: For higher versions of Torch, manual_seed_all is supported
torch.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
def get_rng_state():
python_state = random.getstate()
numpy_state = np.random.get_state()
pytorch_state = torch.get_rng_state()
cuda_states = torch.cuda.get_rng_state_all()
return python_state, numpy_state, pytorch_state, cuda_states
def set_rng_state(state_info):
python_state, numpy_state, pytorch_state, cuda_states = state_info
random.setstate(python_state)
np.random.set_state(numpy_state)
torch.set_rng_state(pytorch_state)
torch.cuda.set_rng_state_all(cuda_states)
def enable_determinism():
logger.info('Enabling determinism.')
torch.backends.cudnn.benchmark = False
torch.set_deterministic(True)
def torch_load(path, **kwargs):
if torch.cuda.is_available():
# No preference on location
map_location = None
else:
# Force CPU
map_location = torch.device('cpu')
return torch.load(path, map_location=map_location, **kwargs)