resnet.py 7.5 KB
Newer Older
F
FDInSky 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 104 105 106 107 108 109 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 263 264 265 266 267 268 269 270 271 272 273 274 275 276
import numpy as np
import paddle.fluid as fluid
from paddle.fluid.dygraph import Layer
from paddle.fluid.dygraph import Conv2D, Pool2D, BatchNorm
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.initializer import Constant

from ppdet.core.workspace import register, serializable


class ConvBNLayer(Layer):
    def __init__(self,
                 name_scope,
                 ch_in,
                 ch_out,
                 filter_size,
                 stride,
                 padding,
                 act='relu',
                 learning_rate=1.0):
        super(ConvBNLayer, self).__init__()

        self._conv = Conv2D(
            num_channels=ch_in,
            num_filters=ch_out,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            groups=1,
            act=act,
            param_attr=ParamAttr(
                name=name_scope + "_weights", learning_rate=learning_rate),
            bias_attr=ParamAttr(name=name_scope + "_bias"))

        if name_scope == "conv1":
            bn_name = "bn_" + name_scope
        else:
            bn_name = "bn" + name_scope[3:]

        self._bn = BatchNorm(
            num_channels=ch_out,
            act=act,
            param_attr=ParamAttr(name=bn_name + '_scale'),
            bias_attr=ParamAttr(bn_name + '_offset'),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance',
            is_test=True)

    def forward(self, inputs):
        x = self._conv(inputs)
        out = self._bn(x)
        return out


class ConvAffineLayer(Layer):
    def __init__(self,
                 name_scope,
                 ch_in,
                 ch_out,
                 filter_size,
                 stride,
                 padding,
                 learning_rate=1.0,
                 act='relu'):
        super(ConvAffineLayer, self).__init__()

        self._conv = Conv2D(
            num_channels=ch_in,
            num_filters=ch_out,
            filter_size=filter_size,
            stride=stride,
            padding=padding,
            act=None,
            param_attr=ParamAttr(
                name=name_scope + "_weights", learning_rate=learning_rate),
            bias_attr=False)

        if name_scope == "conv1":
            bn_name = "bn_" + name_scope
        else:
            bn_name = "bn" + name_scope[3:]
        self.name_scope = name_scope

        self.scale = fluid.Layer.create_parameter(
            shape=[ch_out],
            dtype='float32',
            attr=ParamAttr(
                name=bn_name + '_scale', learning_rate=0.),
            default_initializer=Constant(1.))
        self.bias = fluid.layers.create_parameter(
            shape=[ch_out],
            dtype='float32',
            attr=ParamAttr(
                bn_name + '_offset', learning_rate=0.),
            default_initializer=Constant(0.))

        self.act = act

    def forward(self, inputs):
        conv = self._conv(inputs)
        out = fluid.layers.affine_channel(
            x=conv, scale=self.scale, bias=self.bias)
        if self.act == 'relu':
            out = fluid.layers.relu(x=out)
        return out


class BottleNeck(Layer):
    def __init__(self,
                 name_scope,
                 ch_in,
                 ch_out,
                 stride,
                 shortcut=True,
                 learning_rate=1.0):
        super(BottleNeck, self).__init__()

        self.shortcut = shortcut
        if not shortcut:
            self.short = ConvBNLayer(
                name_scope + "_branch1",
                ch_in=ch_in,
                ch_out=ch_out * 4,
                filter_size=1,
                stride=stride,
                padding=0,
                act=None,
                learning_rate=learning_rate)

        self.conv1 = ConvBNLayer(
            name_scope + "_branch2a",
            ch_in=ch_in,
            ch_out=ch_out,
            filter_size=1,
            stride=stride,
            padding=0,
            learning_rate=learning_rate, )

        self.conv2 = ConvBNLayer(
            name_scope + "_branch2b",
            ch_in=ch_out,
            ch_out=ch_out,
            filter_size=3,
            stride=1,
            padding=1,
            learning_rate=learning_rate)

        self.conv3 = ConvBNLayer(
            name_scope + "_branch2c",
            ch_in=ch_out,
            ch_out=ch_out * 4,
            filter_size=1,
            stride=1,
            padding=0,
            learning_rate=learning_rate,
            act=None)
        self.name_scope = name_scope

    def forward(self, inputs):
        if self.shortcut:
            short = inputs
        else:
            short = self.short(inputs)

        conv1 = self.conv1(inputs)
        conv2 = self.conv2(conv1)
        conv3 = self.conv3(conv2)

        out = fluid.layers.elementwise_add(
            x=short,
            y=conv3,
            act='relu',
            name=self.name_scope + ".add.output.5")

        return out


class Blocks(Layer):
    def __init__(self,
                 name_scope,
                 ch_in,
                 ch_out,
                 count,
                 stride,
                 learning_rate=1.0):
        super(Blocks, self).__init__()

        self.blocks = []
        for i in range(count):
            if i == 0:
                name = name_scope + "a"
                self.stride = stride
                self.shortcut = False
            else:
                name = name_scope + chr(ord("a") + i)
                self.stride = 1
                self.shortcut = True

            block = self.add_sublayer(
                name,
                BottleNeck(
                    name,
                    ch_in=ch_in if i == 0 else ch_out * 4,
                    ch_out=ch_out,
                    stride=self.stride,
                    shortcut=self.shortcut,
                    learning_rate=learning_rate))
            self.blocks.append(block)
            shortcut = True

    def forward(self, inputs):
        res_out = self.blocks[0](inputs)
        for block in self.blocks[1:]:
            res_out = block(res_out)
        return res_out


@register
@serializable
class ResNet(Layer):
    def __init__(
            self,
            norm_type='bn',
            depth=50,
            feature_maps=4,
            freeze_at=2, ):
        super(ResNet, self).__init__()

        if depth == 50:
            blocks = [3, 4, 6, 3]
        elif depth == 101:
            blocks = [3, 4, 23, 3]
        elif depth == 152:
            blocks = [3, 8, 36, 3]

        self.conv = ConvBNLayer(
            "conv1",
            ch_in=3,
            ch_out=64,
            filter_size=7,
            stride=2,
            padding=3,
            learning_rate=0.)

        self.pool2d_max = Pool2D(
            pool_type='max', pool_size=3, pool_stride=2, pool_padding=1)

        self.stage2 = Blocks(
            "res2",
            ch_in=64,
            ch_out=64,
            count=blocks[0],
            stride=1,
            learning_rate=0.)

        self.stage3 = Blocks(
            "res3", ch_in=256, ch_out=128, count=blocks[1], stride=2)

        self.stage4 = Blocks(
            "res4", ch_in=512, ch_out=256, count=blocks[2], stride=2)

    def forward(self, inputs):
        x = inputs['image']

        conv1 = self.conv(x)
        poo1 = self.pool2d_max(conv1)

        res2 = self.stage2(poo1)
        res2.stop_gradient = True

        res3 = self.stage3(res2)

        res4 = self.stage4(res3)

        outs = {'res2': res2, 'res3': res3, 'res4': res4}
        return outs