-
Notifications
You must be signed in to change notification settings - Fork 94
/
train.py
466 lines (356 loc) · 15.8 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
import sys
import os
import time
import json
import random
import math
import numpy as np
import argparse
import cv2
import h5py
import torch
import torch.nn as nn
from torch.autograd import Variable
from torchvision import datasets, transforms
import torch.nn.functional as F
from models.loss import *
from image import load_data, generate_anchor, load_data_rpn
from models.builder import *
import dataset
from mmcv import Config
from utils import save_checkpoint, is_valid_number, bbox_iou
from models.utils import load_pretrain
from models.lr_scheduler import *
parser = argparse.ArgumentParser(description='PyTorch SiameseX')
parser.add_argument('--config', metavar='model', default='configs/SiamRPN.py', type=str,
help='which model to use.')
parser.add_argument('--pre', '-p', metavar='PRETRAINED', default=None, type=str,
help='path to the pretrained model')
parser.add_argument('--gpu', metavar='GPU', default='0', type=str,
help='GPU id to use.')
def main():
global args, best_prec1, weight, segmodel
best_prec1 = 0
prec1 = 0
coco = 0
temp_args = parser.parse_args()
args = Config.fromfile(temp_args.config)
args.pre = temp_args.pre
args.gpu = temp_args.gpu
with open('./data/ilsvrc_vid_new.txt', 'r') as outfile:
args.ilsvrc = json.load(outfile)
with open('./data/vot2018_new.txt', 'r') as outfile:
args.vot2018 = json.load(outfile)
if os.path.isfile('youtube_final_new.txt'):
with open('youtube_final_new.txt', 'r') as outfile:
args.youtube = json.load(outfile)
else:
args.youtube = None
# with open('vot2018.txt', 'r') as outfile:
# args.vot2018 = json.load(outfile)
os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
torch.cuda.manual_seed(args.seed)
if args.model == 'SiamVGG':
model = SiamVGG()
elif args.model == 'SiamFC':
model = SiamFC()
elif args.model == 'SiamFCRes22':
model = SiamFCRes22()
load_pretrain(model, 'models/pretrained/CIResNet22_PRETRAIN.model')
elif args.model == 'SiamFCIncep22':
model = SiamFCIncep22()
load_pretrain(model, 'models/pretrained/CIRIncep22_PRETRAIN.model')
elif args.model == 'SiamFCNext22':
model = SiamFCNext22()
load_pretrain(model, 'models/pretrained/CIRNeXt22_PRETRAIN.model')
elif args.model == 'SiamRPN':
model = SiamRPN()
elif args.model == 'SiamRPNVGG':
model = SiamRPNVGG()
elif args.model == 'SiamRPNRes22':
model = SiamRPNRes22()
load_pretrain(model, 'models/pretrained/CIResNet22_PRETRAIN.model')
elif args.model == 'SiamRPNIncep22':
model = SiamRPNIncep22()
load_pretrain(model, 'models/pretrained/CIRIncep22_PRETRAIN.model')
elif args.model == 'SiamRPNResNeXt22':
model = SiamRPNResNeXt22()
load_pretrain(model, 'models/pretrained/CIRNeXt22_PRETRAIN.model')
elif args.model == 'SiamRPNPP':
model = SiamRPNPP()
load_pretrain(model, 'models/pretrained/ResNet50_PRETRAIN.model')
model = model.cuda()
model = model.eval()
criterion = nn.SoftMarginLoss(size_average=False).cuda() # for SiamFC and SiamVGG
if 'SiamRPNPP' in args.model:
optimizer, lr_scheduler = build_opt_lr(model, args.start_epoch)
else:
optimizer = torch.optim.SGD(model.parameters(), args.lr,
momentum=args.momentum,
weight_decay=args.decay)
if args.pre:
if os.path.isfile(args.pre):
print("=> loading checkpoint '{}'".format(args.pre))
checkpoint = torch.load(args.pre)
args.start_epoch = checkpoint['epoch']
args.start_epoch = 0
best_prec1 = checkpoint['best_prec1']
best_prec1 = 0
model.load_state_dict(checkpoint['state_dict'])
optimizer.load_state_dict(checkpoint['optimizer'])
print("=> loaded checkpoint '{}' (epoch {})"
.format(args.pre, checkpoint['epoch']))
else:
print("=> no checkpoint found at '{}'".format(args.pre))
# prec1 = 0
if not os.path.isdir('./cp/temp'):
os.makedirs('./cp/temp')
for epoch in range(args.start_epoch, args.epochs+1):
if 'SiamRPNPP' in args.model:
if args.backbone_train_epoch == epoch:
print('start training backbone.')
optimizer, lr_scheduler = build_opt_lr(model, epoch)
lr_scheduler.step(epoch)
cur_lr = lr_scheduler.get_cur_lr()
else:
cur_lr = adjust_learning_rate(optimizer, epoch)
print('current learning rate : {}'.format(cur_lr))
if args.model in ['SiamFC', 'SiamVGG', 'SiamFCRes22', 'SiamFCIncep22', 'SiamFCNext22']:
train(model, criterion, optimizer, epoch, coco)
elif args.model in ['SiamRPN', 'SiamRPNVGG', 'SiamRPNRes22', 'SiamRPNIncep22', 'SiamRPNResNeXt22']:
trainRPN(model, optimizer, epoch, coco)
elif args.model in ['SiamRPNPP']:
trainRPNPP(model, optimizer, epoch, coco)
# is_best = False
is_best = prec1 > best_prec1
best_prec1 = max(prec1, best_prec1)
if epoch % 100 == 0:
torch.save(model.state_dict(), './cp/temp/{}_{}.pth'.format(args.model, epoch))
# print(' * best MAE {mae:.3f} '
# .format(mae=best_prec1))
# print(' * MAE {mae:.3f} '
# .format(mae=prec1))
save_checkpoint({
'epoch': epoch + 1,
'arch': args.pre,
'state_dict': model.state_dict(),
'best_prec1': best_prec1,
'optimizer': optimizer.state_dict(),
}, is_best, args.model)
def train(model, criterion, optimizer, epoch, coco):
losses = AverageMeter()
batch_time = AverageMeter()
data_time = AverageMeter()
train_loader = torch.utils.data.DataLoader(
dataset.listDataset(args.ilsvrc, args.youtube, args.data_type,
shuffle=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])]),
train=True,
batch_size=args.batch_size,
num_workers=args.workers, coco=coco),
batch_size=args.batch_size)
model.train()
end = time.time()
for i, (z, x, template, gt_box)in enumerate(train_loader):
data_time.update(time.time() - end)
z = z.cuda()
z = Variable(z)
x = x.cuda()
x = Variable(x)
template = template.type(torch.FloatTensor).cuda()
template = Variable(template)
oup = model(z, x)
if isinstance(model, SiamFC) or isinstance(model, SiamVGG):
loss = criterion(oup, template)
elif isinstance(model, SiamFCRes22):
loss = model.train_loss(oup, template)
losses.update(loss.item(), x.size(0))
optimizer.zero_grad()
loss.backward()
if isinstance(model, SiamFCRes22):
torch.nn.utils.clip_grad_norm_(model.parameters(), 10) # gradient clip
if is_valid_number(loss.item()):
optimizer.step()
# optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
def trainRPN(model, optimizer, epoch, coco):
losses = AverageMeter()
batch_time = AverageMeter()
data_time = AverageMeter()
train_loader = torch.utils.data.DataLoader(
dataset.listDataset(args.ilsvrc, args.youtube, args.data_type,
shuffle=True,
transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
train=True,
batch_size=args.batch_size,
num_workers=args.workers, coco=coco),
batch_size=args.batch_size)
model.train()
end = time.time()
for i, (z, x, regression_target, conf_target) in enumerate(train_loader):
data_time.update(time.time() - end)
z = z.cuda()
z = Variable(z)
x = x.cuda()
x = Variable(x)
pred_score, pred_regression = model(z, x)
pred_conf = pred_score.reshape(-1, 2, 5 * 17 * 17).permute(0, 2, 1)
pred_offset = pred_regression.reshape(-1, 4, 5 * 17 * 17).permute(0, 2, 1)
regression_target = regression_target.type(torch.FloatTensor).cuda()
conf_target = conf_target.type(torch.LongTensor).cuda()
cls_loss = rpn_cross_entropy(pred_conf, conf_target)
reg_loss = rpn_smoothL1(pred_offset, regression_target, conf_target)
loss = cls_loss + reg_loss
losses.update(loss.item(), x.size(0))
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
def trainRPNPP(model, optimizer, epoch, coco):
losses = AverageMeter()
batch_time = AverageMeter()
data_time = AverageMeter()
train_loader = torch.utils.data.DataLoader(
dataset.listDataset(args.ilsvrc, args.youtube, args.data_type,
shuffle=True,
transform=transforms.Compose([
transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225]),
]),
train=True,
batch_size=args.batch_size,
num_workers=args.workers, coco=coco, rpnpp=True),
batch_size=args.batch_size)
model.train()
end = time.time()
for i, (z, x, regression_target, conf_target) in enumerate(train_loader):
data_time.update(time.time() - end)
z = z.cuda()
z = Variable(z)
x = x.cuda()
x = Variable(x)
pred_score, pred_regression = model(z, x)
# print(pred_score.size(), pred_regression.size())
b, a2, h, w = pred_score.size()
pred_conf = pred_score.reshape(-1, 2, 5 * h * w).permute(0, 2, 1)
pred_offset = pred_regression.reshape(-1, 4, 5 * h * w).permute(0, 2, 1)
regression_target = regression_target.type(torch.FloatTensor).cuda()
conf_target = conf_target.type(torch.LongTensor).cuda()
# print(pred_conf.size(), conf_target.size())
cls_loss = rpn_cross_entropy(pred_conf, conf_target)
reg_loss = rpn_smoothL1(pred_offset, regression_target, conf_target)
loss = cls_loss + reg_loss
losses.update(loss.item(), x.size(0))
optimizer.zero_grad()
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 10)
optimizer.step()
batch_time.update(time.time() - end)
end = time.time()
if i % args.print_freq == 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Time {batch_time.val:.3f} ({batch_time.avg:.3f})\t'
'Data {data_time.val:.3f} ({data_time.avg:.3f})\t'
'Loss {loss.val:.4f} ({loss.avg:.4f})\t'.format(
epoch, i, len(train_loader), batch_time=batch_time,
data_time=data_time, loss=losses))
def adjust_learning_rate(optimizer, epoch):
"""Sets the learning rate to the initial LR decayed by 10 every 30 epochs"""
args.lr = args.original_lr
for i in range(len(args.steps)):
scale = args.scales[i] if i < len(args.scales) else 1
if epoch >= args.steps[i]:
args.lr = args.lr * scale
if epoch == args.steps[i]:
break
else:
break
for param_group in optimizer.param_groups:
param_group['lr'] = args.lr
return args.lr
LRs = {
'log': LogScheduler,
'step': StepScheduler,
'multi-step': MultiStepScheduler,
'linear': LinearStepScheduler,
'cos': CosStepScheduler}
def _build_lr_scheduler(optimizer, lr_type, epochs=50, last_epoch=-1):
return LRs[lr_type](optimizer, last_epoch=last_epoch,
epochs=epochs, new_allowed=True)
def _build_warm_up_scheduler(optimizer, epochs=50, last_epoch=-1):
warmup_epoch = args.lr_warm_epoch
sc1 = _build_lr_scheduler(optimizer, 'step', warmup_epoch, last_epoch)
sc2 = _build_lr_scheduler(optimizer, 'log', epochs - warmup_epoch, last_epoch)
return WarmUPScheduler(optimizer, sc1, sc2, epochs, last_epoch)
def build_lr_scheduler(optimizer, epochs=50, last_epoch=-1):
if args.warmup:
return _build_warm_up_scheduler(optimizer, epochs, last_epoch)
else:
return _build_lr_scheduler(optimizer, args.original_lr, epochs, last_epoch)
def build_opt_lr(model, current_epoch=0):
if current_epoch >= 20:
for layer in ['layer2', 'layer3', 'layer4']:
for param in getattr(model.features, layer).parameters():
param.requires_grad = True
for m in getattr(model.features, layer).modules():
if isinstance(m, nn.BatchNorm2d):
m.train()
else:
for param in model.features.parameters():
param.requires_grad = False
for m in model.features.modules():
if isinstance(m, nn.BatchNorm2d):
m.eval()
trainable_params = []
trainable_params += [{'params': filter(lambda x: x.requires_grad,
model.features.parameters()),
'lr': 0.1 * args.original_lr}]
trainable_params += [{'params': model.neck.parameters(),
'lr': args.original_lr}]
trainable_params += [{'params': model.head.parameters(),
'lr': args.original_lr}]
optimizer = torch.optim.SGD(trainable_params,
momentum=args.momentum,
weight_decay=args.decay)
lr_scheduler = build_lr_scheduler(optimizer, epochs=args.epochs)
lr_scheduler.step(args.start_epoch)
return optimizer, lr_scheduler
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
if __name__ == '__main__':
main()