-
Notifications
You must be signed in to change notification settings - Fork 53
/
train.py
487 lines (406 loc) · 18 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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
from __future__ import division
import os
import random
import argparse
import time
import cv2
import numpy as np
from copy import deepcopy
import torch
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
from data.voc0712 import VOCDetection
from data.coco2017 import COCODataset
from data import config
from data import BaseTransform, detection_collate
import tools
from utils import distributed_utils
from utils.com_paras_flops import FLOPs_and_Params
from utils.augmentations import SSDAugmentation, ColorAugmentation
from utils.cocoapi_evaluator import COCOAPIEvaluator
from utils.vocapi_evaluator import VOCAPIEvaluator
from utils.modules import ModelEMA
def parse_args():
parser = argparse.ArgumentParser(description='YOLO Detection')
# basic
parser.add_argument('--cuda', action='store_true', default=False,
help='use cuda.')
parser.add_argument('-bs', '--batch_size', default=16, type=int,
help='Batch size for training')
parser.add_argument('--lr', default=1e-3, type=float,
help='initial learning rate')
parser.add_argument('--wp_epoch', type=int, default=2,
help='The upper bound of warm-up')
parser.add_argument('--start_epoch', type=int, default=0,
help='start epoch to train')
parser.add_argument('-r', '--resume', default=None, type=str,
help='keep training')
parser.add_argument('--momentum', default=0.9, type=float,
help='Momentum value for optim')
parser.add_argument('--weight_decay', default=5e-4, type=float,
help='Weight decay for SGD')
parser.add_argument('--num_workers', default=8, type=int,
help='Number of workers used in dataloading')
parser.add_argument('--num_gpu', default=1, type=int,
help='Number of GPUs to train')
parser.add_argument('--eval_epoch', type=int,
default=10, help='interval between evaluations')
parser.add_argument('--tfboard', action='store_true', default=False,
help='use tensorboard')
parser.add_argument('--save_folder', default='weights/', type=str,
help='Gamma update for SGD')
parser.add_argument('--vis', action='store_true', default=False,
help='visualize target.')
# model
parser.add_argument('-v', '--version', default='yolo_v2',
help='yolov2_d19, yolov2_r50, yolov2_slim, yolov3, yolov3_spp, yolov3_tiny')
# dataset
parser.add_argument('-root', '--data_root', default='/mnt/share/ssd2/dataset',
help='dataset root')
parser.add_argument('-d', '--dataset', default='voc',
help='voc or coco')
# train trick
parser.add_argument('--no_warmup', action='store_true', default=False,
help='do not use warmup')
parser.add_argument('-ms', '--multi_scale', action='store_true', default=False,
help='use multi-scale trick')
parser.add_argument('--mosaic', action='store_true', default=False,
help='use mosaic augmentation')
parser.add_argument('--ema', action='store_true', default=False,
help='use ema training trick')
# DDP train
parser.add_argument('-dist', '--distributed', action='store_true', default=False,
help='distributed training')
parser.add_argument('--dist_url', default='env://',
help='url used to set up distributed training')
parser.add_argument('--world_size', default=1, type=int,
help='number of distributed processes')
parser.add_argument('--sybn', action='store_true', default=False,
help='use sybn.')
return parser.parse_args()
def train():
args = parse_args()
print("Setting Arguments.. : ", args)
print("----------------------------------------------------------")
# set distributed
print('World size: {}'.format(distributed_utils.get_world_size()))
if args.distributed:
distributed_utils.init_distributed_mode(args)
print("git:\n {}\n".format(distributed_utils.get_sha()))
# cuda
if args.cuda:
print('use cuda')
# cudnn.benchmark = True
device = torch.device("cuda")
else:
device = torch.device("cpu")
model_name = args.version
print('Model: ', model_name)
# load model and config file
if model_name == 'yolov2_d19':
from models.yolov2_d19 import YOLOv2D19 as yolo_net
cfg = config.yolov2_d19_cfg
elif model_name == 'yolov2_r50':
from models.yolov2_r50 import YOLOv2R50 as yolo_net
cfg = config.yolov2_r50_cfg
elif model_name == 'yolov3':
from models.yolov3 import YOLOv3 as yolo_net
cfg = config.yolov3_d53_cfg
elif model_name == 'yolov3_spp':
from models.yolov3_spp import YOLOv3Spp as yolo_net
cfg = config.yolov3_d53_cfg
elif model_name == 'yolov3_tiny':
from models.yolov3_tiny import YOLOv3tiny as yolo_net
cfg = config.yolov3_tiny_cfg
else:
print('Unknown model name...')
exit(0)
# path to save model
path_to_save = os.path.join(args.save_folder, args.dataset, args.version)
os.makedirs(path_to_save, exist_ok=True)
# multi-scale
if args.multi_scale:
print('use the multi-scale trick ...')
train_size = cfg['train_size']
val_size = cfg['val_size']
else:
train_size = val_size = cfg['train_size']
# Model ENA
if args.ema:
print('use EMA trick ...')
# dataset and evaluator
if args.dataset == 'voc':
data_dir = os.path.join(args.data_root, 'VOCdevkit')
num_classes = 20
dataset = VOCDetection(data_dir=data_dir,
transform=SSDAugmentation(train_size))
evaluator = VOCAPIEvaluator(data_root=data_dir,
img_size=val_size,
device=device,
transform=BaseTransform(val_size))
elif args.dataset == 'coco':
data_dir = os.path.join(args.data_root, 'COCO')
num_classes = 80
dataset = COCODataset(
data_dir=data_dir,
transform=SSDAugmentation(train_size))
evaluator = COCOAPIEvaluator(
data_dir=data_dir,
img_size=val_size,
device=device,
transform=BaseTransform(val_size))
else:
print('unknow dataset !! Only support voc and coco !!')
exit(0)
print('Training model on:', dataset.name)
print('The dataset size:', len(dataset))
print("----------------------------------------------------------")
# build model
anchor_size = cfg['anchor_size_voc'] if args.dataset == 'voc' else cfg['anchor_size_coco']
net = yolo_net(device=device,
input_size=train_size,
num_classes=num_classes,
trainable=True,
anchor_size=anchor_size)
model = net
model = model.to(device).train()
# SyncBatchNorm
if args.sybn and args.distributed:
print('use SyncBatchNorm ...')
model = torch.nn.SyncBatchNorm.convert_sync_batchnorm(model)
# DDP
model_without_ddp = model
if args.distributed:
model = DDP(model, device_ids=[args.gpu])
model_without_ddp = model.module
# compute FLOPs and Params
if distributed_utils.is_main_process:
model_copy = deepcopy(model_without_ddp)
model_copy.trainable = False
model_copy.eval()
FLOPs_and_Params(model=model_copy,
size=train_size,
device=device)
model_copy.trainable = True
model_copy.train()
if args.distributed:
# wait for all processes to synchronize
dist.barrier()
# dataloader
batch_size = args.batch_size * distributed_utils.get_world_size()
if args.distributed and args.num_gpu > 1:
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
batch_size=batch_size,
collate_fn=detection_collate,
num_workers=args.num_workers,
pin_memory=True,
drop_last=True,
sampler=torch.utils.data.distributed.DistributedSampler(dataset)
)
else:
# dataloader
dataloader = torch.utils.data.DataLoader(
dataset=dataset,
shuffle=True,
batch_size=batch_size,
collate_fn=detection_collate,
num_workers=args.num_workers,
pin_memory=True,
drop_last=True
)
# keep training
if args.resume is not None:
print('keep training model: %s' % (args.resume))
model.load_state_dict(torch.load(args.resume, map_location=device))
# EMA
ema = ModelEMA(model) if args.ema else None
# use tfboard
if args.tfboard:
print('use tensorboard')
from torch.utils.tensorboard import SummaryWriter
c_time = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
log_path = os.path.join('log/', args.dataset, c_time)
os.makedirs(log_path, exist_ok=True)
tblogger = SummaryWriter(log_path)
# optimizer setup
base_lr = (args.lr / 16) * batch_size
tmp_lr = base_lr
optimizer = optim.SGD(model.parameters(),
lr=base_lr,
momentum=args.momentum,
weight_decay=args.weight_decay
)
max_epoch = cfg['max_epoch']
epoch_size = len(dataloader)
best_map = -1.
warmup = not args.no_warmup
t0 = time.time()
# start training loop
for epoch in range(args.start_epoch, max_epoch):
if args.distributed:
dataloader.sampler.set_epoch(epoch)
# use step lr
if epoch in cfg['lr_epoch']:
tmp_lr = tmp_lr * 0.1
set_lr(optimizer, tmp_lr)
for iter_i, (images, targets) in enumerate(dataloader):
# WarmUp strategy for learning rate
ni = iter_i + epoch * epoch_size
# warmup
if epoch < args.wp_epoch and warmup:
nw = args.wp_epoch * epoch_size
tmp_lr = base_lr * pow(ni / nw, 4)
set_lr(optimizer, tmp_lr)
elif epoch == args.wp_epoch and iter_i == 0 and warmup:
# warmup is over
warmup = False
tmp_lr = base_lr
set_lr(optimizer, tmp_lr)
# multi-scale trick
if iter_i % 10 == 0 and iter_i > 0 and args.multi_scale:
# randomly choose a new size
r = cfg['random_size_range']
train_size = random.randint(r[0], r[1]) * 32
model.set_grid(train_size)
if args.multi_scale:
# interpolate
images = torch.nn.functional.interpolate(images, size=train_size, mode='bilinear', align_corners=False)
targets = [label.tolist() for label in targets]
# visualize labels
if args.vis:
vis_data(images, targets, train_size)
continue
# label assignment
if model_name in ['yolov2_d19', 'yolov2_r50']:
targets = tools.gt_creator(input_size=train_size,
stride=net.stride,
label_lists=targets,
anchor_size=anchor_size
)
else:
targets = tools.multi_gt_creator(input_size=train_size,
strides=net.stride,
label_lists=targets,
anchor_size=anchor_size
)
# to device
images = images.float().to(device)
targets = torch.tensor(targets).float().to(device)
# forward
conf_loss, cls_loss, box_loss, iou_loss = model(images, target=targets)
# compute loss
total_loss = conf_loss + cls_loss + box_loss + iou_loss
loss_dict = dict(conf_loss=conf_loss,
cls_loss=cls_loss,
box_loss=box_loss,
iou_loss=iou_loss,
total_loss=total_loss
)
loss_dict_reduced = distributed_utils.reduce_dict(loss_dict)
# check NAN for loss
if torch.isnan(total_loss):
print('loss is nan !!')
continue
# backprop
total_loss.backward()
optimizer.step()
optimizer.zero_grad()
# ema
if args.ema:
ema.update(model)
# display
if distributed_utils.is_main_process() and iter_i % 10 == 0:
if args.tfboard:
# viz loss
tblogger.add_scalar('conf loss', loss_dict_reduced['conf_loss'].item(), iter_i + epoch * epoch_size)
tblogger.add_scalar('cls loss', loss_dict_reduced['cls_loss'].item(), iter_i + epoch * epoch_size)
tblogger.add_scalar('box loss', loss_dict_reduced['box_loss'].item(), iter_i + epoch * epoch_size)
tblogger.add_scalar('iou loss', loss_dict_reduced['iou_loss'].item(), iter_i + epoch * epoch_size)
t1 = time.time()
cur_lr = [param_group['lr'] for param_group in optimizer.param_groups]
# basic infor
log = '[Epoch: {}/{}]'.format(epoch+1, max_epoch)
log += '[Iter: {}/{}]'.format(iter_i, epoch_size)
log += '[lr: {:.6f}]'.format(cur_lr[0])
# loss infor
for k in loss_dict_reduced.keys():
log += '[{}: {:.2f}]'.format(k, loss_dict[k])
# other infor
log += '[time: {:.2f}]'.format(t1 - t0)
log += '[size: {}]'.format(train_size)
# print log infor
print(log, flush=True)
t0 = time.time()
if distributed_utils.is_main_process():
# evaluation
if (epoch % args.eval_epoch) == 0 or (epoch == max_epoch - 1):
if args.ema:
model_eval = ema.ema
else:
model_eval = model_without_ddp
# check evaluator
if evaluator is None:
print('No evaluator ... save model and go on training.')
print('Saving state, epoch: {}'.format(epoch + 1))
weight_name = '{}_epoch_{}.pth'.format(args.version, epoch + 1)
checkpoint_path = os.path.join(path_to_save, weight_name)
torch.save(model_eval.state_dict(), checkpoint_path)
else:
print('eval ...')
# set eval mode
model_eval.trainable = False
model_eval.set_grid(val_size)
model_eval.eval()
# evaluate
evaluator.evaluate(model_eval)
cur_map = evaluator.map
if cur_map > best_map:
# update best-map
best_map = cur_map
# save model
print('Saving state, epoch:', epoch + 1)
weight_name = '{}_epoch_{}_{:.2f}.pth'.format(args.version, epoch + 1, best_map*100)
checkpoint_path = os.path.join(path_to_save, weight_name)
torch.save(model_eval.state_dict(), checkpoint_path)
if args.tfboard:
if args.dataset == 'voc':
tblogger.add_scalar('07test/mAP', evaluator.map, epoch)
elif args.dataset == 'coco':
tblogger.add_scalar('val/AP50_95', evaluator.ap50_95, epoch)
tblogger.add_scalar('val/AP50', evaluator.ap50, epoch)
# set train mode.
model_eval.trainable = True
model_eval.set_grid(train_size)
model_eval.train()
# wait for all processes to synchronize
if args.distributed:
dist.barrier()
if args.tfboard:
tblogger.close()
def set_lr(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def vis_data(images, targets, input_size):
# vis data
mean=(0.406, 0.456, 0.485)
std=(0.225, 0.224, 0.229)
mean = np.array(mean, dtype=np.float32)
std = np.array(std, dtype=np.float32)
img = images[0].permute(1, 2, 0).cpu().numpy()[:, :, ::-1]
img = ((img * std + mean)*255).astype(np.uint8)
img = img.copy()
for box in targets[0]:
xmin, ymin, xmax, ymax = box[:-1]
# print(xmin, ymin, xmax, ymax)
xmin *= input_size
ymin *= input_size
xmax *= input_size
ymax *= input_size
cv2.rectangle(img, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 0, 255), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
if __name__ == '__main__':
train()