-
Notifications
You must be signed in to change notification settings - Fork 13
/
train_kd.py
351 lines (272 loc) · 13.4 KB
/
train_kd.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
# train a student network distilling from teacher
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.optim import SGD, Adam
from tqdm import tqdm
import argparse
import os
import logging
import numpy as np
from utils.utils import RunningAverage, set_logger, Params
from model import *
from data_loader import fetch_dataloader
# ************************** random seed **************************
seed = 0
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# ************************** parameters **************************
parser = argparse.ArgumentParser()
parser.add_argument('--save_path', default='experiments/CIFAR10/kd_normal/cnn', type=str)
parser.add_argument('--teacher_resume', default=None, type=str,
help='If you specify the teacher resume here, we will use it instead of parameters from json file')
parser.add_argument('--resume', default=None, type=str)
parser.add_argument('--gpu_id', default=[0], type=int, nargs='+', help='id(s) for CUDA_VISIBLE_DEVICES')
args = parser.parse_args()
device_ids = args.gpu_id
torch.cuda.set_device(device_ids[0])
def loss_fn_kd(outputs, labels, teacher_outputs, params):
"""
Compute the knowledge-distillation (KD) loss given outputs, labels.
"""
alpha = params.alpha
T = params.temperature
KD_loss = nn.KLDivLoss(reduction='batchmean')(F.log_softmax(outputs/T, dim=1),
F.softmax(teacher_outputs/T, dim=1)) * (alpha * T * T) + \
nn.CrossEntropyLoss()(outputs, labels) * (1. - alpha)
return KD_loss
# ************************** training function **************************
def train_epoch_kd(model, t_model, optim, loss_fn_kd, data_loader, params):
model.train()
t_model.eval()
loss_avg = RunningAverage()
with tqdm(total=len(data_loader)) as t: # Use tqdm for progress bar
for i, (train_batch, labels_batch) in enumerate(data_loader):
if params.cuda:
train_batch = train_batch.cuda() # (B,3,32,32)
labels_batch = labels_batch.cuda() # (B,)
# compute model output and loss
output_batch = model(train_batch) # logit without SoftMax
# get one batch output from teacher_outputs list
with torch.no_grad():
output_teacher_batch = t_model(train_batch) # logit without SoftMax
# CE(output, label) + KLdiv(output, teach_out)
loss = loss_fn_kd(output_batch, labels_batch, output_teacher_batch, params)
optim.zero_grad()
loss.backward()
optim.step()
# update the average loss
loss_avg.update(loss.item())
# tqdm setting
t.set_postfix(loss='{:05.3f}'.format(loss_avg()))
t.update()
return loss_avg()
def evaluate(model, loss_fn, data_loader, params):
model.eval()
# summary for current eval loop
summ = []
with torch.no_grad():
# compute metrics over the dataset
for data_batch, labels_batch in data_loader:
if params.cuda:
data_batch = data_batch.cuda() # (B,3,32,32)
labels_batch = labels_batch.cuda() # (B,)
# compute model output
output_batch = model(data_batch)
loss = loss_fn(output_batch, labels_batch)
# extract data from torch Variable, move to cpu, convert to numpy arrays
output_batch = output_batch.cpu().numpy()
labels_batch = labels_batch.cpu().numpy()
# calculate accuracy
output_batch = np.argmax(output_batch, axis=1)
acc = 100.0 * np.sum(output_batch == labels_batch) / float(labels_batch.shape[0])
summary_batch = {'acc': acc, 'loss': loss.item()}
summ.append(summary_batch)
# compute mean of all metrics in summary
metrics_mean = {metric: np.mean([x[metric] for x in summ]) for metric in summ[0]}
return metrics_mean
def train_and_eval_kd(model, t_model, optim, loss_fn, train_loader, dev_loader, params):
best_val_acc = -1
best_epo = -1
lr = params.learning_rate
for epoch in range(params.num_epochs):
# LR schedule *****************
lr = adjust_learning_rate(optim, epoch, lr, params)
logging.info("Epoch {}/{}".format(epoch + 1, params.num_epochs))
logging.info('Learning Rate {}'.format(lr))
# ********************* one full pass over the training set *********************
train_loss = train_epoch_kd(model, t_model, optim, loss_fn, train_loader, params)
logging.info("- Train loss : {:05.3f}".format(train_loss))
# ********************* Evaluate for one epoch on validation set *********************
val_metrics = evaluate(model, nn.CrossEntropyLoss(), dev_loader, params) # {'acc':acc, 'loss':loss}
metrics_string = " ; ".join("{}: {:05.3f}".format(k, v) for k, v in val_metrics.items())
logging.info("- Eval metrics : " + metrics_string)
# save model
save_name = os.path.join(args.save_path, 'last_model.tar')
torch.save({
'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optim_dict': optim.state_dict()},
save_name)
# ********************* get the best validation accuracy *********************
val_acc = val_metrics['acc']
if val_acc >= best_val_acc:
best_epo = epoch + 1
best_val_acc = val_acc
logging.info('- New best model ')
# save best model
save_name = os.path.join(args.save_path, 'best_model.tar')
torch.save({
'epoch': epoch + 1, 'state_dict': model.state_dict(), 'optim_dict': optim.state_dict()},
save_name)
logging.info('- So far best epoch: {}, best acc: {:05.3f}'.format(best_epo, best_val_acc))
def adjust_learning_rate(opt, epoch, lr, params):
if epoch in params.schedule:
lr = lr * params.gamma
for param_group in opt.param_groups:
param_group['lr'] = lr
return lr
if __name__ == "__main__":
# ************************** set log **************************
set_logger(os.path.join(args.save_path, 'training.log'))
# #################### Load the parameters from json file #####################################
json_path = os.path.join(args.save_path, 'params.json')
assert os.path.isfile(json_path), "No json configuration file found at {}".format(json_path)
params = Params(json_path)
params.cuda = torch.cuda.is_available() # use GPU if available
for k, v in params.__dict__.items():
logging.info('{}:{}'.format(k, v))
# ########################################## Dataset ##########################################
trainloader = fetch_dataloader('train', params)
devloader = fetch_dataloader('dev', params)
# ############################################ Model ############################################
if params.dataset == 'cifar10':
num_class = 10
elif params.dataset == 'cifar100':
num_class = 100
elif params.dataset == 'tiny_imagenet':
num_class = 200
else:
num_class = 10
logging.info('Number of class: ' + str(num_class))
# ############################### Student Model ###############################
logging.info('Create Student Model --- ' + params.model_name)
# ResNet 18 / 34 / 50 ****************************************
if params.model_name == 'resnet18':
model = ResNet18(num_class=num_class)
elif params.model_name == 'resnet34':
model = ResNet34(num_class=num_class)
elif params.model_name == 'resnet50':
model = ResNet50(num_class=num_class)
# PreResNet(ResNet for CIFAR-10) 20/32/56/110 ***************
elif params.model_name.startswith('preresnet20'):
model = PreResNet(depth=20, num_classes=num_class)
elif params.model_name.startswith('preresnet32'):
model = PreResNet(depth=32, num_classes=num_class)
elif params.model_name.startswith('preresnet44'):
model = PreResNet(depth=44, num_classes=num_class)
elif params.model_name.startswith('preresnet56'):
model = PreResNet(depth=56, num_classes=num_class)
elif params.model_name.startswith('preresnet110'):
model = PreResNet(depth=110, num_classes=num_class)
# DenseNet *********************************************
elif params.model_name == 'densenet121':
model = densenet121(num_class=num_class)
elif params.model_name == 'densenet161':
model = densenet161(num_class=num_class)
elif params.model_name == 'densenet169':
model = densenet169(num_class=num_class)
# ResNeXt *********************************************
elif params.model_name == 'resnext29':
model = CifarResNeXt(cardinality=8, depth=29, num_classes=num_class)
elif params.model_name == 'mobilenetv2':
model = MobileNetV2(class_num=num_class)
elif params.model_name == 'shufflenetv2':
model = shufflenetv2(class_num=num_class)
# Basic neural network ********************************
elif params.model_name == 'net':
model = Net(num_class, params)
elif params.model_name == 'mlp':
model = MLP(num_class=num_class)
else:
model = None
print('Not support for model ' + str(params.model_name))
exit()
# ############################### Teacher Model ###############################
logging.info('Create Teacher Model --- ' + params.teacher_model)
# ResNet 18 / 34 / 50 ****************************************
if params.teacher_model == 'resnet18':
teacher_model = ResNet18(num_class=num_class)
elif params.teacher_model == 'resnet34':
teacher_model = ResNet34(num_class=num_class)
elif params.teacher_model == 'resnet50':
teacher_model = ResNet50(num_class=num_class)
# PreResNet(ResNet for CIFAR-10) 20/32/56/110 ***************
elif params.teacher_model.startswith('preresnet20'):
teacher_model = PreResNet(depth=20)
elif params.teacher_model.startswith('preresnet32'):
teacher_model = PreResNet(depth=32)
elif params.teacher_model.startswith('preresnet56'):
teacher_model = PreResNet(depth=56)
elif params.teacher_model.startswith('preresnet110'):
teacher_model = PreResNet(depth=110)
# DenseNet *********************************************
elif params.teacher_model == 'densenet121':
teacher_model = densenet121(num_class=num_class)
elif params.teacher_model == 'densenet161':
teacher_model = densenet161(num_class=num_class)
elif params.teacher_model == 'densenet169':
teacher_model = densenet169(num_class=num_class)
# ResNeXt *********************************************
elif params.teacher_model == 'resnext29':
teacher_model = CifarResNeXt(cardinality=8, depth=29, num_classes=num_class)
elif params.teacher_model == 'mobilenetv2':
teacher_model = MobileNetV2(class_num=num_class)
elif params.teacher_model == 'shufflenetv2':
teacher_model = shufflenetv2(class_num=num_class)
elif params.teacher_model == 'net':
teacher_model = Net(num_class, args)
elif params.teacher_model == 'mlp':
teacher_model = MLP(num_class=num_class)
else:
teacher_model = None
exit()
if params.cuda:
model = model.cuda()
teacher_model = teacher_model.cuda()
if len(args.gpu_id) > 1:
model = nn.DataParallel(model, device_ids=device_ids)
teacher_model = nn.DataParallel(teacher_model, device_ids=device_ids)
# checkpoint ********************************
if args.resume:
logging.info('- Load checkpoint model from {}'.format(args.resume))
checkpoint = torch.load(args.resume, map_location=lambda storage, loc: storage)
model.load_state_dict(checkpoint['state_dict'])
else:
logging.info('- Train from scratch ')
# load teacher model
if args.teacher_resume:
teacher_resume = args.teacher_resume
logging.info('------ Teacher Resume from system parameters!')
else:
teacher_resume = params.teacher_resume
logging.info('- Load Trained teacher model from {}'.format(teacher_resume))
checkpoint = torch.load(teacher_resume)
teacher_model.load_state_dict(checkpoint['state_dict'])
# ############################### Optimizer ###############################
if params.model_name == 'net' or params.model_name == 'mlp':
optimizer = Adam(model.parameters(), lr=params.learning_rate)
logging.info('Optimizer: Adam')
else:
optimizer = SGD(model.parameters(), lr=params.learning_rate, momentum=0.9, weight_decay=5e-4)
logging.info('Optimizer: SGD')
# ************************** LOSS **************************
criterion = loss_fn_kd
# ************************** Teacher ACC **************************
logging.info("- Teacher Model Evaluation ....")
val_metrics = evaluate(teacher_model, nn.CrossEntropyLoss(), devloader, params) # {'acc':acc, 'loss':loss}
metrics_string = " ; ".join("{}: {:05.3f}".format(k, v) for k, v in val_metrics.items())
logging.info("- Teacher Model Eval metrics : " + metrics_string)
# ************************** train and evaluate **************************
train_and_eval_kd(model, teacher_model, optimizer, criterion, trainloader, devloader, params)