darknet.py 5.4 KB
Newer Older
1 2
#  Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
3 4 5
# 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
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
9 10 11 12 13
# 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.
14

15
import paddle
16
from paddle.fluid.param_attr import ParamAttr
17
from paddle.nn import BatchNorm
18
from paddle.regularizer import L2Decay
19 20


21
class ConvBNLayer(paddle.nn.Layer):
22 23 24 25 26 27 28 29 30 31 32
    def __init__(
        self,
        ch_in,
        ch_out,
        filter_size=3,
        stride=1,
        groups=1,
        padding=0,
        act="leaky",
        is_test=True,
    ):
33
        super().__init__()
34

35 36 37 38
        self.conv = paddle.nn.Conv2D(
            in_channels=ch_in,
            out_channels=ch_out,
            kernel_size=filter_size,
39 40 41
            stride=stride,
            padding=padding,
            groups=groups,
42
            weight_attr=ParamAttr(
43
                initializer=paddle.nn.initializer.Normal(0.0, 0.02)
44 45 46
            ),
            bias_attr=False,
        )
47 48 49
        self.batch_norm = BatchNorm(
            num_channels=ch_out,
            is_test=is_test,
50
            param_attr=ParamAttr(
51
                initializer=paddle.nn.initializer.Normal(0.0, 0.02),
52 53 54
                regularizer=L2Decay(0.0),
            ),
            bias_attr=ParamAttr(
55
                initializer=paddle.nn.initializer.Constant(0.0),
56 57 58
                regularizer=L2Decay(0.0),
            ),
        )
59 60 61 62 63 64 65

        self.act = act

    def forward(self, inputs):
        out = self.conv(inputs)
        out = self.batch_norm(out)
        if self.act == 'leaky':
66
            out = paddle.nn.functional.leaky_relu(out, 0.1)
67 68 69
        return out


70
class DownSample(paddle.nn.Layer):
71 72 73
    def __init__(
        self, ch_in, ch_out, filter_size=3, stride=2, padding=1, is_test=True
    ):
74

75
        super().__init__()
76

77 78 79 80 81 82 83 84
        self.conv_bn_layer = ConvBNLayer(
            ch_in=ch_in,
            ch_out=ch_out,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            is_test=is_test,
        )
85 86 87 88 89 90 91
        self.ch_out = ch_out

    def forward(self, inputs):
        out = self.conv_bn_layer(inputs)
        return out


92
class BasicBlock(paddle.nn.Layer):
93
    def __init__(self, ch_in, ch_out, is_test=True):
94
        super().__init__()
95

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
        self.conv1 = ConvBNLayer(
            ch_in=ch_in,
            ch_out=ch_out,
            filter_size=1,
            stride=1,
            padding=0,
            is_test=is_test,
        )
        self.conv2 = ConvBNLayer(
            ch_in=ch_out,
            ch_out=ch_out * 2,
            filter_size=3,
            stride=1,
            padding=1,
            is_test=is_test,
        )
112 113 114 115

    def forward(self, inputs):
        conv1 = self.conv1(inputs)
        conv2 = self.conv2(conv1)
116
        out = paddle.add(x=inputs, y=conv2)
117 118 119
        return out


120
class LayerWarp(paddle.nn.Layer):
121
    def __init__(self, ch_in, ch_out, count, is_test=True):
122
        super().__init__()
123 124 125 126 127 128

        self.basicblock0 = BasicBlock(ch_in, ch_out, is_test=is_test)
        self.res_out_list = []
        for i in range(1, count):
            res_out = self.add_sublayer(
                "basic_block_%d" % (i),
129 130
                BasicBlock(ch_out * 2, ch_out, is_test=is_test),
            )
131 132 133 134 135 136 137 138 139 140 141 142 143
            self.res_out_list.append(res_out)
        self.ch_out = ch_out

    def forward(self, inputs):
        y = self.basicblock0(inputs)
        for basic_block_i in self.res_out_list:
            y = basic_block_i(y)
        return y


DarkNet_cfg = {53: ([1, 2, 8, 8, 4])}


144
class DarkNet53_conv_body(paddle.nn.Layer):
145
    def __init__(self, ch_in=3, is_test=True):
146
        super().__init__()
147 148 149
        self.stages = DarkNet_cfg[53]
        self.stages = self.stages[0:5]

150 151 152 153 154 155 156 157
        self.conv0 = ConvBNLayer(
            ch_in=ch_in,
            ch_out=32,
            filter_size=3,
            stride=1,
            padding=1,
            is_test=is_test,
        )
158 159 160 161 162 163 164 165

        self.downsample0 = DownSample(ch_in=32, ch_out=32 * 2, is_test=is_test)
        self.darknet53_conv_block_list = []
        self.downsample_list = []
        ch_in = [64, 128, 256, 512, 1024]
        for i, stage in enumerate(self.stages):
            conv_block = self.add_sublayer(
                "stage_%d" % (i),
166 167
                LayerWarp(int(ch_in[i]), 32 * (2**i), stage, is_test=is_test),
            )
168 169 170 171
            self.darknet53_conv_block_list.append(conv_block)
        for i in range(len(self.stages) - 1):
            downsample = self.add_sublayer(
                "stage_%d_downsample" % i,
172 173 174 175 176 177
                DownSample(
                    ch_in=32 * (2 ** (i + 1)),
                    ch_out=32 * (2 ** (i + 2)),
                    is_test=is_test,
                ),
            )
178 179 180 181 182 183 184 185 186 187 188 189 190
            self.downsample_list.append(downsample)

    def forward(self, inputs):

        out = self.conv0(inputs)
        out = self.downsample0(out)
        blocks = []
        for i, conv_block_i in enumerate(self.darknet53_conv_block_list):
            out = conv_block_i(out)
            blocks.append(out)
            if i < len(self.stages) - 1:
                out = self.downsample_list[i](out)
        return blocks[-1:-4:-1]