-
Notifications
You must be signed in to change notification settings - Fork 70
/
abl.py
1093 lines (933 loc) · 43.7 KB
/
abl.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
'''
Anti-backdoor learning: Training clean models on poisoned data.
This file is modified based on the following source:
link : https://github.com/bboylyg/ABL.
The defense method is called abl.
@article{li2021anti,
title={Anti-backdoor learning: Training clean models on poisoned data},
author={Li, Yige and Lyu, Xixiang and Koren, Nodens and Lyu, Lingjuan and Li, Bo and Ma, Xingjun},
journal={Advances in Neural Information Processing Systems},
volume={34},
pages={14900--14912},
year={2021}
}
The update include:
1. data preprocess and dataset setting
2. model setting
3. args and config
4. save process
5. new standard: robust accuracy
basic sturcture for defense method:
1. basic setting: args
2. attack result(model, train data, test data)
3. abl defense:
a. pre-train model
b. isolate the special data(loss is low) as backdoor data
c. unlearn the backdoor data and learn the remaining data
4. test the result and get ASR, ACC, RC
'''
import argparse
import os,sys
import numpy as np
import torch
import torch.nn as nn
from tqdm import tqdm
import copy
sys.path.append('../')
sys.path.append(os.getcwd())
from pprint import pformat
import yaml
import logging
import time
from defense.base import defense
from utils.aggregate_block.train_settings_generate import argparser_criterion
from utils.trainer_cls import Metric_Aggregator, PureCleanModelTrainer, all_acc, general_plot_for_epoch, given_dataloader_test
from utils.aggregate_block.fix_random import fix_random
from utils.aggregate_block.model_trainer_generate import generate_cls_model
from utils.log_assist import get_git_info
from utils.aggregate_block.dataset_and_transform_generate import get_input_shape, get_num_classes, get_transform
from utils.save_load_attack import load_attack_result, save_defense_result
from utils.bd_dataset_v2 import dataset_wrapper_with_transform
class LGALoss(nn.Module):
def __init__(self, gamma, criterion):
super(LGALoss, self).__init__()
self.gamma = gamma
self.criterion = criterion
return
def forward(self,output,target):
loss = self.criterion(output, target)
# add Local Gradient Ascent(LGA) loss
loss_ascent = torch.sign(loss - self.gamma) * loss
return loss_ascent
class FloodingLoss(nn.Module):
def __init__(self, flooding, criterion):
super(FloodingLoss, self).__init__()
self.flooding = flooding
self.criterion = criterion
return
def forward(self,output,target):
loss = self.criterion(output, target)
# add Local Gradient Ascent(LGA) loss
loss_ascent = (loss - self.flooding).abs() + self.flooding
return loss_ascent
def adjust_learning_rate(optimizer, epoch, args):
'''set learning rate during the process of pretraining model
optimizer:
optimizer during the pretrain process
epoch:
current epoch
args:
Contains default parameters
'''
if epoch < args.tuning_epochs:
lr = args.lr
else:
lr = 0.01
logging.info('epoch: {} lr: {:.4f}'.format(epoch, lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def compute_loss_value(args, poisoned_data, model_ascent):
'''Calculate loss value per example
args:
Contains default parameters
poisoned_data:
the train dataset which contains backdoor data
model_ascent:
the model after the process of pretrain
'''
# Define loss function
if args.device == 'cuda':
criterion = torch.nn.CrossEntropyLoss().cuda()
else:
criterion = torch.nn.CrossEntropyLoss()
model_ascent.eval()
losses_record = []
example_data_loader = torch.utils.data.DataLoader(dataset=poisoned_data,
batch_size=1,
shuffle=False,
)
for idx, (img, target,_,_,_) in tqdm(enumerate(example_data_loader, start=0)):
img = img.to(args.device)
target = target.to(args.device)
with torch.no_grad():
output = model_ascent(img)
loss = criterion(output, target)
losses_record.append(loss.item())
losses_idx = np.argsort(np.array(losses_record)) # get the index of examples by loss value in descending order
# Show the top 10 loss values
losses_record_arr = np.array(losses_record)
logging.info(f'Top ten loss value: {losses_record_arr[losses_idx[:10]]}')
return losses_idx
def isolate_data(args, result, losses_idx):
'''isolate the backdoor data with the calculated loss
args:
Contains default parameters
result:
the attack result contain the train dataset which contains backdoor data
losses_idx:
the index of order about the loss value for each data
'''
# Initialize lists
other_examples = []
isolation_examples = []
cnt = 0
ratio = args.isolation_ratio
perm = losses_idx[0: int(len(losses_idx) * ratio)]
permnot = losses_idx[int(len(losses_idx) * ratio):]
tf_compose = get_transform(args.dataset, *([args.input_height,args.input_width]) , train = False)
train_dataset = result['bd_train'].wrapped_dataset
data_set_without_tran = train_dataset
data_set_isolate = result['bd_train']
data_set_isolate.wrapped_dataset = data_set_without_tran
data_set_isolate.wrap_img_transform = tf_compose
data_set_other_without_tran = data_set_without_tran.copy()
data_set_other = dataset_wrapper_with_transform(
data_set_other_without_tran,
tf_compose,
None,
)
# x = result['bd_train']['x']
# y = result['bd_train']['y']
data_set_isolate.subset(perm)
data_set_other.subset(permnot)
# isolation_examples = list(zip([x[ii] for ii in perm],[y[ii] for ii in perm]))
# other_examples = list(zip([x[ii] for ii in permnot],[y[ii] for ii in permnot]))
logging.info('Finish collecting {} isolation examples: '.format(len(data_set_isolate)))
logging.info('Finish collecting {} other examples: '.format(len(data_set_other)))
return data_set_isolate, data_set_other
def learning_rate_finetuning(optimizer, epoch, args):
'''set learning rate during the process of finetuing model
optimizer:
optimizer during the pretrain process
epoch:
current epoch
args:
Contains default parameters
'''
if epoch < 40:
lr = 0.01
elif epoch < 60:
lr = 0.001
else:
lr = 0.001
logging.info('epoch: {} lr: {:.4f}'.format(epoch, lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def learning_rate_unlearning(optimizer, epoch, args):
'''set learning rate during the process of unlearning model
optimizer:
optimizer during the pretrain process
epoch:
current epoch
args:
Contains default parameters
'''
if epoch < args.unlearning_epochs:
lr = 0.0001
else:
lr = 0.0001
logging.info('epoch: {} lr: {:.4f}'.format(epoch, lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
class abl(defense):
r"""Anti-backdoor learning: Training clean models on poisoned data.
basic structure:
1. config args, save_path, fix random seed
2. load the backdoor attack data and backdoor test data
3. abl defense:
a. pre-train model
b. isolate the special data(loss is low) as backdoor data
c. unlearn the backdoor data and learn the remaining data
4. test the result and get ASR, ACC, RC
.. code-block:: python
parser = argparse.ArgumentParser(description=sys.argv[0])
abl.add_arguments(parser)
args = parser.parse_args()
abl_method = abl(args)
if "result_file" not in args.__dict__:
args.result_file = 'one_epochs_debug_badnet_attack'
elif args.result_file is None:
args.result_file = 'one_epochs_debug_badnet_attack'
result = abl_method.defense(args.result_file)
.. Note::
@article{li2021anti,
title={Anti-backdoor learning: Training clean models on poisoned data},
author={Li, Yige and Lyu, Xixiang and Koren, Nodens and Lyu, Lingjuan and Li, Bo and Ma, Xingjun},
journal={Advances in Neural Information Processing Systems},
volume={34},
pages={14900--14912},
year={2021}
}
Args:
baisc args: in the base class
tuning_epochs (int): number of the first tuning epochs to run
finetuning_ascent_model (bool): whether finetuning model after sperate the poisoned data
finetuning_epochs (int): number of the finetuning epochs to run
unlearning_epochs (int): number of the unlearning epochs to run
lr_finetuning_init (float): initial finetuning learning rate
lr_unlearning_init (float): initial unlearning learning rate
momentum (float): momentum of sgd during the process of finetuning and unlearning
weight_decay (float): weight decay of sgd during the process of finetuning and unlearning
isolation_ratio (float): ratio of isolation data from the whole poisoned data
gradient_ascent_type (str): type of gradient ascent (LGA, Flooding)
gamma (float): value of gamma for LGA
flooding (float): value of flooding for Flooding
"""
def __init__(self,args):
with open(args.yaml_path, 'r') as f:
defaults = yaml.safe_load(f)
defaults.update({k:v for k,v in args.__dict__.items() if v is not None})
args.__dict__ = defaults
args.terminal_info = sys.argv
args.num_classes = get_num_classes(args.dataset)
args.input_height, args.input_width, args.input_channel = get_input_shape(args.dataset)
args.img_size = (args.input_height, args.input_width, args.input_channel)
args.dataset_path = f"{args.dataset_path}/{args.dataset}"
self.args = args
if 'result_file' in args.__dict__ :
if args.result_file is not None:
self.set_result(args.result_file)
def add_arguments(parser):
parser.add_argument('--device', type=str, help='cuda, cpu')
parser.add_argument("-pm","--pin_memory", type=lambda x: str(x) in ['True', 'true', '1'], help = "dataloader pin_memory")
parser.add_argument("-nb","--non_blocking", type=lambda x: str(x) in ['True', 'true', '1'], help = ".to(), set the non_blocking = ?")
parser.add_argument("-pf", '--prefetch', type=lambda x: str(x) in ['True', 'true', '1'], help='use prefetch')
parser.add_argument('--amp', type=lambda x: str(x) in ['True','true','1'])
parser.add_argument('--checkpoint_load', type=str, help='the location of load model')
parser.add_argument('--checkpoint_save', type=str, help='the location of checkpoint where model is saved')
parser.add_argument('--log', type=str, help='the location of log')
parser.add_argument("--dataset_path", type=str, help='the location of data')
parser.add_argument('--dataset', type=str, help='mnist, cifar10, cifar100, gtrsb, tiny')
parser.add_argument('--result_file', type=str, help='the location of result')
parser.add_argument('--interval', type=int, help='frequency of save model')
parser.add_argument('--epochs', type=int)
parser.add_argument('--batch_size', type=int)
parser.add_argument("--num_workers", type=float)
parser.add_argument('--lr', type=float)
parser.add_argument('--lr_scheduler', type=str, help='the scheduler of lr')
parser.add_argument('--steplr_stepsize', type=int)
parser.add_argument('--steplr_gamma', type=float)
parser.add_argument('--steplr_milestones', type=list)
parser.add_argument('--model', type=str, help='resnet18')
parser.add_argument('--client_optimizer', type=int)
parser.add_argument('--sgd_momentum', type=float)
parser.add_argument('--wd', type=float, help='weight decay of sgd')
parser.add_argument('--frequency_save', type=int,
help=' frequency_save, 0 is never')
parser.add_argument('--random_seed', type=int, help='random seed')
parser.add_argument('--yaml_path', type=str, default="./config/defense/abl/config.yaml", help='the path of yaml')
#set the parameter for the abl defense
parser.add_argument('--tuning_epochs', type=int, help='number of tune epochs to run')
parser.add_argument('--finetuning_ascent_model', type=bool, help='whether finetuning model')
parser.add_argument('--finetuning_epochs', type=int, help='number of finetuning epochs to run')
parser.add_argument('--unlearning_epochs', type=int, help='number of unlearning epochs to run')
parser.add_argument('--lr_finetuning_init', type=float, help='initial finetuning learning rate')
parser.add_argument('--lr_unlearning_init', type=float, help='initial unlearning learning rate')
parser.add_argument('--momentum', type=float, help='momentum')
parser.add_argument('--weight_decay', type=float, help='weight decay')
parser.add_argument('--isolation_ratio', type=float, help='ratio of isolation data')
parser.add_argument('--gradient_ascent_type', type=str, help='type of gradient ascent')
parser.add_argument('--gamma', type=float, help='value of gamma')
parser.add_argument('--flooding', type=float, help='value of flooding')
def set_result(self, result_file):
attack_file = 'record/' + result_file
save_path = 'record/' + result_file + f'/defense/{self.__class__.__name__}/'
if not (os.path.exists(save_path)):
os.makedirs(save_path)
# assert(os.path.exists(save_path))
self.args.save_path = save_path
if self.args.checkpoint_save is None:
self.args.checkpoint_save = save_path + 'checkpoint/'
if not (os.path.exists(self.args.checkpoint_save)):
os.makedirs(self.args.checkpoint_save)
if self.args.log is None:
self.args.log = save_path + 'log/'
if not (os.path.exists(self.args.log)):
os.makedirs(self.args.log)
self.result = load_attack_result(attack_file + '/attack_result.pt')
def set_trainer(self, model):
self.trainer = PureCleanModelTrainer(
model,
)
def set_logger(self):
args = self.args
logFormatter = logging.Formatter(
fmt='%(asctime)s [%(levelname)-8s] [%(filename)s:%(lineno)d] %(message)s',
datefmt='%Y-%m-%d:%H:%M:%S',
)
logger = logging.getLogger()
fileHandler = logging.FileHandler(args.log + '/' + time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()) + '.log')
fileHandler.setFormatter(logFormatter)
logger.addHandler(fileHandler)
consoleHandler = logging.StreamHandler()
consoleHandler.setFormatter(logFormatter)
logger.addHandler(consoleHandler)
logger.setLevel(logging.INFO)
logging.info(pformat(args.__dict__))
try:
logging.info(pformat(get_git_info()))
except:
logging.info('Getting git info fails.')
def set_devices(self):
# self.device = torch.device(
# (
# f"cuda:{[int(i) for i in self.args.device[5:].split(',')][0]}" if "," in self.args.device else self.args.device
# # since DataParallel only allow .to("cuda")
# ) if torch.cuda.is_available() else "cpu"
# )
self.device = self.args.device
def mitigation(self):
self.set_devices()
fix_random(self.args.random_seed)
result = self.result
###a. pre-train model
poisoned_data, model_ascent = self.pre_train(args,result)
###b. isolate the special data(loss is low) as backdoor data
losses_idx = compute_loss_value(args, poisoned_data, model_ascent)
logging.info('----------- Collect isolation data -----------')
isolation_examples, other_examples = isolate_data(args, result, losses_idx)
###c. unlearn the backdoor data and learn the remaining data
model_new = self.train_unlearning(args,result,model_ascent,isolation_examples,other_examples)
result = {}
result['model'] = model_new
save_defense_result(
model_name=args.model,
num_classes=args.num_classes,
model=model_new.cpu().state_dict(),
save_path=args.save_path,
)
return result
def defense(self,result_file):
self.set_result(result_file)
self.set_logger()
result = self.mitigation()
return result
def pre_train(self, args, result):
'''Pretrain the model with raw data
args:
Contains default parameters
result:
attack result(details can be found in utils)
'''
agg = Metric_Aggregator()
# Load models
logging.info('----------- Network Initialization --------------')
model_ascent = generate_cls_model(args.model,args.num_classes)
if "," in self.device:
model_ascent = torch.nn.DataParallel(
model_ascent,
device_ids=[int(i) for i in args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
)
self.args.device = f'cuda:{model_ascent.device_ids[0]}'
model_ascent.to(self.args.device)
else:
model_ascent.to(self.args.device)
logging.info('finished model init...')
# initialize optimizer
# because the optimizer has parameter nesterov
optimizer = torch.optim.SGD(model_ascent.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
nesterov=True)
# define loss functions
# recommend to use cross entropy
criterion = argparser_criterion(args).to(args.device)
if args.gradient_ascent_type == 'LGA':
criterion = LGALoss(args.gamma,criterion).to(args.device)
elif args.gradient_ascent_type == 'Flooding':
criterion = FloodingLoss(args.flooding,criterion).to(args.device)
else:
raise NotImplementedError
logging.info('----------- Data Initialization --------------')
# tf_compose = transforms.Compose([
# transforms.ToTensor()
# ])
tf_compose = get_transform(args.dataset, *([args.input_height,args.input_width]) , train = False)
train_dataset = result['bd_train'].wrapped_dataset
data_set_without_tran = train_dataset
data_set_o = result['bd_train']
data_set_o.wrapped_dataset = data_set_without_tran
data_set_o.wrap_img_transform = tf_compose
# data_set_isolate = result['bd_train']
# data_set_isolate.wrapped_dataset = data_set_without_tran
# data_set_isolate.wrap_img_transform = tf_compose
# # data_set_other = copy.deepcopy(data_set_isolate)
# # x = result['bd_train']['x']
# # y = result['bd_train']['y']
# losses_idx = range(50000)
# ratio = args.isolation_ratio
# perm = losses_idx[0: int(len(losses_idx) * ratio)]
# permnot = losses_idx[int(len(losses_idx) * ratio):]
# data_set_isolate.subset(perm)
# data_set_o.subset(permnot)
# data_set_other = copy.deepcopy(data_set_o)
poisoned_data_loader = torch.utils.data.DataLoader(data_set_o, batch_size=args.batch_size, num_workers=args.num_workers, shuffle=True)
test_tran = get_transform(self.args.dataset, *([self.args.input_height,self.args.input_width]) , train = False)
data_bd_testset = self.result['bd_test']
data_bd_testset.wrap_img_transform = test_tran
data_bd_loader = torch.utils.data.DataLoader(data_bd_testset, batch_size=self.args.batch_size, num_workers=self.args.num_workers,drop_last=False, shuffle=True,pin_memory=args.pin_memory)
data_clean_testset = self.result['clean_test']
data_clean_testset.wrap_img_transform = test_tran
data_clean_loader = torch.utils.data.DataLoader(data_clean_testset, batch_size=self.args.batch_size, num_workers=self.args.num_workers,drop_last=False, shuffle=True,pin_memory=args.pin_memory)
train_loss_list = []
train_mix_acc_list = []
train_clean_acc_list = []
train_asr_list = []
train_ra_list = []
clean_test_loss_list = []
bd_test_loss_list = []
ra_test_loss_list = []
test_acc_list = []
test_asr_list = []
test_ra_list = []
logging.info('----------- Train Initialization --------------')
for epoch in range(0, args.tuning_epochs):
logging.info("Epoch {}:".format(epoch + 1))
adjust_learning_rate(optimizer, epoch, args)
train_epoch_loss_avg_over_batch, \
train_mix_acc, \
train_clean_acc, \
train_asr, \
train_ra = self.train_step(args, poisoned_data_loader, model_ascent, optimizer, criterion, epoch + 1)
clean_test_loss_avg_over_batch, \
bd_test_loss_avg_over_batch, \
ra_test_loss_avg_over_batch, \
test_acc, \
test_asr, \
test_ra = self.eval_step(
model_ascent,
data_clean_loader,
data_bd_loader,
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,
"clean_test_loss_avg_over_batch": clean_test_loss_avg_over_batch,
"bd_test_loss_avg_over_batch": bd_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,
})
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)
clean_test_loss_list.append(clean_test_loss_avg_over_batch)
bd_test_loss_list.append(bd_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)
general_plot_for_epoch(
{
"Train Acc": train_mix_acc_list,
"Test C-Acc": test_acc_list,
"Test ASR": test_asr_list,
"Test RA": test_ra_list,
},
save_path=f"{args.save_path}pre_train_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 RA Loss": ra_test_loss_list,
},
save_path=f"{args.save_path}pre_train_loss_metric_plots.png",
ylabel="percentage",
)
agg.to_dataframe().to_csv(f"{args.save_path}pre_train_df.csv")
if args.frequency_save != 0 and epoch % args.frequency_save == args.frequency_save - 1:
state_dict = {
"model": model_ascent.state_dict(),
"optimizer": optimizer.state_dict(),
"epoch_current": epoch,
}
torch.save(state_dict, args.checkpoint_save + "pre_train_state_dict.pt")
agg.summary().to_csv(f"{args.save_path}pre_train_df_summary.csv")
return data_set_o, model_ascent
def train_unlearning(self, args, result, model_ascent, isolate_poisoned_data, isolate_other_data):
'''train the model with remaining data and unlearn the backdoor data
args:
Contains default parameters
result:
attack result(details can be found in utils)
model_ascent:
the model after pretrain
isolate_poisoned_data:
the dataset of 'backdoor' data
isolate_other_data:
the dataset of remaining data
'''
agg = Metric_Aggregator()
# Load models
### TODO: load model from checkpoint
# logging.info('----------- Network Initialization --------------')
# if "," in args.device:
# model_ascent = torch.nn.DataParallel(
# model_ascent,
# device_ids=[int(i) for i in args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
# )
# else:
# model_ascent.to(args.device)
# model_ascent.to(args.device)
logging.info('Finish loading ascent model...')
# initialize optimizer
# Because nesterov we do not use other optimizer
optimizer = torch.optim.SGD(model_ascent.parameters(),
lr=args.lr,
momentum=args.momentum,
weight_decay=args.weight_decay,
nesterov=True)
# define loss functions
# you can use other criterion, but the paper use cross validation to unlearn sample
if args.device == 'cuda':
criterion = argparser_criterion(args).cuda()
else:
criterion = argparser_criterion(args)
tf_compose_finetuning = get_transform(args.dataset, *([args.input_height,args.input_width]) , train = True)
tf_compose_unlearning = get_transform(args.dataset, *([args.input_height,args.input_width]) , train = True)
isolate_poisoned_data.wrap_img_transform = tf_compose_finetuning
isolate_poisoned_data_loader = torch.utils.data.DataLoader(dataset=isolate_poisoned_data,
batch_size=args.batch_size,
shuffle=True,
)
isolate_other_data.wrap_img_transform = tf_compose_unlearning
isolate_other_data_loader = torch.utils.data.DataLoader(dataset=isolate_other_data,
batch_size=args.batch_size,
shuffle=True,
)
test_tran = get_transform(args.dataset, *([args.input_height,args.input_width]) , train = False)
data_bd_testset = result['bd_test']
data_bd_testset.wrap_img_transform = test_tran
data_bd_loader = torch.utils.data.DataLoader(data_bd_testset, batch_size=args.batch_size, num_workers=args.num_workers,drop_last=False, shuffle=True,pin_memory=args.pin_memory)
data_clean_testset = result['clean_test']
data_clean_testset.wrap_img_transform = test_tran
data_clean_loader = torch.utils.data.DataLoader(data_clean_testset, batch_size=args.batch_size, num_workers=args.num_workers,drop_last=False, shuffle=True,pin_memory=args.pin_memory)
train_loss_list = []
train_mix_acc_list = []
train_clean_acc_list = []
train_asr_list = []
train_ra_list = []
clean_test_loss_list = []
bd_test_loss_list = []
ra_test_loss_list = []
test_acc_list = []
test_asr_list = []
test_ra_list = []
logging.info('----------- Train Initialization --------------')
if args.finetuning_ascent_model == True:
# this is to improve the clean accuracy of isolation model, you can skip this step
logging.info('----------- Finetuning isolation model --------------')
for epoch in range(0, args.finetuning_epochs):
learning_rate_finetuning(optimizer, epoch, args)
train_epoch_loss_avg_over_batch, \
train_mix_acc, \
train_clean_acc, \
train_asr, \
train_ra = self.train_step(args, isolate_other_data_loader, model_ascent, optimizer, criterion, epoch + 1)
clean_test_loss_avg_over_batch, \
bd_test_loss_avg_over_batch, \
ra_test_loss_avg_over_batch, \
test_acc, \
test_asr, \
test_ra = self.eval_step(
model_ascent,
data_clean_loader,
data_bd_loader,
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,
"clean_test_loss_avg_over_batch": clean_test_loss_avg_over_batch,
"bd_test_loss_avg_over_batch": bd_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,
})
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)
clean_test_loss_list.append(clean_test_loss_avg_over_batch)
bd_test_loss_list.append(bd_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)
general_plot_for_epoch(
{
"Train Acc": train_mix_acc_list,
"Test C-Acc": test_acc_list,
"Test ASR": test_asr_list,
"Test RA": test_ra_list,
},
save_path=f"{args.save_path}finetune_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 RA Loss": ra_test_loss_list,
},
save_path=f"{args.save_path}finetune_loss_metric_plots.png",
ylabel="percentage",
)
agg.to_dataframe().to_csv(f"{args.save_path}finetune_df.csv")
if args.frequency_save != 0 and epoch % args.frequency_save == args.frequency_save - 1:
state_dict = {
"model": model_ascent.state_dict(),
"optimizer": optimizer.state_dict(),
"epoch_current": epoch,
}
torch.save(state_dict, args.checkpoint_save + "finetune_state_dict.pt")
agg.summary().to_csv(f"{args.save_path}finetune_df_summary.csv")
best_acc = 0
best_asr = 0
logging.info('----------- Model unlearning --------------')
for epoch in range(0, args.unlearning_epochs):
learning_rate_unlearning(optimizer, epoch, args)
train_epoch_loss_avg_over_batch, \
train_mix_acc, \
train_clean_acc, \
train_asr, \
train_ra = self.train_step_unlearn(args, isolate_poisoned_data_loader, model_ascent, optimizer, criterion, epoch + 1)
clean_test_loss_avg_over_batch, \
bd_test_loss_avg_over_batch, \
ra_test_loss_avg_over_batch, \
test_acc, \
test_asr, \
test_ra = self.eval_step(
model_ascent,
data_clean_loader,
data_bd_loader,
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,
"clean_test_loss_avg_over_batch": clean_test_loss_avg_over_batch,
"bd_test_loss_avg_over_batch": bd_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,
})
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)
clean_test_loss_list.append(clean_test_loss_avg_over_batch)
bd_test_loss_list.append(bd_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)
general_plot_for_epoch(
{
"Train Acc": train_mix_acc_list,
"Test C-Acc": test_acc_list,
"Test ASR": test_asr_list,
"Test RA": test_ra_list,
},
save_path=f"{args.save_path}unlearn_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 RA Loss": ra_test_loss_list,
},
save_path=f"{args.save_path}unlearn_loss_metric_plots.png",
ylabel="percentage",
)
agg.to_dataframe().to_csv(f"{args.save_path}unlearn_df.csv")
if args.frequency_save != 0 and epoch % args.frequency_save == args.frequency_save - 1:
state_dict = {
"model": model_ascent.state_dict(),
"optimizer": optimizer.state_dict(),
"epoch_current": epoch,
}
torch.save(state_dict, args.checkpoint_save + "unlearn_state_dict.pt")
agg.summary().to_csv(f"{args.save_path}unlearn_df_summary.csv")
agg.summary().to_csv(f"{args.save_path}abl_df_summary.csv")
return model_ascent
def train_step(self, args, train_loader, model_ascent, optimizer, criterion, epoch):
'''Pretrain the model with raw data for each step
args:
Contains default parameters
train_loader:
the dataloader of train data
model_ascent:
the initial model
optimizer:
optimizer during the pretrain process
criterion:
criterion during the pretrain process
epoch:
current epoch
'''
losses = 0
size = 0
batch_loss_list = []
batch_predict_list = []
batch_label_list = []
batch_original_index_list = []
batch_poison_indicator_list = []
batch_original_targets_list = []
model_ascent.train()
for idx, (img, target, original_index, poison_indicator, original_targets) in enumerate(train_loader, start=1):
img = img.to(args.device)
target = target.to(args.device)
pred = model_ascent(img)
loss_ascent = criterion(pred,target)
losses += loss_ascent * img.size(0)
size += img.size(0)
optimizer.zero_grad()
loss_ascent.backward()
optimizer.step()
batch_loss_list.append(loss_ascent.item())
batch_predict_list.append(torch.max(pred, -1)[1].detach().clone().cpu())
batch_label_list.append(target.detach().clone().cpu())
batch_original_index_list.append(original_index.detach().clone().cpu())
batch_poison_indicator_list.append(poison_indicator.detach().clone().cpu())
batch_original_targets_list.append(original_targets.detach().clone().cpu())
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_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_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
def train_step_unlearn(self, args, train_loader, model_ascent, optimizer, criterion, epoch):
'''Pretrain the model with raw data for each step
args:
Contains default parameters
train_loader:
the dataloader of train data
model_ascent:
the initial model
optimizer:
optimizer during the pretrain process
criterion:
criterion during the pretrain process
epoch:
current epoch
'''
losses = 0
size = 0
batch_loss_list = []
batch_predict_list = []
batch_label_list = []
batch_original_index_list = []
batch_poison_indicator_list = []
batch_original_targets_list = []
model_ascent.train()
for idx, (img, target, original_index, poison_indicator, original_targets) in enumerate(train_loader, start=1):
img = img.to(args.device)
target = target.to(args.device)
pred = model_ascent(img)
loss_ascent = criterion(pred,target)
losses += loss_ascent * img.size(0)
size += img.size(0)
optimizer.zero_grad()
(-loss_ascent).backward()
optimizer.step()
batch_loss_list.append(loss_ascent.item())
batch_predict_list.append(torch.max(pred, -1)[1].detach().clone().cpu())
batch_label_list.append(target.detach().clone().cpu())
batch_original_index_list.append(original_index.detach().clone().cpu())
batch_poison_indicator_list.append(poison_indicator.detach().clone().cpu())
batch_original_targets_list.append(original_targets.detach().clone().cpu())