mobilenet_ssd.py 3.5 KB
Newer Older
G
gaoyuan 已提交
1 2 3 4 5 6
import paddle.v2 as paddle
import paddle.fluid as fluid
from paddle.fluid.initializer import MSRA
from paddle.fluid.param_attr import ParamAttr


D
dangqingqing 已提交
7 8 9 10 11 12 13 14 15
def conv_bn(input,
            filter_size,
            num_filters,
            stride,
            padding,
            channels=None,
            num_groups=1,
            act='relu',
            use_cudnn=True):
16
    parameter_attr = ParamAttr(learning_rate=0.1, initializer=MSRA())
G
gaoyuan 已提交
17 18 19 20 21 22 23 24 25 26 27
    conv = fluid.layers.conv2d(
        input=input,
        num_filters=num_filters,
        filter_size=filter_size,
        stride=stride,
        padding=padding,
        groups=num_groups,
        act=None,
        use_cudnn=use_cudnn,
        param_attr=parameter_attr,
        bias_attr=False)
28 29 30
    parameter_attr = ParamAttr(learning_rate=0.1, initializer=MSRA())
    bias_attr = ParamAttr(learning_rate=0.2)
    return fluid.layers.batch_norm(input=conv, act=act)
G
gaoyuan 已提交
31 32 33 34


def depthwise_separable(input, num_filters1, num_filters2, num_groups, stride,
                        scale):
D
dangqingqing 已提交
35
    depthwise_conv = conv_bn(
G
gaoyuan 已提交
36 37 38 39 40 41 42 43
        input=input,
        filter_size=3,
        num_filters=int(num_filters1 * scale),
        stride=stride,
        padding=1,
        num_groups=int(num_groups * scale),
        use_cudnn=False)

D
dangqingqing 已提交
44
    pointwise_conv = conv_bn(
G
gaoyuan 已提交
45 46 47 48 49 50 51 52 53
        input=depthwise_conv,
        filter_size=1,
        num_filters=int(num_filters2 * scale),
        stride=1,
        padding=0)
    return pointwise_conv


def extra_block(input, num_filters1, num_filters2, num_groups, stride, scale):
D
dangqingqing 已提交
54 55
    # 1x1 conv
    pointwise_conv = conv_bn(
G
gaoyuan 已提交
56 57 58 59 60 61 62
        input=input,
        filter_size=1,
        num_filters=int(num_filters1 * scale),
        stride=1,
        num_groups=int(num_groups * scale),
        padding=0)

D
dangqingqing 已提交
63 64
    # 3x3 conv
    normal_conv = conv_bn(
G
gaoyuan 已提交
65 66 67 68 69 70 71 72 73
        input=pointwise_conv,
        filter_size=3,
        num_filters=int(num_filters2 * scale),
        stride=2,
        num_groups=int(num_groups * scale),
        padding=1)
    return normal_conv


X
Xingyuan Bu 已提交
74
def mobile_net(num_classes, img, img_shape, scale=1.0):
G
gaoyuan 已提交
75
    # 300x300
D
dangqingqing 已提交
76
    tmp = conv_bn(img, 3, int(32 * scale), 2, 1, 3)
G
gaoyuan 已提交
77
    # 150x150
D
dangqingqing 已提交
78 79
    tmp = depthwise_separable(tmp, 32, 64, 32, 1, scale)
    tmp = depthwise_separable(tmp, 64, 128, 64, 2, scale)
G
gaoyuan 已提交
80
    # 75x75
D
dangqingqing 已提交
81 82
    tmp = depthwise_separable(tmp, 128, 128, 128, 1, scale)
    tmp = depthwise_separable(tmp, 128, 256, 128, 2, scale)
G
gaoyuan 已提交
83
    # 38x38
D
dangqingqing 已提交
84 85
    tmp = depthwise_separable(tmp, 256, 256, 256, 1, scale)
    tmp = depthwise_separable(tmp, 256, 512, 256, 2, scale)
G
gaoyuan 已提交
86 87 88

    # 19x19
    for i in range(5):
D
dangqingqing 已提交
89
        tmp = depthwise_separable(tmp, 512, 512, 512, 1, scale)
G
gaoyuan 已提交
90
    module11 = tmp
D
dangqingqing 已提交
91
    tmp = depthwise_separable(tmp, 512, 1024, 512, 2, scale)
G
gaoyuan 已提交
92 93

    # 10x10
D
dangqingqing 已提交
94 95
    module13 = depthwise_separable(tmp, 1024, 1024, 1024, 1, scale)
    module14 = extra_block(module13, 256, 512, 1, 2, scale)
G
gaoyuan 已提交
96
    # 5x5
D
dangqingqing 已提交
97
    module15 = extra_block(module14, 128, 256, 1, 2, scale)
G
gaoyuan 已提交
98
    # 3x3
D
dangqingqing 已提交
99
    module16 = extra_block(module15, 128, 256, 1, 2, scale)
G
gaoyuan 已提交
100
    # 2x2
D
dangqingqing 已提交
101
    module17 = extra_block(module16, 64, 128, 1, 2, scale)
X
Xingyuan Bu 已提交
102

G
gaoyuan 已提交
103 104 105
    mbox_locs, mbox_confs, box, box_var = fluid.layers.multi_box_head(
        inputs=[module11, module13, module14, module15, module16, module17],
        image=img,
X
Xingyuan Bu 已提交
106
        num_classes=num_classes,
G
gaoyuan 已提交
107 108
        min_ratio=20,
        max_ratio=90,
D
dangqingqing 已提交
109 110
        min_sizes=[60.0, 105.0, 150.0, 195.0, 240.0, 285.0],
        max_sizes=[[], 150.0, 195.0, 240.0, 285.0, 300.0],
G
gaoyuan 已提交
111 112 113
        aspect_ratios=[[2.], [2., 3.], [2., 3.], [2., 3.], [2., 3.], [2., 3.]],
        base_size=img_shape[2],
        offset=0.5,
114
        flip=True)
G
gaoyuan 已提交
115 116

    return mbox_locs, mbox_confs, box, box_var