-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
380 lines (320 loc) · 13.2 KB
/
train.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
import json
import os
import shutil
from time import time
import config
import numpy as np
import torch
import torch.nn.functional as F
import torchvision
from classifier_models import PreActResNet18, ResNet18
from networks.models import Denormalizer, NetC_MNIST, Normalizer
from torch import nn
from torch.utils.tensorboard import SummaryWriter
from torchvision.transforms import RandomErasing
from utils.dataloader import PostTensorTransform, get_dataloader
from utils.utils import progress_bar
def get_model(opt):
netC = None
optimizerC = None
schedulerC = None
if opt.dataset == "cifar10" or opt.dataset == "gtsrb":
netC = PreActResNet18(num_classes=opt.num_classes).to(opt.device)
if opt.dataset == "celeba":
netC = ResNet18().to(opt.device)
if opt.dataset == "mnist":
netC = NetC_MNIST().to(opt.device)
# Optimizer
optimizerC = torch.optim.SGD(netC.parameters(), opt.lr_C, momentum=0.9, weight_decay=5e-4)
# Scheduler
schedulerC = torch.optim.lr_scheduler.MultiStepLR(optimizerC, opt.schedulerC_milestones, opt.schedulerC_lambda)
return netC, optimizerC, schedulerC
def train(netC, optimizerC, schedulerC, train_dl, noise_grid, identity_grid, tf_writer, epoch, opt):
print(" Train:")
netC.train()
rate_bd = opt.pc
total_loss_ce = 0
total_sample = 0
total_clean = 0
total_bd = 0
total_cross = 0
total_clean_correct = 0
total_bd_correct = 0
total_cross_correct = 0
criterion_CE = torch.nn.CrossEntropyLoss()
criterion_BCE = torch.nn.BCELoss()
denormalizer = Denormalizer(opt)
transforms = PostTensorTransform(opt).to(opt.device)
total_time = 0
avg_acc_cross = 0
for batch_idx, (inputs, targets) in enumerate(train_dl):
optimizerC.zero_grad()
inputs, targets = inputs.to(opt.device), targets.to(opt.device)
bs = inputs.shape[0]
# Create backdoor data
num_bd = int(bs * rate_bd)
num_cross = int(num_bd * opt.cross_ratio)
grid_temps = (identity_grid + opt.s * noise_grid / opt.input_height) * opt.grid_rescale
grid_temps = torch.clamp(grid_temps, -1, 1)
ins = torch.rand(num_cross, opt.input_height, opt.input_height, 2).to(opt.device) * 2 - 1
grid_temps2 = grid_temps.repeat(num_cross, 1, 1, 1) + ins / opt.input_height
grid_temps2 = torch.clamp(grid_temps2, -1, 1)
inputs_bd = F.grid_sample(inputs[:num_bd], grid_temps.repeat(num_bd, 1, 1, 1), align_corners=True)
if opt.attack_mode == "all2one":
targets_bd = torch.ones_like(targets[:num_bd]) * opt.target_label
if opt.attack_mode == "all2all":
targets_bd = torch.remainder(targets[:num_bd] + 1, opt.num_classes)
inputs_cross = F.grid_sample(inputs[num_bd : (num_bd + num_cross)], grid_temps2, align_corners=True)
total_inputs = torch.cat([inputs_bd, inputs_cross, inputs[(num_bd + num_cross) :]], dim=0)
total_inputs = transforms(total_inputs)
total_targets = torch.cat([targets_bd, targets[num_bd:]], dim=0)
start = time()
total_preds = netC(total_inputs)
total_time += time() - start
loss_ce = criterion_CE(total_preds, total_targets)
loss = loss_ce
loss.backward()
optimizerC.step()
total_sample += bs
total_loss_ce += loss_ce.detach()
total_clean += bs - num_bd - num_cross
total_bd += num_bd
total_cross += num_cross
total_clean_correct += torch.sum(
torch.argmax(total_preds[(num_bd + num_cross) :], dim=1) == total_targets[(num_bd + num_cross) :]
)
total_bd_correct += torch.sum(torch.argmax(total_preds[:num_bd], dim=1) == targets_bd)
if num_cross:
total_cross_correct += torch.sum(
torch.argmax(total_preds[num_bd : (num_bd + num_cross)], dim=1)
== total_targets[num_bd : (num_bd + num_cross)]
)
avg_acc_cross = total_cross_correct * 100.0 / total_cross
avg_acc_clean = total_clean_correct * 100.0 / total_clean
avg_acc_bd = total_bd_correct * 100.0 / total_bd
avg_loss_ce = total_loss_ce / total_sample
if num_cross:
progress_bar(
batch_idx,
len(train_dl),
"CE Loss: {:.4f} | Clean Acc: {:.4f} | Bd Acc: {:.4f} | Cross Acc: {:.4f}".format(
avg_loss_ce, avg_acc_clean, avg_acc_bd, avg_acc_cross
),
)
else:
progress_bar(
batch_idx,
len(train_dl),
"CE Loss: {:.4f} | Clean Acc: {:.4f} | Bd Acc: {:.4f} ".format(avg_loss_ce, avg_acc_clean, avg_acc_bd),
)
# Save image for debugging
if not batch_idx % 50:
if not os.path.exists(opt.temps):
os.makedirs(opt.temps)
path = os.path.join(opt.temps, "backdoor_image.png")
torchvision.utils.save_image(inputs_bd, path, normalize=True)
# Image for tensorboard
if batch_idx == len(train_dl) - 2:
residual = inputs_bd - inputs[:num_bd]
batch_img = torch.cat([inputs[:num_bd], inputs_bd, total_inputs[:num_bd], residual], dim=2)
batch_img = denormalizer(batch_img)
batch_img = F.upsample(batch_img, scale_factor=(4, 4))
grid = torchvision.utils.make_grid(batch_img, normalize=True)
# for tensorboard
if not epoch % 1:
tf_writer.add_scalars(
"Clean Accuracy", {"Clean": avg_acc_clean, "Bd": avg_acc_bd, "Cross": avg_acc_cross}, epoch
)
tf_writer.add_image("Images", grid, global_step=epoch)
schedulerC.step()
def eval(
netC,
optimizerC,
schedulerC,
test_dl,
noise_grid,
identity_grid,
best_clean_acc,
best_bd_acc,
best_cross_acc,
tf_writer,
epoch,
opt,
):
print(" Eval:")
netC.eval()
total_sample = 0
total_clean_correct = 0
total_bd_correct = 0
total_cross_correct = 0
total_ae_loss = 0
criterion_BCE = torch.nn.BCELoss()
for batch_idx, (inputs, targets) in enumerate(test_dl):
with torch.no_grad():
inputs, targets = inputs.to(opt.device), targets.to(opt.device)
bs = inputs.shape[0]
total_sample += bs
# Evaluate Clean
preds_clean = netC(inputs)
total_clean_correct += torch.sum(torch.argmax(preds_clean, 1) == targets)
# Evaluate Backdoor
grid_temps = (identity_grid + opt.s * noise_grid / opt.input_height) * opt.grid_rescale
grid_temps = torch.clamp(grid_temps, -1, 1)
ins = torch.rand(bs, opt.input_height, opt.input_height, 2).to(opt.device) * 2 - 1
grid_temps2 = grid_temps.repeat(bs, 1, 1, 1) + ins / opt.input_height
grid_temps2 = torch.clamp(grid_temps2, -1, 1)
inputs_bd = F.grid_sample(inputs, grid_temps.repeat(bs, 1, 1, 1), align_corners=True)
if opt.attack_mode == "all2one":
targets_bd = torch.ones_like(targets) * opt.target_label
if opt.attack_mode == "all2all":
targets_bd = torch.remainder(targets + 1, opt.num_classes)
preds_bd = netC(inputs_bd)
total_bd_correct += torch.sum(torch.argmax(preds_bd, 1) == targets_bd)
acc_clean = total_clean_correct * 100.0 / total_sample
acc_bd = total_bd_correct * 100.0 / total_sample
# Evaluate cross
if opt.cross_ratio:
inputs_cross = F.grid_sample(inputs, grid_temps2, align_corners=True)
preds_cross = netC(inputs_cross)
total_cross_correct += torch.sum(torch.argmax(preds_cross, 1) == targets)
acc_cross = total_cross_correct * 100.0 / total_sample
info_string = (
"Clean Acc: {:.4f} - Best: {:.4f} | Bd Acc: {:.4f} - Best: {:.4f} | Cross: {:.4f}".format(
acc_clean, best_clean_acc, acc_bd, best_bd_acc, acc_cross, best_cross_acc
)
)
else:
info_string = "Clean Acc: {:.4f} - Best: {:.4f} | Bd Acc: {:.4f} - Best: {:.4f}".format(
acc_clean, best_clean_acc, acc_bd, best_bd_acc
)
progress_bar(batch_idx, len(test_dl), info_string)
# tensorboard
if not epoch % 1:
tf_writer.add_scalars("Test Accuracy", {"Clean": acc_clean, "Bd": acc_bd}, epoch)
# Save checkpoint
if acc_clean > best_clean_acc or (acc_clean > best_clean_acc - 0.1 and acc_bd > best_bd_acc):
print(" Saving...")
best_clean_acc = acc_clean
best_bd_acc = acc_bd
if opt.cross_ratio:
best_cross_acc = acc_cross
else:
best_cross_acc = torch.tensor([0])
state_dict = {
"netC": netC.state_dict(),
"schedulerC": schedulerC.state_dict(),
"optimizerC": optimizerC.state_dict(),
"best_clean_acc": best_clean_acc,
"best_bd_acc": best_bd_acc,
"best_cross_acc": best_cross_acc,
"epoch_current": epoch,
"identity_grid": identity_grid,
"noise_grid": noise_grid,
}
torch.save(state_dict, opt.ckpt_path)
with open(os.path.join(opt.ckpt_folder, "results.txt"), "w+") as f:
results_dict = {
"clean_acc": best_clean_acc.item(),
"bd_acc": best_bd_acc.item(),
"cross_acc": best_cross_acc.item(),
}
json.dump(results_dict, f, indent=2)
return best_clean_acc, best_bd_acc, best_cross_acc
def main():
opt = config.get_arguments().parse_args()
if opt.dataset in ["mnist", "cifar10"]:
opt.num_classes = 10
elif opt.dataset == "gtsrb":
opt.num_classes = 43
elif opt.dataset == "celeba":
opt.num_classes = 8
else:
raise Exception("Invalid Dataset")
if opt.dataset == "cifar10":
opt.input_height = 32
opt.input_width = 32
opt.input_channel = 3
elif opt.dataset == "gtsrb":
opt.input_height = 32
opt.input_width = 32
opt.input_channel = 3
elif opt.dataset == "mnist":
opt.input_height = 28
opt.input_width = 28
opt.input_channel = 1
elif opt.dataset == "celeba":
opt.input_height = 64
opt.input_width = 64
opt.input_channel = 3
else:
raise Exception("Invalid Dataset")
# Dataset
train_dl = get_dataloader(opt, True)
test_dl = get_dataloader(opt, False)
# prepare model
netC, optimizerC, schedulerC = get_model(opt)
# Load pretrained model
mode = opt.attack_mode
opt.ckpt_folder = os.path.join(opt.checkpoints, opt.dataset)
opt.ckpt_path = os.path.join(opt.ckpt_folder, "{}_{}_morph.pth.tar".format(opt.dataset, mode))
opt.log_dir = os.path.join(opt.ckpt_folder, "log_dir")
if not os.path.exists(opt.log_dir):
os.makedirs(opt.log_dir)
if opt.continue_training:
if os.path.exists(opt.ckpt_path):
print("Continue training!!")
state_dict = torch.load(opt.ckpt_path)
netC.load_state_dict(state_dict["netC"])
optimizerC.load_state_dict(state_dict["optimizerC"])
schedulerC.load_state_dict(state_dict["schedulerC"])
best_clean_acc = state_dict["best_clean_acc"]
best_bd_acc = state_dict["best_bd_acc"]
best_cross_acc = state_dict["best_cross_acc"]
epoch_current = state_dict["epoch_current"]
identity_grid = state_dict["identity_grid"]
noise_grid = state_dict["noise_grid"]
tf_writer = SummaryWriter(log_dir=opt.log_dir)
else:
print("Pretrained model doesnt exist")
exit()
else:
print("Train from scratch!!!")
best_clean_acc = 0.0
best_bd_acc = 0.0
best_cross_acc = 0.0
epoch_current = 0
# Prepare grid
ins = torch.rand(1, 2, opt.k, opt.k) * 2 - 1
ins = ins / torch.mean(torch.abs(ins))
noise_grid = (
F.upsample(ins, size=opt.input_height, mode="bicubic", align_corners=True)
.permute(0, 2, 3, 1)
.to(opt.device)
)
array1d = torch.linspace(-1, 1, steps=opt.input_height)
x, y = torch.meshgrid(array1d, array1d)
identity_grid = torch.stack((y, x), 2)[None, ...].to(opt.device)
shutil.rmtree(opt.ckpt_folder, ignore_errors=True)
os.makedirs(opt.log_dir)
with open(os.path.join(opt.ckpt_folder, "opt.json"), "w+") as f:
json.dump(opt.__dict__, f, indent=2)
tf_writer = SummaryWriter(log_dir=opt.log_dir)
for epoch in range(epoch_current, opt.n_iters):
print("Epoch {}:".format(epoch + 1))
train(netC, optimizerC, schedulerC, train_dl, noise_grid, identity_grid, tf_writer, epoch, opt)
best_clean_acc, best_bd_acc, best_cross_acc = eval(
netC,
optimizerC,
schedulerC,
test_dl,
noise_grid,
identity_grid,
best_clean_acc,
best_bd_acc,
best_cross_acc,
tf_writer,
epoch,
opt,
)
if __name__ == "__main__":
main()