-
Notifications
You must be signed in to change notification settings - Fork 71
/
inputaware.py
executable file
·1145 lines (968 loc) · 50.1 KB
/
inputaware.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
'''
Input-aware dynamic backdoor attack
This file is modified based on the following source:
link : https://github.com/VinAIResearch/input-aware-backdoor-attack-release
@article{nguyen2020input,
title={Input-aware dynamic backdoor attack},
author={Nguyen, Tuan Anh and Tran, Anh},
journal={Advances in Neural Information Processing Systems},
volume={33},
pages={3454--3464},
year={2020}
}
The original license is placed at the end of this file.
The update include:
1. data preprocess and dataset setting
2. model setting
3. args and config
4. during training the backdoor attack generalization to lower poison ratio (generalize_to_lower_pratio)
5. calculate part of ASR
6. save process
basic sturcture for main:
1. config args, save_path, fix random seed
2. set the device, model, criterion, optimizer, training schedule.
3. set the clean train data and clean test data
4. clean train 25 epochs
5. training with backdoor modification simultaneously
6. save attack result
Note that since this attack rely on batch-wise modification of the input data,
when this method encounters lower poison ratio, the original implementation
will fail (poison ratio < 1 / batch size), we add a function named generalize_to_lower_pratio
to generalize the attack to lower the poison ratio. The basic idea is to calculate the theoretical
the number of poison samples each batch should have, then randomly select batches to do poisoning.
This change may result in instability and a higher variance in final
results' metrics, but it is a necessary change to make the attack workable in a low poison ratio.
Please be careful when you use this attack in a low poison ratio, and interpret the results with
caution.
'''
import argparse
import logging
import os
import sys
import time
import torch
sys.path = ["./"] + sys.path
import numpy as np
from copy import deepcopy
import torch.nn as nn
from torch.utils.data import DataLoader
import torch.nn.functional as F
from attack.badnet import BadNet, add_common_attack_args
from torchvision import transforms
from utils.aggregate_block.dataset_and_transform_generate import get_dataset_normalization, get_dataset_denormalization
from utils.aggregate_block.model_trainer_generate import generate_cls_model
from utils.trainer_cls import Metric_Aggregator
from utils.save_load_attack import save_attack_result
from utils.trainer_cls import all_acc, given_dataloader_test, general_plot_for_epoch
from utils.bd_dataset_v2 import prepro_cls_DatasetBD_v2, dataset_wrapper_with_transform
term_width = int(60)
TOTAL_BAR_LENGTH = 65.0
last_time = time.time()
begin_time = last_time
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH * current / total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(" [")
for i in range(cur_len):
sys.stdout.write("=")
sys.stdout.write(">")
for i in range(rest_len):
sys.stdout.write(".")
sys.stdout.write("]")
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
if msg:
L.append(" | " + msg)
msg = "".join(L)
sys.stdout.write(msg)
for i in range(term_width - int(TOTAL_BAR_LENGTH) - len(msg) - 3):
sys.stdout.write(" ")
# Go back to the center of the bar.
for i in range(term_width - int(TOTAL_BAR_LENGTH / 2) + 2):
sys.stdout.write("\b")
sys.stdout.write(" %d/%d " % (current + 1, total))
if current < total - 1:
sys.stdout.write("\r")
else:
sys.stdout.write("\n")
sys.stdout.flush()
class Conv2dBlock(nn.Module):
def __init__(self, in_c, out_c, ker_size=(3, 3), stride=1, padding=1, batch_norm=True, relu=True):
super(Conv2dBlock, self).__init__()
self.conv2d = nn.Conv2d(in_c, out_c, ker_size, stride, padding)
if batch_norm:
self.batch_norm = nn.BatchNorm2d(out_c, eps=1e-5, momentum=0.05, affine=True)
if relu:
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
for module in self.children():
x = module(x)
return x
class DownSampleBlock(nn.Module):
def __init__(self, ker_size=(2, 2), stride=2, dilation=(1, 1), ceil_mode=False, p=0.0):
super(DownSampleBlock, self).__init__()
self.maxpooling = nn.MaxPool2d(kernel_size=ker_size, stride=stride, dilation=dilation, ceil_mode=ceil_mode)
if p:
self.dropout = nn.Dropout(p)
def forward(self, x):
for module in self.children():
x = module(x)
return x
class UpSampleBlock(nn.Module):
def __init__(self, scale_factor=(2, 2), mode="bilinear", p=0.0):
super(UpSampleBlock, self).__init__()
self.upsample = nn.Upsample(scale_factor=scale_factor, mode=mode)
if p:
self.dropout = nn.Dropout(p)
def forward(self, x):
for module in self.children():
x = module(x)
return x
class Normalize:
def __init__(self, args, expected_values, variance):
self.n_channels = args.input_channel
self.expected_values = expected_values
self.variance = variance
assert self.n_channels == len(self.expected_values)
def __call__(self, x):
x_clone = x.clone()
for channel in range(self.n_channels):
x_clone[:, channel] = (x[:, channel] - self.expected_values[channel]) / self.variance[channel]
return x_clone
class Denormalize:
def __init__(self, args, expected_values, variance):
self.n_channels = args.input_channel
self.expected_values = expected_values
self.variance = variance
assert self.n_channels == len(self.expected_values)
def __call__(self, x):
x_clone = x.clone()
for channel in range(self.n_channels):
x_clone[:, channel] = x[:, channel] * self.variance[channel] + self.expected_values[channel]
return x_clone
class InputAwareGenerator(nn.Sequential):
def __init__(self, args, out_channels=None):
super(InputAwareGenerator, self).__init__()
self.args = args
if self.args.dataset == "mnist":
channel_init = 16
steps = 2
else:
channel_init = 32
steps = 3
channel_current = self.args.input_channel
channel_next = channel_init
for step in range(steps):
self.add_module("convblock_down_{}".format(2 * step), Conv2dBlock(channel_current, channel_next))
self.add_module("convblock_down_{}".format(2 * step + 1), Conv2dBlock(channel_next, channel_next))
self.add_module("downsample_{}".format(step), DownSampleBlock())
if step < steps - 1:
channel_current = channel_next
channel_next *= 2
self.add_module("convblock_middle", Conv2dBlock(channel_next, channel_next))
channel_current = channel_next
channel_next = channel_current // 2
for step in range(steps):
self.add_module("upsample_{}".format(step), UpSampleBlock())
self.add_module("convblock_up_{}".format(2 * step), Conv2dBlock(channel_current, channel_current))
if step == steps - 1:
self.add_module(
"convblock_up_{}".format(2 * step + 1), Conv2dBlock(channel_current, channel_next, relu=False)
)
else:
self.add_module("convblock_up_{}".format(2 * step + 1), Conv2dBlock(channel_current, channel_next))
channel_current = channel_next
channel_next = channel_next // 2
if step == steps - 2:
if out_channels is None:
channel_next = self.args.input_channel
else:
channel_next = out_channels
self._EPSILON = 1e-7
self._normalizer = self._get_normalize(self.args)
self._denormalizer = self._get_denormalize(self.args)
self.tanh = nn.Tanh()
def _get_denormalize(self, args):
denormalizer = Denormalize(args, get_dataset_normalization(self.args.dataset).mean,
get_dataset_normalization(self.args.dataset).std)
return denormalizer
def _get_normalize(self, args):
normalizer = Normalize(args, get_dataset_normalization(self.args.dataset).mean,
get_dataset_normalization(self.args.dataset).std)
return normalizer
def forward(self, x):
for module in self.children():
x = module(x)
x = nn.Tanh()(x) / (2 + self._EPSILON) + 0.5
return x
def normalize_pattern(self, x):
if self._normalizer:
x = self._normalizer(x)
return x
def denormalize_pattern(self, x):
if self._denormalizer:
x = self._denormalizer(x)
return x
def threshold(self, x):
return nn.Tanh()(x * 20 - 10) / (2 + self._EPSILON) + 0.5
class Threshold(nn.Module):
def __init__(self):
super(Threshold, self).__init__()
self.tanh = nn.Tanh()
def forward(self, x):
return self.tanh(x * 20 - 10) / (2 + 1e-7) + 0.5
def generalize_to_lower_pratio(pratio, bs):
if pratio * bs >= 1:
# the normal case that each batch can have at least one poison sample
return pratio * bs
else:
# then randomly return number of poison sample
if np.random.uniform(0,
1) < pratio * bs: # eg. pratio = 1/1280, then 1/10 of batch(bs=128) should contains one sample
return 1
else:
return 0
class InputAware(BadNet):
def __init__(self):
super(InputAware, self).__init__()
def set_bd_args(cls, parser: argparse.ArgumentParser) -> argparse.ArgumentParser:
parser = add_common_attack_args(parser)
parser.add_argument('--bd_yaml_path', type=str, default='./config/attack/inputaware/default.yaml',
help='path for yaml file provide additional default attributes')
parser.add_argument("--lr_G", type=float, ) # default=1e-2)
parser.add_argument("--lr_C", type=float, ) # default=1e-2)
parser.add_argument("--lr_M", type=float, ) # default=1e-2)
parser.add_argument('--C_lr_scheduler', type=str)
parser.add_argument("--schedulerG_milestones", type=list, ) # default=[200, 300, 400, 500])
parser.add_argument("--schedulerC_milestones", type=list, ) # default=[100, 200, 300, 400])
parser.add_argument("--schedulerM_milestones", type=list, ) # default=[10, 20])
parser.add_argument("--schedulerG_lambda", type=float, ) # default=0.1)
parser.add_argument("--schedulerC_lambda", type=float, ) # default=0.1)
parser.add_argument("--schedulerM_lambda", type=float, ) # default=0.1)
parser.add_argument("--lambda_div", type=float, ) # default=1)
parser.add_argument("--lambda_norm", type=float, ) # default=100)
parser.add_argument("--mask_density", type=float, ) # default=0.032)
parser.add_argument("--EPSILON", type=float, ) # default=1e-7)
parser.add_argument('--clean_train_epochs', type=int)
parser.add_argument("--random_rotation", type=int, ) # default=10)
parser.add_argument("--random_crop", type=int, ) # default=5)
return parser
def stage1_non_training_data_prepare(self):
logging.info("stage1 start")
assert "args" in self.__dict__
args = self.args
train_dataset_without_transform, \
train_img_transform, \
train_label_transform, \
test_dataset_without_transform, \
test_img_transform, \
test_label_transform, \
clean_train_dataset_with_transform, \
clean_train_dataset_targets, \
clean_test_dataset_with_transform, \
clean_test_dataset_targets \
= self.benign_prepare()
clean_train_dataloader1 = DataLoader(clean_train_dataset_with_transform, pin_memory=args.pin_memory,
batch_size=args.batch_size, num_workers=args.num_workers, shuffle=True)
clean_train_dataloader2 = DataLoader(clean_train_dataset_with_transform, pin_memory=args.pin_memory,
batch_size=args.batch_size, num_workers=args.num_workers, shuffle=True)
clean_test_dataloader1 = DataLoader(clean_test_dataset_with_transform, pin_memory=args.pin_memory,
batch_size=args.batch_size,
num_workers=args.num_workers, shuffle=True)
clean_test_dataloader2 = DataLoader(clean_test_dataset_with_transform, pin_memory=args.pin_memory,
batch_size=args.batch_size,
num_workers=args.num_workers, shuffle=True)
self.stage1_results = clean_train_dataset_with_transform, clean_train_dataloader1, \
clean_train_dataloader2, \
clean_test_dataset_with_transform, \
clean_test_dataloader1, \
clean_test_dataloader2
def stage2_training(self):
# since we need the network to do poison,
# we can only put prepare of bd dataset to stage2 with training process.
logging.info(f"stage2 start")
assert 'args' in self.__dict__
args = self.args
agg = Metric_Aggregator()
clean_train_dataset_with_transform, \
clean_train_dataloader1, \
clean_train_dataloader2, \
clean_test_dataset_with_transform, \
clean_test_dataloader1, \
clean_test_dataloader2 = self.stage1_results
self.device = torch.device(
(
f"cuda:{[int(i) for i in args.device[5:].split(',')][0]}" if "," in args.device else args.device
) if torch.cuda.is_available() else "cpu"
)
netC = generate_cls_model(
model_name=args.model,
num_classes=args.num_classes,
image_size=args.img_size[0],
).to(self.device, non_blocking=args.non_blocking)
netG = InputAwareGenerator(args).to(self.device, non_blocking=args.non_blocking)
netM = InputAwareGenerator(args, out_channels=1).to(self.device, non_blocking=args.non_blocking)
self.threshold = Threshold().to(self.device, non_blocking=args.non_blocking)
if "," in args.device:
netC = torch.nn.DataParallel(
netC,
device_ids=[int(i) for i in args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
)
netG = torch.nn.DataParallel(
netG,
device_ids=[int(i) for i in args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
)
netM = torch.nn.DataParallel(
netM,
device_ids=[int(i) for i in args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
)
self.threshold = torch.nn.DataParallel(
self.threshold,
device_ids=[int(i) for i in args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
)
optimizerC = torch.optim.SGD(netC.parameters(), args.lr_C, momentum=0.9, weight_decay=5e-4)
optimizerG = torch.optim.Adam(netG.parameters(), args.lr_G, betas=(0.5, 0.9))
optimizerM = torch.optim.Adam(netM.parameters(), args.lr_M, betas=(0.5, 0.9))
if args.C_lr_scheduler == "ReduceLROnPlateau":
schedulerC = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizerC)
else:
schedulerC = torch.optim.lr_scheduler.MultiStepLR(optimizerC, args.schedulerC_milestones,
args.schedulerC_lambda)
schedulerG = torch.optim.lr_scheduler.MultiStepLR(optimizerG, args.schedulerG_milestones,
args.schedulerG_lambda)
schedulerM = torch.optim.lr_scheduler.MultiStepLR(optimizerM, args.schedulerM_milestones,
args.schedulerM_lambda)
self.normalizer = Normalize(args, get_dataset_normalization(self.args.dataset).mean,
get_dataset_normalization(self.args.dataset).std)
epoch = 1
# first clean_train_epochs epoch clean train
for i in range(args.clean_train_epochs):
netM.train()
logging.info(
"Epoch {} - {} - {} | mask_density: {} - lambda_div: {} - lambda_norm: {}:".format(
epoch, args.dataset, args.attack_label_trans, args.mask_density, args.lambda_div, args.lambda_norm
)
)
self.train_mask_step(
netM, optimizerM, schedulerM, clean_train_dataloader1, clean_train_dataloader2, args
)
epoch = self.eval_mask(netM, optimizerM, schedulerM, clean_test_dataloader1, clean_test_dataloader2, epoch,
args)
if args.frequency_save != 0 and epoch % args.frequency_save == args.frequency_save - 1:
logging.info(f'saved. epoch:{epoch}')
state_dict = {
"netM": netM.state_dict(),
"optimizerM": optimizerM.state_dict(),
"schedulerM": schedulerM.state_dict(),
"epoch": epoch,
"args": args,
}
torch.save(state_dict, args.save_path + "/mask_state_dict.pt")
epoch += 1
netM.eval()
netM.requires_grad_(False)
# real train (attack)
train_loss_list = []
train_mix_acc_list = []
train_clean_acc_list = []
train_asr_list = []
train_ra_list = []
train_cross_acc_only_list = []
clean_test_loss_list = []
bd_test_loss_list = []
cross_test_loss_list = []
ra_test_loss_list = []
test_acc_list = []
test_asr_list = []
test_ra_list = []
test_cross_acc_list = []
for i in range(args.epochs):
logging.info(
"Epoch {} - {} - {} | mask_density: {} - lambda_div: {}:".format(
epoch, args.dataset, args.attack_label_trans, args.mask_density, args.lambda_div
)
)
train_epoch_loss_avg_over_batch, \
train_mix_acc, \
train_clean_acc, \
train_asr, \
train_ra, \
train_cross_acc = self.train_step(
netC,
netG,
netM,
optimizerC,
optimizerG,
schedulerC,
schedulerG,
clean_train_dataloader1,
clean_train_dataloader2,
args,
)
clean_test_loss_avg_over_batch, \
bd_test_loss_avg_over_batch, \
cross_test_loss_avg_over_batch, \
ra_test_loss_avg_over_batch, \
test_acc, \
test_asr, \
test_ra, \
test_cross_acc = self.eval_step(
netC,
netG,
netM,
optimizerC,
optimizerG,
schedulerC,
schedulerG,
clean_test_dataset_with_transform,
epoch,
args,
)
agg({
"epoch": epoch,
"train_epoch_loss_avg_over_batch": train_epoch_loss_avg_over_batch,
"train_acc": train_mix_acc,
"train_acc_clean_only": train_clean_acc,
"train_asr_bd_only": train_asr,
"train_ra_bd_only": train_ra,
"train_cross_acc_only": train_cross_acc,
"clean_test_loss_avg_over_batch": clean_test_loss_avg_over_batch,
"bd_test_loss_avg_over_batch": bd_test_loss_avg_over_batch,
"cross_test_loss_avg_over_batch": cross_test_loss_avg_over_batch,
"ra_test_loss_avg_over_batch": ra_test_loss_avg_over_batch,
"test_acc": test_acc,
"test_asr": test_asr,
"test_ra": test_ra,
"test_cross_acc": test_cross_acc,
})
if args.frequency_save != 0 and epoch % args.frequency_save == args.frequency_save - 1:
logging.info(f'saved. epoch:{epoch}')
state_dict = {
"netC": netC.state_dict(),
"netG": netG.state_dict(),
"netM": netM.state_dict(),
"optimizerC": optimizerC.state_dict(),
"optimizerG": optimizerG.state_dict(),
"schedulerC": schedulerC.state_dict(),
"schedulerG": schedulerG.state_dict(),
"epoch": epoch,
"args": args,
}
torch.save(state_dict, args.save_path + "/netCGM.pt")
train_loss_list.append(train_epoch_loss_avg_over_batch)
train_mix_acc_list.append(train_mix_acc)
train_clean_acc_list.append(train_clean_acc)
train_asr_list.append(train_asr)
train_ra_list.append(train_ra)
train_cross_acc_only_list.append(train_cross_acc)
clean_test_loss_list.append(clean_test_loss_avg_over_batch)
bd_test_loss_list.append(bd_test_loss_avg_over_batch)
cross_test_loss_list.append(cross_test_loss_avg_over_batch)
ra_test_loss_list.append(ra_test_loss_avg_over_batch)
test_acc_list.append(test_acc)
test_asr_list.append(test_asr)
test_ra_list.append(test_ra)
test_cross_acc_list.append(test_cross_acc)
general_plot_for_epoch(
{
"Train Acc": train_mix_acc_list,
"Train Acc (clean sample only)": train_clean_acc_list,
"Train ASR": train_asr_list,
"Train RA": train_ra_list,
"Train Cross Acc": train_cross_acc_only_list,
"Test C-Acc": test_acc_list,
"Test ASR": test_asr_list,
"Test RA": test_ra_list,
"Test Cross Acc": test_cross_acc_list,
},
save_path=f"{args.save_path}/acc_like_metric_plots.png",
ylabel="percentage",
)
general_plot_for_epoch(
{
"Train Loss": train_loss_list,
"Test Clean Loss": clean_test_loss_list,
"Test Backdoor Loss": bd_test_loss_list,
"Test Cross Loss": cross_test_loss_list,
"Test RA Loss": ra_test_loss_list,
},
save_path=f"{args.save_path}/loss_metric_plots.png",
ylabel="percentage",
)
epoch += 1
if epoch > args.epochs:
break
agg.to_dataframe().to_csv(f"{args.save_path}/attack_df.csv")
agg.summary().to_csv(f"{args.save_path}/attack_df_summary.csv")
### save the poison train data for inputaware
bd_train_dataset = prepro_cls_DatasetBD_v2(
clean_train_dataset_with_transform.wrapped_dataset,
save_folder_path=f"{args.save_path}/bd_train_dataset"
)
transforms_reversible = transforms.Compose(
list(
filter(
lambda x: isinstance(x, (transforms.Normalize, transforms.Resize, transforms.ToTensor)),
deepcopy(clean_train_dataset_with_transform.wrap_img_transform.transforms)
)
)
)
clean_train_dataset_with_transform.wrap_img_transform = transforms_reversible # change it to reversiable
# get denormalizer
for trans_t in deepcopy(transforms_reversible.transforms):
if isinstance(trans_t, transforms.Normalize):
denormalizer = get_dataset_denormalization(trans_t)
logging.info(f"{denormalizer}")
clean_train_dataloader_without_shuffle = torch.utils.data.DataLoader(clean_train_dataset_with_transform,
batch_size=args.batch_size,
pin_memory=args.pin_memory,
num_workers=args.num_workers,
shuffle=False)
clean_train_dataloader2.dataset.wrap_img_transform = transforms_reversible
# change it to reversiable, notice that this dataloader is shuffled, since we need img2 is different from img1 (whose dataloader is not shuffled)
netC.eval()
netG.eval()
with torch.no_grad():
for batch_idx, (inputs1, targets1), (inputs2, targets2) in zip(
range(len(clean_train_dataloader_without_shuffle)),
clean_train_dataloader_without_shuffle,
clean_train_dataloader2):
inputs1, targets1 = inputs1.to(self.device, non_blocking=args.non_blocking), targets1.to(self.device,
non_blocking=args.non_blocking)
inputs2, targets2 = inputs2.to(self.device, non_blocking=args.non_blocking), targets2.to(self.device,
non_blocking=args.non_blocking)
num_bd = int(generalize_to_lower_pratio(args.pratio, inputs1.shape[0])) # int(args.pratio * bs)
num_cross = num_bd
inputs_bd, targets_bd, patterns1, masks1 = self.create_bd(inputs1[:num_bd], targets1[:num_bd], netG,
netM, args,
'train')
inputs_cross, patterns2, masks2 = self.create_cross(
inputs1[num_bd: num_bd + num_cross], inputs2[num_bd: num_bd + num_cross], netG, netM, args,
)
input_changed = torch.cat([inputs_bd, inputs_cross, ], dim=0)
# input_changed = p_transforms(input_changed) # this is non-reversible part, should not be included
input_changed = denormalizer( # since we normalized once, we need to denormalize it back.
input_changed
).detach().clone().cpu()
target_changed = torch.cat([targets_bd, targets1[num_bd: (num_bd + num_cross)], ],
dim=0).detach().clone().cpu()
# save to the container
for idx_in_batch, t_img in enumerate(
input_changed
):
# here we know it starts from 0 and they are consecutive
bd_train_dataset.set_one_bd_sample(
selected_index=int(batch_idx * int(args.batch_size) + idx_in_batch),
img=(t_img),
bd_label=int(target_changed[idx_in_batch]),
label=int(targets1[idx_in_batch]),
)
save_attack_result(
model_name=args.model,
num_classes=args.num_classes,
model=netC.cpu().state_dict(),
data_path=args.dataset_path,
img_size=args.img_size,
clean_data=args.dataset,
bd_train=bd_train_dataset,
bd_test=self.bd_test_dataset,
save_path=args.save_path,
)
def train_mask_step(self, netM, optimizerM, schedulerM, train_dataloader1, train_dataloader2, args):
netM.train()
total = 0
total_loss = 0
criterion_div = nn.MSELoss(reduction="none")
for batch_idx, (inputs1, targets1), (inputs2, targets2) in zip(range(len(train_dataloader1)), train_dataloader1,
train_dataloader2):
optimizerM.zero_grad()
inputs1, targets1 = inputs1.to(self.device, non_blocking=args.non_blocking), targets1.to(self.device,
non_blocking=args.non_blocking)
inputs2, targets2 = inputs2.to(self.device, non_blocking=args.non_blocking), targets2.to(self.device,
non_blocking=args.non_blocking)
# bs = inputs1.shape[0]
masks1 = netM(inputs1)
masks1, masks2 = self.threshold(netM(inputs1)), self.threshold(netM(inputs2))
# Calculating diversity loss
distance_images = criterion_div(inputs1, inputs2)
distance_images = torch.mean(distance_images, dim=(1, 2, 3))
distance_images = torch.sqrt(distance_images)
distance_patterns = criterion_div(masks1, masks2)
distance_patterns = torch.mean(distance_patterns, dim=(1, 2, 3))
distance_patterns = torch.sqrt(distance_patterns)
loss_div = distance_images / (distance_patterns + args.EPSILON)
loss_div = torch.mean(loss_div) * args.lambda_div
loss_norm = torch.mean(F.relu(masks1 - args.mask_density))
total_loss = args.lambda_norm * loss_norm + args.lambda_div * loss_div
total_loss.backward()
optimizerM.step()
infor_string = "Mask loss: {:.4f} - Norm: {:.3f} | Diversity: {:.3f}".format(total_loss, loss_norm,
loss_div)
progress_bar(batch_idx, len(train_dataloader1), infor_string)
schedulerM.step()
def eval_mask(self, netM, optimizerM, schedulerM, test_dataloader1, test_dataloader2, epoch, args):
netM.eval()
logging.info(" Eval:")
total = 0.0
criterion_div = nn.MSELoss(reduction="none")
for batch_idx, (inputs1, targets1), (inputs2, targets2) in zip(range(len(test_dataloader1)), test_dataloader1,
test_dataloader2):
with torch.no_grad():
inputs1, targets1 = inputs1.to(self.device, non_blocking=args.non_blocking), targets1.to(self.device,
non_blocking=args.non_blocking)
inputs2, targets2 = inputs2.to(self.device, non_blocking=args.non_blocking), targets2.to(self.device,
non_blocking=args.non_blocking)
# bs = inputs1.shape[0]
masks1, masks2 = self.threshold(netM(inputs1)), self.threshold(netM(inputs2))
# Calculating diversity loss
distance_images = criterion_div(inputs1, inputs2)
distance_images = torch.mean(distance_images, dim=(1, 2, 3))
distance_images = torch.sqrt(distance_images)
distance_patterns = criterion_div(masks1, masks2)
distance_patterns = torch.mean(distance_patterns, dim=(1, 2, 3))
distance_patterns = torch.sqrt(distance_patterns)
loss_div = distance_images / (distance_patterns + args.EPSILON)
loss_div = torch.mean(loss_div) * args.lambda_div
loss_norm = torch.mean(F.relu(masks1 - args.mask_density))
infor_string = "Norm: {:.3f} | Diversity: {:.3f}".format(loss_norm, loss_div)
progress_bar(batch_idx, len(test_dataloader1), infor_string)
return epoch
def create_targets_bd(self, targets, args):
if args.attack_label_trans == "all2one":
bd_targets = torch.ones_like(targets) * args.attack_target
elif args.attack_label_trans == "all2all":
bd_targets = torch.tensor([(label + 1) % args.num_classes for label in targets])
else:
raise Exception("{} attack mode is not implemented".format(args.attack_label_trans))
return bd_targets.to(self.device, non_blocking=args.non_blocking)
def create_bd(self, inputs, targets, netG, netM, args, train_or_test):
if train_or_test == 'train':
bd_targets = self.create_targets_bd(targets, args)
if inputs.__len__() == 0: # for case that no sample should be poisoned
return inputs, bd_targets, inputs.detach().clone(), inputs.detach().clone()
patterns = netG(inputs)
patterns = self.normalizer(patterns)
masks_output = self.threshold(netM(inputs))
bd_inputs = inputs + (patterns - inputs) * masks_output
return bd_inputs, bd_targets, patterns, masks_output
if train_or_test == 'test':
bd_targets = self.create_targets_bd(targets, args)
position_changed = (
bd_targets - targets != 0) # no matter all2all or all2one, we want location changed to tell whether the bd is effective
inputs, bd_targets = inputs[position_changed], bd_targets[position_changed]
if inputs.__len__() == 0: # for case that no sample should be poisoned
return torch.tensor([]), torch.tensor([]), None, None, position_changed, targets
patterns = netG(inputs)
patterns = self.normalizer(patterns)
masks_output = self.threshold(netM(inputs))
bd_inputs = inputs + (patterns - inputs) * masks_output
return bd_inputs, bd_targets, patterns, masks_output, position_changed, targets
def create_cross(self, inputs1, inputs2, netG, netM, args):
if inputs1.__len__() == 0: # for case that no sample should be poisoned
return inputs2.detach().clone(), inputs2, inputs2.detach().clone()
patterns2 = netG(inputs2)
patterns2 = self.normalizer(patterns2)
masks_output = self.threshold(netM(inputs2))
inputs_cross = inputs1 + (patterns2 - inputs1) * masks_output
return inputs_cross, patterns2, masks_output
def train_step(self,
netC, netG, netM, optimizerC, optimizerG, schedulerC, schedulerG, train_dataloader1,
train_dataloader2, args,
):
netC.train()
netG.train()
logging.info(" Training:")
criterion = nn.CrossEntropyLoss()
criterion_div = nn.MSELoss(reduction="none")
batch_loss_list = []
batch_predict_list = []
batch_label_list = []
batch_poison_indicator_list = []
batch_original_targets_list = []
for batch_idx, (inputs1, targets1), (inputs2, targets2) in zip(range(len(train_dataloader1)), train_dataloader1,
train_dataloader2):
optimizerC.zero_grad()
inputs1, targets1 = inputs1.to(self.device, non_blocking=args.non_blocking), targets1.to(self.device,
non_blocking=args.non_blocking)
inputs2, targets2 = inputs2.to(self.device, non_blocking=args.non_blocking), targets2.to(self.device,
non_blocking=args.non_blocking)
# bs = int(args.batch_size)
num_bd = int(generalize_to_lower_pratio(args.pratio, inputs1.shape[0])) # int(args.pratio * bs)
num_cross = num_bd
inputs_bd, targets_bd, patterns1, masks1 = self.create_bd(inputs1[:num_bd], targets1[:num_bd], netG, netM,
args,
'train')
inputs_cross, patterns2, masks2 = self.create_cross(
inputs1[num_bd: num_bd + num_cross], inputs2[num_bd: num_bd + num_cross], netG, netM, args,
)
total_inputs = torch.cat((inputs_bd, inputs_cross, inputs1[num_bd + num_cross:]), 0)
total_targets = torch.cat((targets_bd, targets1[num_bd:]), 0)
preds = netC(total_inputs)
loss_ce = criterion(preds, total_targets)
# Calculating diversity loss
distance_images = criterion_div(inputs1[:num_bd], inputs2[num_bd: num_bd + num_bd])
distance_images = torch.mean(distance_images, dim=(1, 2, 3))
distance_images = torch.sqrt(distance_images)
distance_patterns = criterion_div(patterns1, patterns2)
distance_patterns = torch.mean(distance_patterns, dim=(1, 2, 3))
distance_patterns = torch.sqrt(distance_patterns)
loss_div = distance_images / (distance_patterns + args.EPSILON)
loss_div = torch.mean(loss_div) * args.lambda_div
total_loss = loss_ce + loss_div
total_loss.backward()
optimizerC.step()
optimizerG.step()
batch_loss_list.append(total_loss.item())
batch_predict_list.append(torch.max(preds, -1)[1].detach().clone().cpu())
batch_label_list.append(total_targets.detach().clone().cpu())
poison_indicator = torch.zeros(inputs1.shape[0])
poison_indicator[:num_bd] = 1 # all others are cross/clean samples cannot conut up to train acc
poison_indicator[num_bd:num_cross + num_bd] = 2 # indicate for the cross terms
batch_poison_indicator_list.append(poison_indicator)
batch_original_targets_list.append(targets1.detach().clone().cpu())
if args.C_lr_scheduler == "ReduceLROnPlateau":
schedulerC.step(loss_ce)
else:
schedulerC.step()
schedulerG.step()
train_epoch_loss_avg_over_batch, \
train_epoch_predict_list, \
train_epoch_label_list, \
train_epoch_poison_indicator_list, \
train_epoch_original_targets_list = sum(batch_loss_list) / len(batch_loss_list), \
torch.cat(batch_predict_list), \
torch.cat(batch_label_list), \
torch.cat(batch_poison_indicator_list), \
torch.cat(batch_original_targets_list)
train_mix_acc = all_acc(train_epoch_predict_list, train_epoch_label_list)
train_bd_idx = torch.where(train_epoch_poison_indicator_list == 1)[0]
train_cross_idx = torch.where(train_epoch_poison_indicator_list == 2)[0]
train_clean_idx = torch.where(train_epoch_poison_indicator_list == 0)[0]
train_clean_acc = all_acc(
train_epoch_predict_list[train_clean_idx],
train_epoch_label_list[train_clean_idx],
)
train_asr = all_acc(
train_epoch_predict_list[train_bd_idx],
train_epoch_label_list[train_bd_idx],
)
train_cross_acc = all_acc(
train_epoch_predict_list[train_cross_idx],
train_epoch_label_list[train_cross_idx],
)
train_ra = all_acc(
train_epoch_predict_list[train_bd_idx],
train_epoch_original_targets_list[train_bd_idx],
)
return train_epoch_loss_avg_over_batch, \
train_mix_acc, \
train_clean_acc, \
train_asr, \
train_ra, \
train_cross_acc
def eval_step(
self,
netC,
netG,
netM,
optimizerC,
optimizerG,
schedulerC,
schedulerG,
clean_test_dataset_with_transform,
epoch,
args,
):
netC.eval()
netG.eval()
# set shuffle here = False, since other place need randomness to generate cross sample.
transforms_reversible = transforms.Compose(
list(
filter(
lambda x: isinstance(x, (transforms.Normalize, transforms.Resize, transforms.ToTensor)),
deepcopy(clean_test_dataset_with_transform.wrap_img_transform.transforms)
)
)
)
# get denormalizer
for trans_t in deepcopy(clean_test_dataset_with_transform.wrap_img_transform.transforms):
if isinstance(trans_t, transforms.Normalize):
denormalizer = get_dataset_denormalization(trans_t)
logging.info(f"{denormalizer}")
# Notice that due to the fact that we need random sequence to get cross samples
# So we set the reversible_test_dataloader2 with shuffle = True
reversible_test_dataset1 = (clean_test_dataset_with_transform)
reversible_test_dataset1.wrap_img_transform = transforms_reversible
reversible_test_dataloader1 = DataLoader(reversible_test_dataset1, batch_size=args.batch_size,
pin_memory=args.pin_memory,
num_workers=args.num_workers,
shuffle=False)
reversible_test_dataset2 = (clean_test_dataset_with_transform)
reversible_test_dataset2.wrap_img_transform = transforms_reversible
reversible_test_dataloader2 = DataLoader(reversible_test_dataset2, batch_size=args.batch_size,
pin_memory=args.pin_memory,
num_workers=args.num_workers,
shuffle=True)
self.bd_test_dataset = prepro_cls_DatasetBD_v2(
clean_test_dataset_with_transform.wrapped_dataset, save_folder_path=f"{args.save_path}/bd_test_dataset"
)
self.cross_test_dataset = prepro_cls_DatasetBD_v2(
clean_test_dataset_with_transform.wrapped_dataset, save_folder_path=f"{args.save_path}/cross_test_dataset"
)
for batch_idx, (inputs1, targets1), (inputs2, targets2) in zip(range(len(reversible_test_dataloader1)),
reversible_test_dataloader1,
reversible_test_dataloader2):
with torch.no_grad():
inputs1, targets1 = inputs1.to(self.device, non_blocking=args.non_blocking), targets1.to(self.device,
non_blocking=args.non_blocking)
inputs2, targets2 = inputs2.to(self.device, non_blocking=args.non_blocking), targets2.to(self.device,
non_blocking=args.non_blocking)
inputs_bd, targets_bd, _, _, position_changed, targets = self.create_bd(inputs1, targets1, netG, netM,
args,
'test')
inputs_cross, _, _ = self.create_cross(inputs1, inputs2, netG, netM, args)
targets1 = targets1.detach().clone().cpu()
y_poison_batch = targets_bd.detach().clone().cpu().tolist()
for idx_in_batch, t_img in enumerate(inputs_bd.detach().clone().cpu()):
self.bd_test_dataset.set_one_bd_sample(
selected_index=int(
batch_idx * int(args.batch_size) + torch.where(position_changed.detach().clone().cpu())[0][