Skip to content

Commit

Permalink
fit for fcos (PaddlePaddle#2133)
Browse files Browse the repository at this point in the history
  • Loading branch information
nemonameless authored Jan 29, 2021
1 parent 4b7917c commit d48b3ff
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 24 deletions.
6 changes: 2 additions & 4 deletions dygraph/configs/fcos/_base_/fcos_r50_fpn.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ ResNet:
num_stages: 4

FPN:
in_channels: [256, 512, 1024, 2048]
out_channel: 256
min_level: 1
max_level: 5
spatial_scale: [0.125, 0.0625, 0.03125]
spatial_scales: [0.125, 0.0625, 0.03125]
extra_stage: 2
has_extra_convs: true
use_c5: false

Expand Down
49 changes: 33 additions & 16 deletions dygraph/ppdet/modeling/architectures/fcos.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from __future__ import print_function

import paddle
from ppdet.core.workspace import register
from ppdet.core.workspace import register, create
from .meta_arch import BaseArch

__all__ = ['FCOS']
Expand All @@ -26,12 +26,7 @@
@register
class FCOS(BaseArch):
__category__ = 'architecture'
__inject__ = [
'backbone',
'neck',
'fcos_head',
'fcos_post_process',
]
__inject__ = ['fcos_post_process']

def __init__(self,
backbone,
Expand All @@ -44,16 +39,32 @@ def __init__(self,
self.fcos_head = fcos_head
self.fcos_post_process = fcos_post_process

def model_arch(self, ):
body_feats = self.backbone(self.inputs)
@classmethod
def from_config(cls, cfg, *args, **kwargs):
backbone = create(cfg['backbone'])

kwargs = {'input_shape': backbone.out_shape}
neck = create(cfg['neck'], **kwargs)

fpn_feats, spatial_scale = self.neck(body_feats)
kwargs = {'input_shape': neck.out_shape}
fcos_head = create(cfg['fcos_head'], **kwargs)

self.fcos_head_outs = self.fcos_head(fpn_feats, self.training)
return {
'backbone': backbone,
'neck': neck,
"fcos_head": fcos_head,
}

def _forward(self):
body_feats = self.backbone(self.inputs)
fpn_feats = self.neck(body_feats)
fcos_head_outs = self.fcos_head(fpn_feats, self.training)
if not self.training:
self.bboxes = self.fcos_post_process(self.fcos_head_outs,
self.inputs['scale_factor'])
scale_factor = self.inputs['scale_factor']
bboxes = self.fcos_post_process(fcos_head_outs, scale_factor)
return bboxes
else:
return fcos_head_outs

def get_loss(self, ):
loss = {}
Expand All @@ -70,17 +81,23 @@ def get_loss(self, ):
if k_ctn in self.inputs:
tag_centerness.append(self.inputs[k_ctn])

loss_fcos = self.fcos_head.get_loss(self.fcos_head_outs, tag_labels,
fcos_head_outs = self._forward()
loss_fcos = self.fcos_head.get_loss(fcos_head_outs, tag_labels,
tag_bboxes, tag_centerness)
loss.update(loss_fcos)
total_loss = paddle.add_n(list(loss.values()))
loss.update({'loss': total_loss})
return loss

def get_pred(self):
bbox, bbox_num = self.bboxes
bboxes, bbox_num = self._forward()
label = bboxes[:, 0]
score = bboxes[:, 1]
bbox = bboxes[:, 2:]
output = {
'bbox': bbox,
'bbox_num': bbox_num,
'score': score,
'label': label,
'bbox_num': bbox_num
}
return output
13 changes: 9 additions & 4 deletions dygraph/ppdet/modeling/necks/fpn.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ def __init__(self,
self.fpn_convs = []
fan = out_channel * 3 * 3

for i in range(len(in_channels)):
# stage index 0,1,2,3 stands for res2,res3,res4,res5 on ResNet Backbone
# 0 <= st_stage < ed_stage <= 3
st_stage = 4 - len(in_channels)
ed_stage = st_stage + len(in_channels) - 1
for i in range(st_stage, ed_stage + 1):
if i == 3:
lateral_name = 'fpn_inner_res5_sum'
else:
lateral_name = 'fpn_inner_res{}_sum_lateral'.format(i + 2)
in_c = in_channels[i]
in_c = in_channels[i - st_stage]
lateral = self.add_sublayer(
lateral_name,
Conv2D(
Expand All @@ -82,8 +86,9 @@ def __init__(self,

# add extra conv levels for RetinaNet(use_c5)/FCOS(use_p5)
if self.has_extra_convs:
for lvl in range(self.extra_stage): # P6 P7 ...
if lvl == 0 and self.use_c5:
for i in range(self.extra_stage):
lvl = ed_stage + 1 + i
if i == 0 and self.use_c5:
in_c = in_channels[-1]
else:
in_c = out_channel
Expand Down

0 comments on commit d48b3ff

Please sign in to comment.