fpn.py 4.26 KB
Newer Older
WenmuZhou's avatar
WenmuZhou committed
1
2
3
4
"""
This code is refer from:
https://github.com/whai362/PSENet/blob/python3/models/neck/fpn.py
"""
WenmuZhou's avatar
add fpn  
WenmuZhou committed
5
6
7
8
9
10

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

WenmuZhou's avatar
WenmuZhou committed
11

WenmuZhou's avatar
add fpn  
WenmuZhou committed
12
class Conv_BN_ReLU(nn.Layer):
WenmuZhou's avatar
WenmuZhou committed
13
14
15
16
17
18
    def __init__(self,
                 in_planes,
                 out_planes,
                 kernel_size=1,
                 stride=1,
                 padding=0):
WenmuZhou's avatar
add fpn  
WenmuZhou committed
19
        super(Conv_BN_ReLU, self).__init__()
WenmuZhou's avatar
WenmuZhou committed
20
21
22
23
24
25
26
        self.conv = nn.Conv2D(
            in_planes,
            out_planes,
            kernel_size=kernel_size,
            stride=stride,
            padding=padding,
            bias_attr=False)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
27
28
29
30
31
32
        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
WenmuZhou's avatar
WenmuZhou committed
33
34
35
36
37
                m.weight = paddle.create_parameter(
                    shape=m.weight.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Normal(
                        0, math.sqrt(2. / n)))
WenmuZhou's avatar
add fpn  
WenmuZhou committed
38
            elif isinstance(m, nn.BatchNorm2D):
WenmuZhou's avatar
WenmuZhou committed
39
40
41
42
43
44
45
46
                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))
WenmuZhou's avatar
add fpn  
WenmuZhou committed
47
48
49
50

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

WenmuZhou's avatar
WenmuZhou committed
51

WenmuZhou's avatar
add fpn  
WenmuZhou committed
52
53
54
55
56
class FPN(nn.Layer):
    def __init__(self, in_channels, out_channels):
        super(FPN, self).__init__()

        # Top layer
WenmuZhou's avatar
WenmuZhou committed
57
58
        self.toplayer_ = Conv_BN_ReLU(
            in_channels[3], out_channels, kernel_size=1, stride=1, padding=0)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
59
        # Lateral layers
WenmuZhou's avatar
WenmuZhou committed
60
61
        self.latlayer1_ = Conv_BN_ReLU(
            in_channels[2], out_channels, kernel_size=1, stride=1, padding=0)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
62

WenmuZhou's avatar
WenmuZhou committed
63
64
        self.latlayer2_ = Conv_BN_ReLU(
            in_channels[1], out_channels, kernel_size=1, stride=1, padding=0)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
65

WenmuZhou's avatar
WenmuZhou committed
66
67
        self.latlayer3_ = Conv_BN_ReLU(
            in_channels[0], out_channels, kernel_size=1, stride=1, padding=0)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
68
69

        # Smooth layers
WenmuZhou's avatar
WenmuZhou committed
70
71
        self.smooth1_ = Conv_BN_ReLU(
            out_channels, out_channels, kernel_size=3, stride=1, padding=1)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
72

WenmuZhou's avatar
WenmuZhou committed
73
74
        self.smooth2_ = Conv_BN_ReLU(
            out_channels, out_channels, kernel_size=3, stride=1, padding=1)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
75

WenmuZhou's avatar
WenmuZhou committed
76
77
        self.smooth3_ = Conv_BN_ReLU(
            out_channels, out_channels, kernel_size=3, stride=1, padding=1)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
78
79
80
81
82

        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
WenmuZhou's avatar
WenmuZhou committed
83
84
85
86
87
                m.weight = paddle.create_parameter(
                    shape=m.weight.shape,
                    dtype='float32',
                    default_initializer=paddle.nn.initializer.Normal(
                        0, math.sqrt(2. / n)))
WenmuZhou's avatar
add fpn  
WenmuZhou committed
88
            elif isinstance(m, nn.BatchNorm2D):
WenmuZhou's avatar
WenmuZhou committed
89
90
91
92
93
94
95
96
                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))
WenmuZhou's avatar
add fpn  
WenmuZhou committed
97

WenmuZhou's avatar
WenmuZhou committed
98
99
    def _upsample(self, x, scale=1):
        return F.upsample(x, scale_factor=scale, mode='bilinear')
WenmuZhou's avatar
add fpn  
WenmuZhou committed
100

WenmuZhou's avatar
WenmuZhou committed
101
102
    def _upsample_add(self, x, y, scale=1):
        return F.upsample(x, scale_factor=scale, mode='bilinear') + y
WenmuZhou's avatar
add fpn  
WenmuZhou committed
103
104
105
106
107
108

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

        f4 = self.latlayer1_(f4)
WenmuZhou's avatar
WenmuZhou committed
109
        p4 = self._upsample_add(p5, f4, 2)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
110
111
112
        p4 = self.smooth1_(p4)

        f3 = self.latlayer2_(f3)
WenmuZhou's avatar
WenmuZhou committed
113
        p3 = self._upsample_add(p4, f3, 2)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
114
115
116
        p3 = self.smooth2_(p3)

        f2 = self.latlayer3_(f2)
WenmuZhou's avatar
WenmuZhou committed
117
        p2 = self._upsample_add(p3, f2, 2)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
118
119
        p2 = self.smooth3_(p2)

WenmuZhou's avatar
WenmuZhou committed
120
121
122
        p3 = self._upsample(p3, 2)
        p4 = self._upsample(p4, 4)
        p5 = self._upsample(p5, 8)
WenmuZhou's avatar
add fpn  
WenmuZhou committed
123
124

        fuse = paddle.concat([p2, p3, p4, p5], axis=1)
WenmuZhou's avatar
WenmuZhou committed
125
        return fuse