-
Notifications
You must be signed in to change notification settings - Fork 3
/
decision_boundary_binarization.py
1402 lines (1268 loc) · 56.2 KB
/
decision_boundary_binarization.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# 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 functools
import traceback
import sys
import warnings
from typing import Callable
from typing import List
from torch.utils.data import SequentialSampler
from typing_extensions import Literal
from typing import Optional
from typing import Tuple
from typing import Union
import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC, SVC
import torch
import torch.utils.data
from torch.utils.data import DataLoader
import tqdm
import argparse_utils as aut
import networks
import utils as ut
__all__ = ["interior_boundary_discrimination_attack"]
LogitRescalingType = Optional[
Union[Literal["fixed"], Literal["adaptive"], Literal["tight"]]
]
SolutionGoodnessType = Union[Literal["perfect"], Literal["good"], None]
OptimizerType = Union[
Literal["sklearn"], Literal["sklearn-svm"], Literal["sgd"], Literal["adam"]
]
class __KwargsSequential(torch.nn.Sequential):
"""
Modification of a torch.nn.Sequential model that allows kwargs in the
forward pass. These will be passed to the first module of the network.
"""
def forward(self, x, **kwargs):
for idx, module in enumerate(self):
if idx == 0:
x = module(x, **kwargs)
else:
x = module(x)
return x
def _create_raw_data(
x: torch.Tensor,
y: torch.Tensor,
n_inner_points: int,
n_boundary_points: int,
n_boundary_adversarial_points: int,
n_far_off_boundary_points: int,
n_far_off_adversarial_points: int,
batch_size: int,
fill_batches_for_verification: bool,
verify_valid_inner_input_data_fn: Optional[Callable],
verify_valid_boundary_input_data_fn: Optional[Callable],
get_boundary_adversarials_fn: Optional[
Callable[[torch.Tensor, torch.Tensor, int, float], torch.Tensor]
],
device: str,
epsilon: float,
norm: ut.NormType,
n_boundary_classes: int = 1,
eta: float = 0.95,
xi: float = 1.50,
include_original: bool = True,
rejection_resampling_max_repetitions: int = 10,
sample_boundary_from_corners: bool = False,
) -> Tuple[DataLoader, DataLoader, DataLoader, int]:
"""Creates the raw training data in image space. Label 0 corresponds to
inner points and label 1 to boundary points."""
def _sample_inner_points(n_samples):
# We want to keep the original data point -> only generate n-1 new points
x_inner = torch.repeat_interleave(torch.unsqueeze(x, 0), n_samples, 0)
if norm == "linf":
# Random noise in [-1, 1].
delta_inner = 2 * torch.rand_like(x_inner) - 1.0
# Random noise in [-eps*eta, eps*eta]
delta_inner = delta_inner * eta * epsilon
elif norm == "l2":
# sample uniformly in ball with max radius eta*epsilon
delta_inner = torch.randn_like(x_inner)
delta_inner /= torch.norm(delta_inner, p=2, dim=[1, 2, 3], keepdim=True)
delta_inner *= torch.pow(
torch.rand(
(len(delta_inner), 1, 1, 1),
dtype=delta_inner.dtype,
device=delta_inner.device,
),
1 / np.prod(x.shape[1:]),
)
delta_inner *= epsilon * eta
else:
raise ValueError
if norm != "linf":
_, delta_inner = ut.clipping_aware_rescaling(
x_inner,
delta_inner,
target_distance=epsilon * eta,
norm=norm,
shrinking=True,
return_delta=True,
)
x_inner = torch.clamp(x_inner + delta_inner, 0, 1)
y_inner = torch.zeros(len(x_inner), dtype=torch.long, device=device)
return x_inner, y_inner
def _sample_boundary_points(n_samples, distance=epsilon):
x_boundary = torch.unsqueeze(x, 0).repeat(
tuple([n_samples] + [1] * len(x.shape))
)
if norm == "linf":
if sample_boundary_from_corners:
delta_boundary = torch.randint(
0,
2,
size=x_boundary.shape,
device=x_boundary.device,
dtype=x_boundary.dtype,
)
delta_boundary = (delta_boundary * 2.0 - 1.0) * distance
else:
delta_boundary = (torch.rand_like(x_boundary) * 2.0 - 1.0) * distance
elif norm == "l2":
# sample uniformly on sphere with radius epsilon
delta_boundary = torch.randn_like(x_boundary)
delta_boundary /= torch.norm(
delta_boundary, p=2, dim=[1, 2, 3], keepdim=True
)
delta_boundary *= distance
else:
raise ValueError
if not sample_boundary_from_corners:
_, delta_boundary = ut.clipping_aware_rescaling(
x_boundary,
delta_boundary,
target_distance=distance,
norm=norm,
growing=True,
shrinking=True,
return_delta=True,
)
x_boundary = torch.clamp(x_boundary + delta_boundary, 0, 1)
y_boundary = torch.ones(len(x_boundary), dtype=torch.long, device=device)
return x_boundary, y_boundary
def _create_boundary_data():
# TODO(zimmerrol): Extend this logic for multiple boundary classes
n_random_boundary_samples = n_boundary_points - n_boundary_adversarial_points
if n_random_boundary_samples == 0:
x_random, y_random = None, None
else:
if verify_valid_boundary_input_data_fn is None:
x_random, y_random = _sample_boundary_points(n_random_boundary_samples)
else:
x_random, y_random = _rejection_resampling(
_sample_boundary_points,
n_random_boundary_samples,
verify_valid_boundary_input_data_fn,
n_repetitions=rejection_resampling_max_repetitions,
)
if n_random_boundary_samples == n_boundary_points:
# do not have to add any special adversarial points anymore
x_total, y_total = x_random, y_random
else:
x_adv = get_boundary_adversarials_fn(
x.clone(), y, n_boundary_adversarial_points, epsilon
)
y_adv = torch.ones(len(x_adv), dtype=y.dtype, device=y.device)
if x_random is not None:
x_total = torch.cat((x_random, x_adv))
y_total = torch.cat((y_random, y_adv))
else:
x_total, y_total = x_adv, y_adv
if n_boundary_classes > 1:
raise NotImplementedError("n_boundary_classes > 1 is not yet implemented.")
if n_far_off_boundary_points > 0:
# add examples that have magnitude larger than epsilon but can be used
# e.g., by logit matching attacks as a reference point
n_random_far_off_samples = (
n_far_off_boundary_points - n_far_off_adversarial_points
)
if n_random_boundary_samples == 0:
x_faroff_random, y_faroff_random = None, None
else:
if verify_valid_boundary_input_data_fn is None:
x_faroff_random, y_faroff_random = _sample_boundary_points(
n_random_far_off_samples * n_boundary_classes,
distance=xi * epsilon,
)
else:
x_faroff_random, y_faroff_random = _rejection_resampling(
functools.partial(
_sample_boundary_points, distance=xi * epsilon
),
n_random_far_off_samples * n_boundary_classes,
verify_valid_boundary_input_data_fn,
n_repetitions=rejection_resampling_max_repetitions,
)
if n_boundary_classes > 1:
raise NotImplementedError(
"n_boundary_classes > 1 is not yet implemented."
)
if n_far_off_adversarial_points > 0:
x_faroff_adv = get_boundary_adversarials_fn(
x.clone(), y, n_far_off_adversarial_points, epsilon
)
y_faroff_adv = torch.ones(
len(x_faroff_adv), dtype=y.dtype, device=y.device
)
if x_faroff_random is not None:
x_faroff = torch.cat((x_faroff_random, x_faroff_adv))
y_faroff = torch.cat((y_faroff_random, y_faroff_adv))
else:
x_faroff, y_faroff = x_faroff_adv, y_faroff_adv
else:
x_faroff, y_faroff = x_faroff_random, y_faroff_random
x_total = torch.cat((x_total, x_faroff))
y_total = torch.cat((y_total, y_faroff))
return x_total, y_total
def _create_inner_data():
if include_original:
n_random_points = n_inner_points - 1
else:
n_random_points = n_inner_points
if n_random_points > 0:
if verify_valid_inner_input_data_fn is None:
x_random, y_random = _sample_inner_points(n_inner_points)
else:
x_random, y_random = _rejection_resampling(
_sample_inner_points,
n_inner_points,
verify_valid_inner_input_data_fn,
n_repetitions=rejection_resampling_max_repetitions,
)
if include_original:
x_total = torch.cat((torch.unsqueeze(x, 0), x_random))
y_total = torch.zeros(
len(y_random) + 1, dtype=y_random.dtype, device=y_random.device
)
else:
x_total, y_total = x_random, y_random
else:
x_total = torch.unsqueeze(x, 0)
y_total = torch.zeros(1, dtype=y_boundary.dtype, device=y_boundary.device)
return x_total, y_total
def _rejection_resampling(
sampling_fn, n_samples, verify_valid_input_data_fn, n_repetitions=10
):
"""Resample & replace until all samples returned by the sampling_fn are
valid according to verify_valid_input_data_fn."""
# do not waste time but running a non-full batch
if fill_batches_for_verification:
n_sampling_samples = max(n_samples, batch_size)
else:
n_sampling_samples = n_samples
x, y = sampling_fn(n_sampling_samples)
x_valid_mask = verify_valid_input_data_fn(x)
for i in range(n_repetitions + 1):
if np.sum(x_valid_mask) >= n_samples:
# found enough samples
# now restrict x to the valid samples
# and x and y such that their length matches n_samples
x = x[x_valid_mask]
x = x[:n_samples]
y = y[:n_samples]
return x, y
if i == n_repetitions:
raise RuntimeError(
f"Rejection resampling failed after {n_repetitions} " f"rounds."
)
# check how many samples to be replaced
n_x_invalid = len(x_valid_mask) - np.sum(x_valid_mask)
# generate new samples
c = sampling_fn(n_sampling_samples)[0]
# check how many of them are valid and are needed
c_valid_mask = verify_valid_input_data_fn(c)
c = c[c_valid_mask][:n_x_invalid]
c_valid_mask = c_valid_mask[c_valid_mask][:n_x_invalid]
n_x_invalid_c_valid = min(n_x_invalid, len(c))
# replace samples and update the mask
x[~x_valid_mask][:n_x_invalid_c_valid] = c
x_valid_mask[~x_valid_mask][:n_x_invalid_c_valid] = c_valid_mask
if not n_inner_points > 0:
raise ValueError("n_inner_points must be > 0.")
if not n_boundary_points > 0:
raise ValueError("n_boundary_points must be > 0.")
if not n_boundary_classes == 1:
raise NotImplementedError("More than 1 boundary class is not yet supported.")
if not n_far_off_adversarial_points >= 0:
raise ValueError("n_far_off_adversarial_points must not be negative.")
if not n_far_off_boundary_points >= 0:
raise ValueError("n_far_off_boundary_points must not be negative.")
if not n_boundary_adversarial_points >= 0:
raise ValueError("n_boundary_adversarial_points must not be negative.")
x = x.to(device)
y = y.to(device)
(x_boundary, y_boundary) = _create_boundary_data()
(x_inner, y_inner) = _create_inner_data()
x = torch.cat((x_inner, x_boundary))
y = torch.cat((y_inner, y_boundary))
dataset = torch.utils.data.TensorDataset(x, y)
dataset_boundary = torch.utils.data.TensorDataset(x_boundary, y_boundary)
dataset_inner = torch.utils.data.TensorDataset(x_inner, y_inner)
dataloader = torch.utils.data.DataLoader(
dataset, shuffle=False, batch_size=batch_size
)
dataloader_boundary = torch.utils.data.DataLoader(
dataset_boundary, shuffle=False, batch_size=batch_size
)
dataloader_inner = torch.utils.data.DataLoader(
dataset_inner, shuffle=False, batch_size=batch_size
)
return dataloader, dataloader_boundary, dataloader_inner, len(x)
def _get_data_features_and_maybe_logits(
classifier: Callable,
raw_data_loader: torch.utils.data.DataLoader,
get_logits: bool,
device: str,
include_raw_data: bool = False,
raw_data_loader_boundary: Optional[torch.utils.data.DataLoader] = None,
raw_data_loader_inner: Optional[torch.utils.data.DataLoader] = None,
n_repetitions_boundary: Optional[int] = None,
n_repetitions_inner: Optional[int] = None,
) -> Tuple[torch.utils.data.DataLoader, torch.Tensor, int, int]:
"""
Collects the intermediate features for a classifier and creates a new data
loader consisting only of these features.
Args:
classifier: Classifier to use as a feature extractor.
raw_data_loader: Data loader that contains images which
shall be mapped to intermediate features.
get_logits: Extract not only features but also logits
device: torch device.
include_raw_data: Include raw images in the data loader.
Returns:
Data loader mapping intermediate features to class labels.
"""
all_features = []
all_logits = [] if get_logits else None
all_labels = []
all_images = []
def _process_dataloader(dataloader: DataLoader):
with torch.no_grad():
for x, y in dataloader:
x_ = x.to(device)
if get_logits:
features, logits = classifier(x_, features_and_logits=True)
all_logits.append(logits)
else:
features = classifier(x_, features_only=True)
all_features.append(features.detach())
all_labels.append(y)
if include_raw_data:
all_images.append(x)
_process_dataloader(raw_data_loader)
if n_repetitions_boundary is not None:
raw_data_loader_boundary = torch.utils.data.DataLoader(
torch.utils.data.TensorDataset(
torch.repeat_interleave(
raw_data_loader_boundary.dataset.tensors[0],
n_repetitions_boundary,
0,
),
torch.repeat_interleave(
raw_data_loader_boundary.dataset.tensors[1],
n_repetitions_boundary,
0,
),
),
batch_size=raw_data_loader_boundary.batch_size,
)
_process_dataloader(raw_data_loader_boundary)
if n_repetitions_inner is not None:
raw_data_loader_inner = torch.utils.data.DataLoader(
torch.utils.data.TensorDataset(
torch.repeat_interleave(
raw_data_loader_inner.dataset.tensors[0], n_repetitions_inner, 0
),
torch.repeat_interleave(
raw_data_loader_inner.dataset.tensors[1], n_repetitions_inner, 0
),
),
batch_size=raw_data_loader_inner.batch_size,
)
_process_dataloader(raw_data_loader_inner)
all_features = torch.cat(all_features, 0)
if get_logits:
all_logits = torch.cat(all_logits, 0)
all_labels = torch.cat(all_labels, 0)
if include_raw_data:
all_images = torch.cat(all_images)
if len(all_features.shape) > 2:
warnings.warn(
f"Features are not vectors but higher dimensional "
f"({len(all_features.shape) - 1})"
)
if include_raw_data:
dataset = torch.utils.data.TensorDataset(all_features, all_labels, all_images)
else:
dataset = torch.utils.data.TensorDataset(all_features, all_labels)
dataloader = torch.utils.data.DataLoader(
dataset,
shuffle=not isinstance(raw_data_loader.sampler, SequentialSampler),
batch_size=raw_data_loader.batch_size,
)
return dataloader, all_logits, all_features.shape[-1], all_features.shape[0]
def _train_logistic_regression_classifier(
n_features: int,
train_loader: DataLoader,
classifier_logits: Optional[torch.Tensor],
optimizer: OptimizerType,
lr: float,
device: str,
n_classes: int = 2,
rescale_logits: LogitRescalingType = "fixed",
decision_boundary_closeness: Optional[float] = None,
solution_goodness: SolutionGoodnessType = "perfect",
class_weight: Optional[Union[Literal["balanced"], dict]] = None,
) -> torch.nn.Module:
"""
Trains a logistic regression model.
Args:
n_features: Feature dimensionality.
train_loader: Data loader containing the data to fit model on.
classifier_logits: Logits of the underlying classifier; will be used for logit
rescaling.
optimizer: Type of optimizer to use.
lr: Learning rate (only applies to explicit gradient-descent optimizer).
device: torch device.
rescale_logits: Rescale weights of model such that the logits have
at most unit absolute magnitude.
decision_boundary_closeness: (optional) The larger this value, the will
the decision boundary be placed to the boundary sample(s).
Returns:
Logistic regression model.
"""
if rescale_logits == "adaptive" and classifier_logits is None:
raise ValueError("classifier_logits must be supplied for adaptive rescaling")
def get_accuracy() -> Tuple[float, float, float]:
"""
:return: (total accuracy, accuracy for inner samples, for outer samples)
"""
# calculate accuracy
n_correct = {0: 0, 1: 0}
n_total = {0: 0, 1: 0}
with torch.no_grad():
for x, y in train_loader:
x = x.to(device)
logits = binary_classifier(x)
for k in n_total.keys():
n_correct[k] += (
(logits.argmax(-1).cpu() == y.cpu())
.float()[y == k]
.sum()
.item()
)
n_total[k] += len(x[y == k])
accuracy = (n_correct[0] + n_correct[1]) / (n_total[0] + n_total[1])
accuracy_inner = n_correct[0] / n_total[0]
accuracy_outer = n_correct[1] / n_total[1]
return accuracy, accuracy_inner, accuracy_outer
if not n_classes == 2:
raise NotImplementedError("Currently only supports 1 boundary class")
if optimizer.startswith("sklearn"):
if optimizer == "sklearn":
# Use logistic regression of sklearn to speed up fitting.
regression = LogisticRegression(
penalty="none",
max_iter=max(1000, int(lr)),
multi_class="multinomial",
class_weight=class_weight,
)
elif optimizer == "sklearn-svm":
# Since the problem should be perfectly possible to solve, C should not
# have any effect.
regression = LinearSVC(
penalty="l2", C=10e5, max_iter=max(1000, int(lr)), multi_class="ovr"
)
else:
raise ValueError("Invalid optimizer choice.")
regression.fit(
train_loader.dataset.tensors[0].cpu().numpy(),
train_loader.dataset.tensors[1].cpu().numpy(),
)
binary_classifier = torch.nn.Linear(n_features, n_classes)
binary_classifier.weight.data = torch.Tensor(
np.concatenate((-regression.coef_, regression.coef_), 0)
)
binary_classifier.bias.data = torch.Tensor(
np.concatenate((-regression.intercept_, regression.intercept_), 0)
)
binary_classifier = binary_classifier.to(device)
accuracy, accuracy_inner, accuracy_outer = get_accuracy()
if solution_goodness is not None and accuracy < 1.0:
raise_error = solution_goodness == "perfect"
raise_error |= (
solution_goodness == "good"
and accuracy_inner == 0
or accuracy_outer == 0
)
message = (
f"sklearn solver failed to find perfect solution, "
f"Accuracy = {accuracy:.4f} instead of 1.0; "
f"{accuracy_inner:.4f} and {accuracy_outer:.4f} for "
f"inner and boundary points."
)
if raise_error:
raise RuntimeError(message)
else:
warnings.warn(message)
else:
binary_classifier = torch.nn.Linear(n_features, n_classes).to(device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = {"sgd": torch.optim.SGD, "adam": torch.optim.Adam}[optimizer](
lr=lr, params=binary_classifier.parameters()
)
epoch = 0
while True:
epoch += 1
for x, y in train_loader:
optimizer.zero_grad()
x = x.to(device)
y = y.to(device)
logits = binary_classifier(x)
loss = criterion(logits, y)
loss.backward()
optimizer.step()
if epoch > 50000:
raise RuntimeError(
f"Could not fit binary discriminator in 50k iterations "
f"(Loss = {loss.item()}."
"Consider using different settings for the optimizer."
)
accuracy = get_accuracy()
# stop training once perfect accuracy is achieved
if accuracy == 1.0:
break
if rescale_logits is not None or decision_boundary_closeness is not None:
# Get value range of binarized logits.
with torch.no_grad():
logits = binary_classifier(
train_loader.dataset.tensors[0].to(device)
).detach()
if decision_boundary_closeness is not None:
logit_differences = logits[:, 0] - logits[:, 1]
lowest_difference = np.min(logit_differences.cpu().numpy())
binary_classifier.bias.data[0] -= (
decision_boundary_closeness * lowest_difference / 2
)
binary_classifier.bias.data[1] += (
decision_boundary_closeness * lowest_difference / 2
)
if rescale_logits is not None:
binarized_logit_range = (
logits.detach().cpu().numpy().max()
- logits.detach().cpu().numpy().min()
)
if rescale_logits == "fixed":
target_logit_range = 1.0
logit_offset = 0.0
logit_rescaling_factor = binarized_logit_range / target_logit_range
elif rescale_logits == "adaptive":
# Rescale the binarized logits such that they have the same value range
# i.e., min and max value match.
target_logit_range = (
classifier_logits.detach().cpu().numpy().max()
- classifier_logits.detach().cpu().numpy().min()
)
logit_rescaling_factor = binarized_logit_range / target_logit_range
logit_offset = (
classifier_logits.detach().cpu().numpy().min()
- logits.detach().cpu().numpy().min() / logit_rescaling_factor
)
elif rescale_logits == "tight":
# Rescale/shift weights such that the distance between the decision
# boundary and the boundary data is small.
# Calculate distance of boundary points to decision boundary.
distances = binary_classifier(
train_loader.dataset.tensors[0].to(device)[
train_loader.dataset.tensors[1].to(device) != 0
]
)[:, 1:].cpu()
min_distance = distances.min()
# Move decision boundary close to true boundary points
logit_rescaling_factor = 1.0
logit_offset = torch.tensor(
[+0.999 * min_distance.item(), -0.999 * min_distance.item()],
device=device,
)
else:
raise ValueError(f"Invalid value for rescale_logits: {rescale_logits}")
binary_classifier.bias.data /= logit_rescaling_factor
binary_classifier.bias.data += logit_offset
binary_classifier.weight.data /= logit_rescaling_factor
return binary_classifier
def _get_interior_boundary_discriminator_and_dataloaders(
classifier: torch.nn.Module,
x: torch.Tensor,
y: torch.Tensor,
linearization_settings: aut.DecisionBoundaryBinarizationSettings,
device: str,
batch_size: int = 512,
rescale_logits: LogitRescalingType = "fixed",
n_samples_evaluation: int = 0,
n_samples_asr_evaluation: int = 0,
verify_valid_inner_training_data_fn: Optional[
Callable[[torch.Tensor], np.ndarray]
] = None,
verify_valid_boundary_training_data_fn: Optional[
Callable[[torch.Tensor], np.ndarray]
] = None,
verify_valid_inner_validation_data_fn: Optional[
Callable[[torch.Tensor], np.ndarray]
] = None,
verify_valid_boundary_validation_data_fn: Optional[
Callable[[torch.Tensor], np.ndarray]
] = None,
get_boundary_adversarials_fn: Optional[
Callable[[torch.Tensor, torch.Tensor, float], np.ndarray]
] = None,
fill_batches_for_verification: bool = False,
far_off_distance: float = 1.25,
rejection_resampling_max_repetitions: int = 10,
train_classifier_fn: Callable[
[int, DataLoader, DataLoader, torch.Tensor, str, LogitRescalingType],
torch.nn.Module,
] = None,
decision_boundary_closeness: Optional[float] = None,
n_inference_repetitions_boundary: Optional[int] = None,
n_inference_repetitions_inner: Optional[int] = None,
relative_inner_boundary_gap: Optional[float] = 0.05,
sample_training_data_from_corners: bool = False,
) -> Tuple[
Tuple[torch.nn.Module, torch.nn.Module],
Tuple[DataLoader, DataLoader],
Tuple[float, float, float, float, bool, bool],
]:
"""
Creates a number of perturbed images, obtains the features for these images
and trains a linear, binary discriminator of these samples.
Args:
classifier: The classifier that will be used as a feature encoder.
x: The single clean image to apply apply the method on.
y: The ground-truth classification label of x.
linearization_settings: How to construct the binary classifier.
device: torch device
batch_size: Max batch size allowed to use
rescale_logits: Rescale weights of linear classifier such that logits
have a max scale of 1
n_samples_evaluation: Number of random samples to use for evaluation
verify_valid_inner_training_data_fn: Can be used for e.g. detector-based defenses.
Check whether all input points used for training/testing are actually valid
and won't get filtered out be the model/detector.
verify_valid_boundary_training_data_fn: See verify_valid_inner_training_data_fn but for boundary samples.
verify_valid_inner_validation_data_fn: See
verify_valid_inner_training_data_fn but for calculating the validation
scores, i.e. the random ASR.
verify_valid_boundary_validation_data_fn: See
verify_valid_boundary_training_data_fn but for calculating the validation
scores, i.e. the random ASR.
get_boundary_adversarials_fn: If given, use this function to
generate all but one of the boundary samples. This can be used for
evaluating detector-based evaluation functions.
fill_batches_for_verification: If computational cost of verification_fn
does not depend on the batch size, set this to True.
far_off_distance: Relative multiplier (in terms of epsilon) controlling
the distance between clean and far off training samples.
decision_boundary_closeness: (optional) The larger this value, the will
the decision boundary be placed to the boundary sample(s).
n_inference_repetitions_boundary: (optional) How often to repeat
inference for boundary samples for obtaining their features.
n_inference_repetitions_inner: (optional) How often to repeat
inference for inner samples for obtaining their features.
relative_inner_boundary_gap: (optional) Gap between interior and
boundary data relative to epsilon (i.e. a value of 0 means boundary points
can lie directly next to inner points)
sample_training_data_from_corners: Sample training data from the corners
of the epsilon ball; this setting is only possible when using linf norm.
Returns:
Tuple containing ((binary discriminator between interior and boundary points,
binary readout only),
(dataset of perturbed images, dataset of features of perturbed images),
(validation accuracies of inner/boundary/boundary surface/boundary corner points,
random attack success rate of surface/corner points))
"""
if sample_training_data_from_corners and linearization_settings.norm != "linf":
raise ValueError("Corners are only defined for linf norm.")
# Check if model is compatible with this check.
try:
with torch.no_grad():
if rescale_logits == "adaptive":
classifier(
torch.ones((1, 1, 1, 1), device=device), features_and_logits=True
)
else:
classifier(torch.ones((1, 1, 1, 1), device=device), features_only=True)
except TypeError as e:
message = str(e)
if "unexpected keyword argument 'features_only'" in message:
raise ValueError(
"model does not support `features_only` flag in forward pass."
)
if "unexpected keyword argument 'features_and_logits'" in message:
raise ValueError(
"model does not support `features_and_logits` flag in forward pass."
)
except Exception:
pass
(
raw_train_loader,
raw_train_loader_boundary,
raw_train_loader_inner,
n_raw_training_samples,
) = _create_raw_data(
x,
y,
linearization_settings.n_inner_points,
linearization_settings.n_boundary_points,
linearization_settings.n_boundary_adversarial_points,
linearization_settings.n_far_off_boundary_points,
linearization_settings.n_far_off_adversarial_points,
batch_size=batch_size,
fill_batches_for_verification=fill_batches_for_verification,
verify_valid_inner_input_data_fn=verify_valid_inner_training_data_fn,
verify_valid_boundary_input_data_fn=verify_valid_boundary_training_data_fn,
get_boundary_adversarials_fn=get_boundary_adversarials_fn,
device=device,
epsilon=linearization_settings.epsilon,
norm=linearization_settings.norm,
n_boundary_classes=1,
include_original=True,
xi=far_off_distance,
eta=1.0 - relative_inner_boundary_gap,
rejection_resampling_max_repetitions=rejection_resampling_max_repetitions,
sample_boundary_from_corners=sample_training_data_from_corners,
)
# Get features to train binary classifier on.
(
train_loader,
logits,
n_features,
n_training_samples,
) = _get_data_features_and_maybe_logits(
classifier,
raw_train_loader,
rescale_logits == "adaptive",
device,
include_raw_data=False,
n_repetitions_boundary=n_inference_repetitions_boundary,
n_repetitions_inner=n_inference_repetitions_inner,
raw_data_loader_boundary=raw_train_loader_boundary,
raw_data_loader_inner=raw_train_loader_inner,
)
if not (n_features > n_training_samples):
warnings.warn(
f"Feature dimension ({n_features}) should not be smaller than the "
f"number of training samples ({n_training_samples})",
RuntimeWarning,
)
# finally train new binary classifier on features
if train_classifier_fn is None:
binary_classifier = _train_logistic_regression_classifier(
n_features,
train_loader,
logits,
linearization_settings.optimizer,
linearization_settings.lr,
device,
class_weight=linearization_settings.class_weight,
rescale_logits=rescale_logits,
decision_boundary_closeness=decision_boundary_closeness,
)
linearized_model = __KwargsSequential(
networks.Lambda(
lambda x, **kwargs: classifier(x, features_only=True, **kwargs)
),
binary_classifier,
)
else:
binary_classifier = None
linearized_model = train_classifier_fn(
n_features,
train_loader,
raw_train_loader,
logits,
device,
rescale_logits=rescale_logits,
)
# evaluate on another set of random samples (we are only interested in the
# performance of points inside the epsilon ball)
if n_samples_evaluation > 0:
raw_validation_loader, _, _, _ = _create_raw_data(
x,
y,
n_samples_evaluation,
n_samples_evaluation,
0,
0,
0,
batch_size=batch_size,
fill_batches_for_verification=fill_batches_for_verification,
# TODO(zimmerrol): check if this makes sense. The motivation to remove this here
# was that the moved the check down to when the accuracy is calculated
verify_valid_boundary_input_data_fn=None,
verify_valid_inner_input_data_fn=None,
# verify_valid_input_data_fn=verify_valid_input_validation_data_fn,
get_boundary_adversarials_fn=get_boundary_adversarials_fn,
device=device,
epsilon=linearization_settings.epsilon,
norm=linearization_settings.norm,
n_boundary_classes=1,
include_original=False,
xi=far_off_distance,
rejection_resampling_max_repetitions=rejection_resampling_max_repetitions,
eta=1.0,
sample_boundary_from_corners=False,
)
_, raw_validation_loader_corners, _, _ = _create_raw_data(
x,
y,
1,
n_samples_evaluation,
0,
0,
0,
batch_size=batch_size,
fill_batches_for_verification=fill_batches_for_verification,
verify_valid_boundary_input_data_fn=None,
verify_valid_inner_input_data_fn=None,
get_boundary_adversarials_fn=get_boundary_adversarials_fn,
device=device,
epsilon=linearization_settings.epsilon,
norm=linearization_settings.norm,
n_boundary_classes=1,
include_original=False,
xi=far_off_distance,
rejection_resampling_max_repetitions=rejection_resampling_max_repetitions,
eta=1.0,
sample_boundary_from_corners=True,
)
raw_validation_loader = torch.utils.data.DataLoader(
torch.utils.data.TensorDataset(
torch.cat(
(
raw_validation_loader.dataset.tensors[0],
raw_validation_loader_corners.dataset.tensors[0],
),
0,
),
torch.cat(
(
raw_validation_loader.dataset.tensors[1],
raw_validation_loader_corners.dataset.tensors[1],
),
0,
),
),
batch_size=raw_validation_loader.batch_size,
shuffle=False,
)
# Get features to test binary classifier on.
validation_loader, _, _, _ = _get_data_features_and_maybe_logits(
classifier, raw_validation_loader, False, device, include_raw_data=True
)
inner_correctly_classified = []
boundary_correctly_classified = []
for it, (x_features, y, x_images) in enumerate(validation_loader):
x_features = x_features.to(device)
x_images = x_images.to(device)
# If we use a custom train method use the raw images for validation as
# it is possible that the classifier has no simple linear readout.
# TODO(zimmerrol): also allow detector-like models here
# if the verify_valid_input_data_fn is used, this shouldn't be a
# concern anymore since all samples generated here have already passed
# the detector
with torch.no_grad():
if binary_classifier is not None:
y_pred = binary_classifier(x_features).argmax(-1).to("cpu")
else:
y_pred = linearized_model(x_images).argmax(-1).to("cpu")
# flag predictions for invalid data points such they are not
# counted as correctly classified samples
if verify_valid_inner_validation_data_fn is not None:
is_valid_input = verify_valid_inner_validation_data_fn(
x_images[y == 0]
)
y_pred[y == 0][~is_valid_input] = -1
if verify_valid_boundary_validation_data_fn is not None:
is_valid_input = verify_valid_boundary_validation_data_fn(
x_images[y == 1]
)
y_pred[y == 1][~is_valid_input] = -1