fpn.py 4.9 KB
Newer Older
W
WenmuZhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
# copyright (c) 2021 PaddlePaddle Authors. All Rights Reserve.
#
# 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.
W
WenmuZhou 已提交
14 15 16 17
"""
This code is refer from:
https://github.com/whai362/PSENet/blob/python3/models/neck/fpn.py
"""
W
add fpn  
WenmuZhou 已提交
18 19 20 21 22 23

import paddle.nn as nn
import paddle
import math
import paddle.nn.functional as F

W
WenmuZhou 已提交
24

W
add fpn  
WenmuZhou 已提交
25
class Conv_BN_ReLU(nn.Layer):
W
WenmuZhou 已提交
26 27 28 29 30 31
    def __init__(self,
                 in_planes,
                 out_planes,
                 kernel_size=1,
                 stride=1,
                 padding=0):
W
add fpn  
WenmuZhou 已提交
32
        super(Conv_BN_ReLU, self).__init__()
W
WenmuZhou 已提交
33 34 35 36 37 38 39
        self.conv = nn.Conv2D(
            in_planes,
            out_planes,
            kernel_size=kernel_size,
            stride=stride,
            padding=padding,
            bias_attr=False)
W
add fpn  
WenmuZhou 已提交
40 41 42 43 44 45
        self.bn = nn.BatchNorm2D(out_planes, momentum=0.1)
        self.relu = nn.ReLU()

        for m in self.sublayers():
            if isinstance(m, nn.Conv2D):
                n = m._kernel_size[0] * m._kernel_size[1] * m._out_channels
W
WenmuZhou 已提交
46 47 48 49 50
                m.weight = paddle.create_parameter(
                    shape=m.weight.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Normal(
                        0, math.sqrt(2. / n)))
W
add fpn  
WenmuZhou 已提交
51
            elif isinstance(m, nn.BatchNorm2D):
W
WenmuZhou 已提交
52 53 54 55 56 57 58 59
                m.weight = paddle.create_parameter(
                    shape=m.weight.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Constant(1.0))
                m.bias = paddle.create_parameter(
                    shape=m.bias.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Constant(0.0))
W
add fpn  
WenmuZhou 已提交
60 61 62 63

    def forward(self, x):
        return self.relu(self.bn(self.conv(x)))

W
WenmuZhou 已提交
64

W
add fpn  
WenmuZhou 已提交
65 66 67 68 69
class FPN(nn.Layer):
    def __init__(self, in_channels, out_channels):
        super(FPN, self).__init__()

        # Top layer
W
WenmuZhou 已提交
70 71
        self.toplayer_ = Conv_BN_ReLU(
            in_channels[3], out_channels, kernel_size=1, stride=1, padding=0)
W
add fpn  
WenmuZhou 已提交
72
        # Lateral layers
W
WenmuZhou 已提交
73 74
        self.latlayer1_ = Conv_BN_ReLU(
            in_channels[2], out_channels, kernel_size=1, stride=1, padding=0)
W
add fpn  
WenmuZhou 已提交
75

W
WenmuZhou 已提交
76 77
        self.latlayer2_ = Conv_BN_ReLU(
            in_channels[1], out_channels, kernel_size=1, stride=1, padding=0)
W
add fpn  
WenmuZhou 已提交
78

W
WenmuZhou 已提交
79 80
        self.latlayer3_ = Conv_BN_ReLU(
            in_channels[0], out_channels, kernel_size=1, stride=1, padding=0)
W
add fpn  
WenmuZhou 已提交
81 82

        # Smooth layers
W
WenmuZhou 已提交
83 84
        self.smooth1_ = Conv_BN_ReLU(
            out_channels, out_channels, kernel_size=3, stride=1, padding=1)
W
add fpn  
WenmuZhou 已提交
85

W
WenmuZhou 已提交
86 87
        self.smooth2_ = Conv_BN_ReLU(
            out_channels, out_channels, kernel_size=3, stride=1, padding=1)
W
add fpn  
WenmuZhou 已提交
88

W
WenmuZhou 已提交
89 90
        self.smooth3_ = Conv_BN_ReLU(
            out_channels, out_channels, kernel_size=3, stride=1, padding=1)
W
add fpn  
WenmuZhou 已提交
91 92 93 94 95

        self.out_channels = out_channels * 4
        for m in self.sublayers():
            if isinstance(m, nn.Conv2D):
                n = m._kernel_size[0] * m._kernel_size[1] * m._out_channels
W
WenmuZhou 已提交
96 97 98 99 100
                m.weight = paddle.create_parameter(
                    shape=m.weight.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Normal(
                        0, math.sqrt(2. / n)))
W
add fpn  
WenmuZhou 已提交
101
            elif isinstance(m, nn.BatchNorm2D):
W
WenmuZhou 已提交
102 103 104 105 106 107 108 109
                m.weight = paddle.create_parameter(
                    shape=m.weight.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Constant(1.0))
                m.bias = paddle.create_parameter(
                    shape=m.bias.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Constant(0.0))
W
add fpn  
WenmuZhou 已提交
110

W
WenmuZhou 已提交
111 112
    def _upsample(self, x, scale=1):
        return F.upsample(x, scale_factor=scale, mode='bilinear')
W
add fpn  
WenmuZhou 已提交
113

W
WenmuZhou 已提交
114 115
    def _upsample_add(self, x, y, scale=1):
        return F.upsample(x, scale_factor=scale, mode='bilinear') + y
W
add fpn  
WenmuZhou 已提交
116 117 118 119 120 121

    def forward(self, x):
        f2, f3, f4, f5 = x
        p5 = self.toplayer_(f5)

        f4 = self.latlayer1_(f4)
W
WenmuZhou 已提交
122
        p4 = self._upsample_add(p5, f4, 2)
W
add fpn  
WenmuZhou 已提交
123 124 125
        p4 = self.smooth1_(p4)

        f3 = self.latlayer2_(f3)
W
WenmuZhou 已提交
126
        p3 = self._upsample_add(p4, f3, 2)
W
add fpn  
WenmuZhou 已提交
127 128 129
        p3 = self.smooth2_(p3)

        f2 = self.latlayer3_(f2)
W
WenmuZhou 已提交
130
        p2 = self._upsample_add(p3, f2, 2)
W
add fpn  
WenmuZhou 已提交
131 132
        p2 = self.smooth3_(p2)

W
WenmuZhou 已提交
133 134 135
        p3 = self._upsample(p3, 2)
        p4 = self._upsample(p4, 4)
        p5 = self._upsample(p5, 8)
W
add fpn  
WenmuZhou 已提交
136 137

        fuse = paddle.concat([p2, p3, p4, p5], axis=1)
W
WenmuZhou 已提交
138
        return fuse