-
Notifications
You must be signed in to change notification settings - Fork 2
/
train_on_cleansed_set.py
402 lines (308 loc) · 15.2 KB
/
train_on_cleansed_set.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
'''codes used to train models on cleansed dataset (by poison cleanser)
'''
import argparse
import os, sys
from tqdm import tqdm
import config
from torchvision import datasets, transforms
from torch import nn
import torch
from utils import default_args, supervisor, tools, imagenet
import time
from torch.cuda.amp import autocast, GradScaler
parser = argparse.ArgumentParser()
parser.add_argument('-dataset', type=str, required=False,
default=default_args.parser_default['dataset'],
choices=default_args.parser_choices['dataset'])
parser.add_argument('-poison_type', type=str, required=False,
default='none',
choices=default_args.parser_choices['poison_type'])
parser.add_argument('-poison_rate', type=float, required=False,
choices=default_args.parser_choices['poison_rate'],
default=default_args.parser_default['poison_rate'])
parser.add_argument('-cover_rate', type=float, required=False,
choices=default_args.parser_choices['cover_rate'],
default=default_args.parser_default['cover_rate'])
parser.add_argument('-ember_options', type=str, required=False,
choices=['constrained', 'unconstrained', 'none'],
default='unconstrained')
parser.add_argument('-alpha', type=float, required=False,
default=default_args.parser_default['alpha'])
parser.add_argument('-test_alpha', type=float, required=False, default=None)
parser.add_argument('-trigger', type=str, required=False,
default=None)
parser.add_argument('-no_aug', default=False, action='store_true')
parser.add_argument('-no_normalize', default=False, action='store_true')
parser.add_argument('-devices', type=str, default='0')
parser.add_argument('-cleanser', type=str, choices=['SCAn','AC','SS', 'CT', 'SPECTRE', 'Strip', 'SentiNet'], default='CT')
parser.add_argument('-log', default=False, action='store_true')
parser.add_argument('-seed', type=int, required=False, default=default_args.seed)
args = parser.parse_args()
os.environ["CUDA_VISIBLE_DEVICES"] = "%s" % args.devices
tools.setup_seed(args.seed)
if args.trigger is None:
if args.dataset != 'imagenette' and args.dataset != 'imagenet':
args.trigger = config.trigger_default[args.poison_type]
elif args.dataset == 'imagenet':
args.trigger = imagenet.triggers[args.poison_type]
else:
if args.poison_type == 'badnet':
args.trigger = 'badnet_high_res.png'
else:
raise NotImplementedError('%s not implemented for imagenette' % args.poison_type)
all_to_all = False
if args.poison_type == 'badnet_all_to_all':
all_to_all = True
if args.log:
out_path = 'logs'
if not os.path.exists(out_path): os.mkdir(out_path)
out_path = os.path.join(out_path, '%s_seed=%s' % (args.dataset, args.seed))
if not os.path.exists(out_path): os.mkdir(out_path)
out_path = os.path.join(out_path, args.cleanser)
if not os.path.exists(out_path): os.mkdir(out_path)
out_path = os.path.join(out_path, '%s_aug.out' % (supervisor.get_dir_core(args, include_poison_seed=config.record_poison_seed)))
fout = open(out_path, 'w')
ferr = open('/dev/null', 'a')
sys.stdout = fout
sys.stderr = ferr
batch_size = 128
if args.dataset == 'cifar10':
num_classes = 10
data_transform_aug = transforms.Compose([
transforms.RandomHorizontalFlip(),
transforms.RandomCrop(32, 4),
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261]),
])
data_transform_no_aug = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261]),
])
trigger_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.4914, 0.4822, 0.4465], [0.247, 0.243, 0.261])
])
momentum = 0.9
weight_decay = 1e-4
milestones = [50, 75]
epochs = 100
learning_rate = 0.1
elif args.dataset == 'gtsrb':
num_classes = 43
data_transform_aug = transforms.Compose([
transforms.RandomRotation(15),
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), (0.2672, 0.2564, 0.2629))
])
data_transform_no_aug = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), (0.2672, 0.2564, 0.2629))
])
trigger_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.3337, 0.3064, 0.3171), (0.2672, 0.2564, 0.2629))
])
momentum = 0.9
weight_decay = 1e-4
epochs = 100
milestones = [30, 60]
learning_rate = 0.01
elif args.dataset == 'imagenet':
num_classes = 1000
arch = config.arch[args.dataset]
momentum = 0.9
weight_decay = 1e-4
epochs = 90
milestones = torch.tensor([30, 60])
learning_rate = 0.1
batch_size = 256
elif args.dataset == 'ember':
num_classes = 2
arch = config.arch[args.dataset]
momentum = 0.9
weight_decay = 1e-6
epochs = 10
learning_rate = 0.1
milestones = torch.tensor([])
batch_size = 512
print('[Non-image Dataset] Amber')
else:
raise Exception("Invalid Dataset")
if args.dataset == 'imagenet':
kwargs = {'num_workers': 32, 'pin_memory': True}
else:
kwargs = {'num_workers': 4, 'pin_memory': True}
if args.dataset != 'ember' and args.dataset != 'imagenet':
poison_set_dir = supervisor.get_poison_set_dir(args)
poisoned_set_img_dir = os.path.join(poison_set_dir, 'data')
poisoned_set_label_path = os.path.join(poison_set_dir, 'labels')
poisoned_set = tools.IMG_Dataset(data_dir=poisoned_set_img_dir,
label_path=poisoned_set_label_path, transforms=data_transform_aug)
cleansed_set_indices_dir = supervisor.get_cleansed_set_indices_dir(args)
print('load : %s' % cleansed_set_indices_dir)
cleansed_set_indices = torch.load(cleansed_set_indices_dir)
elif args.dataset == 'imagenet':
poison_set_dir = supervisor.get_poison_set_dir(args)
poison_indices_path = os.path.join(poison_set_dir, 'poison_indices')
poisoned_set_img_dir = os.path.join(poison_set_dir, 'data')
print('dataset : %s' % poison_set_dir)
poison_indices = torch.load(poison_indices_path)
root_dir = '/path_to_imagenet/'
train_set_dir = os.path.join(root_dir, 'train')
test_set_dir = os.path.join(root_dir, 'val')
from utils import imagenet
poisoned_set = imagenet.imagenet_dataset(directory=train_set_dir, poison_directory=poisoned_set_img_dir,
poison_indices = poison_indices, target_class=imagenet.target_class,
num_classes=1000)
cleansed_set_indices_dir = supervisor.get_cleansed_set_indices_dir(args)
print('load : %s' % cleansed_set_indices_dir)
cleansed_set_indices = torch.load(cleansed_set_indices_dir)
else:
poison_set_dir = os.path.join('poisoned_train_set', 'ember', args.ember_options)
poison_indices_path = os.path.join(poison_set_dir, 'poison_indices')
# stats_path = os.path.join('data', 'ember', 'stats')
poisoned_set = tools.EMBER_Dataset(x_path=os.path.join(poison_set_dir, 'watermarked_X.npy'),
y_path=os.path.join(poison_set_dir, 'watermarked_y.npy'))
cleansed_set_indices_dir = os.path.join(poison_set_dir, 'cleansed_set_indices_seed=%d' % args.seed)
print('load : %s' % cleansed_set_indices_dir)
cleansed_set_indices = torch.load(cleansed_set_indices_dir)
poisoned_indices = torch.load(os.path.join(poison_set_dir, 'poison_indices'))
cleansed_set_indices.sort()
poisoned_indices.sort()
tot_poison = len(poisoned_indices)
num_poison = 0
if tot_poison > 0:
pt = 0
for pid in cleansed_set_indices:
while poisoned_indices[pt] < pid and pt + 1 < tot_poison: pt += 1
if poisoned_indices[pt] == pid:
num_poison += 1
print('remaining poison samples in cleansed set : ', num_poison)
cleansed_set = torch.utils.data.Subset(poisoned_set, cleansed_set_indices)
train_set = cleansed_set
if args.dataset != 'ember' and args.dataset != 'imagenet':
# Set Up Test Set for Debug & Evaluation
test_set_dir = os.path.join('clean_set', args.dataset, 'test_split')
test_set_img_dir = os.path.join(test_set_dir, 'data')
test_set_label_path = os.path.join(test_set_dir, 'labels')
test_set = tools.IMG_Dataset(data_dir=test_set_img_dir,
label_path=test_set_label_path, transforms=data_transform_no_aug)
print('with no aug...')
test_set_loader = torch.utils.data.DataLoader(
test_set,
batch_size=batch_size, shuffle=False, worker_init_fn=tools.worker_init, **kwargs)
# Poison Transform for Testing
poison_transform = supervisor.get_poison_transform(poison_type=args.poison_type, dataset_name=args.dataset,
target_class=config.target_class[args.dataset], trigger_transform=trigger_transform,
is_normalized_input=True,
alpha=args.alpha if args.test_alpha is None else args.test_alpha,
trigger_name=args.trigger, args=args)
elif args.dataset == 'imagenet':
poison_transform = imagenet.get_poison_transform_for_imagenet(args.poison_type)
test_set = imagenet.imagenet_dataset(directory=test_set_dir, shift=False, aug=False,
label_file=imagenet.test_set_labels, num_classes=1000)
test_set_backdoor = imagenet.imagenet_dataset(directory=test_set_dir, shift=False, aug=False,
label_file=imagenet.test_set_labels, num_classes=1000, poison_transform=poison_transform)
test_split_meta_dir = os.path.join('clean_set', args.dataset, 'test_split')
test_indices = torch.load(os.path.join(test_split_meta_dir, 'test_indices'))
test_set = torch.utils.data.Subset(test_set, test_indices)
test_set_loader = torch.utils.data.DataLoader(
test_set,
batch_size=batch_size, shuffle=False, worker_init_fn=tools.worker_init, **kwargs)
test_set_backdoor = torch.utils.data.Subset(test_set_backdoor, test_indices)
test_set_backdoor_loader = torch.utils.data.DataLoader(
test_set_backdoor,
batch_size=batch_size, shuffle=False, worker_init_fn=tools.worker_init, **kwargs)
else:
normalizer = poisoned_set.normal
test_set_dir = os.path.join('clean_set', args.dataset, 'test_split')
test_set = tools.EMBER_Dataset(x_path=os.path.join(test_set_dir, 'X.npy'),
y_path=os.path.join(test_set_dir, 'Y.npy'),
normalizer = normalizer)
test_set_loader = torch.utils.data.DataLoader(
test_set,
batch_size=batch_size, shuffle=False, worker_init_fn=tools.worker_init, **kwargs)
backdoor_test_set_dir = os.path.join('poisoned_train_set', 'ember', args.ember_options)
backdoor_test_set = tools.EMBER_Dataset(x_path=os.path.join(poison_set_dir, 'watermarked_X_test.npy'),
y_path=None, normalizer = normalizer)
backdoor_test_set_loader = torch.utils.data.DataLoader(
backdoor_test_set,
batch_size=batch_size, shuffle=False, worker_init_fn=tools.worker_init, **kwargs)
train_loader = torch.utils.data.DataLoader(
train_set,
batch_size=batch_size, shuffle=True, worker_init_fn=tools.worker_init, **kwargs)
arch = config.arch[args.dataset]
if args.poison_type == 'TaCT':
source_classes = [config.source_class]
else:
source_classes = None
#milestones = milestones.tolist()
model = arch(num_classes=num_classes)
model = nn.DataParallel(model)
model = model.cuda()
if args.dataset != 'ember':
print(f"Will save to '{supervisor.get_model_dir(args, cleanse=True)}'.")
if os.path.exists(supervisor.get_model_dir(args, cleanse=True)): # exit if there is an already trained model
pass
#print(f"Model '{supervisor.get_model_dir(args, cleanse=True)}' already exists!")
#model = arch(num_classes=num_classes)
#model.load_state_dict(torch.load(supervisor.get_model_dir(args, cleanse=True)))
#model = model.cuda()
#tools.test(model=model, test_loader=test_set_loader, poison_test=True, poison_transform=poison_transform,
# num_classes=num_classes, source_classes=source_classes)
#exit(0)
criterion = nn.CrossEntropyLoss().cuda()
else:
model_path = os.path.join('poisoned_train_set', 'ember', args.ember_options, 'model_trained_on_cleansed_data_seed=%d.pt' % args.seed)
print(f"Will save to '{model_path}'.")
if os.path.exists(model_path):
print(f"Model '{model_path}' already exists!")
criterion = nn.BCELoss().cuda()
print('milestones:', milestones)
optimizer = torch.optim.SGD(model.parameters(), learning_rate, momentum=momentum, weight_decay=weight_decay)
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, milestones=milestones)
cnt = 0
from tqdm import tqdm
#scaler = GradScaler()
for epoch in range(1,epochs+1):
start_time = time.perf_counter()
model.train()
for data, target in tqdm(train_loader):
#data = data.cuda(non_blocking=True)
#target = target.cuda(non_blocking=True)
data, target = data.cuda(), target.cuda()
#optimizer.zero_grad(set_to_none=True)
optimizer.zero_grad()
#with autocast():
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
#scaler.scale(loss).backward()
#scaler.step(optimizer)
#scaler.update()
scheduler.step()
end_time = time.perf_counter()
elapsed_time = end_time - start_time
print('<Cleansed Training> Train Epoch: {} \tLoss: {:.6f}, lr: {:.6f}, Time: {:.2f}s'.format(epoch,
loss.item(), optimizer.param_groups[0]['lr'], elapsed_time))
# Test
if args.dataset != 'ember':
if epoch % 20 == 0:
if args.dataset == 'imagenet':
tools.test_imagenet(model=model, test_loader=test_set_loader,
test_backdoor_loader=test_set_backdoor_loader)
torch.save(model.module.state_dict(), supervisor.get_model_dir(args, cleanse=True))
else:
tools.test(model=model, test_loader=test_set_loader, poison_test=True,
poison_transform=poison_transform, num_classes=num_classes, source_classes=source_classes,
all_to_all=all_to_all)
torch.save(model.module.state_dict(), supervisor.get_model_dir(args, cleanse=True))
else:
if epoch % 5 == 0:
tools.test_ember(model=model, test_loader=test_set_loader, backdoor_test_loader=backdoor_test_set_loader)
torch.save(model.module.state_dict(), model_path)
if args.dataset != 'ember':
torch.save(model.module.state_dict(), supervisor.get_model_dir(args, cleanse=True))
else:
torch.save(model.module.state_dict(), model_path)