-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.py
204 lines (155 loc) · 7.01 KB
/
main.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
from torch import nn
from models.selector import *
from utils.util import *
from data_loader import get_train_loader, get_test_loader
from at import AT
from config import get_arguments
def train_step(opt, train_loader, nets, optimizer, criterions, epoch):
at_losses = AverageMeter()
top1 = AverageMeter()
top5 = AverageMeter()
snet = nets['snet']
tnet = nets['tnet']
criterionCls = criterions['criterionCls']
criterionAT = criterions['criterionAT']
snet.train()
for idx, (img, target) in enumerate(train_loader, start=1):
if opt.cuda:
img = img.cuda()
target = target.cuda()
activation1_s, activation2_s, activation3_s, output_s = snet(img)
activation1_t, activation2_t, activation3_t, _ = tnet(img)
cls_loss = criterionCls(output_s, target)
at3_loss = criterionAT(activation3_s, activation3_t.detach()) * opt.beta3
at2_loss = criterionAT(activation2_s, activation2_t.detach()) * opt.beta2
at1_loss = criterionAT(activation1_s, activation1_t.detach()) * opt.beta1
at_loss = at1_loss + at2_loss + at3_loss + cls_loss
prec1, prec5 = accuracy(output_s, target, topk=(1, 5))
at_losses.update(at_loss.item(), img.size(0))
top1.update(prec1.item(), img.size(0))
top5.update(prec5.item(), img.size(0))
optimizer.zero_grad()
at_loss.backward()
optimizer.step()
if idx % opt.print_freq == 0:
print('Epoch[{0}]:[{1:03}/{2:03}] '
'AT_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=at_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']
tnet = nets['tnet']
criterionCls = criterions['criterionCls']
criterionAT = criterions['criterionAT']
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():
activation1_s, activation2_s, activation3_s, output_s = snet(img)
activation1_t, activation2_t, activation3_t, _ = tnet(img)
at3_loss = criterionAT(activation3_s, activation3_t.detach()) * opt.beta3
at2_loss = criterionAT(activation2_s, activation2_t.detach()) * opt.beta2
at1_loss = criterionAT(activation1_s, activation1_t.detach()) * opt.beta1
at_loss = at3_loss + at2_loss + at1_loss
cls_loss = criterionCls(output_s, target)
prec1, prec5 = accuracy(output_s, target, topk=(1, 5))
cls_losses.update(cls_loss.item(), img.size(0))
at_losses.update(at_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, at_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 + '/results.csv'
test_process.append(
(epoch, acc_clean[0], acc_bd[0], acc_bd[2], acc_bd[3]))
df = pd.DataFrame(test_process, columns=(
"epoch", "test_clean_acc", "test_bad_acc", "test_bad_cls_loss", "test_bad_at_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 --------------')
teacher = select_model(dataset=opt.data_name,
model_name=opt.t_name,
pretrained=True,
pretrained_models_path=opt.t_model,
n_classes=opt.num_class).to(opt.device)
print('finished teacher model init...')
student = select_model(dataset=opt.data_name,
model_name=opt.s_name,
pretrained=True,
pretrained_models_path=opt.s_model,
n_classes=opt.num_class).to(opt.device)
print('finished student model init...')
teacher.eval()
nets = {'snet': student, 'tnet': teacher}
for param in teacher.parameters():
param.requires_grad = False
# 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()
criterionAT = AT(opt.p)
else:
criterionCls = nn.CrossEntropyLoss()
criterionAT = AT(opt.p)
print('----------- DATA Initialization --------------')
train_loader = get_train_loader(opt)
test_clean_loader, test_bad_loader = get_test_loader(opt)
print('----------- Train Initialization --------------')
for epoch in range(0, opt.epochs):
adjust_learning_rate(optimizer, epoch, opt.lr)
# train every epoch
criterions = {'criterionCls': criterionCls, 'criterionAT': criterionAT}
if epoch == 0:
# before training test firstly
test(opt, test_clean_loader, test_bad_loader, nets,
criterions, epoch)
train_step(opt, train_loader, nets, optimizer, criterions, epoch+1)
# evaluate on testing set
print('testing the models......')
acc_clean, acc_bad = test(opt, test_clean_loader, test_bad_loader, nets, criterions, epoch+1)
# remember best precision and save checkpoint
# save_root = opt.checkpoint_root + '/' + opt.s_name
if opt.save:
is_best = acc_clean[0] > opt.threshold_clean
opt.threshold_clean = min(acc_bad[0], opt.threshold_clean)
best_clean_acc = acc_clean[0]
best_bad_acc = acc_bad[0]
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, opt.s_name)
def main():
# Prepare arguments
opt = get_arguments().parse_args()
train(opt)
if (__name__ == '__main__'):
main()