未验证 提交 d48b3ff7 编写于 作者: F Feng Ni 提交者: GitHub

fit for fcos (#2133)

上级 4b7917cd
...@@ -17,11 +17,9 @@ ResNet: ...@@ -17,11 +17,9 @@ ResNet:
num_stages: 4 num_stages: 4
FPN: FPN:
in_channels: [256, 512, 1024, 2048]
out_channel: 256 out_channel: 256
min_level: 1 spatial_scales: [0.125, 0.0625, 0.03125]
max_level: 5 extra_stage: 2
spatial_scale: [0.125, 0.0625, 0.03125]
has_extra_convs: true has_extra_convs: true
use_c5: false use_c5: false
......
...@@ -17,7 +17,7 @@ from __future__ import division ...@@ -17,7 +17,7 @@ from __future__ import division
from __future__ import print_function from __future__ import print_function
import paddle import paddle
from ppdet.core.workspace import register from ppdet.core.workspace import register, create
from .meta_arch import BaseArch from .meta_arch import BaseArch
__all__ = ['FCOS'] __all__ = ['FCOS']
...@@ -26,12 +26,7 @@ __all__ = ['FCOS'] ...@@ -26,12 +26,7 @@ __all__ = ['FCOS']
@register @register
class FCOS(BaseArch): class FCOS(BaseArch):
__category__ = 'architecture' __category__ = 'architecture'
__inject__ = [ __inject__ = ['fcos_post_process']
'backbone',
'neck',
'fcos_head',
'fcos_post_process',
]
def __init__(self, def __init__(self,
backbone, backbone,
...@@ -44,16 +39,32 @@ class FCOS(BaseArch): ...@@ -44,16 +39,32 @@ class FCOS(BaseArch):
self.fcos_head = fcos_head self.fcos_head = fcos_head
self.fcos_post_process = fcos_post_process self.fcos_post_process = fcos_post_process
def model_arch(self, ): @classmethod
body_feats = self.backbone(self.inputs) 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: if not self.training:
self.bboxes = self.fcos_post_process(self.fcos_head_outs, scale_factor = self.inputs['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, ): def get_loss(self, ):
loss = {} loss = {}
...@@ -70,7 +81,8 @@ class FCOS(BaseArch): ...@@ -70,7 +81,8 @@ class FCOS(BaseArch):
if k_ctn in self.inputs: if k_ctn in self.inputs:
tag_centerness.append(self.inputs[k_ctn]) 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) tag_bboxes, tag_centerness)
loss.update(loss_fcos) loss.update(loss_fcos)
total_loss = paddle.add_n(list(loss.values())) total_loss = paddle.add_n(list(loss.values()))
...@@ -78,9 +90,14 @@ class FCOS(BaseArch): ...@@ -78,9 +90,14 @@ class FCOS(BaseArch):
return loss return loss
def get_pred(self): def get_pred(self):
bbox, bbox_num = self.bboxes bboxes, bbox_num = self._forward()
label = bboxes[:, 0]
score = bboxes[:, 1]
bbox = bboxes[:, 2:]
output = { output = {
'bbox': bbox, 'bbox': bbox,
'bbox_num': bbox_num, 'score': score,
'label': label,
'bbox_num': bbox_num
} }
return output return output
...@@ -52,12 +52,16 @@ class FPN(Layer): ...@@ -52,12 +52,16 @@ class FPN(Layer):
self.fpn_convs = [] self.fpn_convs = []
fan = out_channel * 3 * 3 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: if i == 3:
lateral_name = 'fpn_inner_res5_sum' lateral_name = 'fpn_inner_res5_sum'
else: else:
lateral_name = 'fpn_inner_res{}_sum_lateral'.format(i + 2) 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 = self.add_sublayer(
lateral_name, lateral_name,
Conv2D( Conv2D(
...@@ -82,8 +86,9 @@ class FPN(Layer): ...@@ -82,8 +86,9 @@ class FPN(Layer):
# add extra conv levels for RetinaNet(use_c5)/FCOS(use_p5) # add extra conv levels for RetinaNet(use_c5)/FCOS(use_p5)
if self.has_extra_convs: if self.has_extra_convs:
for lvl in range(self.extra_stage): # P6 P7 ... for i in range(self.extra_stage):
if lvl == 0 and self.use_c5: lvl = ed_stage + 1 + i
if i == 0 and self.use_c5:
in_c = in_channels[-1] in_c = in_channels[-1]
else: else:
in_c = out_channel in_c = out_channel
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册