db_fpn.py 6.2 KB
Newer Older
W
WenmuZhou 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# copyright (c) 2019 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.

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

import paddle
from paddle import nn
import paddle.nn.functional as F
from paddle import ParamAttr
L
LDOUBLEV 已提交
23
from ppocr.backbones.det_mobilenet_v3 import SEModule
W
WenmuZhou 已提交
24 25


D
dyning 已提交
26
class DBFPN(nn.Layer):
W
WenmuZhou 已提交
27
    def __init__(self, in_channels, out_channels, **kwargs):
D
dyning 已提交
28
        super(DBFPN, self).__init__()
W
WenmuZhou 已提交
29
        self.out_channels = out_channels
W
WenmuZhou 已提交
30
        weight_attr = paddle.nn.initializer.KaimingUniform()
W
WenmuZhou 已提交
31

D
dyning 已提交
32
        self.in2_conv = nn.Conv2D(
W
WenmuZhou 已提交
33 34 35
            in_channels=in_channels[0],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
36
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
37
            bias_attr=False)
D
dyning 已提交
38
        self.in3_conv = nn.Conv2D(
W
WenmuZhou 已提交
39 40 41
            in_channels=in_channels[1],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
42
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
43
            bias_attr=False)
D
dyning 已提交
44
        self.in4_conv = nn.Conv2D(
W
WenmuZhou 已提交
45 46 47
            in_channels=in_channels[2],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
48
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
49
            bias_attr=False)
D
dyning 已提交
50
        self.in5_conv = nn.Conv2D(
W
WenmuZhou 已提交
51 52 53
            in_channels=in_channels[3],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
54
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
55
            bias_attr=False)
D
dyning 已提交
56
        self.p5_conv = nn.Conv2D(
W
WenmuZhou 已提交
57 58 59 60
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
61
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
62
            bias_attr=False)
D
dyning 已提交
63
        self.p4_conv = nn.Conv2D(
W
WenmuZhou 已提交
64 65 66 67
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
68
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
69
            bias_attr=False)
D
dyning 已提交
70
        self.p3_conv = nn.Conv2D(
W
WenmuZhou 已提交
71 72 73 74
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
75
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
76
            bias_attr=False)
D
dyning 已提交
77
        self.p2_conv = nn.Conv2D(
W
WenmuZhou 已提交
78 79 80 81
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey 已提交
82
            weight_attr=ParamAttr(initializer=weight_attr),
W
WenmuZhou 已提交
83 84 85 86 87 88 89 90 91 92
            bias_attr=False)

    def forward(self, x):
        c2, c3, c4, c5 = x

        in5 = self.in5_conv(c5)
        in4 = self.in4_conv(c4)
        in3 = self.in3_conv(c3)
        in2 = self.in2_conv(c2)

W
WenmuZhou 已提交
93 94 95 96 97 98
        out4 = in4 + F.upsample(
            in5, scale_factor=2, mode="nearest", align_mode=1)  # 1/16
        out3 = in3 + F.upsample(
            out4, scale_factor=2, mode="nearest", align_mode=1)  # 1/8
        out2 = in2 + F.upsample(
            out3, scale_factor=2, mode="nearest", align_mode=1)  # 1/4
W
WenmuZhou 已提交
99 100 101 102 103

        p5 = self.p5_conv(in5)
        p4 = self.p4_conv(out4)
        p3 = self.p3_conv(out3)
        p2 = self.p2_conv(out2)
W
WenmuZhou 已提交
104 105 106
        p5 = F.upsample(p5, scale_factor=8, mode="nearest", align_mode=1)
        p4 = F.upsample(p4, scale_factor=4, mode="nearest", align_mode=1)
        p3 = F.upsample(p3, scale_factor=2, mode="nearest", align_mode=1)
W
WenmuZhou 已提交
107 108 109

        fuse = paddle.concat([p5, p4, p3, p2], axis=1)
        return fuse
L
LDOUBLEV 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181


class CALayer(nn.Layer):
    def __init__(self, in_channels, out_channels, kernel_size, shortcut=True):
        super(CALayer, self).__init__()
        weight_attr = paddle.nn.initializer.KaimingUniform()
        self.in_conv = nn.Conv2D(
            in_channels=in_channels,
            out_channels=self.out_channels,
            kernel_size=kernel_size,
            padding=int(kernel_size // 2),
            weight_attr=ParamAttr(initializer=weight_attr),
            bias_attr=False)
        self.se_block = SEModule(self.out_channels)
        self.shortcut = shortcut

    def forward(self, ins):
        x = self.in_conv(ins)
        if self.shortcut:
            out = x + self.se_block(x)
        else:
            out = self.se_block(x)
        return out


class CAFPN(nn.Layer):
    def __init__(self, in_channels, out_channels, shortcut, **kwargs):
        super(CAFPN, self).__init__()

        self.ins_convs = []
        self.inp_convs = []

        for i in range(len(in_channels)):
            self.ins_conv.append(
                CALayer(
                    in_channels[i],
                    out_channels,
                    kernel_size=1,
                    shortcut=shortcut))
            self.inp_conv.append(
                CALayer(
                    out_channels,
                    out_channels // 4,
                    kernel_size=3,
                    shortcut=shortcut))

    def forward(self, x):
        c2, c3, c4, c5 = x

        in5 = self.ins_conv[3](c5)
        in4 = self.ins_conv[2](c4)
        in3 = self.ins_conv[1](c3)
        in2 = self.ins_conv[0](c2)

        out4 = in4 + F.upsample(
            in5, scale_factor=2, mode="nearest", align_mode=1)  # 1/16
        out3 = in3 + F.upsample(
            out4, scale_factor=2, mode="nearest", align_mode=1)  # 1/8
        out2 = in2 + F.upsample(
            out3, scale_factor=2, mode="nearest", align_mode=1)  # 1/4

        p5 = self.inp_conv[3](in5)
        p4 = self.inp_conv[2](out4)
        p3 = self.inp_conv[1](out3)
        p2 = self.inp_conv[0](out2)

        p5 = F.upsample(p5, scale_factor=8, mode="nearest", align_mode=1)
        p4 = F.upsample(p4, scale_factor=4, mode="nearest", align_mode=1)
        p3 = F.upsample(p3, scale_factor=2, mode="nearest", align_mode=1)

        fuse = paddle.concat([p5, p4, p3, p2], axis=1)
        return fuse