-
Notifications
You must be signed in to change notification settings - Fork 70
/
clp.py
442 lines (366 loc) · 17.1 KB
/
clp.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
'''
Data-free Backdoor Removal based on Channel Lipschitzness
This file is modified based on the following source:
link : https://github.com/rkteddy/channel-Lipschitzness-based-pruning.
The defense method is called clp.
@inproceedings{zheng2022data,
title={Data-free backdoor removal based on channel lipschitzness},
author={Zheng, Runkai and Tang, Rongjun and Li, Jianze and Liu, Li},
booktitle={European Conference on Computer Vision},
pages={175--191},
year={2022},
organization={Springer}}
The update include:
1. data preprocess and dataset setting
2. model setting
3. args and config
4. save process
5. new standard: robust accuracy
6. draw the corresponding images of asr and acc according to different proportions
basic sturcture for defense method:
1. basic setting: args
2. attack result(model, train data, test data)
3. clp defense:
a. prune the model depend on the estimate of TAC
4. test the result and get ASR, ACC, RC with regard to the chosen threshold and interval
'''
import argparse
import copy
import os,sys
import numpy as np
import torch
import torch.nn as nn
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, general_plot_for_epoch
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
def CLP_prune(net, u):
params = net.state_dict()
# conv = None
for name, m in net.named_modules():
if isinstance(m, nn.BatchNorm2d):
std = m.running_var.sqrt()
weight = m.weight
channel_lips = []
for idx in range(weight.shape[0]):
# Combining weights of convolutions and BN
w = conv.weight[idx].reshape(conv.weight.shape[1], -1) * (weight[idx]/std[idx]).abs()
channel_lips.append(torch.svd(w.cpu())[1].max())
channel_lips = torch.Tensor(channel_lips)
index = torch.where(channel_lips>channel_lips.mean() + u*channel_lips.std())[0]
params[name+'.weight'][index] = 0
params[name+'.bias'][index] = 0
# print(index)
# Convolutional layer should be followed by a BN layer by default
elif isinstance(m, nn.Conv2d):
conv = m
net.load_state_dict(params)
class clp(defense):
r"""Data-free backdoor removal based on channel lipschitzness
basic structure:
1. config args, save_path, fix random seed
2. load the backdoor test data
3. load the backdoor attack model
4. clp defense:
a. prune the model depend on the estimate of TAC
5. test the result and get ASR, ACC, RC with regard to the chosen threshold and interval
.. code-block:: python
parser = argparse.ArgumentParser(description=sys.argv[0])
clp.add_arguments(parser)
args = parser.parse_args()
clp_method = clp(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 = clp_method.defense(args.result_file)
.. Note::
@inproceedings{zheng2022data,
title={Data-free backdoor removal based on channel lipschitzness},
author={Zheng, Runkai and Tang, Rongjun and Li, Jianze and Liu, Li},
booktitle={European Conference on Computer Vision},
pages={175--191},
year={2022},
organization={Springer}
}
Args:
baisc args: in the base class
u (float): the threshold of channel lipschitzness
u_min (float): the minimum value of u
u_max (float): the maximum value of u
u_num (float): the number of u
Update:
All threshold evaluation results will be saved in the save_path folder as a picture, and the selected fixed threshold model results will be saved to defense_result.pt
"""
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('--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/clp/config.yaml", help='the path of yaml')
#set the parameter for the clp defense
parser.add_argument('--u', type=float, help='the default value of u')
parser.add_argument('--u_min', type=float, help='the default minimum value of u')
parser.add_argument('--u_max', type=float, help='the default maximum value of u')
parser.add_argument('--u_num', type=float, help='the default number of u')
def set_result(self, result_file):
attack_file = 'record/' + result_file
save_path = 'record/' + result_file + '/defense/clp/'
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)
# Prepare model, optimizer, scheduler
model = generate_cls_model(self.args.model,self.args.num_classes)
model.load_state_dict(self.result['model'])
if "," in self.device:
model = torch.nn.DataParallel(
model,
device_ids=[int(i) for i in self.args.device[5:].split(",")] # eg. "cuda:2,3,7" -> [2,3,7]
)
self.args.device = f'cuda:{model.device_ids[0]}'
model.to(self.args.device)
else:
model.to(self.args.device)
# criterion = nn.CrossEntropyLoss()
criterion = argparser_criterion(args)
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,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,pin_memory=args.pin_memory)
# model.eval()
default_u = np.linspace(self.args.u_min, self.args.u_max, self.args.u_num)
agg_all = Metric_Aggregator()
clean_test_loss_list = []
bd_test_loss_list = []
test_acc_list = []
test_asr_list = []
test_ra_list = []
for u in default_u:
model_copy = copy.deepcopy(model)
model_copy.eval()
CLP_prune(model_copy, u)
# model.eval()
model_copy.eval()
test_dataloader_dict = {}
test_dataloader_dict["clean_test_dataloader"] = data_clean_loader
test_dataloader_dict["bd_test_dataloader"] = data_bd_loader
self.set_trainer(model_copy)
self.trainer.set_with_dataloader(
### the train_dataload has nothing to do with the backdoor defense
train_dataloader = data_bd_loader,
test_dataloader_dict = test_dataloader_dict,
criterion = criterion,
optimizer = None,
scheduler = None,
device = self.args.device,
amp = self.args.amp,
frequency_save = self.args.frequency_save,
save_folder_path = self.args.save_path,
save_prefix = 'clp',
prefetch = self.args.prefetch,
prefetch_transform_attr_name = "ori_image_transform_in_loading",
non_blocking = self.args.non_blocking,
)
clean_test_loss_avg_over_batch, \
bd_test_loss_avg_over_batch, \
test_acc, \
test_asr, \
test_ra = self.trainer.test_current_model(
test_dataloader_dict, self.args.device,
)
clean_test_loss_list.append(clean_test_loss_avg_over_batch)
bd_test_loss_list.append(bd_test_loss_avg_over_batch)
test_acc_list.append(test_acc)
test_asr_list.append(test_asr)
test_ra_list.append(test_ra)
agg_all({
"u": u,
"clean_test_loss_avg_over_batch": clean_test_loss_avg_over_batch,
"bd_test_loss_avg_over_batch": bd_test_loss_avg_over_batch,
"test_acc": test_acc,
"test_asr": test_asr,
"test_ra": test_ra,
})
general_plot_for_epoch(
{
"Test C-Acc": test_acc_list,
"Test ASR": test_asr_list,
"Test RA": test_ra_list,
},
save_path=f"{args.save_path}u_step_acc_like_metric_plots.png",
ylabel="percentage",
)
general_plot_for_epoch(
{
"Test Clean Loss": clean_test_loss_list,
"Test Backdoor Loss": bd_test_loss_list,
},
save_path=f"{args.save_path}u_step_loss_metric_plots.png",
ylabel="percentage",
)
general_plot_for_epoch(
{
"u": default_u,
},
save_path=f"{args.save_path}u_step_plots.png",
ylabel="percentage",
)
agg_all.to_dataframe().to_csv(f"{args.save_path}u_step_df.csv")
agg = Metric_Aggregator()
CLP_prune(model, self.args.u)
test_dataloader_dict = {}
test_dataloader_dict["clean_test_dataloader"] = data_clean_loader
test_dataloader_dict["bd_test_dataloader"] = data_bd_loader
self.set_trainer(model)
self.trainer.set_with_dataloader(
### the train_dataload has nothing to do with the backdoor defense
train_dataloader = data_bd_loader,
test_dataloader_dict = test_dataloader_dict,
criterion = criterion,
optimizer = None,
scheduler = None,
device = self.args.device,
amp = self.args.amp,
frequency_save = self.args.frequency_save,
save_folder_path = self.args.save_path,
save_prefix = 'clp',
prefetch = self.args.prefetch,
prefetch_transform_attr_name = "ori_image_transform_in_loading",
non_blocking = self.args.non_blocking,
# continue_training_path = continue_training_path,
# only_load_model = only_load_model,
)
clean_test_loss_avg_over_batch, \
bd_test_loss_avg_over_batch, \
test_acc, \
test_asr, \
test_ra = self.trainer.test_current_model(
test_dataloader_dict, self.args.device,
)
agg({
"u": self.args.u,
"clean_test_loss_avg_over_batch": clean_test_loss_avg_over_batch,
"bd_test_loss_avg_over_batch": bd_test_loss_avg_over_batch,
"test_acc": test_acc,
"test_asr": test_asr,
"test_ra": test_ra,
})
agg.to_dataframe().to_csv(f"{args.save_path}clp_df_summary.csv")
logging.info(f'the threshold{args.u} clean_loss:{clean_test_loss_avg_over_batch} bd_loss:{bd_test_loss_avg_over_batch} clean_acc:{test_acc} asr:{test_asr} ra:{test_ra}')
result = {}
result['model'] = model
save_defense_result(
model_name=args.model,
num_classes=args.num_classes,
model=model.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
if __name__ == '__main__':
parser = argparse.ArgumentParser(description=sys.argv[0])
clp.add_arguments(parser)
args = parser.parse_args()
method = clp(args)
if "result_file" not in args.__dict__:
args.result_file = 'defense_test_badnet'
elif args.result_file is None:
args.result_file = 'defense_test_badnet'
result = method.defense(args.result_file)