From d48b3ff7b8f86663b0f1c8c0cc720eeeeb246f85 Mon Sep 17 00:00:00 2001 From: Feng Ni Date: Fri, 29 Jan 2021 11:07:36 +0800 Subject: [PATCH] fit for fcos (#2133) --- dygraph/configs/fcos/_base_/fcos_r50_fpn.yml | 6 +-- dygraph/ppdet/modeling/architectures/fcos.py | 49 +++++++++++++------- dygraph/ppdet/modeling/necks/fpn.py | 13 ++++-- 3 files changed, 44 insertions(+), 24 deletions(-) diff --git a/dygraph/configs/fcos/_base_/fcos_r50_fpn.yml b/dygraph/configs/fcos/_base_/fcos_r50_fpn.yml index 02ae32a4d..fd8904cb0 100644 --- a/dygraph/configs/fcos/_base_/fcos_r50_fpn.yml +++ b/dygraph/configs/fcos/_base_/fcos_r50_fpn.yml @@ -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 diff --git a/dygraph/ppdet/modeling/architectures/fcos.py b/dygraph/ppdet/modeling/architectures/fcos.py index b53702109..c8db8c0be 100644 --- a/dygraph/ppdet/modeling/architectures/fcos.py +++ b/dygraph/ppdet/modeling/architectures/fcos.py @@ -17,7 +17,7 @@ from __future__ import division 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'] @@ -26,12 +26,7 @@ __all__ = ['FCOS'] @register class FCOS(BaseArch): __category__ = 'architecture' - __inject__ = [ - 'backbone', - 'neck', - 'fcos_head', - 'fcos_post_process', - ] + __inject__ = ['fcos_post_process'] def __init__(self, backbone, @@ -44,16 +39,32 @@ class FCOS(BaseArch): 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 = {} @@ -70,7 +81,8 @@ class FCOS(BaseArch): 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())) @@ -78,9 +90,14 @@ class FCOS(BaseArch): 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 diff --git a/dygraph/ppdet/modeling/necks/fpn.py b/dygraph/ppdet/modeling/necks/fpn.py index 99434c422..aa7198ea8 100644 --- a/dygraph/ppdet/modeling/necks/fpn.py +++ b/dygraph/ppdet/modeling/necks/fpn.py @@ -52,12 +52,16 @@ class FPN(Layer): 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( @@ -82,8 +86,9 @@ class FPN(Layer): # 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 -- GitLab