fpn.py 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

from collections import OrderedDict
W
wangguanzhong 已提交
20
import copy
21 22 23 24 25 26
from paddle import fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.initializer import Xavier
from paddle.fluid.regularizer import L2Decay

from ppdet.core.workspace import register
Y
Yuan Gao 已提交
27
from ppdet.modeling.ops import ConvNorm
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

__all__ = ['FPN']


@register
class FPN(object):
    """
    Feature Pyramid Network, see https://arxiv.org/abs/1612.03144

    Args:
        num_chan (int): number of feature channels
        min_level (int): lowest level of the backbone feature map to use
        max_level (int): highest level of the backbone feature map to use
        spatial_scale (list): feature map scaling factor
        has_extra_convs (bool): whether has extral convolutions in higher levels
Y
Yuan Gao 已提交
43
        norm_type (str|None): normalization type, 'bn'/'sync_bn'/'affine_channel'
44 45
        norm_decay (float): weight decay for normalization layer weights.
        reverse_out (bool): whether to flip the output.
46
    """
Y
Yuan Gao 已提交
47
    __shared__ = ['norm_type', 'freeze_norm']
48 49 50 51 52 53

    def __init__(self,
                 num_chan=256,
                 min_level=2,
                 max_level=6,
                 spatial_scale=[1. / 32., 1. / 16., 1. / 8., 1. / 4.],
Y
Yuan Gao 已提交
54
                 has_extra_convs=False,
Y
Yuan Gao 已提交
55
                 norm_type=None,
56
                 norm_decay=0.,
57
                 freeze_norm=False,
58 59
                 use_c5=True,
                 reverse_out=False):
Y
Yuan Gao 已提交
60
        self.freeze_norm = freeze_norm
61 62 63 64 65
        self.num_chan = num_chan
        self.min_level = min_level
        self.max_level = max_level
        self.spatial_scale = spatial_scale
        self.has_extra_convs = has_extra_convs
Y
Yuan Gao 已提交
66
        self.norm_type = norm_type
67
        self.norm_decay = norm_decay
68
        self.use_c5 = use_c5
69
        self.reverse_out = reverse_out
70 71 72 73 74

    def _add_topdown_lateral(self, body_name, body_input, upper_output):
        lateral_name = 'fpn_inner_' + body_name + '_lateral'
        topdown_name = 'fpn_topdown_' + body_name
        fan = body_input.shape[1]
Y
Yuan Gao 已提交
75 76 77 78 79 80 81 82
        if self.norm_type:
            initializer = Xavier(fan_out=fan)
            lateral = ConvNorm(
                body_input,
                self.num_chan,
                1,
                initializer=initializer,
                norm_type=self.norm_type,
83
                norm_decay=self.norm_decay,
Y
Yuan Gao 已提交
84
                freeze_norm=self.freeze_norm,
Y
Yuan Gao 已提交
85
                name=lateral_name,
Y
Yuan Gao 已提交
86
                norm_name=lateral_name)
Y
Yuan Gao 已提交
87 88 89 90 91 92 93 94 95 96 97 98
        else:
            lateral = fluid.layers.conv2d(
                body_input,
                self.num_chan,
                1,
                param_attr=ParamAttr(
                    name=lateral_name + "_w", initializer=Xavier(fan_out=fan)),
                bias_attr=ParamAttr(
                    name=lateral_name + "_b",
                    learning_rate=2.,
                    regularizer=L2Decay(0.)),
                name=lateral_name)
99 100 101 102 103 104 105 106
        if body_input.shape[2] == -1 and body_input.shape[3] == -1:
            topdown = fluid.layers.resize_nearest(
                upper_output, scale=2., name=topdown_name)
        else:
            topdown = fluid.layers.resize_nearest(
                upper_output,
                out_shape=[body_input.shape[2], body_input.shape[3]],
                name=topdown_name)
107

G
Guanghua Yu 已提交
108
        return fluid.layers.elementwise_add(lateral, topdown)
109 110 111 112 113 114 115 116 117 118 119 120 121 122

    def get_output(self, body_dict):
        """
        Add FPN onto backbone.

        Args:
            body_dict(OrderedDict): Dictionary of variables and each element is the
                output of backbone.

        Return:
            fpn_dict(OrderedDict): A dictionary represents the output of FPN with
                their name.
            spatial_scale(list): A list of multiplicative spatial scale factor.
        """
W
wangguanzhong 已提交
123
        spatial_scale = copy.deepcopy(self.spatial_scale)
124 125 126 127 128 129
        body_name_list = list(body_dict.keys())[::-1]
        num_backbone_stages = len(body_name_list)
        self.fpn_inner_output = [[] for _ in range(num_backbone_stages)]
        fpn_inner_name = 'fpn_inner_' + body_name_list[0]
        body_input = body_dict[body_name_list[0]]
        fan = body_input.shape[1]
Y
Yuan Gao 已提交
130 131 132 133 134 135 136 137
        if self.norm_type:
            initializer = Xavier(fan_out=fan)
            self.fpn_inner_output[0] = ConvNorm(
                body_input,
                self.num_chan,
                1,
                initializer=initializer,
                norm_type=self.norm_type,
138
                norm_decay=self.norm_decay,
Y
Yuan Gao 已提交
139
                freeze_norm=self.freeze_norm,
Y
Yuan Gao 已提交
140
                name=fpn_inner_name,
Y
Yuan Gao 已提交
141
                norm_name=fpn_inner_name)
Y
Yuan Gao 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154
        else:
            self.fpn_inner_output[0] = fluid.layers.conv2d(
                body_input,
                self.num_chan,
                1,
                param_attr=ParamAttr(
                    name=fpn_inner_name + "_w",
                    initializer=Xavier(fan_out=fan)),
                bias_attr=ParamAttr(
                    name=fpn_inner_name + "_b",
                    learning_rate=2.,
                    regularizer=L2Decay(0.)),
                name=fpn_inner_name)
155 156 157 158 159 160 161 162 163 164 165 166
        for i in range(1, num_backbone_stages):
            body_name = body_name_list[i]
            body_input = body_dict[body_name]
            top_output = self.fpn_inner_output[i - 1]
            fpn_inner_single = self._add_topdown_lateral(body_name, body_input,
                                                         top_output)
            self.fpn_inner_output[i] = fpn_inner_single
        fpn_dict = {}
        fpn_name_list = []
        for i in range(num_backbone_stages):
            fpn_name = 'fpn_' + body_name_list[i]
            fan = self.fpn_inner_output[i].shape[1] * 3 * 3
Y
Yuan Gao 已提交
167 168 169 170 171 172 173 174
            if self.norm_type:
                initializer = Xavier(fan_out=fan)
                fpn_output = ConvNorm(
                    self.fpn_inner_output[i],
                    self.num_chan,
                    3,
                    initializer=initializer,
                    norm_type=self.norm_type,
175
                    norm_decay=self.norm_decay,
Y
Yuan Gao 已提交
176
                    freeze_norm=self.freeze_norm,
Y
Yuan Gao 已提交
177
                    name=fpn_name,
Y
Yuan Gao 已提交
178
                    norm_name=fpn_name)
Y
Yuan Gao 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191
            else:
                fpn_output = fluid.layers.conv2d(
                    self.fpn_inner_output[i],
                    self.num_chan,
                    filter_size=3,
                    padding=1,
                    param_attr=ParamAttr(
                        name=fpn_name + "_w", initializer=Xavier(fan_out=fan)),
                    bias_attr=ParamAttr(
                        name=fpn_name + "_b",
                        learning_rate=2.,
                        regularizer=L2Decay(0.)),
                    name=fpn_name)
192 193 194
            fpn_dict[fpn_name] = fpn_output
            fpn_name_list.append(fpn_name)
        if not self.has_extra_convs and self.max_level - self.min_level == len(
W
wangguanzhong 已提交
195
                spatial_scale):
196 197 198 199 200 201 202 203 204
            body_top_name = fpn_name_list[0]
            body_top_extension = fluid.layers.pool2d(
                fpn_dict[body_top_name],
                1,
                'max',
                pool_stride=2,
                name=body_top_name + '_subsampled_2x')
            fpn_dict[body_top_name + '_subsampled_2x'] = body_top_extension
            fpn_name_list.insert(0, body_top_name + '_subsampled_2x')
W
wangguanzhong 已提交
205
            spatial_scale.insert(0, spatial_scale[0] * 0.5)
206
        # Coarser FPN levels introduced for RetinaNet
W
wangguanzhong 已提交
207
        highest_backbone_level = self.min_level + len(spatial_scale) - 1
208
        if self.has_extra_convs and self.max_level > highest_backbone_level:
209 210 211 212
            if self.use_c5:
                fpn_blob = body_dict[body_name_list[0]]
            else:
                fpn_blob = fpn_dict[fpn_name_list[0]]
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
            for i in range(highest_backbone_level + 1, self.max_level + 1):
                fpn_blob_in = fpn_blob
                fpn_name = 'fpn_' + str(i)
                if i > highest_backbone_level + 1:
                    fpn_blob_in = fluid.layers.relu(fpn_blob)
                fan = fpn_blob_in.shape[1] * 3 * 3
                fpn_blob = fluid.layers.conv2d(
                    input=fpn_blob_in,
                    num_filters=self.num_chan,
                    filter_size=3,
                    stride=2,
                    padding=1,
                    param_attr=ParamAttr(
                        name=fpn_name + "_w", initializer=Xavier(fan_out=fan)),
                    bias_attr=ParamAttr(
                        name=fpn_name + "_b",
                        learning_rate=2.,
                        regularizer=L2Decay(0.)),
                    name=fpn_name)
                fpn_dict[fpn_name] = fpn_blob
                fpn_name_list.insert(0, fpn_name)
W
wangguanzhong 已提交
234
                spatial_scale.insert(0, spatial_scale[0] * 0.5)
235 236 237

        if self.reverse_out:
            fpn_name_list = fpn_name_list[::-1]
238
        res_dict = OrderedDict([(k, fpn_dict[k]) for k in fpn_name_list])
W
wangguanzhong 已提交
239
        return res_dict, spatial_scale