db_fpn.py 11.4 KB
Newer Older
WenmuZhou's avatar
WenmuZhou committed
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
LDOUBLEV's avatar
LDOUBLEV committed
23
import os
LDOUBLEV's avatar
fix det  
LDOUBLEV committed
24
25
import sys

LDOUBLEV's avatar
fix  
LDOUBLEV committed
26
27
28
29
__dir__ = os.path.dirname(os.path.abspath(__file__))
sys.path.append(__dir__)
sys.path.insert(0, os.path.abspath(os.path.join(__dir__, '../../..')))

LDOUBLEV's avatar
LDOUBLEV committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from ppocr.modeling.backbones.det_mobilenet_v3 import SEModule


class ConvBNLayer(nn.Layer):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 padding,
                 stride=1,
                 groups=None,
                 if_act=True,
                 act="relu"):
        super(ConvBNLayer, self).__init__()
        if groups == None:
            groups = in_channels
        self.if_act = if_act
        self.act = act
        self.conv1 = nn.Conv2D(
            in_channels=in_channels,
            out_channels=in_channels,
            kernel_size=kernel_size,
            stride=stride,
            padding=padding,
            groups=groups,
            bias_attr=False)

        self.bn1 = nn.BatchNorm(num_channels=in_channels, act=None)

        self.conv2 = nn.Conv2D(
            in_channels=in_channels,
            out_channels=int(in_channels * 4),
            kernel_size=1,
            stride=1,
            bias_attr=False)

        self.bn2 = nn.BatchNorm(num_channels=int(in_channels * 4), act=None)

        self.conv3 = nn.Conv2D(
            in_channels=int(in_channels * 4),
            out_channels=out_channels,
            kernel_size=1,
            stride=1,
            bias_attr=False)
        self._c = [in_channels, out_channels]
        if in_channels != out_channels:
            self.conv_end = nn.Conv2D(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=1,
                stride=1,
                bias_attr=False)

    def forward(self, inputs):

        x = self.conv1(inputs)
        x = self.bn1(x)

        x = self.conv2(x)
        x = self.bn2(x)
        if self.if_act:
            if self.act == "relu":
                x = F.relu(x)
            elif self.act == "hardswish":
                x = F.hardswish(x)
            else:
                print("The activation function({}) is selected incorrectly.".
                      format(self.act))
                exit()

        x = self.conv3(x)
        if self._c[0] != self._c[1]:
            x = x + self.conv_end(inputs)
        return x
WenmuZhou's avatar
WenmuZhou committed
104
105


dyning's avatar
dyning committed
106
class DBFPN(nn.Layer):
WenmuZhou's avatar
WenmuZhou committed
107
    def __init__(self, in_channels, out_channels, **kwargs):
dyning's avatar
dyning committed
108
        super(DBFPN, self).__init__()
WenmuZhou's avatar
WenmuZhou committed
109
        self.out_channels = out_channels
110
        weight_attr = paddle.nn.initializer.KaimingUniform()
WenmuZhou's avatar
WenmuZhou committed
111

dyning's avatar
dyning committed
112
        self.in2_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
113
114
115
            in_channels=in_channels[0],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey committed
116
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
117
            bias_attr=False)
dyning's avatar
dyning committed
118
        self.in3_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
119
120
121
            in_channels=in_channels[1],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey committed
122
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
123
            bias_attr=False)
dyning's avatar
dyning committed
124
        self.in4_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
125
126
127
            in_channels=in_channels[2],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey committed
128
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
129
            bias_attr=False)
dyning's avatar
dyning committed
130
        self.in5_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
131
132
133
            in_channels=in_channels[3],
            out_channels=self.out_channels,
            kernel_size=1,
littletomatodonkey's avatar
littletomatodonkey committed
134
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
135
            bias_attr=False)
dyning's avatar
dyning committed
136
        self.p5_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
137
138
139
140
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey committed
141
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
142
            bias_attr=False)
dyning's avatar
dyning committed
143
        self.p4_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
144
145
146
147
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey committed
148
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
149
            bias_attr=False)
dyning's avatar
dyning committed
150
        self.p3_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
151
152
153
154
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey committed
155
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
156
            bias_attr=False)
dyning's avatar
dyning committed
157
        self.p2_conv = nn.Conv2D(
WenmuZhou's avatar
WenmuZhou committed
158
159
160
161
            in_channels=self.out_channels,
            out_channels=self.out_channels // 4,
            kernel_size=3,
            padding=1,
littletomatodonkey's avatar
littletomatodonkey committed
162
            weight_attr=ParamAttr(initializer=weight_attr),
WenmuZhou's avatar
WenmuZhou committed
163
164
165
166
167
168
169
170
171
172
            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)

WenmuZhou's avatar
WenmuZhou committed
173
174
175
176
177
178
        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
WenmuZhou's avatar
WenmuZhou committed
179
180
181
182
183

        p5 = self.p5_conv(in5)
        p4 = self.p4_conv(out4)
        p3 = self.p3_conv(out3)
        p2 = self.p2_conv(out2)
WenmuZhou's avatar
WenmuZhou committed
184
185
186
        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)
WenmuZhou's avatar
WenmuZhou committed
187
188
189

        fuse = paddle.concat([p5, p4, p3, p2], axis=1)
        return fuse
LDOUBLEV's avatar
LDOUBLEV committed
190
191
192
193
194
195


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()
LDOUBLEV's avatar
fix  
LDOUBLEV committed
196
        self.out_channels = out_channels
LDOUBLEV's avatar
LDOUBLEV committed
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
        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):
LDOUBLEV's avatar
fix det  
LDOUBLEV committed
217
    def __init__(self, in_channels, out_channels, shortcut=True, **kwargs):
LDOUBLEV's avatar
LDOUBLEV committed
218
        super(CAFPN, self).__init__()
LDOUBLEV's avatar
fix det  
LDOUBLEV committed
219
        self.out_channels = out_channels
LDOUBLEV's avatar
LDOUBLEV committed
220
221
        self.ins_conv = nn.LayerList()
        self.inp_conv = nn.LayerList()
LDOUBLEV's avatar
LDOUBLEV committed
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262

        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
LDOUBLEV's avatar
LDOUBLEV committed
263
264
265
266
267
268
269
270


class FEPAN(nn.Layer):
    def __init__(self, in_channels, out_channels, **kwargs):
        super(FEPAN, self).__init__()
        self.out_channels = out_channels
        weight_attr = paddle.nn.initializer.KaimingUniform()

LDOUBLEV's avatar
fix det  
LDOUBLEV committed
271
272
        self.ins_conv = []
        self.inp_conv = []
LDOUBLEV's avatar
LDOUBLEV committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
        # pan head
        self.pan_head_conv = []
        self.pan_lat_conv = []

        for i in range(len(in_channels)):
            self.ins_conv.append(
                nn.Conv2D(
                    in_channels=in_channels[0],
                    out_channels=self.out_channels,
                    kernel_size=1,
                    weight_attr=ParamAttr(initializer=weight_attr),
                    bias_attr=False))

            self.inp_conv.append(
                ConvBNLayer(
                    in_channels=self.out_channels,
                    out_channels=self.out_channels // 4,
                    kernel_size=9,
                    padding=4))

            if i > 0:
                self.pan_head_conv.append(
                    nn.Conv2D(
                        in_channels=self.out_channels // 4,
                        out_channels=self.out_channels // 4,
                        kernel_size=3,
                        padding=1,
                        stride=2,
                        weight_attr=ParamAttr(initializer=weight_attr),
                        bias_attr=False))
            self.pan_lat_conv.append(
                ConvBNLayer(
                    in_channels=self.out_channels // 4,
                    out_channels=self.out_channels // 4,
                    kernel_size=9,
                    padding=4))

    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

        f5 = self.inp_conv[3](in5)
        f4 = self.inp_conv[2](out4)
        f3 = self.inp_conv[1](out3)
        f2 = self.inp_conv[0](out2)

        pan3 = f3 + self.pan_head[0](f2)
        pan4 = f4 + self.pan_head[1](pan3)
        pan5 = f5 + self.pan_head[2](pan4)

        p2 = self.pan_lat[0](f2)
        p3 = self.pan_lat[1](pan3)
        p4 = self.pan_lat[2](pan4)
        p5 = self.pan_lat[3](pan5)

        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