convolutiondepthwise.py 5.3 KB
Newer Older
S
SunAhong1993 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
from .register import register
from x2paddle.core.util import *
import numbers


def convolutiondepthwise_shape(input_shape,
                               num_output=None,
                               pad=None,
                               kernel_size=None,
                               stride=None,
                               dilation=None,
                               pad_h=None,
                               pad_w=None,
                               kernel_h=None,
                               kernel_w=None,
                               stride_h=None,
                               stride_w=None):
    [k_h, k_w] = [1, 1]
    if isinstance(kernel_size, numbers.Number):
        [k_h, k_w] = [kernel_size] * 2
S
SunAhong1993 已提交
21
    elif len(kernel_size) > 0:
S
SunAhong1993 已提交
22 23 24 25 26
        k_h = kernel_h if kernel_h > 0 else kernel_size[0]
        k_w = kernel_w if kernel_w > 0 else kernel_size[len(kernel_size) - 1]
    elif kernel_h > 0 or kernel_w > 0:
        k_h = kernel_h
        k_w = kernel_w
S
SunAhong1993 已提交
27 28 29
    [s_h, s_w] = [1, 1]
    if isinstance(stride, numbers.Number):
        [s_h, s_w] = [stride] * 2
S
SunAhong1993 已提交
30
    elif len(stride) > 0:
S
SunAhong1993 已提交
31 32 33 34 35
        s_h = stride_h if stride_h > 0 else stride[0]
        s_w = stride_w if stride_w > 0 else stride[len(stride) - 1]
    elif stride_h > 0 or stride_w > 0:
        s_h = stride_h
        s_w = stride_w
S
SunAhong1993 已提交
36 37 38
    [p_h, p_w] = [0, 0]
    if isinstance(pad, numbers.Number):
        [p_h, p_w] = [pad] * 2
S
SunAhong1993 已提交
39
    elif len(pad) > 0:
S
SunAhong1993 已提交
40 41 42 43 44
        p_h = pad_h if pad_h > 0 else pad[0]
        p_w = pad_w if pad_w > 0 else pad[len(pad) - 1]
    elif pad_h > 0 or pad_w > 0:
        p_h = pad_h
        p_w = pad_w
S
SunAhong1993 已提交
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
    dila_len = len(dilation)
    dila_h = 1
    dila_w = 1
    if dila_len == 2:
        dila_h = dilation[0]
        dila_w = dilation[1]
    elif dila_len == 1:
        dila_h = dila_w = dilation[0]
    else:
        assert dila_len == 0, "invalid length[%s] of dilation in convolution" % (
            dila_len)
    i_w = input_shape[0][2]
    i_h = input_shape[0][3]
    o_h = (i_h + 2 * p_h - (dila_h * (k_h - 1) + 1)) / float(s_h) + 1
    o_w = (i_w + 2 * p_w - (dila_w * (k_w - 1) + 1)) / float(s_w) + 1
    import math
    o_h = int(math.floor(o_h))
    o_w = int(math.floor(o_w))
    c = num_output if num_output is not None else input_shape[0][1]
    return [[input_shape[0][0], c, o_h, o_w]]


def convolutiondepthwise_layer(inputs,
                               num_output=None,
                               pad=None,
                               kernel_size=None,
                               stride=None,
                               dilation=None,
                               pad_h=None,
                               pad_w=None,
                               kernel_h=None,
                               kernel_w=None,
                               stride_h=None,
                               stride_w=None,
S
SunAhong1993 已提交
79
                               input_shape=None,
S
SunAhong1993 已提交
80
                               name=None):
S
SunAhong1993 已提交
81
    import numbers
S
SunAhong1993 已提交
82 83 84
    [k_h, k_w] = [1, 1]
    if isinstance(kernel_size, numbers.Number):
        [k_h, k_w] = [kernel_size] * 2
S
SunAhong1993 已提交
85
    elif len(kernel_size) > 0:
S
SunAhong1993 已提交
86 87 88 89 90
        k_h = kernel_h if kernel_h > 0 else kernel_size[0]
        k_w = kernel_w if kernel_w > 0 else kernel_size[len(kernel_size) - 1]
    elif kernel_h > 0 or kernel_w > 0:
        k_h = kernel_h
        k_w = kernel_w
S
SunAhong1993 已提交
91 92 93
    [s_h, s_w] = [1, 1]
    if isinstance(stride, numbers.Number):
        [s_h, s_w] = [stride] * 2
S
SunAhong1993 已提交
94
    elif len(stride) > 0:
S
SunAhong1993 已提交
95 96 97 98 99
        s_h = stride_h if stride_h > 0 else stride[0]
        s_w = stride_w if stride_w > 0 else stride[len(stride) - 1]
    elif stride_h > 0 or stride_w > 0:
        s_h = stride_h
        s_w = stride_w
S
SunAhong1993 已提交
100 101 102
    [p_h, p_w] = [0, 0]
    if isinstance(pad, numbers.Number):
        [p_h, p_w] = [pad] * 2
S
SunAhong1993 已提交
103
    elif len(pad) > 0:
S
SunAhong1993 已提交
104 105 106 107 108
        p_h = pad_h if pad_h > 0 else pad[0]
        p_w = pad_w if pad_w > 0 else pad[len(pad) - 1]
    elif pad_h > 0 or pad_w > 0:
        p_h = pad_h
        p_w = pad_w
S
SunAhong1993 已提交
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
    input = inputs[0]
    dila_len = len(dilation)
    dila_h = 1
    dila_w = 1
    if dila_len == 2:
        dila_h = dilation[0]
        dila_w = dilation[1]
    elif dila_len == 1:
        dila_h = dila_w = dilation[0]
    else:
        assert dila_len == 0, "invalid length[%s] of dilation in convolution" % (
            dila_len)
    c_in = input_shape[0][1]
    c_out = num_output if num_output is not None else input_shape[0][1]
    group = int(c_in / (c_in / c_out)) if c_in > c_out else int(c_in /
                                                                (c_out / c_in))
    out = fluid.layers.conv2d(input,
                              dilation=[dila_h, dila_w],
                              filter_size=[k_h, k_w],
                              stride=[s_h, s_w],
                              padding=[p_h, p_w],
                              groups=group,
                              num_filters=c_out,
                              param_attr=name + '_weights',
                              bias_attr=name + '_bias',
                              name=name)
    return out


def convolutiondepthwise_weights(name, data=None):
    weights_name = []
    weights_name.append(name + '_weights')
    weights_name.append(name + '_bias')
    return weights_name


register(kind='ConvolutionDepthwise',
         shape=convolutiondepthwise_shape,
         layer=convolutiondepthwise_layer,
         weights=convolutiondepthwise_weights)