-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
369 lines (319 loc) · 13.5 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
import warnings
warnings.filterwarnings('ignore')
import os
import torch
import torch.nn as nn
import logging
import time
import random
import numpy as np
from torch.nn.parallel import DistributedDataParallel as DDP
from lib.models.builder import build_model
from lib.models.losses import CrossEntropyLabelSmooth, \
SoftTargetCrossEntropy
from lib.dataset.builder import build_dataloader
from lib.utils.optim import build_optimizer
from lib.utils.scheduler import build_scheduler
from lib.utils.args import parse_args
from lib.utils.dist_utils import init_dist, init_logger
from lib.utils.misc import accuracy, AverageMeter, \
CheckpointManager, AuxiliaryOutputBuffer
from lib.utils.model_ema import ModelEMA
from lib.utils.measure import get_params, get_flops
# torch.backends.cudnn.benchmark = True
'''init logger'''
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s',
datefmt='%H:%M:%S')
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def main():
args, args_text = parse_args()
args.exp_dir = f'experiments/{args.experiment}'
'''distributed'''
init_dist(args)
init_logger(args)
# save args
if args.rank == 0:
with open(os.path.join(args.exp_dir, 'args.yaml'), 'w') as f:
f.write(args_text)
'''fix random seed'''
seed = args.seed + args.rank
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
np.random.seed(seed)
random.seed(seed)
# torch.backends.cudnn.deterministic = True
'''build dataloader'''
train_dataset, val_dataset, train_loader, val_loader = \
build_dataloader(args)
'''build model'''
if args.mixup > 0. or args.cutmix > 0 or args.cutmix_minmax is not None:
loss_fn = SoftTargetCrossEntropy()
elif args.smoothing == 0.:
loss_fn = nn.CrossEntropyLoss().cuda()
else:
loss_fn = CrossEntropyLabelSmooth(num_classes=args.num_classes,
epsilon=args.smoothing).cuda()
val_loss_fn = loss_fn
model = build_model(args, args.model)
logger.info(model)
logger.info(
f'Model {args.model} created, params: {get_params(model) / 1e6:.3f} M, '
f'FLOPs: {get_flops(model, input_shape=args.input_shape) / 1e9:.3f} G')
# Diverse Branch Blocks
if args.dbb:
# convert 3x3 convs to dbb blocks
from lib.models.utils.dbb_converter import convert_to_dbb
convert_to_dbb(model)
logger.info(model)
logger.info(
f'Converted to DBB blocks, model params: {get_params(model) / 1e6:.3f} M, '
f'FLOPs: {get_flops(model, input_shape=args.input_shape) / 1e9:.3f} G')
model.cuda()
model = DDP(model,
device_ids=[args.rank],
find_unused_parameters=False)
# knowledge distillation
if args.kd != '':
# build teacher model
teacher_model = build_model(args, args.teacher_model, args.teacher_pretrained, args.teacher_ckpt)
logger.info(
f'Teacher model {args.teacher_model} created, params: {get_params(teacher_model) / 1e6:.3f} M, '
f'FLOPs: {get_flops(teacher_model, input_shape=args.input_shape) / 1e9:.3f} G')
teacher_model.cuda()
test_metrics = validate(args, 0, teacher_model, val_loader, val_loss_fn, log_suffix=' (teacher)')
logger.info(f'Top-1 accuracy of teacher model {args.teacher_model}: {test_metrics["top1"]:.2f}')
# build kd loss
from lib.models.losses.kd_loss import KDLoss
loss_fn = KDLoss(model, teacher_model, loss_fn, args.kd, args.student_module,
args.teacher_module, args.ori_loss_weight, args.kd_loss_weight)
if args.model_ema:
model_ema = ModelEMA(model, decay=args.model_ema_decay)
else:
model_ema = None
'''build optimizer'''
optimizer = build_optimizer(args.opt,
model.module,
args.lr,
eps=args.opt_eps,
momentum=args.momentum,
weight_decay=args.weight_decay,
filter_bias_and_bn=not args.opt_no_filter,
nesterov=not args.sgd_no_nesterov,
sort_params=args.dyrep)
'''build scheduler'''
steps_per_epoch = len(train_loader)
warmup_steps = args.warmup_epochs * steps_per_epoch
decay_steps = args.decay_epochs * steps_per_epoch
total_steps = args.epochs * steps_per_epoch
scheduler = build_scheduler(args.sched,
optimizer,
warmup_steps,
args.warmup_lr,
decay_steps,
args.decay_rate,
total_steps,
steps_per_epoch=steps_per_epoch,
decay_by_epoch=args.decay_by_epoch,
min_lr=args.min_lr)
'''dyrep'''
if args.dyrep:
from lib.models.utils.dyrep import DyRep
from lib.models.utils.recal_bn import recal_bn
dyrep = DyRep(
model.module,
optimizer,
recal_bn_fn=lambda m: recal_bn(model.module, train_loader,
args.dyrep_recal_bn_iters, m),
filter_bias_and_bn=not args.opt_no_filter)
logger.info('Init DyRep done.')
else:
dyrep = None
'''amp'''
if args.amp:
loss_scaler = torch.cuda.amp.GradScaler()
else:
loss_scaler = None
'''resume'''
ckpt_manager = CheckpointManager(model,
optimizer,
ema_model=model_ema,
save_dir=args.exp_dir,
rank=args.rank,
additions={
'scaler': loss_scaler,
'dyrep': dyrep
})
if args.resume:
start_epoch = ckpt_manager.load(args.resume) + 1
if start_epoch > args.warmup_epochs:
scheduler.finished = True
scheduler.step(start_epoch * len(train_loader))
if args.dyrep:
model = DDP(model.module,
device_ids=[args.local_rank],
find_unused_parameters=True)
logger.info(
f'Resume ckpt {args.resume} done, '
f'start training from epoch {start_epoch}'
)
else:
start_epoch = 0
'''auxiliary tower'''
if args.auxiliary:
auxiliary_buffer = AuxiliaryOutputBuffer(model, args.auxiliary_weight)
else:
auxiliary_buffer = None
'''train & val'''
for epoch in range(start_epoch, args.epochs):
train_loader.loader.sampler.set_epoch(epoch)
if args.drop_path_rate > 0. and args.drop_path_strategy == 'linear':
# update drop path rate
if hasattr(model.module, 'drop_path_rate'):
model.module.drop_path_rate = \
args.drop_path_rate * epoch / args.epochs
# train
metrics = train_epoch(args, epoch, model, model_ema, train_loader,
optimizer, loss_fn, scheduler, auxiliary_buffer,
dyrep, loss_scaler)
# validate
test_metrics = validate(args, epoch, model, val_loader, val_loss_fn)
if model_ema is not None:
test_metrics = validate(args,
epoch,
model_ema.module,
val_loader,
loss_fn,
log_suffix='(EMA)')
# dyrep
if dyrep is not None:
if epoch < args.dyrep_max_adjust_epochs:
if (epoch + 1) % args.dyrep_adjust_interval == 0:
# adjust
logger.info('DyRep: adjust model.')
dyrep.adjust_model()
logger.info(
f'Model params: {get_params(model)/1e6:.3f} M, FLOPs: {get_flops(model, input_shape=args.input_shape)/1e9:.3f} G'
)
# re-init DDP
model = DDP(model.module,
device_ids=[args.local_rank],
find_unused_parameters=True)
test_metrics = validate(args, epoch, model, val_loader, val_loss_fn)
elif args.dyrep_recal_bn_every_epoch:
logger.info('DyRep: recalibrate BN.')
recal_bn(model.module, train_loader, 200)
test_metrics = validate(args, epoch, model, val_loader, val_loss_fn)
metrics.update(test_metrics)
ckpts = ckpt_manager.update(epoch, metrics)
logger.info('\n'.join(['Checkpoints:'] + [
' {} : {:.3f}%'.format(ckpt, score) for ckpt, score in ckpts
]))
def train_epoch(args,
epoch,
model,
model_ema,
loader,
optimizer,
loss_fn,
scheduler,
auxiliary_buffer=None,
dyrep=None,
loss_scaler=None):
loss_m = AverageMeter(dist=True)
data_time_m = AverageMeter(dist=True)
batch_time_m = AverageMeter(dist=True)
start_time = time.time()
model.train()
for batch_idx, (input, target) in enumerate(loader):
data_time = time.time() - start_time
data_time_m.update(data_time)
# optimizer.zero_grad()
# use optimizer.zero_grad(set_to_none=False) for speedup
for p in model.parameters():
p.grad = None
if not args.kd:
# print(f"{os.environ['LOCAL_RANK']=}, {model.device=}, {input.device=}")
# print(input)
output = model(input)
# print(f"{len(output)=}, {output[0].size()=}, {output[1].size()=}, {target.size()=}")
loss = loss_fn(output, target)
else:
loss = loss_fn(input, target)
if auxiliary_buffer is not None:
loss_aux = loss_fn(auxiliary_buffer.output, target)
loss += loss_aux * auxiliary_buffer.loss_weight
if loss_scaler is None:
loss.backward()
else:
# amp
loss_scaler.scale(loss).backward()
if args.clip_grad_norm:
if loss_scaler is not None:
loss_scaler.unscale_(optimizer)
torch.nn.utils.clip_grad_norm_(model.parameters(),
args.clip_grad_max_norm)
if dyrep is not None:
# record states of model in dyrep
dyrep.record_metrics()
if loss_scaler is None:
optimizer.step()
else:
loss_scaler.step(optimizer)
loss_scaler.update()
if model_ema is not None:
model_ema.update(model)
loss_m.update(loss.item(), n=input.size(0))
batch_time = time.time() - start_time
batch_time_m.update(batch_time)
if batch_idx % args.log_interval == 0 or batch_idx == len(loader) - 1:
logger.info('Train: {} [{:>4d}/{}] '
'Loss: {loss.val:.3f} ({loss.avg:.3f}) '
'LR: {lr:.3e} '
'Time: {batch_time.val:.2f}s ({batch_time.avg:.2f}s) '
'Data: {data_time.val:.2f}s'.format(
epoch,
batch_idx,
len(loader),
loss=loss_m,
lr=optimizer.param_groups[0]['lr'],
batch_time=batch_time_m,
data_time=data_time_m))
scheduler.step(epoch * len(loader) + batch_idx + 1)
start_time = time.time()
return {'train_loss': loss_m.avg}
def validate(args, epoch, model, loader, loss_fn, log_suffix=''):
loss_m = AverageMeter(dist=True)
top1_m = AverageMeter(dist=True)
top5_m = AverageMeter(dist=True)
batch_time_m = AverageMeter(dist=True)
start_time = time.time()
model.eval()
for batch_idx, (input, target) in enumerate(loader):
with torch.no_grad():
output = model(input)
loss = loss_fn(output, target)
top1, top5 = accuracy(output, target, topk=(1, 5))
loss_m.update(loss.item(), n=input.size(0))
top1_m.update(top1 * 100, n=input.size(0))
top5_m.update(top5 * 100, n=input.size(0))
batch_time = time.time() - start_time
batch_time_m.update(batch_time)
if batch_idx % args.log_interval == 0 or batch_idx == len(loader) - 1:
logger.info('Test{}: {} [{:>4d}/{}] '
'Loss: {loss.val:.3f} ({loss.avg:.3f}) '
'Top-1: {top1.val:.3f}% ({top1.avg:.3f}%) '
'Top-5: {top5.val:.3f}% ({top5.avg:.3f}%) '
'Time: {batch_time.val:.2f}s'.format(
log_suffix,
epoch,
batch_idx,
len(loader),
loss=loss_m,
top1=top1_m,
top5=top5_m,
batch_time=batch_time_m))
start_time = time.time()
return {'test_loss': loss_m.avg, 'top1': top1_m.avg, 'top5': top5_m.avg}
if __name__ == '__main__':
main()