forked from VDIGPKU/M2Det
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
98 lines (90 loc) · 4.2 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
from __future__ import print_function
import os
import warnings
warnings.filterwarnings('ignore')
import time
import torch
import shutil
import argparse
from m2det import build_net
import torch.utils.data as data
import torch.backends.cudnn as cudnn
from layers.functions import PriorBox
from data import detection_collate
from configs.CC import Config
from utils.core import *
parser = argparse.ArgumentParser(description='M2Det Training')
parser.add_argument('-c', '--config', default='configs/m2det320_vgg16.py')
parser.add_argument('-d', '--dataset', default='COCO', help='VOC or COCO dataset')
parser.add_argument('--ngpu', default=1, type=int, help='gpus')
parser.add_argument('--resume_net', default=None, help='resume net for retraining')
parser.add_argument('--resume_epoch', default=0, type=int, help='resume iter for retraining')
parser.add_argument('-t', '--tensorboard', type=bool, default=False, help='Use tensorborad to show the Loss Graph')
args = parser.parse_args()
print_info('----------------------------------------------------------------------\n'
'| M2Det Training Program |\n'
'----------------------------------------------------------------------',['yellow','bold'])
logger = set_logger(args.tensorboard)
global cfg
cfg = Config.fromfile(args.config)
net = build_net('train',
size = cfg.model.input_size, # Only 320, 512, 704 and 800 are supported
config = cfg.model.m2det_config)
init_net(net, cfg, args.resume_net) # init the network with pretrained weights or resumed weights
if args.ngpu>1:
net = torch.nn.DataParallel(net)
if cfg.train_cfg.cuda:
net.cuda()
cudnn.benchmark = True
optimizer = set_optimizer(net, cfg)
criterion = set_criterion(cfg)
priorbox = PriorBox(anchors(cfg))
with torch.no_grad():
priors = priorbox.forward()
if cfg.train_cfg.cuda:
priors = priors.cuda()
if __name__ == '__main__':
net.train()
epoch = args.resume_epoch
print_info('===> Loading Dataset...',['yellow','bold'])
dataset = get_dataloader(cfg, args.dataset, 'train_sets')
epoch_size = len(dataset) // (cfg.train_cfg.per_batch_size * args.ngpu)
max_iter = getattr(cfg.train_cfg.step_lr,args.dataset)[-1] * epoch_size
stepvalues = [_*epoch_size for _ in getattr(cfg.train_cfg.step_lr, args.dataset)[:-1]]
print_info('===> Training M2Det on ' + args.dataset, ['yellow','bold'])
step_index = 0
if args.resume_epoch > 0:
start_iter = args.resume_epoch * epoch_size
else:
start_iter = 0
for iteration in range(start_iter, max_iter):
if iteration % epoch_size == 0:
batch_iterator = iter(data.DataLoader(dataset,
cfg.train_cfg.per_batch_size * args.ngpu,
shuffle=True,
num_workers=cfg.train_cfg.num_workers,
collate_fn=detection_collate))
if epoch % cfg.model.save_eposhs == 0:
save_checkpoint(net, cfg, final=False, datasetname = args.dataset, epoch=epoch)
epoch += 1
load_t0 = time.time()
if iteration in stepvalues:
step_index += 1
lr = adjust_learning_rate(optimizer, cfg.train_cfg.gamma, epoch, step_index, iteration, epoch_size, cfg)
images, targets = next(batch_iterator)
if cfg.train_cfg.cuda:
images = images.cuda()
targets = [anno.cuda() for anno in targets]
out = net(images)
optimizer.zero_grad()
loss_l, loss_c = criterion(out, priors, targets)
loss = loss_l + loss_c
write_logger({'loc_loss':loss_l.item(),
'conf_loss':loss_c.item(),
'loss':loss.item()},logger,iteration,status=args.tensorboard)
loss.backward()
optimizer.step()
load_t1 = time.time()
print_train_log(iteration, cfg.train_cfg.print_epochs,
[time.ctime(),epoch,iteration%epoch_size,epoch_size,iteration,loss_l.item(),loss_c.item(),load_t1-load_t0,lr])
save_checkpoint(net, cfg, final=True, datasetname=args.dataset,epoch=-1)