-
Notifications
You must be signed in to change notification settings - Fork 10
/
train.py
349 lines (326 loc) · 12.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
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
import os
import torch
torch.set_num_threads(2)
import torch.nn as nn
import torch.optim as optim
import torch.backends.cudnn as cudnn
import torch.nn.init as init
import argparse
from torch.autograd import Variable
import torch.utils.data as data
from data import COCODetection, VOCDetection, BaseTransform, preproc
from layers.modules import MultiBoxLoss, HSDMultiBoxLoss
from layers.functions import Detect
from utils.nms_wrapper import nms, soft_nms
from configs.config import cfg, cfg_from_file
import numpy as np
import time
import os
import sys
import pickle
import datetime
from models.model_builder import SSD
import yaml
def arg_parse():
parser = argparse.ArgumentParser(description='HSD Training')
parser.add_argument(
'--cfg',
dest='cfg_file',
required=True,
help='Config file for training (and optionally testing)')
parser.add_argument(
'--num_workers',
default=8,
type=int,
help='Number of workers used in dataloading')
parser.add_argument('--ngpu', default=2, 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(
'--save_folder',
default='./weights/hsd',
help='Location to save checkpoint models')
args = parser.parse_args()
return args
def detection_collate(batch):
"""Custom collate fn for dealing with batches of images that have a different
number of associated object annotations (bounding boxes).
Arguments:
batch: (tuple) A tuple of tensor images and lists of annotations
Return:
A tuple containing:
1) (tensor) batch of images stacked on their 0 dim
2) (list of tensors) annotations for a given image are stacked on 0 dim
"""
targets = []
imgs = []
img_info = []
for sample in batch:
imgs.append(sample[0])
targets.append(torch.FloatTensor(sample[1]))
img_info.append(torch.FloatTensor(sample[2]))
return torch.stack(imgs, 0), targets, img_info
def adjust_learning_rate(optimizer, epoch, step_epoch, gamma, epoch_size,
iteration):
"""Sets the learning rate
# Adapted from PyTorch Imagenet example:
# https://github.com/pytorch/examples/blob/master/imagenet/main.py
"""
## warmup
if epoch <= cfg.TRAIN.WARMUP_EPOCH:
if cfg.TRAIN.WARMUP:
iteration += (epoch_size * (epoch - 1))
lr = 1e-6 + (cfg.SOLVER.BASE_LR - 1e-6) * iteration / (
epoch_size * cfg.TRAIN.WARMUP_EPOCH)
else:
lr = cfg.SOLVER.BASE_LR
else:
div = 0
if epoch > step_epoch[-1]:
div = len(step_epoch) - 1
else:
for idx, v in enumerate(step_epoch):
if epoch > step_epoch[idx] and epoch <= step_epoch[idx + 1]:
div = idx
break
lr = cfg.SOLVER.BASE_LR * (gamma**div)
for param_group in optimizer.param_groups:
param_group['lr'] = lr
return lr
def train(train_loader, net, criterion, optimizer, epoch, epoch_step, gamma,
end_epoch, cfg):
net.train()
begin = time.time()
epoch_size = len(train_loader)
for iteration, (imgs, targets, _) in enumerate(train_loader):
t0 = time.time()
lr = adjust_learning_rate(optimizer, epoch, epoch_step, gamma,
epoch_size, iteration)
imgs = imgs.cuda()
imgs.requires_grad_()
with torch.no_grad():
targets = [anno.cuda() for anno in targets]
output = net(imgs)
optimizer.zero_grad()
if not cfg.MODEL.CASCADE:
ssd_criterion = criterion[0]
loss_l, loss_c = ssd_criterion(output, targets)
loss = loss_l + loss_c
else:
arm_criterion = criterion[0]
odm_criterion = criterion[1]
arm_loss_l, arm_loss_c = arm_criterion(output, targets)
odm_loss_l, odm_loss_c = odm_criterion(
output, targets, use_arm=True, filter_object=True)
loss = arm_loss_l + arm_loss_c + odm_loss_l + odm_loss_c
loss.backward()
optimizer.step()
t1 = time.time()
iteration_time = t1 - t0
all_time = ((end_epoch - epoch) * epoch_size +
(epoch_size - iteration)) * iteration_time
eta = str(datetime.timedelta(seconds=int(all_time)))
if iteration % 10 == 0:
if not cfg.MODEL.CASCADE:
print('Epoch:' + repr(epoch) + ' || epochiter: ' +
repr(iteration % epoch_size) + '/' + repr(epoch_size) +
' || L: %.4f C: %.4f||' %
(loss_l.item(), loss_c.item()) +
'iteration time: %.4f sec. ||' % (t1 - t0) +
'LR: %.5f' % (lr) + ' || eta time: {}'.format(eta))
else:
print('Epoch:' + repr(epoch) + ' || epochiter: ' +
repr(iteration % epoch_size) + '/' + repr(epoch_size) +
'|| 1st_L: %.4f 1st_C: %.4f||' %
(arm_loss_l.item(), arm_loss_c.item()) +
' 2rd_L: %.4f 2rd_C: %.4f||' %
(odm_loss_l.item(), odm_loss_c.item()) +
' loss: %.4f||' % (loss.item()) +
'iteration time: %.4f sec. ||' % (t1 - t0) +
'LR: %.5f' % (lr) + ' || eta time: {}'.format(eta))
def save_checkpoint(net, epoch, size, optimizer):
save_name = os.path.join(
args.save_folder,
cfg.MODEL.TYPE + "_epoch_{}_{}".format(str(epoch), str(size)) + '.pth')
torch.save({
'epoch': epoch,
'size': size,
'batch_size': cfg.TRAIN.BATCH_SIZE,
'model': net.state_dict(),
'optimizer': optimizer.state_dict()
}, save_name)
def eval_net(val_dataset,
val_loader,
net,
detector,
cfg,
transform,
max_per_image=300,
thresh=0.01,
batch_size=1):
net.eval()
num_images = len(val_dataset)
num_classes = cfg.MODEL.NUM_CLASSES
eval_save_folder = "./eval/"
if not os.path.exists(eval_save_folder):
os.mkdir(eval_save_folder)
all_boxes = [[[] for _ in range(num_images)] for _ in range(num_classes)]
det_file = os.path.join(eval_save_folder, 'detections.pkl')
st = time.time()
for idx, (imgs, _, img_info) in enumerate(val_loader):
with torch.no_grad():
t1 = time.time()
x = imgs
x = x.cuda()
output = net(x)
t4 = time.time()
boxes, scores = detector.forward(output)
t2 = time.time()
for k in range(boxes.size(0)):
i = idx * batch_size + k
boxes_ = boxes[k]
scores_ = scores[k]
boxes_ = boxes_.cpu().numpy()
scores_ = scores_.cpu().numpy()
img_wh = img_info[k]
scale = np.array([img_wh[0], img_wh[1], img_wh[0], img_wh[1]])
boxes_ *= scale
for j in range(1, num_classes):
inds = np.where(scores_[:, j] > thresh)[0]
if len(inds) == 0:
all_boxes[j][i] = np.empty([0, 5], dtype=np.float32)
continue
c_bboxes = boxes_[inds]
c_scores = scores_[inds, j]
c_dets = np.hstack((c_bboxes,
c_scores[:, np.newaxis])).astype(
np.float32, copy=False)
keep = nms(c_dets, cfg.TEST.NMS_OVERLAP, force_cpu=True)
keep = keep[:50]
c_dets = c_dets[keep, :]
all_boxes[j][i] = c_dets
t3 = time.time()
detect_time = t2 - t1
nms_time = t3 - t2
forward_time = t4 - t1
if idx % 10 == 0:
print('im_detect: {:d}/{:d} {:.3f}s {:.3f}s {:.3f}s'.format(
i + 1, num_images, forward_time, detect_time, nms_time))
print("detect time: ", time.time() - st)
with open(det_file, 'wb') as f:
pickle.dump(all_boxes, f, pickle.HIGHEST_PROTOCOL)
print('Evaluating detections')
val_dataset.evaluate_detections(all_boxes, eval_save_folder)
def main():
global args
args = arg_parse()
cfg_from_file(args.cfg_file)
save_folder = args.save_folder
batch_size = cfg.TRAIN.BATCH_SIZE
bgr_means = cfg.TRAIN.BGR_MEAN
p = 0.6
gamma = cfg.SOLVER.GAMMA
momentum = cfg.SOLVER.MOMENTUM
weight_decay = cfg.SOLVER.WEIGHT_DECAY
size = cfg.MODEL.SIZE
thresh = cfg.TEST.CONFIDENCE_THRESH
if cfg.DATASETS.DATA_TYPE == 'VOC':
trainvalDataset = VOCDetection
top_k = 200
else:
trainvalDataset = COCODetection
top_k = 300
dataset_name = cfg.DATASETS.DATA_TYPE
dataroot = cfg.DATASETS.DATAROOT
trainSet = cfg.DATASETS.TRAIN_TYPE
valSet = cfg.DATASETS.VAL_TYPE
num_classes = cfg.MODEL.NUM_CLASSES
start_epoch = args.resume_epoch
epoch_step = cfg.SOLVER.EPOCH_STEPS
end_epoch = cfg.SOLVER.END_EPOCH
if not os.path.exists(save_folder):
os.mkdir(save_folder)
torch.set_default_tensor_type('torch.cuda.FloatTensor')
net = SSD(cfg)
print(net)
if cfg.MODEL.SIZE == '300':
size_cfg = cfg.SMALL
else:
size_cfg = cfg.BIG
optimizer = optim.SGD(
net.parameters(),
lr=cfg.SOLVER.BASE_LR,
momentum=momentum,
weight_decay=weight_decay)
if args.resume_net != None:
checkpoint = torch.load(args.resume_net)
state_dict = checkpoint['model']
from collections import OrderedDict
new_state_dict = OrderedDict()
for k, v in state_dict.items():
head = k[:7]
if head == 'module.':
name = k[7:] # remove `module.`
else:
name = k
new_state_dict[name] = v
net.load_state_dict(new_state_dict)
optimizer.load_state_dict(checkpoint['optimizer'])
print('Loading resume network...')
if args.ngpu > 1:
net = torch.nn.DataParallel(net)
net.cuda()
cudnn.benchmark = True
criterion = list()
if cfg.MODEL.CASCADE:
detector = Detect(cfg)
arm_criterion = HSDMultiBoxLoss(cfg, 2)
odm_criterion = HSDMultiBoxLoss(cfg, cfg.MODEL.NUM_CLASSES)
criterion.append(arm_criterion)
criterion.append(odm_criterion)
else:
detector = Detect(cfg)
ssd_criterion = MultiBoxLoss(cfg)
criterion.append(ssd_criterion)
TrainTransform = preproc(size_cfg.IMG_WH, bgr_means, p)
ValTransform = BaseTransform(size_cfg.IMG_WH, bgr_means, (2, 0, 1))
val_dataset = trainvalDataset(dataroot, valSet, ValTransform, dataset_name)
val_loader = data.DataLoader(
val_dataset,
batch_size,
shuffle=False,
num_workers=args.num_workers,
collate_fn=detection_collate)
for epoch in range(start_epoch + 1, end_epoch + 1):
train_dataset = trainvalDataset(dataroot, trainSet, TrainTransform,
dataset_name)
epoch_size = len(train_dataset)
train_loader = data.DataLoader(
train_dataset,
batch_size,
shuffle=True,
num_workers=args.num_workers,
collate_fn=detection_collate)
train(train_loader, net, criterion, optimizer, epoch, epoch_step,
gamma, end_epoch, cfg)
if (epoch % 10 == 0) or (epoch % 10 == 0 and epoch >= 110):
save_checkpoint(net, epoch, size, optimizer)
if (epoch >= 50 and epoch % 10 == 0):
eval_net(
val_dataset,
val_loader,
net,
detector,
cfg,
ValTransform,
top_k,
thresh=thresh,
batch_size=batch_size)
save_checkpoint(net, end_epoch, size, optimizer)
if __name__ == '__main__':
main()