alexnet.py 12.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
import numpy as np
import argparse
import paddle
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
from paddle.fluid.layer_helper import LayerHelper
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear
from paddle.fluid.dygraph.base import to_variable

from paddle.fluid import framework
W
WuHaobo 已提交
11 12

import math
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
import sys
import time

__all__ = ['Xception41', 'Xception65', 'Xception71']


class ConvBNLayer(fluid.dygraph.Layer):
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
                 act=None,
                 name=None):
        super(ConvBNLayer, self).__init__()

        self._conv = Conv2D(
            num_channels=num_channels,
            num_filters=num_filters,
            filter_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            act=None,
            param_attr=ParamAttr(name=name + "_weights"),
            bias_attr=False)
        bn_name = "bn_" + name
        self._batch_norm = BatchNorm(
            num_filters,
            act=act,
            param_attr=ParamAttr(name=bn_name + "_scale"),
            bias_attr=ParamAttr(name=bn_name + "_offset"),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')

    def forward(self, inputs):
        y = self._conv(inputs)
        y = self._batch_norm(y)
        return y


class Separable_Conv(fluid.dygraph.Layer):
    def __init__(self, input_channels, output_channels, stride=1, name=None):
        super(Separable_Conv, self).__init__()

        self._pointwise_conv = ConvBNLayer(
            input_channels, output_channels, 1, name=name + "_sep")
        self._depthwise_conv = ConvBNLayer(
            output_channels,
            output_channels,
            3,
            stride=stride,
            groups=output_channels,
            name=name + "_dw")

    def forward(self, inputs):
        x = self._pointwise_conv(inputs)
        x = self._depthwise_conv(x)
        return x


class Entry_Flow_Bottleneck_Block(fluid.dygraph.Layer):
    def __init__(self,
                 input_channels,
                 output_channels,
                 stride=2,
                 name=None,
                 relu_first=False):
        super(Entry_Flow_Bottleneck_Block, self).__init__()
        self.relu_first = relu_first

        self._short = Conv2D(
            num_channels=input_channels,
            num_filters=output_channels,
            filter_size=1,
            stride=stride,
            padding=0,
            act=None,
            param_attr=ParamAttr(name + "_branch1_weights"),
            bias_attr=False)
        self._conv1 = Separable_Conv(
            input_channels,
            output_channels,
            stride=1,
            name=name + "_branch2a_weights")
        self._conv2 = Separable_Conv(
            output_channels,
            output_channels,
            stride=1,
            name=name + "_branch2b_weights")
        self._pool = Pool2D(
            pool_size=3, pool_stride=stride, pool_padding=1, pool_type="max")

    def forward(self, inputs):
        conv0 = inputs
        short = self._short(inputs)
        layer_helper = LayerHelper(self.full_name(), act="relu")
        if self.relu_first:
            conv0 = layer_helper.append_activation(conv0)
        conv1 = self._conv1(conv0)
        conv2 = layer_helper.append_activation(conv1)
        conv2 = self._conv2(conv2)
        pool = self._pool(conv2)
        return fluid.layers.elementwise_add(x=short, y=pool)


class Entry_Flow(fluid.dygraph.Layer):
    def __init__(self, block_num=3):
        super(Entry_Flow, self).__init__()

        name = "entry_flow"
        self.block_num = block_num
        self._conv1 = ConvBNLayer(
            3, 32, 3, stride=2, act="relu", name=name + "_conv1")
        self._conv2 = ConvBNLayer(32, 64, 3, act="relu", name=name + "_conv2")
        if block_num == 3:
            self._conv_0 = Entry_Flow_Bottleneck_Block(
                64, 128, stride=2, name=name + "_0", relu_first=False)
            self._conv_1 = Entry_Flow_Bottleneck_Block(
                128, 256, stride=2, name=name + "_1", relu_first=True)
            self._conv_2 = Entry_Flow_Bottleneck_Block(
                256, 728, stride=2, name=name + "_2", relu_first=True)
        elif block_num == 5:
            self._conv_0 = Entry_Flow_Bottleneck_Block(
                64, 128, stride=2, name=name + "_0", relu_first=False)
            self._conv_1 = Entry_Flow_Bottleneck_Block(
                128, 256, stride=1, name=name + "_1", relu_first=True)
            self._conv_2 = Entry_Flow_Bottleneck_Block(
                256, 256, stride=2, name=name + "_2", relu_first=True)
            self._conv_3 = Entry_Flow_Bottleneck_Block(
                256, 728, stride=1, name=name + "_3", relu_first=True)
            self._conv_4 = Entry_Flow_Bottleneck_Block(
                728, 728, stride=2, name=name + "_4", relu_first=True)
        else:
            sys.exit(-1)

    def forward(self, inputs):
        x = self._conv1(inputs)
        x = self._conv2(x)

        if self.block_num == 3:
            x = self._conv_0(x)
            x = self._conv_1(x)
            x = self._conv_2(x)
        elif self.block_num == 5:
            x = self._conv_0(x)
            x = self._conv_1(x)
            x = self._conv_2(x)
            x = self._conv_3(x)
            x = self._conv_4(x)
        return x
W
WuHaobo 已提交
165 166


167 168 169 170 171 172 173
class Middle_Flow_Bottleneck_Block(fluid.dygraph.Layer):
    def __init__(self, input_channels, output_channels, name):
        super(Middle_Flow_Bottleneck_Block, self).__init__()

        self._conv_0 = Separable_Conv(
            input_channels,
            output_channels,
W
WuHaobo 已提交
174
            stride=1,
175 176 177 178
            name=name + "_branch2a_weights")
        self._conv_1 = Separable_Conv(
            output_channels,
            output_channels,
W
WuHaobo 已提交
179
            stride=1,
180 181 182 183
            name=name + "_branch2b_weights")
        self._conv_2 = Separable_Conv(
            output_channels,
            output_channels,
W
WuHaobo 已提交
184
            stride=1,
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 277 278 279
            name=name + "_branch2c_weights")

    def forward(self, inputs):
        layer_helper = LayerHelper(self.full_name(), act="relu")
        conv0 = layer_helper.append_activation(inputs)
        conv0 = self._conv_0(conv0)
        conv1 = layer_helper.append_activation(conv0)
        conv1 = self._conv_1(conv1)
        conv2 = layer_helper.append_activation(conv1)
        conv2 = self._conv_2(conv2)
        return fluid.layers.elementwise_add(x=inputs, y=conv2)


class Middle_Flow(fluid.dygraph.Layer):
    def __init__(self, block_num=8):
        super(Middle_Flow, self).__init__()

        self.block_num = block_num
        self._conv_0 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_0")
        self._conv_1 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_1")
        self._conv_2 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_2")
        self._conv_3 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_3")
        self._conv_4 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_4")
        self._conv_5 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_5")
        self._conv_6 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_6")
        self._conv_7 = Middle_Flow_Bottleneck_Block(
            728, 728, name="middle_flow_7")
        if block_num == 16:
            self._conv_8 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_8")
            self._conv_9 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_9")
            self._conv_10 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_10")
            self._conv_11 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_11")
            self._conv_12 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_12")
            self._conv_13 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_13")
            self._conv_14 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_14")
            self._conv_15 = Middle_Flow_Bottleneck_Block(
                728, 728, name="middle_flow_15")

    def forward(self, inputs):
        x = self._conv_0(inputs)
        x = self._conv_1(x)
        x = self._conv_2(x)
        x = self._conv_3(x)
        x = self._conv_4(x)
        x = self._conv_5(x)
        x = self._conv_6(x)
        x = self._conv_7(x)
        if self.block_num == 16:
            x = self._conv_8(x)
            x = self._conv_9(x)
            x = self._conv_10(x)
            x = self._conv_11(x)
            x = self._conv_12(x)
            x = self._conv_13(x)
            x = self._conv_14(x)
            x = self._conv_15(x)
        return x


class Exit_Flow_Bottleneck_Block(fluid.dygraph.Layer):
    def __init__(self, input_channels, output_channels1, output_channels2,
                 name):
        super(Exit_Flow_Bottleneck_Block, self).__init__()

        self._short = Conv2D(
            num_channels=input_channels,
            num_filters=output_channels2,
            filter_size=1,
            stride=2,
            padding=0,
            act=None,
            param_attr=ParamAttr(name + "_branch1_weights"),
            bias_attr=False)
        self._conv_1 = Separable_Conv(
            input_channels,
            output_channels1,
            stride=1,
            name=name + "_branch2a_weights")
        self._conv_2 = Separable_Conv(
            output_channels1,
            output_channels2,
W
WuHaobo 已提交
280
            stride=1,
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
            name=name + "_branch2b_weights")
        self._pool = Pool2D(
            pool_size=3, pool_stride=2, pool_padding=1, pool_type="max")

    def forward(self, inputs):
        short = self._short(inputs)
        layer_helper = LayerHelper(self.full_name(), act="relu")
        conv0 = layer_helper.append_activation(inputs)
        conv1 = self._conv_1(conv0)
        conv2 = layer_helper.append_activation(conv1)
        conv2 = self._conv_2(conv2)
        pool = self._pool(conv2)
        return fluid.layers.elementwise_add(x=short, y=pool)


class Exit_Flow(fluid.dygraph.Layer):
    def __init__(self, class_dim):
        super(Exit_Flow, self).__init__()

        name = "exit_flow"

        self._conv_0 = Exit_Flow_Bottleneck_Block(
            728, 728, 1024, name=name + "_1")
        self._conv_1 = Separable_Conv(1024, 1536, stride=1, name=name + "_2")
        self._conv_2 = Separable_Conv(1536, 2048, stride=1, name=name + "_3")
        self._pool = Pool2D(pool_type="avg", global_pooling=True)
        stdv = 1.0 / math.sqrt(2048 * 1.0)
        self._out = Linear(
            2048,
            class_dim,
            param_attr=ParamAttr(
                name="fc_weights",
                initializer=fluid.initializer.Uniform(-stdv, stdv)),
            bias_attr=ParamAttr(name="fc_offset"))

    def forward(self, inputs):
        layer_helper = LayerHelper(self.full_name(), act="relu")
        conv0 = self._conv_0(inputs)
        conv1 = self._conv_1(conv0)
        conv1 = layer_helper.append_activation(conv1)
        conv2 = self._conv_2(conv1)
        conv2 = layer_helper.append_activation(conv2)
        pool = self._pool(conv2)
        pool = fluid.layers.reshape(pool, [0, -1])
        out = self._out(pool)
W
WuHaobo 已提交
326
        return out
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360


class Xception(fluid.dygraph.Layer):
    def __init__(self,
                 entry_flow_block_num=3,
                 middle_flow_block_num=8,
                 class_dim=1000):
        super(Xception, self).__init__()
        self.entry_flow_block_num = entry_flow_block_num
        self.middle_flow_block_num = middle_flow_block_num
        self._entry_flow = Entry_Flow(entry_flow_block_num)
        self._middle_flow = Middle_Flow(middle_flow_block_num)
        self._exit_flow = Exit_Flow(class_dim)

    def forward(self, inputs):
        x = self._entry_flow(inputs)
        x = self._middle_flow(x)
        x = self._exit_flow(x)
        return x


def Xception41():
    model = Xception(entry_flow_block_num=3, middle_flow_block_num=8)
    return model


def Xception65():
    model = Xception(entry_flow_block_num=3, middle_flow_block_num=16)
    return model


def Xception71():
    model = Xception(entry_flow_block_num=5, middle_flow_block_num=16)
    return model