-
Notifications
You must be signed in to change notification settings - Fork 14
/
train_badnet.py
175 lines (133 loc) · 5.52 KB
/
train_badnet.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
from models.selector import *
from utils.util import *
from data_loader import get_test_loader, get_backdoor_loader
from config import get_arguments
def train_step(opt, train_loader, nets, optimizer, criterions, epoch):
cls_losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
snet = nets['snet']
criterionCls = criterions['criterionCls']
snet.train()
for idx, (img, target) in enumerate(train_loader, start=1):
if opt.cuda:
img = img.cuda()
target = target.cuda()
_, _, _, output_s = snet(img)
cls_loss = criterionCls(output_s, target)
prec1, prec5 = accuracy(output_s, target, topk=(1, 5))
cls_losses.update(cls_loss.item(), img.size(0))
top1.update(prec1.item(), img.size(0))
top5.update(prec5.item(), img.size(0))
optimizer.zero_grad()
cls_loss.backward()
optimizer.step()
if idx % opt.print_freq == 0:
print('Epoch[{0}]:[{1:03}/{2:03}] '
'cls_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=cls_losses, top1=top1, top5=top5))
def test(opt, test_clean_loader, test_bad_loader, nets, criterions, epoch):
test_process = []
top1 = AverageMeter()
top5 = AverageMeter()
snet = nets['snet']
criterionCls = criterions['criterionCls']
snet.eval()
for idx, (img, target) in enumerate(test_clean_loader, start=1):
img = img.cuda()
target = target.cuda()
with torch.no_grad():
_, _, _, output_s = snet(img)
prec1, prec5 = accuracy(output_s, target, topk=(1, 5))
top1.update(prec1.item(), img.size(0))
top5.update(prec5.item(), img.size(0))
acc_clean = [top1.avg, top5.avg]
cls_losses = AverageMeter()
at_losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
for idx, (img, target) in enumerate(test_bad_loader, start=1):
img = img.cuda()
target = target.cuda()
with torch.no_grad():
_, _, _, output_s = snet(img)
cls_loss = criterionCls(output_s, target)
prec1, prec5 = accuracy(output_s, target, topk=(1, 5))
cls_losses.update(cls_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, cls_losses.avg]
print('[clean]Prec@1: {:.2f}'.format(acc_clean[0]))
print('[bad]Prec@1: {:.2f}'.format(acc_bd[0]))
# save training progress
log_root = opt.log_root + '/backdoor_results.csv'
test_process.append(
(epoch, acc_clean[0], acc_bd[0], acc_bd[2]))
df = pd.DataFrame(test_process, columns=(
"epoch", "test_clean_acc", "test_bad_acc", "test_bad_cls_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 --------------')
student = select_model(dataset=opt.data_name,
model_name=opt.s_name,
pretrained=False,
pretrained_models_path=opt.s_model,
n_classes=opt.num_class).to(opt.device)
print('finished student model init...')
nets = {'snet': student}
# initialize optimizer
optimizer = torch.optim.SGD(student.parameters(),
lr=opt.lr,
momentum=opt.momentum,
weight_decay=opt.weight_decay,
nesterov=True)
# define loss functions
if opt.cuda:
criterionCls = nn.CrossEntropyLoss().cuda()
else:
criterionCls = nn.CrossEntropyLoss()
print('----------- DATA Initialization --------------')
train_loader = get_backdoor_loader(opt)
test_clean_loader, test_bad_loader = get_test_loader(opt)
print('----------- Train Initialization --------------')
for epoch in range(1, opt.epochs):
_adjust_learning_rate(optimizer, epoch, opt.lr)
# train every epoch
criterions = {'criterionCls': criterionCls}
train_step(opt, train_loader, nets, optimizer, criterions, epoch)
# evaluate on testing set
print('testing the models......')
acc_clean, acc_bad = test(opt, test_clean_loader, test_bad_loader, nets, criterions, epoch)
# remember best precision and save checkpoint
if opt.save:
is_best = acc_bad[0] > opt.threshold_bad
opt.threshold_bad = min(acc_bad[0], opt.threshold_bad)
best_clean_acc = acc_clean[0]
best_bad_acc = acc_bad[0]
s_name = opt.s_name + '-S-model_best.pth'
save_checkpoint({
'epoch': epoch,
'state_dict': student.state_dict(),
'best_clean_acc': best_clean_acc,
'best_bad_acc': best_bad_acc,
'optimizer': optimizer.state_dict(),
}, is_best, opt.checkpoint_root, s_name)
def _adjust_learning_rate(optimizer, epoch, lr):
if epoch < 21:
lr = lr
elif epoch < 30:
lr = 0.01 * lr
else:
lr = 0.0009
print('epoch: {} lr: {:.4f}'.format(epoch, lr))
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def main():
# Prepare arguments
opt = get_arguments().parse_args()
train(opt)
if (__name__ == '__main__'):
main()