alexnet.py 4.2 KB
Newer Older
1 2 3
import paddle
import paddle.fluid as fluid
from paddle.fluid.param_attr import ParamAttr
W
wqz960 已提交
4
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear, Dropout
W
WuHaobo 已提交
5
import math
6

W
wqz960 已提交
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
__all__ = ["AlexNet"]

class ConvPoolLayer(fluid.dygraph.Layer):
    def __init__(self, 
                inputc_channels,
                output_channels,
                filter_size,
                stride,
                padding,
                stdv,
                groups=1,
                act=None,
                name=None):
        super(ConvPoolLayer, self).__init__()

        self._conv = Conv2D(num_channels=inputc_channels,
                            num_filters=output_channels,
                            filter_size=filter_size,
                            stride=stride,
                            padding=padding,
                            groups=groups,
                            param_attr=ParamAttr(name=name + "_weights",
                                initializer=fluid.initializer.Uniform(-stdv, stdv)),
                            bias_attr=ParamAttr(name=name + "_offset",
                                initializer=fluid.initializer.Uniform(-stdv, stdv)),
                            act=act)
        self._pool = Pool2D(pool_size=3,
                            pool_stride=2,
                            pool_padding=0,
                            pool_type="max")
37 38

    def forward(self, inputs):
W
wqz960 已提交
39 40
        x = self._conv(inputs)
        x = self._pool(x)
41 42 43
        return x


W
wqz960 已提交
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
class AlexNetDY(fluid.dygraph.Layer):
    def __init__(self, class_dim=1000):
        super(AlexNetDY, self).__init__()

        stdv = 1.0/math.sqrt(3*11*11)
        self._conv1 = ConvPoolLayer(
            3, 64, 11, 4, 2, stdv, act="relu", name="conv1") 
        stdv = 1.0/math.sqrt(64*5*5)
        self._conv2 = ConvPoolLayer(
            64, 192, 5, 1, 2, stdv, act="relu", name="conv2")
        stdv = 1.0/math.sqrt(192*3*3)
        self._conv3 = Conv2D(192, 384, 3, stride=1, padding=1, 
            param_attr=ParamAttr(name="conv3_weights", initializer=fluid.initializer.Uniform(-stdv, stdv)),
            bias_attr=ParamAttr(name="conv3_offset", initializer=fluid.initializer.Uniform(-stdv, stdv)), 
            act="relu")
        stdv = 1.0/math.sqrt(384*3*3)
        self._conv4 = Conv2D(384, 256, 3, stride=1, padding=1,
            param_attr=ParamAttr(name="conv4_weights", initializer=fluid.initializer.Uniform(-stdv, stdv)),
            bias_attr=ParamAttr(name="conv4_offset", initializer=fluid.initializer.Uniform(-stdv, stdv)), 
            act="relu")
        stdv = 1.0/math.sqrt(256*3*3)
        self._conv5 = ConvPoolLayer(
            256, 256, 3, 1, 1, stdv, act="relu", name="conv5")
        stdv = 1.0/math.sqrt(256*6*6)

        self._drop1 = Dropout(p=0.5)
        self._fc6 = Linear(input_dim=256*6*6, 
                        output_dim=4096, 
                        param_attr=ParamAttr(name="fc6_weights", initializer=fluid.initializer.Uniform(-stdv, stdv)),
                        bias_attr=ParamAttr(name="fc6_offset", initializer=fluid.initializer.Uniform(-stdv, stdv)),
                        act="relu")
        
        self._drop2 = Dropout(p=0.5)
        self._fc7 = Linear(input_dim=4096,
                        output_dim=4096,
                        param_attr=ParamAttr(name="fc7_weights", initializer=fluid.initializer.Uniform(-stdv, stdv)),
                        bias_attr=ParamAttr(name="fc7_offset", initializer=fluid.initializer.Uniform(-stdv, stdv)),
                        act="relu")
        self._fc8 = Linear(input_dim=4096,
                        output_dim=class_dim,
                        param_attr=ParamAttr(name="fc8_weights", initializer=fluid.initializer.Uniform(-stdv, stdv)),
                        bias_attr=ParamAttr(name="fc8_offset", initializer=fluid.initializer.Uniform(-stdv, stdv)))
86 87 88 89

    def forward(self, inputs):
        x = self._conv1(inputs)
        x = self._conv2(x)
W
wqz960 已提交
90 91 92 93 94 95 96 97 98
        x = self._conv3(x)
        x = self._conv4(x)
        x = self._conv5(x)
        x = fluid.layers.flatten(x, axis=0)
        x = self._drop1(x)
        x = self._fc6(x)
        x = self._drop2(x)
        x = self._fc7(x)
        x = self._fc8(x)
99
        return x
W
WuHaobo 已提交
100

W
wqz960 已提交
101 102
def AlexNet(**args):
    model = AlexNetDY(**args)
103
    return model