-
Notifications
You must be signed in to change notification settings - Fork 77
/
train_model.py
255 lines (208 loc) · 8.99 KB
/
train_model.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
#! /usr/bin/env python3
import argparse
import copy
import os
import chainer
import numpy as np
from chainer import serializers
from chainer import training
from chainer.datasets import TransformDataset
from chainer.iterators import MultiprocessIterator
from chainer.iterators import SerialIterator
from chainer.optimizer import WeightDecay
from chainer.training import extensions
from chainercv import transforms
from chainercv.datasets import voc_bbox_label_names
from chainercv.extensions import DetectionVOCEvaluator
from chainercv.links.model.faster_rcnn import FasterRCNNTrainChain
from chainercv.links.model.ssd import GradientScaling
from chainercv.links.model.ssd import multibox_loss
from chainercv.links.model.ssd import random_crop_with_bbox_constraints
from chainercv.links.model.ssd import random_distort
from chainercv.links.model.ssd import resize_with_random_interpolation
import helper
import opt
from helper import get_detection_dataset
class ConcatenatedDataset(chainer.dataset.DatasetMixin):
def __init__(self, *datasets):
for dataset in datasets:
assert (
issubclass(type(dataset), chainer.dataset.DatasetMixin)), type(
dataset)
self._datasets = datasets
def __len__(self):
return sum(len(dataset) for dataset in self._datasets)
def get_example(self, i):
if i < 0:
raise IndexError
for dataset in self._datasets:
if i < len(dataset):
return dataset[i]
i -= len(dataset)
raise IndexError
class SSDMultiboxTrainChain(chainer.Chain):
def __init__(self, model, alpha=1, k=3):
super(SSDMultiboxTrainChain, self).__init__()
with self.init_scope():
self.model = model
self.alpha = alpha
self.k = k
def __call__(self, imgs, gt_mb_locs, gt_mb_labels):
mb_locs, mb_confs = self.model(imgs)
loc_loss, conf_loss = multibox_loss(
mb_locs, mb_confs, gt_mb_locs, gt_mb_labels, self.k)
loss = loc_loss * self.alpha + conf_loss
chainer.reporter.report(
{'loss': loss, 'loss/loc': loc_loss, 'loss/conf': conf_loss},
self)
return loss
class SSDTransform(object):
def __init__(self, coder, size, mean):
# to send cpu, make a copy
self.coder = copy.copy(coder)
self.coder.to_cpu()
self.size = size
self.mean = mean
def __call__(self, in_data):
# There are five data augmentation steps
# 1. Color augmentation
# 2. Random expansion
# 3. Random cropping
# 4. Resizing with random interpolation
# 5. Random horizontal flipping
img, bbox, label = in_data
# 1. Color augmentation
img = random_distort(img)
# 2. Random expansion
if np.random.randint(2):
img, param = transforms.random_expand(
img, fill=self.mean, return_param=True)
bbox = transforms.translate_bbox(
bbox, y_offset=param['y_offset'], x_offset=param['x_offset'])
# 3. Random cropping
img, param = random_crop_with_bbox_constraints(
img, bbox, return_param=True)
bbox, param = transforms.crop_bbox(
bbox, y_slice=param['y_slice'], x_slice=param['x_slice'],
allow_outside_center=False, return_param=True)
label = label[param['index']]
# 4. Resizing with random interpolatation
_, H, W = img.shape
img = resize_with_random_interpolation(img, (self.size, self.size))
bbox = transforms.resize_bbox(bbox, (H, W), (self.size, self.size))
img = np.clip(img, a_min=0.0, a_max=255.0)
# 5. Random horizontal flipping
img, params = transforms.random_flip(
img, x_random=True, return_param=True)
bbox = transforms.flip_bbox(
bbox, (self.size, self.size), x_flip=params['x_flip'])
# Preparation for SSD network
img -= self.mean
mb_loc, mb_label = self.coder.encode(bbox, label)
return img, mb_loc, mb_label
class FasterRCNNTransform(object):
def __init__(self, faster_rcnn):
self.faster_rcnn = faster_rcnn
def __call__(self, in_data):
img, bbox, label = in_data
_, H, W = img.shape
img = self.faster_rcnn.prepare(img)
_, o_H, o_W = img.shape
scale = o_H / H
bbox = transforms.resize_bbox(bbox, (H, W), (o_H, o_W))
# horizontally flip
img, params = transforms.random_flip(
img, x_random=True, return_param=True)
bbox = transforms.flip_bbox(
bbox, (o_H, o_W), x_flip=params['x_flip'])
return img, bbox, label, scale
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--root', required=True)
parser.add_argument('--subset', required=True)
parser.add_argument('--result', required=True)
parser.add_argument('--gpu', type=int, default=-1)
parser.add_argument('--data_type', default='clipart',
choices=opt.data_types)
parser.add_argument('--det_type', choices=opt.detectors, default='ssd300')
parser.add_argument('--resume',
help='path of the model to resume from')
parser.add_argument('--load', help='load original trained model')
parser.add_argument('--eval_root')
# Optional hyper parameters that you can change
parser.add_argument('--batchsize', type=int, default=32)
parser.add_argument('--init_lr', type=float, default=1e-5)
parser.add_argument('--max_iter', type=int, default=10000)
parser.add_argument('--log_interval', type=tuple,
default=(10, 'iteration'))
parser.add_argument('--snapshot_interval', type=int, default=5000)
parser.add_argument('--eval_interval', type=int, default=250)
args = parser.parse_args()
datasets_train = get_detection_dataset(args.data_type, args.subset,
args.root)
dataset_test = get_detection_dataset(args.data_type, 'test',
args.eval_root if args.eval_root else args.root)
model_args = {'n_fg_class': len(voc_bbox_label_names),
'pretrained_model': 'voc0712'}
model = helper.get_detector(args.det_type, model_args)
if not os.path.exists(args.result):
os.mkdir(args.result)
if args.load:
chainer.serializers.load_npz(args.load, model)
model.use_preset('evaluate')
if args.det_type == 'faster':
train_chain = FasterRCNNTrainChain(model)
train_transform = FasterRCNNTransform(model)
else:
train_chain = SSDMultiboxTrainChain(model)
train_transform = SSDTransform(model.coder, model.insize, model.mean)
if args.gpu >= 0:
chainer.cuda.get_device_from_id(args.gpu).use()
model.to_gpu()
train = TransformDataset(datasets_train, train_transform)
train_iter = MultiprocessIterator(train, args.batchsize, n_processes=4,
shared_mem=100000000)
test_iter = SerialIterator(
dataset_test, args.batchsize, repeat=False, shuffle=False)
optimizer = chainer.optimizers.MomentumSGD(lr=args.init_lr)
optimizer.setup(train_chain)
for param in train_chain.params():
if param.name == 'b':
param.update_rule.add_hook(GradientScaling(2))
else:
param.update_rule.add_hook(WeightDecay(0.0005))
updater = training.StandardUpdater(train_iter, optimizer, device=args.gpu)
trainer = training.Trainer(updater, (args.max_iter, 'iteration'),
args.result)
trainer.extend(
DetectionVOCEvaluator(
test_iter, model, use_07_metric=True,
label_names=voc_bbox_label_names),
trigger=(args.eval_interval, 'iteration'))
trainer.extend(extensions.LogReport(trigger=args.log_interval))
trainer.extend(extensions.observe_lr(), trigger=args.log_interval)
trainer.extend(extensions.PrintReport(
['epoch', 'iteration', 'lr',
'main/loss', 'main/loss/loc', 'main/loss/conf',
'validation/main/map']),
trigger=args.log_interval)
trainer.extend(extensions.ProgressBar(update_interval=10))
trainer.extend(extensions.snapshot(), trigger=(args.max_iter, 'iteration'))
trainer.extend(
extensions.snapshot_object(model, 'model_iter_{.updater.iteration}'),
trigger=(args.snapshot_interval, 'iteration'))
# Save two plot images to the result dir
if extensions.PlotReport.available():
trainer.extend(
extensions.PlotReport(
['main/loss', 'main/loss/loc', 'main/loss/conf'],
'iteration', trigger=args.log_interval,
file_name='loss.png'))
trainer.extend(
extensions.PlotReport(
['validation/main/map'], 'iteration',
trigger=(args.eval_interval, 'iteration'),
file_name='map.png'))
if args.resume:
serializers.load_npz(args.resume, trainer)
trainer.run()