googlenet.py 5.0 KB
Newer Older
W
wwhu 已提交
1 2 3 4 5
import paddle.v2 as paddle

__all__ = ['googlenet']


W
wwhu 已提交
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
def inception2(name, input, channels, filter1, filter3R, filter3, filter5R,
               filter5, proj):
    cov1 = paddle.layer.img_conv(
        name=name + '_1',
        input=input,
        filter_size=1,
        num_channels=channels,
        num_filters=filter1,
        stride=1,
        padding=0)

    cov3r = paddle.layer.img_conv(
        name=name + '_3r',
        input=input,
        filter_size=1,
        num_channels=channels,
        num_filters=filter3R,
        stride=1,
        padding=0)
    cov3 = paddle.layer.img_conv(
        name=name + '_3',
        input=cov3r,
        filter_size=3,
        num_filters=filter3,
        stride=1,
        padding=1)

    cov5r = paddle.layer.img_conv(
        name=name + '_5r',
        input=input,
        filter_size=1,
        num_channels=channels,
        num_filters=filter5R,
        stride=1,
        padding=0)
    cov5 = paddle.layer.img_conv(
        name=name + '_5',
        input=cov5r,
        filter_size=5,
        num_filters=filter5,
        stride=1,
        padding=2)

    pool1 = paddle.layer.img_pool(
        name=name + '_max',
        input=input,
        pool_size=3,
        num_channels=channels,
        stride=1,
        padding=1)
    covprj = paddle.layer.img_conv(
        name=name + '_proj',
        input=pool1,
        filter_size=1,
        num_filters=proj,
        stride=1,
        padding=0)

    cat = paddle.layer.concat(name=name, input=[cov1, cov3, cov5, covprj])
    return cat


def googlenet(input, class_dim=100):
W
wwhu 已提交
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
    # stage 1
    conv1 = paddle.layer.img_conv(
        name="conv1",
        input=input,
        filter_size=7,
        num_channels=3,
        num_filters=64,
        stride=2,
        padding=3)
    pool1 = paddle.layer.img_pool(
        name="pool1", input=conv1, pool_size=3, num_channels=64, stride=2)

    # stage 2
    conv2_1 = paddle.layer.img_conv(
        name="conv2_1",
        input=pool1,
        filter_size=1,
        num_filters=64,
        stride=1,
        padding=0)
    conv2_2 = paddle.layer.img_conv(
        name="conv2_2",
        input=conv2_1,
        filter_size=3,
        num_filters=192,
        stride=1,
        padding=1)
    pool2 = paddle.layer.img_pool(
        name="pool2", input=conv2_2, pool_size=3, num_channels=192, stride=2)

    # stage 3
W
wwhu 已提交
100 101
    ince3a = inception2("ince3a", pool2, 192, 64, 96, 128, 16, 32, 32)
    ince3b = inception2("ince3b", ince3a, 256, 128, 128, 192, 32, 96, 64)
W
wwhu 已提交
102 103 104 105
    pool3 = paddle.layer.img_pool(
        name="pool3", input=ince3b, num_channels=480, pool_size=3, stride=2)

    # stage 4
W
wwhu 已提交
106 107 108 109 110
    ince4a = inception2("ince4a", pool3, 480, 192, 96, 208, 16, 48, 64)
    ince4b = inception2("ince4b", ince4a, 512, 160, 112, 224, 24, 64, 64)
    ince4c = inception2("ince4c", ince4b, 512, 128, 128, 256, 24, 64, 64)
    ince4d = inception2("ince4d", ince4c, 512, 112, 144, 288, 32, 64, 64)
    ince4e = inception2("ince4e", ince4d, 528, 256, 160, 320, 32, 128, 128)
W
wwhu 已提交
111 112 113 114
    pool4 = paddle.layer.img_pool(
        name="pool4", input=ince4e, num_channels=832, pool_size=3, stride=2)

    # stage 5
W
wwhu 已提交
115 116
    ince5a = inception2("ince5a", pool4, 832, 256, 160, 320, 32, 128, 128)
    ince5b = inception2("ince5b", ince5a, 832, 384, 192, 384, 48, 128, 128)
W
wwhu 已提交
117 118 119 120 121 122 123 124 125 126 127 128
    pool5 = paddle.layer.img_pool(
        name="pool5",
        input=ince5b,
        num_channels=1024,
        pool_size=7,
        stride=7,
        pool_type=paddle.pooling.Avg())
    dropout = paddle.layer.addto(
        input=pool5,
        layer_attr=paddle.attr.Extra(drop_rate=0.4),
        act=paddle.activation.Linear())

W
wwhu 已提交
129 130 131
    out = paddle.layer.fc(
        input=dropout, size=class_dim, act=paddle.activation.Softmax())

W
wwhu 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    # fc for output 1
    pool_o1 = paddle.layer.img_pool(
        name="pool_o1",
        input=ince4a,
        num_channels=512,
        pool_size=5,
        stride=3,
        pool_type=paddle.pooling.Avg())
    conv_o1 = paddle.layer.img_conv(
        name="conv_o1",
        input=pool_o1,
        filter_size=1,
        num_filters=128,
        stride=1,
        padding=0)
    fc_o1 = paddle.layer.fc(
        name="fc_o1",
        input=conv_o1,
        size=1024,
        layer_attr=paddle.attr.Extra(drop_rate=0.7),
        act=paddle.activation.Relu())
W
wwhu 已提交
153 154
    out1 = paddle.layer.fc(
        input=fc_o1, size=class_dim, act=paddle.activation.Softmax())
W
wwhu 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

    # fc for output 2
    pool_o2 = paddle.layer.img_pool(
        name="pool_o2",
        input=ince4d,
        num_channels=528,
        pool_size=5,
        stride=3,
        pool_type=paddle.pooling.Avg())
    conv_o2 = paddle.layer.img_conv(
        name="conv_o2",
        input=pool_o2,
        filter_size=1,
        num_filters=128,
        stride=1,
        padding=0)
    fc_o2 = paddle.layer.fc(
        name="fc_o2",
        input=conv_o2,
        size=1024,
        layer_attr=paddle.attr.Extra(drop_rate=0.7),
        act=paddle.activation.Relu())
W
wwhu 已提交
177 178
    out2 = paddle.layer.fc(
        input=fc_o2, size=class_dim, act=paddle.activation.Softmax())
W
wwhu 已提交
179

W
wwhu 已提交
180
    return out, out1, out2