forked from yingxingde/FasterRCNN-pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
170 lines (149 loc) · 6.04 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
# -*- codingL utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from lib.model.train_val import get_training_roidb, train_net
from lib.model.config import cfg, cfg_from_file, cfg_from_list, get_output_dir, get_output_model_dir
from lib.datasets.factory import get_imdb
import lib.datasets.imdb
from lib.nets.vgg16 import VGG16
from lib.nets.resnet import Resnet
from lib.nets.network import FasterRCNN
from lib.nets.network_fpn import FasterRCNN as FPN
from lib.nets.fpn import FPN_Resnet
import argparse
import pprint
import numpy as np
import sys
import os
def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Train a Fast R-CNN network')
parser.add_argument('--cfg', dest='cfg_file',
help='optional config file',
default='./', type=str)
parser.add_argument('--weight', dest='weight',
help='initialize with pretrained model weights',
default='./',
type=str)
parser.add_argument('--imdb', dest='imdb_name',
help='dataset to train on',
default='voc_2007_trainval', type=str)
parser.add_argument('--imdbval', dest='imdbval_name',
help='dataset to validate on',
default='voc_2007_test', type=str)
parser.add_argument('--iters', dest='max_iters',
help='number of iterations to train',
default=70000, type=int)
parser.add_argument('--resume', dest='resume',
help='resume checkpoint',
default=None, type=int)
parser.add_argument('--tag', dest='tag',
help='tag of the model',
default=None, type=str)
parser.add_argument('--net', dest='net',
help='vgg16, res50, res101, res152, mobile',
default='vgg16', type=str)
parser.add_argument('--set', dest='set_cfgs',
help='set config keys', default=None,
nargs=argparse.REMAINDER)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
return args
def combined_roidb(imdb_names):
"""
Combine multiple roidbs
"""
def get_roidb(imdb_name):
imdb = get_imdb(imdb_name)
print('Loaded dataset `{:s}` for training'.format(imdb.name))
imdb.set_proposal_method(cfg.TRAIN.PROPOSAL_METHOD)
# print('Set proposal method: {:s}'.format(cfg.TRAIN.PROPOSAL_METHOD))
roidb = get_training_roidb(imdb)
return roidb
roidbs = [get_roidb(s) for s in imdb_names.split('+')]
roidb = roidbs[0]
if len(roidbs) > 1:
for r in roidbs[1:]:
roidb.extend(r)
tmp = get_imdb(imdb_names.split('+')[1])
imdb = lib.datasets.imdb.imdb(imdb_names, tmp.classes)
else:
imdb = get_imdb(imdb_names)
return imdb, roidb
if __name__ == '__main__':
args = parse_args()
print('Called with args:')
print(args)
# args.max_iters = 100000
# args.tag = 'vgg16_3'
# args.resume = 80000
# os.environ['CUDA_VISIBLE_DEVICES'] = '3'
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)
if args.set_cfgs is not None:
cfg_from_list(args.set_cfgs)
# train set
imdb, roidb = combined_roidb(args.imdb_name)
print('{:d} roidb entries'.format(len(roidb)))
# # output directory where the models are saved
# output_dir = get_output_dir(imdb, args.tag)
# output_dir = os.path.join(output_dir, cfg.TRAIN.SNAPSHOT_PREFIX)
# print('Output will be saved to `{:s}`'.format(output_dir))
# model directory where the summaries are saved during training
model_dir = get_output_model_dir(imdb, args.tag)
model_dir = os.path.join(model_dir, cfg.TRAIN.SNAPSHOT_PREFIX)
print('Model will be saved to `{:s}`'.format(model_dir))
# also add the validation set, but with no flipping images
orgflip = cfg.TRAIN.USE_FLIPPED
cfg.TRAIN.USE_FLIPPED = False
_, valroidb = combined_roidb(args.imdbval_name)
print('{:d} validation roidb entries'.format(len(valroidb)))
cfg.TRAIN.USE_FLIPPED = orgflip
# load network
if args.net == 'vgg16':
net = FasterRCNN(VGG16(feat_strdie=(16,),
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS), imdb.classes)
cfg.TRAIN.INIT_WAY = 'vgg'
# elif args.net == 'res18':
# net = FasterRCNN(Resnet(resnet_type=18, feat_strdie=(16,),
# anchor_scales=cfg.ANCHOR_SCALES,
# anchor_ratios=cfg.ANCHOR_RATIOS), imdb.classes)
# cfg.TRAIN.INIT_WAY = 'resnet'
elif args.net == 'res50':
net = FasterRCNN(Resnet(resnet_type=50, feat_strdie=(16,),
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS), imdb.classes)
cfg.TRAIN.INIT_WAY = 'resnet'
elif args.net == 'res101':
net = FasterRCNN(Resnet(resnet_type=101, feat_strdie=(16,),
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS), imdb.classes)
cfg.TRAIN.INIT_WAY = 'resnet'
elif args.net == 'fpn50':
net = FPN(FPN_Resnet(resnet_type=50, feat_strdie=(4, 8, 16, 32, 64),
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS), imdb.classes)
cfg.TRAIN.INIT_WAY = 'resnet'
elif args.net == 'fpn101':
net = FPN(FPN_Resnet(resnet_type=101, feat_strdie=(4, 8, 16, 32, 64),
anchor_scales=cfg.ANCHOR_SCALES,
anchor_ratios=cfg.ANCHOR_RATIOS), imdb.classes)
cfg.TRAIN.INIT_WAY = 'resnet'
else:
raise NotImplementedError
learn_dict = {
'disp_interval': cfg.TRAIN.DISPLAY,
'use_tensorboard': True,
'use_valid': True,
'save_point_interval': cfg.TRAIN.SAVE_POINT_INTERVAL,
'lr_decay_steps': cfg.TRAIN.STEPSIZE
}
resume = args.resume
train_net(net, imdb, roidb, valroidb, model_dir, learn_dict, resume,
pretrained_model=args.weight, max_iters=args.max_iters)