-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbackdoor_isolation.py
309 lines (244 loc) · 11 KB
/
backdoor_isolation.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
from models.selector import *
from utils.util import *
from data_loader import *
from torch.utils.data import DataLoader
from config import get_arguments
from tqdm import tqdm
def compute_loss_value(opt, poisoned_data, model_ascent):
# Calculate loss value per example
# Define loss function
if opt.cuda:
criterion = nn.CrossEntropyLoss().cuda()
else:
criterion = nn.CrossEntropyLoss()
model_ascent.eval()
losses_record = []
example_data_loader = DataLoader(dataset=poisoned_data,
batch_size=1,
shuffle=False,
)
for idx, (img, target) in tqdm(enumerate(example_data_loader, start=0)):
if opt.cuda:
img = img.cuda()
target = target.cuda()
with torch.no_grad():
output = model_ascent(img)
loss = criterion(output, target)
# print(loss.item())
losses_record.append(loss.item())
losses_idx = np.argsort(np.array(losses_record)) # get the index of examples by loss value in ascending order
# Show the lowest 10 loss values
losses_record_arr = np.array(losses_record)
print('Top ten loss value:', losses_record_arr[losses_idx[:10]])
return losses_idx
def isolate_data(opt, poisoned_data, losses_idx):
# Initialize lists
other_examples = []
isolation_examples = []
cnt = 0
ratio = opt.isolation_ratio
example_data_loader = DataLoader(dataset=poisoned_data,
batch_size=1,
shuffle=False,
)
# print('full_poisoned_data_idx:', len(losses_idx))
perm = losses_idx[0: int(len(losses_idx) * ratio)]
for idx, (img, target) in tqdm(enumerate(example_data_loader, start=0)):
img = img.squeeze()
target = target.squeeze()
img = np.transpose((img * 255).cpu().numpy(), (1, 2, 0)).astype('uint8')
target = target.cpu().numpy()
# Filter the examples corresponding to losses_idx
if idx in perm:
isolation_examples.append((img, target))
cnt += 1
else:
other_examples.append((img, target))
# Save data
if opt.save:
data_path_isolation = os.path.join(opt.isolate_data_root, "{}_isolation{}%_examples.npy".format(opt.model_name,
opt.isolation_ratio * 100))
data_path_other = os.path.join(opt.isolate_data_root, "{}_other{}%_examples.npy".format(opt.model_name,
100 - opt.isolation_ratio * 100))
if os.path.exists(data_path_isolation):
raise ValueError('isolation data already exists')
else:
# save the isolation examples
np.save(data_path_isolation, isolation_examples)
np.save(data_path_other, other_examples)
print('Finish collecting {} isolation examples: '.format(len(isolation_examples)))
print('Finish collecting {} other examples: '.format(len(other_examples)))
def train_step(opt, train_loader, model_ascent, optimizer, criterion, epoch):
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model_ascent.train()
for idx, (img, target) in enumerate(train_loader, start=1):
if opt.cuda:
img = img.cuda()
target = target.cuda()
if opt.gradient_ascent_type == 'LGA':
output = model_ascent(img)
loss = criterion(output, target)
# add Local Gradient Ascent(LGA) loss
loss_ascent = torch.sign(loss - opt.gamma) * loss
elif opt.gradient_ascent_type == 'Flooding':
output = model_ascent(img)
# output = student(img)
loss = criterion(output, target)
# add flooding loss
loss_ascent = (loss - opt.flooding).abs() + opt.flooding
else:
raise NotImplementedError
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss_ascent.item(), img.size(0))
top1.update(prec1.item(), img.size(0))
top5.update(prec5.item(), img.size(0))
optimizer.zero_grad()
loss_ascent.backward()
optimizer.step()
if idx % opt.print_freq == 0:
print('Epoch[{0}]:[{1:03}/{2:03}] '
'Loss:{losses.val:.4f}({losses.avg:.4f}) '
'Prec@1:{top1.val:.2f}({top1.avg:.2f}) '
'Prec@5:{top5.val:.2f}({top5.avg:.2f})'.format(epoch, idx, len(train_loader), losses=losses, top1=top1, top5=top5))
def test(opt, test_clean_loader, test_bad_loader, model_ascent, criterion, epoch):
test_process = []
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
model_ascent.eval()
for idx, (img, target) in enumerate(test_clean_loader, start=1):
if opt.cuda:
img = img.cuda()
target = target.cuda()
with torch.no_grad():
output = model_ascent(img)
loss = criterion(output, target)
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), img.size(0))
top1.update(prec1.item(), img.size(0))
top5.update(prec5.item(), img.size(0))
acc_clean = [top1.avg, top5.avg, losses.avg]
losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
for idx, (img, target) in enumerate(test_bad_loader, start=1):
if opt.cuda:
img = img.cuda()
target = target.cuda()
with torch.no_grad():
output = model_ascent(img)
loss = criterion(output, target)
prec1, prec5 = accuracy(output, target, topk=(1, 5))
losses.update(loss.item(), img.size(0))
top1.update(prec1.item(), img.size(0))
top5.update(prec5.item(), img.size(0))
acc_bd = [top1.avg, top5.avg, losses.avg]
print('[Clean] Prec@1: {:.2f}, Loss: {:.4f}'.format(acc_clean[0], acc_clean[2]))
print('[Bad] Prec@1: {:.2f}, Loss: {:.4f}'.format(acc_bd[0], acc_bd[2]))
# save training progress
if epoch < opt.tuning_epochs + 1:
log_root = opt.log_root + '/ABL_results_tuning_epochs.csv'
test_process.append(
(epoch, acc_clean[0], acc_bd[0], acc_clean[2], acc_bd[2]))
df = pd.DataFrame(test_process, columns=("Epoch", "Test_clean_acc", "Test_bad_acc",
"Test_clean_loss", "Test_bad_loss"))
df.to_csv(log_root, mode='a', index=False, encoding='utf-8')
return acc_clean, acc_bd
def train(opt):
# Load models
print('----------- Network Initialization --------------')
model_ascent, _ = select_model(dataset=opt.dataset,
model_name=opt.model_name,
pretrained=False,
pretrained_models_path=opt.isolation_model_root,
n_classes=opt.num_class)
model_ascent.to(opt.device)
print('finished model init...')
# initialize optimizer
optimizer = torch.optim.SGD(model_ascent.parameters(),
lr=opt.lr,
momentum=opt.momentum,
weight_decay=opt.weight_decay,
nesterov=True)
# define loss functions
if opt.cuda:
criterion = nn.CrossEntropyLoss().cuda()
else:
criterion = nn.CrossEntropyLoss()
print('----------- Data Initialization --------------')
if opt.load_fixed_data:
tf_compose = transforms.Compose([
transforms.ToTensor()
])
# load the fixed poisoned data, e.g. Dynamic, FC, DFST attacks etc.
poisoned_data = np.load(opt.poisoned_data_path, allow_pickle=True)
poisoned_data_loader = DataLoader(dataset=poisoned_data,
batch_size=opt.batch_size,
shuffle=True,
)
else:
poisoned_data, poisoned_data_loader = get_backdoor_loader(opt)
test_clean_loader, test_bad_loader = get_test_loader(opt)
print('----------- Train Initialization --------------')
for epoch in range(0, opt.tuning_epochs):
adjust_learning_rate(optimizer, epoch, opt)
# train every epoch
if epoch == 0:
# before training test firstly
test(opt, test_clean_loader, test_bad_loader, model_ascent,
criterion, epoch + 1)
train_step(opt, poisoned_data_loader, model_ascent, optimizer, criterion, epoch + 1)
# evaluate on testing set
print('testing the ascended model......')
acc_clean, acc_bad = test(opt, test_clean_loader, test_bad_loader, model_ascent, criterion, epoch + 1)
if opt.save:
# remember best precision and save checkpoint
# is_best = acc_clean[0] > opt.threshold_clean
# opt.threshold_clean = min(acc_clean[0], opt.threshold_clean)
#
# best_clean_acc = acc_clean[0]
# best_bad_acc = acc_bad[0]
#
# save_checkpoint({
# 'epoch': epoch,
# 'state_dict': model_ascent.state_dict(),
# 'clean_acc': best_clean_acc,
# 'bad_acc': best_bad_acc,
# 'optimizer': optimizer.state_dict(),
# }, epoch, is_best, opt.checkpoint_root, opt.model_name)
# save checkpoint at interval epoch
if epoch % opt.interval == 0:
is_best = True
save_checkpoint({
'epoch': epoch + 1,
'state_dict': model_ascent.state_dict(),
'clean_acc': acc_clean[0],
'bad_acc': acc_bad[0],
'optimizer': optimizer.state_dict(),
}, epoch, is_best, opt)
return poisoned_data, model_ascent
def adjust_learning_rate(optimizer, epoch, opt):
if epoch < opt.tuning_epochs:
lr = opt.lr
else:
lr = 0.01
print('epoch: {} lr: {:.4f}'.format(epoch, lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def save_checkpoint(state, epoch, is_best, opt):
if is_best:
filepath = os.path.join(opt.save, opt.model_name + r'-tuning_epochs{}.tar'.format(epoch))
torch.save(state, filepath)
print('[info] Finish saving the model')
def main():
print('----------- Train isolated model -----------')
opt = get_arguments().parse_args()
poisoned_data, ascent_model = train(opt)
print('----------- Calculate loss value per example -----------')
losses_idx = compute_loss_value(opt, poisoned_data, ascent_model)
print('----------- Collect isolation data -----------')
isolate_data(opt, poisoned_data, losses_idx)
if (__name__ == '__main__'):
main()