提交 d92ac586 编写于 作者: C chenyuntc

add support for auto lr-decay

上级 a78c14c0
...@@ -12,3 +12,5 @@ checkpoints ...@@ -12,3 +12,5 @@ checkpoints
model/utils/build/ model/utils/build/
imgs/ imgs/
*.png
*.jpg
...@@ -338,7 +338,7 @@ class FasterRCNN(nn.Module): ...@@ -338,7 +338,7 @@ class FasterRCNN(nn.Module):
cls_bbox[:, 0::2] = (cls_bbox[:, 0::2]).clamp(min = 0, max = size[0]) cls_bbox[:, 0::2] = (cls_bbox[:, 0::2]).clamp(min = 0, max = size[0])
cls_bbox[:, 1::2] = (cls_bbox[:, 1::2]).clamp(min=0,max=size[1]) cls_bbox[:, 1::2] = (cls_bbox[:, 1::2]).clamp(min=0,max=size[1])
prob = at.tonumpy(F.softmax(at.tovariable(roi_score))) prob = at.tonumpy(F.softmax(at.tovariable(roi_score),dim=1))
raw_cls_bbox = at.tonumpy(cls_bbox) raw_cls_bbox = at.tonumpy(cls_bbox)
raw_prob = at.tonumpy(prob) raw_prob = at.tonumpy(prob)
...@@ -353,7 +353,7 @@ class FasterRCNN(nn.Module): ...@@ -353,7 +353,7 @@ class FasterRCNN(nn.Module):
return bboxes, labels, scores return bboxes, labels, scores
def get_optimizer_3(self): def get_optimizer_adam(self):
self.lr1,self.lr2,self.lr3 = opt.lr1,opt.lr2,opt.lr3 self.lr1,self.lr2,self.lr3 = opt.lr1,opt.lr2,opt.lr3
param_groups = [ param_groups = [
{'params':[param for param in self.extractor.parameters() if param.requires_grad], 'lr':opt.lr1}, {'params':[param for param in self.extractor.parameters() if param.requires_grad], 'lr':opt.lr1},
......
...@@ -37,8 +37,8 @@ def decom_vgg16_chainer(pretrained=True): ...@@ -37,8 +37,8 @@ def decom_vgg16_chainer(pretrained=True):
classifier = nn.Sequential(*classifier) classifier = nn.Sequential(*classifier)
# chainer ceil mode = True for maxpooling # chainer ceil mode = True for maxpooling
for idx in [4,9,16,23]: # for idx in [4,9,16,23]:
features[idx].ceil_mode=True # features[idx].ceil_mode=True
# #
# del classifier._modules['6'] # del classifier._modules['6']
...@@ -47,8 +47,8 @@ def decom_vgg16_chainer(pretrained=True): ...@@ -47,8 +47,8 @@ def decom_vgg16_chainer(pretrained=True):
# for p in layer.parameters(): # for p in layer.parameters():
# p.requires_grad=False # p.requires_grad=False
return nn.Sequential(*features),classifier return nn.Sequential(*features),classifier
def decom_vgg16bn(pretrained=True): def decom_vgg16bn(pretrained=True):
# the 30th layer of features is relu of conv5_3 # the 30th layer of features is relu of conv5_3
model = vgg16_bn(pretrained) model = vgg16_bn(pretrained)
...@@ -130,7 +130,7 @@ class FasterRCNNVGG16(FasterRCNN): ...@@ -130,7 +130,7 @@ class FasterRCNNVGG16(FasterRCNN):
min_size=600, max_size=1000, min_size=600, max_size=1000,
ratios=[0.5, 1, 2], anchor_scales=[8, 16, 32] ratios=[0.5, 1, 2], anchor_scales=[8, 16, 32]
): ):
extractor,classifier = decom_vgg16(not opt.load_path) extractor,classifier = decom_vgg16_chainer(not opt.load_path)
rpn = RegionProposalNetwork( rpn = RegionProposalNetwork(
512, 512, 512, 512,
......
...@@ -52,7 +52,7 @@ def train(**kwargs): ...@@ -52,7 +52,7 @@ def train(**kwargs):
test_dataloader = data_.DataLoader(testset, test_dataloader = data_.DataLoader(testset,
batch_size=1, batch_size=1,
num_workers=2, num_workers=2,
shuffle=True,\ shuffle=False,\
# pin_memory=True # pin_memory=True
) )
...@@ -64,7 +64,7 @@ def train(**kwargs): ...@@ -64,7 +64,7 @@ def train(**kwargs):
print('load pretrained model from %s' %opt.load_path) print('load pretrained model from %s' %opt.load_path)
trainer.vis.text(dataset.db.label_names,win='labels') trainer.vis.text(dataset.db.label_names,win='labels')
best_map = 0
for epoch in range(opt.epoch): for epoch in range(opt.epoch):
trainer.reset_meters() trainer.reset_meters()
for ii,(img, bbox_, label_, scale, ori_img) in tqdm(enumerate(dataloader)): for ii,(img, bbox_, label_, scale, ori_img) in tqdm(enumerate(dataloader)):
...@@ -107,9 +107,16 @@ def train(**kwargs): ...@@ -107,9 +107,16 @@ def train(**kwargs):
trainer.faster_rcnn.update_optimizer(opt.lr_decay) trainer.faster_rcnn.update_optimizer(opt.lr_decay)
eval_result = eval(test_dataloader,faster_rcnn) eval_result = eval(test_dataloader,faster_rcnn)
if eval_result['map']>best_map:
best_path = trainer.save()
best_map = eval_result['map']
else:
trainer.load(best_path)
trainer.faster_rcnn.update_optimizer(opt.lr_decay)
trainer.vis.plot('test_map', eval_result['map']) trainer.vis.plot('test_map', eval_result['map'])
trainer.vis.log('map:{},loss:{},roi_cm:{}'.format(str(eval_result),str(trainer.get_meter_data()),str(trainer.rpn_cm.conf.tolist()))) trainer.vis.log('map:{},loss:{},roi_cm:{}'.format(str(eval_result),str(trainer.get_meter_data()),str(trainer.rpn_cm.conf.tolist())))
trainer.save()
# t.save(trainer.state_dict(),'checkpoints/fasterrcnn_%s.pth' %epoch) # t.save(trainer.state_dict(),'checkpoints/fasterrcnn_%s.pth' %epoch)
# t.vis.save([opt.env]) # t.vis.save([opt.env])
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册