-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.py
1699 lines (1449 loc) · 56 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
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
"""From https://github.com/clvrai/ACGAN-PyTorch"""
import os
import numpy as np
import sys
from functools import partial, cache
import itertools
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
from tqdm import tqdm
from torchvision.utils import save_image
import torchvision.transforms as transforms
from torchvision import datasets
from loss import mag_calc
import torchvision.models as tv_models
from resnet.models.resnet import ResNet18, ResNet50
from network import _netG, _netG_CIFAR10
import models
from loaders import SingleClassSampler
import argparse
from robustness.model_utils import make_and_restore_model
from robustness.datasets import CIFAR
# gtsrb from https://zenodo.org/record/3490959 and
# https://github.com/JayanthRR/german-traffic-sign-classification
from model import Net as gtsrb_net
from gtsrb_aug import (
data_transforms,
data_jitter_hue,
data_jitter_brightness,
data_jitter_saturation,
data_jitter_contrast,
data_rotate,
data_hvflip,
data_shear,
data_translate,
data_center,
data_hflip,
data_vflip,
)
from torch.utils.data import Dataset
import pandas as pd
from PIL import Image
from cifar_resnet import cifar_resnet56, cifar_resnet32, cifar_resnet20, cifar_resnet44
TRAIN_TYPES = ["gan", "victim", "samples", "threshold"]
class Warnings:
def __init__(self, print_fun=print):
self.print_fun = print_fun
self.warned = dict()
def __call__(self, warn_id: str, warn_msg: str) -> None:
if warn_id in self.warned:
return
self.warned[warn_id] = True
self.print_fun(f"\n[WARN] {warn_id}: {warn_msg}\n")
WARN_ONCE = Warnings(print_fun=tqdm.write)
def setup_args(mode):
assert mode in TRAIN_TYPES, f"{mode} not found in {TRAIN_TYPES}"
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
#### GENERATOR OPTIONS ####
gen_group = parser.add_argument_group("Generator Options")
gen_group.add_argument(
"--clip",
type=float,
default=2.0,
help="min/max ratio to clip the triggers with (any value >1.0 has no effect)",
)
gen_group.add_argument(
"--baseline",
action="store_true",
help="Use random noise instead of a generator",
)
#### OUTPUT OPTIONS ####
out_group = parser.add_argument_group("Output Options")
out_group.add_argument(
"--output_directory",
"-d",
default="./results",
help="Directory to save output files to",
)
#### DATASET OPTIONS ####
dat_group = parser.add_argument_group("Dataset Options")
avail_sets = [
"cifar10",
"cifar10_r56",
"cifar10_r44",
"cifar10_r32",
"cifar10_r20",
"cifar10_pgd",
"imagenet",
"svhn",
"gtsrb",
]
dat_group.add_argument("--data-dir", type=str, default="/scratch/jose/data")
dat_group.add_argument("--idx-dir", type=str, default="/scratch/jose/data")
dat_group.add_argument(
"--num_workers",
type=int,
default=12,
help="Number of workers to load the dataset with",
)
dat_group.add_argument(
"--dataset",
default="cifar10",
choices=avail_sets,
help="Choose dataset/model [ " + " | ".join(avail_sets) + " ]",
)
if mode == "gan":
gen_group.add_argument(
"--norm-type", type=str, default="L2", choices=["L2", "Linf"]
)
gen_group.add_argument(
"--latent-dim",
type=int,
default=110,
help="dimensionality of the generator latent "
"space Should match generator configuration",
)
gen_group.add_argument(
"--target-label", "-t", type=int, default=0, help="Target label"
)
train_group = parser.add_argument_group("Generator Training Options")
train_group.add_argument(
"--lr", type=float, default=0.01, help="adam: learning rate"
)
train_group.add_argument(
"--gamma", type=float, default=0.1, help="adam: learning rate decay"
)
train_group.add_argument("--step-size", "-s", type=int, default=200)
train_group.add_argument(
"--n-epochs",
"-e",
type=int,
default=400,
help="number of epochs of training",
)
train_group.add_argument(
"--batch-size", "-bs", type=int, default=10, help="size of the batches"
)
train_group.add_argument(
"--cutoff",
"-c",
type=float,
default=20,
help="Point at which to invert margin loss",
)
train_group.add_argument(
"--cutoff_range",
"-cr",
type=float,
default=10,
help="Range around which to invert margin loss",
)
loss_group = parser.add_mutually_exclusive_group(required=True)
loss_group.add_argument("--margin_loss", "-m", action="store_true")
loss_group.add_argument("--bbox_loss", "-b", action="store_true")
loss_group.add_argument("--base_loss", action="store_true")
optim_group = parser.add_argument_group("Optimizer Options")
optim_group.add_argument(
"--b1", type=float, default=0.5, help="adam: decay of first order momentum"
)
optim_group.add_argument(
"--b2",
type=float,
default=0.999,
help="adam: decay of second order momentum",
)
dat_group.add_argument(
"--sample-interval",
"-i",
type=int,
default=100,
help="interval between image sampling",
)
dat_group.add_argument(
"--threshold",
type=float,
default=0.3,
help="When to consider a sample selected",
)
elif mode == "victim":
atk_group = parser.add_argument_group("Attack Options")
atk_group.add_argument(
"bias", type=float, help="Number of samples which are biased"
)
atk_group.add_argument(
"scale", type=float, default=1.0, help="Amount to scale mask by"
)
gen_group.add_argument(
"generator", type=str, help="Generator to use for trojan creation"
)
atk_group.add_argument(
"--multi-scale",
action="store_true",
help="Use multiple scales during training",
)
atk_group.add_argument(
"--max-scale", default="20", type=float, help="Max scale to test at"
)
atk_group.add_argument(
"--test-scale",
type=float,
default=2.0,
help="Amount to scale mask by during testing",
)
atk_group.add_argument("--run-info", type=str, default="r0", help="Run info")
atk_group.add_argument(
"--epochs", type=int, default=9, help="Epochs to train for"
)
train_group = parser.add_argument_group("Training Options")
train_group.add_argument(
"--batch_size",
type=int,
default=128,
help="batch size to train the victim with",
)
train_group.add_argument(
"--weight-decay",
"--wd",
default=5e-4,
type=float,
metavar="W",
help="weight decay (default: 5e-4 for cifar)",
)
train_group.add_argument(
"--lr",
type=float,
default=0.0001,
help="Learning rate to use during training",
)
train_group.add_argument(
"--pre-atk-delay",
type=int,
default=0,
help="Number of epochs to delay adversarial"
"training by (performs non-adversarial"
"training during these epochs)",
)
train_group.add_argument(
"--post-atk-delay",
type=int,
default=0,
help="Number of epochs to delay adversarial"
"exploit by (performs non-adversarial"
"training during these epochs)",
)
elif mode == "samples" or mode == "threshold":
gen_group.add_argument(
"generator", type=str, help="Generator to use for trojan creation"
)
out_group = parser.add_argument_group("Sampler options")
out_group.add_argument(
"--batch_size",
type=int,
default=1,
help="number of samples per output image",
)
out_group.add_argument(
"--train_set",
action="store_true",
help="Generate samples using train set " "instead of test set",
)
out_group.add_argument("--clean", action="store_true")
out_group.add_argument(
"--limit",
default=None,
type=int,
help="Maximum number of images to generate",
)
out_group.add_argument(
"--skip", default=None, type=int, help="Number of images to skip at start"
)
out_group.add_argument(
"--no-noise",
action="store_true",
help="Do not generate trigger-only images ("
"these are _only_ the trigger on a black "
"background)",
)
out_group.add_argument(
"--no-mult", action="store_true", help="Save only the base scales"
)
out_group.add_argument(
"--selectable-only",
action="store_true",
help="Only generate samples which are " "selectable",
)
else:
raise NotImplementedError
if mode == "samples":
gen_group.add_argument("--target-class", "-t", help="Image class to generate")
gen_group.add_argument(
"--only-print-totals", action="store_true", help="Quit after showing totals"
)
opt = parser.parse_args()
if mode == "samples":
opt.bias = 1.0
elif mode == "gan":
assert opt.cutoff > 0
assert opt.cutoff_range > 0
print(f"Mode: {mode}, {opt}")
if opt.baseline:
opt.clip = 1
return opt
def evaluate(stats, generator, victim, opts, **kwargs):
"""Evaluate and gather statistics about the adversarial status of the
network.
Iterates over the entire validation set, a single class at a time"""
bias = kwargs.get("bias") or 1.0
epochs = kwargs.get("epochs") or 1
clip = kwargs.get("clip") or 2.0
if clip < 1.0:
WARN_ONCE("clp_in_eval", "Clipping is being used in evaluate")
device = kwargs.get("device") or get_device()[0]
if opts["cutoff"] is not None: # if none, a baseline run
upper_bound = opts["cutoff"] + opts["cutoff_range"]
else:
upper_bound = None
normalize = opts["normalize"]
loader = opts["test_loader"]
target_label = opts["target_label"]
gseed = opts["noise_gen"]
num_samples = opts["num_samples"]
with torch.no_grad():
with tqdm(loader, unit="Batches", desc="Testing", dynamic_ncols=True) as tbar:
for imgs, lbls in tbar:
for cfig in stats:
mask, _ = get_trig(
generator, imgs.size(0), gseed, opts["norm_type"], upper_bound
)
# apply clipping
max_clip = torch.max(mask).detach() * clip
min_clip = torch.min(mask).detach() * clip
mask[mask < min_clip] = min_clip
mask[mask > max_clip] = max_clip
mask *= cfig[0]
# resize mask for _batch size_
if mask.size(0) == 1:
fake_img = imgs.to(device) + torch.cat(imgs.size(0) * [mask])
elif mask.size(0) > imgs.size(0): # trim mask
fake_img = imgs.to(device) + mask[: imgs.size(0)]
elif mask.size(0) < imgs.size(0): # extend mask
fake_img = imgs.to(device)
steps = imgs.size(0) // mask.size(0)
for step in range(steps):
s_idx = mask.size(0) * step
fake_img[s_idx : s_idx + mask.size(0)] += mask
fake_img[steps * mask.size(0) :] += mask[
: (imgs.size(0) - steps * mask.size(0))
]
else: # size match, just apply
fake_img = imgs.to(device) + mask
# generate triggered images and baseline images
if not kwargs["pgd"]:
fake_img = torch.stack(list(map(normalize, fake_img)))
nor_img = torch.stack(list(map(normalize, imgs.to(device))))
else: # pgd
nor_img = imgs.to(device)
f_out = victim(fake_img)
r_out = victim(nor_img)
# accumulate stats
cfig[1].accumulate(f_out, r_out, lbls)
maxSr_config = -1
maxSr = stats[-1][1].success_rate
for cfg, stat in enumerate(stats):
if stat[1].success_rate > maxSr:
maxSr_config = cfg
maxSr = stat[1].success_rate
tbar.set_postfix(
sr=f"{stats[maxSr_config][1].success_rate}%",
t1=f"{stats[maxSr_config][1].top[1]}%",
t5=f"{stats[maxSr_config][1].top[5]}%",
srt=f"{stats[maxSr_config][1].p2targ[target_label]}%",
s=",".join(f"{s[0]:.2f}" for s in stats),
)
e_str = f" {str(stats[0][1].epoch)}/{epochs} epochs "
tqdm.write(e_str.center(len(e_str) + 32, "-"))
tqdm.write(f"Maximum {int(num_samples)} samples ({100*bias:.1f}%)")
for cfig in stats:
c_str = f" {cfig[0]} "
tqdm.write(c_str.center(len(c_str) + 6, "-"))
tqdm.write(cfig[1].show_stats())
def check_range(opt, low_coff, high_coff, low_range, high_range):
coff = coff_r = ""
if opt.cutoff < low_coff:
coff = "small"
if opt.cutoff > high_coff:
coff = "large"
if opt.cutoff_range < low_range:
coff_r = "small"
if opt.cutoff_range > high_range:
coff_r = "large"
if coff or coff_r:
WARN_ONCE("ranges", "Suggested ranges determined empirically")
if (
input(
f"{coff or coff_r} {opt.norm_type} "
f'{"cutoff" if coff else "range"}, continue? '
f"[y]/n"
).lower()
or "y"
) != "y":
print("Terminating")
sys.exit()
@cache
def outfile(_scale, target_label, args, prefix="train"):
"""Format outfile-log name based on the scale value"""
os.makedirs(args.output_directory, exist_ok=True)
outfile_name = (
f"{args.output_directory}/"
f"{prefix}_{args.dataset}_"
f"t{target_label}_"
f"{args.bias}_"
f"te{_scale * 100:.2f}_"
f"tr{args.scale * 100:.2f}_"
f"pre{args.pre_atk_delay}_"
f"post{args.pre_atk_delay}_"
f"{args.run_info}.csv"
)
return outfile_name
def weights_init(m):
"""custom weights initialization called on netG and netD"""
classname = m.__class__.__name__
if classname.find("Conv") != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find("BatchNorm") != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def get_trig(generator, batch_size, seed, norm_type, upper_bound):
gen_imgs = generator(seed())
if upper_bound is None:
return gen_imgs
m_mags = lambda g: mag_calc(g, norm_type)
exceeded = lambda g: (m_mags(g) > upper_bound).sum().item()
sample_count = 1
while exceeded(gen_imgs) > 0:
# fast path: attempt to clone trigger instead of resampling
if len(gen_imgs[m_mags(gen_imgs) < upper_bound]) > 0:
gen_imgs[m_mags(gen_imgs) > upper_bound] = gen_imgs[
m_mags(gen_imgs) < upper_bound
][0]
break
if sample_count == 10:
tqdm.write(
f"[WARN] Resampling very often, consider "
f"changing parameters! (okay if epoch is 0)"
)
if sample_count == 100:
tqdm.write(
f"[WARN] Failed to sample a single viable "
f"candidate in 100 rounds. Giving up. (okay if epoch "
f"is 0)"
)
return None, None
gen_imgs = generator(seed())
sample_count += 1
# resize mask for _batch size_
if gen_imgs.size(0) == 1:
gen_imgs = torch.cat(batch_size * [gen_imgs])
elif gen_imgs.size(0) > batch_size: # trim mask
gen_imgs = gen_imgs[:batch_size]
elif gen_imgs.size(0) < batch_size: # extend mask
extended = list()
gen_it = itertools.cycle(gen_imgs)
while len(extended) < batch_size:
extended.append(next(gen_it))
gen_imgs = torch.stack(extended)
return gen_imgs, sample_count
class SampleImage:
"""Visualize trojans"""
def __init__(
self,
device,
dataset,
normalize,
prefix,
target,
noise_gen,
upper_bound,
norm_type,
):
self.device = device
self.dataset = dataset
self.normalize = normalize
self.path = f'{prefix.strip("/")}/images'
self.target = target
os.makedirs(self.path, exist_ok=True)
self.noise_gen = noise_gen
self.upper_bound = upper_bound
self.norm_type = norm_type
self.margin = 0.3
def gen_imgs(
self,
identifier,
target_loader,
generator,
clip,
scale=(1.0,),
img_limit=None,
skip=None,
victim=None,
):
"""Saves a grid of trojaned samples for the target class"""
generator.eval()
tqdm.write(f"Generating images at " f"{', '.join(f'{s:.2f}' for s in scale)}")
os.makedirs(f"{self.path}/{identifier}", exist_ok=True)
tqdm.write(
"\033[92m" + f"Saving images to {self.path}/{identifier}/" + "\u001b[0m"
)
with torch.no_grad():
with tqdm(
scale,
unit="Scale",
desc=f"Saving {identifier}",
position=0,
dynamic_ncols=True,
) as sbar:
for c_scale in sbar:
count = 0
with tqdm(
target_loader,
unit="Batches",
disable=len(target_loader) == 1,
mininterval=0.1,
position=1,
dynamic_ncols=True,
desc=f"Saving {c_scale:.2f}x",
total=img_limit,
) as bbar:
for idx, (imgs, _) in enumerate(bbar):
if skip is not None and idx < skip:
continue
# Sample trigger
gen_imgs, _ = get_trig(
generator,
imgs.size(0),
self.noise_gen,
self.norm_type,
self.upper_bound,
)
if gen_imgs is None:
return
# apply clipping
max_clip = torch.max(gen_imgs).detach() * clip
min_clip = torch.min(gen_imgs).detach() * clip
gen_imgs[gen_imgs < min_clip] = min_clip
gen_imgs[gen_imgs > max_clip] = max_clip
gen_imgs *= c_scale
# resize mask for _batch size_
if gen_imgs.size(0) == 1:
nimg = imgs.to(self.device) + torch.cat(
imgs.size(0) * [gen_imgs]
)
elif gen_imgs.size(0) > imgs.size(0): # trim mask
nimg = imgs.to(self.device) + gen_imgs[: imgs.size(0)]
elif gen_imgs.size(0) < imgs.size(0): # extend
nimg = imgs.to(self.device)
steps = imgs.size(0) // gen_imgs.size(0)
for step in range(steps):
s_idx = gen_imgs.size(0) * step
nimg[s_idx : s_idx + gen_imgs.size(0)] += gen_imgs
nimg[steps * gen_imgs.size(0) :] += gen_imgs[
: (imgs.size(0) - steps * gen_imgs.size(0))
]
else: # size match, just apply
nimg = imgs.to(self.device) + gen_imgs
if victim is not None: # only output selectable
# norm_img = torch.stack(list(map(self.normalize,
# nimg)))
confidences = victim(nimg)
top2 = torch.topk(F.softmax(confidences, dim=1), 2)
top2_sp = torch.split(top2[0], 1, dim=1)
margin = top2_sp[0] - top2_sp[1]
margin = margin.squeeze()
# if none are selectable, skip
if sum(margin > self.margin) == nimg.size(0):
continue
# zero out any non-selectable
nimg[margin > self.margin] = 0
count += 1
save_image(
nimg.data,
f"{self.path}/{identifier}/"
f"{self.dataset}"
f"_{self.target}"
f"_{identifier}"
f"_clp{clip if clip > 1.0 else 'NA'}"
f"_s{int(c_scale * 100)}_{idx}.png",
normalize=False,
)
if img_limit is not None and count >= img_limit:
bbar.close()
break
def gen_noise(self, identifier, generator, clip, scale=(1.0,)):
"""Saves only the noise, for inspection"""
generator.eval()
with torch.no_grad():
with tqdm(
scale,
unit="Scale",
desc=f"Saving {identifier} trigger",
position=0,
dynamic_ncols=True,
) as sbar:
# single image per scale
for c_scale in sbar:
# Sample noise
gen_imgs = generator(self.noise_gen())
# apply clipping
max_clip = torch.max(gen_imgs).detach() * clip
min_clip = torch.min(gen_imgs).detach() * clip
gen_imgs[gen_imgs < min_clip] = min_clip
gen_imgs[gen_imgs > max_clip] = max_clip
nimg = gen_imgs * c_scale
save_image(
nimg.data,
f"{self.path}/n_{self.dataset}"
f"_{self.target}"
f"_{identifier}"
f"_s{int(c_scale * 100)}.png",
normalize=False,
)
def gen_raw(self, target_loader, img_limit=None, skip=None):
"""saves the raw, unmodified images, for inspection"""
with torch.no_grad():
with tqdm(
target_loader,
unit="Batches",
dynamic_ncols=True,
position=0,
disable=len(target_loader) == 1,
desc="Saving",
total=img_limit,
) as bbar:
for idx, (imgs, _) in enumerate(bbar):
if skip is not None and idx < skip:
continue
save_image(
imgs.data,
f"{self.path}/"
f"raw"
f"_{self.dataset}"
f"_{self.target}"
f"_{idx}.png",
normalize=False,
)
if img_limit is not None and idx >= img_limit:
bbar.close()
break
class TrojanSampler:
def __init__(self, opt, device, generator, noise_gen):
self.target_label = opt.target_label
self.bias = opt.bias
self.device = device
self.generator = generator
self.noise_gen = noise_gen
def noise(self, batch):
"""Conditionally trojan samples, if they meet the right criteria..."""
imgs = []
lbls = []
self.generator.eval()
for sample in batch:
if sample[1] == self.target_label:
mask = self.generator(self.noise_gen())
imgs.append(sample[0] + mask[0])
else:
imgs.append(sample[0])
lbls.append(sample[1])
return torch.stack(imgs), torch.from_numpy(np.array(lbls))
class baseline_generator:
"""Baseline generator is just random noise
Generate _once_ to maximize likelihood of trojan success
"""
def __init__(self, opt, img_size, device):
"""compute constant noise to return"""
self.noise = Variable(torch.FloatTensor(opt.batch_size, 3, img_size, img_size))
self.noise.data.normal_(0, 1).to(device)
self.device = device
def eval(self):
"""Empty function, to maintain compatibility with GAN generator"""
return None
def __call__(self, *args, **kwargs):
"""Return precomputed noise"""
return self.noise.to(self.device)
class baseline_trojan:
"""Baseline generator is just random noise
Generate _once_ to maximize likelihood of trojan success
"""
def __init__(self, opt, img_size, device):
"""compute constant noise to return"""
self.noise = Variable(torch.FloatTensor(opt.batch_size, 3, img_size, img_size))
self.noise.data[:] = 0
self.noise.data[:, :, 4:12, 4:12] = 1
self.device = device
def eval(self):
"""Empty function, to maintain compatibility with GAN generator"""
return None
def __call__(self, *args, **kwargs):
"""Return precomputed noise"""
return self.noise.to(self.device)
def setup_dataset(opt, train_type):
"""Setup dataset based on name defined in opts"""
print(f"Setting up for {opt.dataset}")
assert train_type in TRAIN_TYPES, f"{train_type} not found in " f"{TRAIN_TYPES}"
return {
"imagenet": lambda: imagenet(opt, train_type),
"cifar10_r56": lambda: cifar10(opt, train_type, pgd=False, model_type=56),
"cifar10_r44": lambda: cifar10(opt, train_type, pgd=False, model_type=44),
"cifar10_r32": lambda: cifar10(opt, train_type, pgd=False, model_type=32),
"cifar10_r20": lambda: cifar10(opt, train_type, pgd=False, model_type=20),
"cifar10": lambda: cifar10(opt, train_type, pgd=False),
"cifar10_pgd": lambda: cifar10(opt, train_type, pgd=True),
"mnist": lambda: mnist(opt, train_type),
"cifar100": lambda: cifar100(opt, train_type),
"gtsrb": lambda: gtsrb(opt, train_type),
"svhn": lambda: svhn(opt, train_type),
}.get(opt.dataset, lambda: "Invalid dataset")()
def loader_setup(
train_type, train_set, test_set, opt, ldr_args, num_classes, idx_path, gen_opts
):
target_loader_test = DataLoader(
test_set,
batch_size=opt.batch_size,
shuffle=False,
sampler=SingleClassSampler(
gen_opts["target_label"], test_set, num_classes, f"{idx_path}_test"
),
**ldr_args,
)
if train_type == "victim":
train_loader = DataLoader(
train_set, batch_size=opt.batch_size, shuffle=True, **ldr_args
)
test_loader = DataLoader(test_set, batch_size=128, shuffle=False, **ldr_args)
target_loader_train = None
num_samples = int(len(train_set) / num_classes * opt.bias)
else:
train_loader = None
test_loader = DataLoader(
test_set, batch_size=opt.batch_size, shuffle=False, **ldr_args
)
target_loader_train = DataLoader(
train_set,
batch_size=opt.batch_size,
shuffle=False,
sampler=SingleClassSampler(
gen_opts["target_label"], train_set, num_classes, f"{idx_path}"
),
**ldr_args,
)
num_samples = None
return (
train_loader,
test_loader,
target_loader_train,
target_loader_test,
num_samples,
)
def get_gen(opt, train_type, img_size, device, gen_net, gen_noise):
if train_type != "gan" and opt.baseline:
print("Using a random trigger")
# generator = baseline_generator(opt, img_size, device)
generator = baseline_trojan(opt, img_size, device)
latent_dim = 110 # no effect
cutoff = cutoff_range = None
norm_type = "L2"
target_label = 3
else:
if train_type != "gan": # don't load if this is GAN training
ckpt = torch.load(opt.generator)
try:
target_label = ckpt.get("target")
if target_label is None:
target_label = input("Enter target (def=0): ") or 0
if train_type == "samples":
target_label = opt.target_class
latent_dim = (
ckpt.get("latent_dim")
or input("Enter latent_dim (def=110): ")
or 110
)
latent_dim = int(latent_dim)
norm_type = (
ckpt.get("norm_type") or input("Enter norm_type (def=L2): ") or "L2"
)
cutoff = (
ckpt.get("cutoff") or input("Enter cutoff value (def=20): ") or 20
)
cutoff_range = (
ckpt.get("cutoff_range")
or input("Enter range value (def=10): ")
or 10
)
target_label = int(target_label)
latent_dim = int(latent_dim)
cutoff = float(cutoff)
cutoff_range = float(cutoff_range)
except KeyboardInterrupt:
print("\nCanceled loading")
sys.exit()
print(
f"Using target={target_label}, latent_dim={latent_dim}"
f" cutoff={cutoff}, range={cutoff_range}, norm={norm_type}"
)
# this condition is triggered on loading a checkpoint with a
# missing entry; and gives the option to update the checkpoint
if (
(target_label != ckpt.get("target") and train_type != "samples")
or latent_dim != ckpt.get("latent_dim")
or cutoff != ckpt.get("cutoff")
or norm_type != ckpt.get("norm_type")
or cutoff_range != ckpt.get("cutoff_range")
):
try:
overwrite = input("Save? [Y/n]").capitalize() or "Y"
except KeyboardInterrupt:
sys.exit()
if overwrite == "Y":
torch.save(
{
"net": ckpt["net"],
"cutoff": cutoff,
"target": target_label,
"latent_dim": latent_dim,
"norm_type": norm_type,
"cutoff_range": cutoff_range,
},
opt.generator,
)
print(f"Updated {opt.generator}")
else:
print(f"Did not update {opt.generator}")
generator = gen_net(latent_dim).to(device)
generator.load_state_dict(ckpt["net"])
else: # training a new generator
target_label = opt.target_label
latent_dim = opt.latent_dim
cutoff = opt.cutoff
cutoff_range = opt.cutoff_range
norm_type = opt.norm_type
generator = gen_net(latent_dim).to(device)
assert norm_type in ["L2", "Linf"]
return generator, {
"cutoff": cutoff,
"cutoff_range": cutoff_range,
"target_label": target_label,
"latent_dim": latent_dim,
"norm_type": norm_type,
}
def get_device():
cuda = torch.cuda.is_available()
if cuda:
device = torch.device("cuda")
else: