caffe_shape.py 11.5 KB
Newer Older
S
SunAhong1993 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2019  PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import math
16 17 18 19 20 21 22 23 24
import numbers
from functools import reduce


def get_kernel_parameters(params):
    [k_h, k_w] = [1, 1]
    if isinstance(params.kernel_size, numbers.Number):
        [k_h, k_w] = [params.kernel_size] * 2
    elif len(params.kernel_size) > 0:
S
SunAhong1993 已提交
25 26
        k_h = params.kernel_h if params.kernel_h > 0 else params.kernel_size[0]
        k_w = params.kernel_w if params.kernel_w > 0 else params.kernel_size[
27
            len(params.kernel_size) - 1]
S
SunAhong1993 已提交
28 29 30
    elif params.kernel_h > 0 or params.kernel_w > 0:
        k_h = params.kernel_h
        k_w = params.kernel_w
31 32 33 34
    [s_h, s_w] = [1, 1]
    if isinstance(params.stride, numbers.Number):
        [s_h, s_w] = [params.stride] * 2
    elif len(params.stride) > 0:
S
SunAhong1993 已提交
35 36
        s_h = params.stride_h if params.stride_h > 0 else params.stride[0]
        s_w = params.stride_w if params.stride_w > 0 else params.stride[
37
            len(params.stride) - 1]
S
SunAhong1993 已提交
38 39 40
    elif params.stride_h > 0 or params.stride_w > 0:
        s_h = params.stride_h
        s_w = params.stride_w
41 42 43 44
    [p_h, p_w] = [0, 0]
    if isinstance(params.pad, numbers.Number):
        [p_h, p_w] = [params.pad] * 2
    elif len(params.pad) > 0:
S
SunAhong1993 已提交
45
        p_h = params.pad_h if params.pad_h > 0 else params.pad[0]
C
channingss 已提交
46 47
        p_w = params.pad_w if params.pad_w > 0 else params.pad[len(params.pad) -
                                                               1]
S
SunAhong1993 已提交
48 49 50
    elif params.pad_h > 0 or params.pad_w > 0:
        p_h = params.pad_h
        p_w = params.pad_w
51
    dila_h = dila_w = 1
S
SunAhong1993 已提交
52
    if hasattr(params, 'dilation'):
53 54
        dila_len = len(params.dilation)
        if dila_len == 2:
S
SunAhong1993 已提交
55 56
            dila_h = params.dilation[0]
            dila_w = params.dilation[1]
57 58
        elif dila_len == 1:
            dila_h = dila_w = params.dilation[0]
S
SunAhong1993 已提交
59
        else:
60 61 62
            assert dila_len == 0, "invalid length[%s] of dilation in convolution" % (
                dila_len)
    return dila_h, dila_w, p_h, p_w, k_h, k_w, s_h, s_w
S
SunAhong1993 已提交
63 64


65 66 67 68
def get_strided_kernel_output_shape(params, input_shape, round_func):
    i_h = input_shape[2]
    i_w = input_shape[3]
    dila_h, dila_w, pad_h, pad_w, kernel_h, kernel_w, stride_h, stride_w = get_kernel_parameters(
S
SunAhong1993 已提交
69 70 71 72 73
        params)
    o_h = (i_h + 2 * pad_h - (dila_h *
                              (kernel_h - 1) + 1)) / float(stride_h) + 1
    o_w = (i_w + 2 * pad_w - (dila_w *
                              (kernel_w - 1) + 1)) / float(stride_w) + 1
74 75
    o_h = int(round_func(o_h))
    o_w = int(round_func(o_w))
S
SunAhong1993 已提交
76 77 78 79 80 81 82 83 84 85 86
    has_c_o = hasattr(params, 'num_output')
    c = params.num_output if has_c_o else input_shape[1]
    return [[input_shape[0], c, o_h, o_w]]


def shape_convolution(layer, input_shape):
    params = layer.convolution_param
    return get_strided_kernel_output_shape(params, input_shape[0], math.floor)


def shape_deconvolution(layer, input_shape):
S
SunAhong1993 已提交
87 88 89

    h_i = input_shape[0][2]
    w_i = input_shape[0][3]
S
SunAhong1993 已提交
90 91

    params = layer.convolution_param
S
SunAhong1993 已提交
92
    dila_h, dila_w, pad_h, pad_w, kernel_h, kernel_w, stride_h, stride_w = get_kernel_parameters(
S
SunAhong1993 已提交
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
        params)

    h_o = (h_i - 1) * stride_h - 2 * pad_h + dila_h * (kernel_h - 1) + 1
    w_o = (w_i - 1) * stride_w - 2 * pad_w + dila_w * (kernel_w - 1) + 1

    has_c_o = hasattr(params, 'num_output')
    c = params.num_output if has_c_o else input_shape.channels
    return [[input_shape[0][0], c, h_o, w_o]]


def shape_pooling(layer, input_shape):
    params = layer.pooling_param
    global_pool = getattr(params, 'global_pooling', False)
    if global_pool:
        return [[input_shape[0][0], input_shape[0][1], 1, 1]]

    ceil_mode = getattr(params, 'ceil_mode', True)
    if ceil_mode is True:
        method = math.ceil
    else:
        method = math.floor
    return get_strided_kernel_output_shape(params, input_shape[0], method)


def shape_innerproduct(layer, input_shape):
    params = layer.inner_product_param
    return [[input_shape[0][0], params.num_output]]


def shape_lrn(layer, input_shape):
    return input_shape


def shape_relu(layer, input_shape):
    return input_shape


def shape_softmax(layer, input_shape):
    return input_shape


def shape_input(layer, input_shape):
    return [list(layer.input_param.shape[0].dim)]
S
SunAhong1993 已提交
136

S
SunAhong1993 已提交
137

S
SunAhong1993 已提交
138 139 140 141 142 143 144 145 146 147
def shape_memorydata(layer, input_shape):
    params = layer.memory_data_param
    shape = []
    shape.append(int(params.batch_size))
    shape.append(int(params.channels))
    shape.append(int(params.height))
    shape.append(int(params.width))
    return [shape]


S
SunAhong1993 已提交
148 149 150 151 152 153
def shape_concat(layer, input_shape):
    params = layer.concat_param
    axis = params.axis
    output_shape = None
    for shape in input_shape:
        if output_shape is None:
154 155 156
            output_shape = []
            for i in range(len(shape)):
                output_shape.append(shape[i])
S
SunAhong1993 已提交
157 158
        else:
            output_shape[axis] += shape[axis]
S
SunAhong1993 已提交
159 160 161 162 163
    return [output_shape]


def shape_slice(layer, input_shape):
    inshape = input_shape[0]
S
SunAhong1993 已提交
164 165

    top_len = len(layer.top)
S
SunAhong1993 已提交
166 167
    params = layer.slice_param
    axis = params.axis
S
SunAhong1993 已提交
168 169 170
    slice_dim = params.slice_dim
    if slice_dim != 1 and axis == 1:
        axis = slice_dim
S
SunAhong1993 已提交
171
    points = list(params.slice_point)
S
SunAhong1993 已提交
172 173 174 175 176 177 178 179
    count = inshape[axis]
    if len(points) == 0:
        assert count % top_len == 0, "the parameter of Slice is wrong"
        part = count / top_len
        t = part
        while t < count:
            points.append(int(t))
            t += part
S
SunAhong1993 已提交
180 181 182
    points = [0] + points + [count]
    output_shape = []
    for i in range(len(points)):
183 184 185
        shape = []
        for ii in range(len(inshape)):
            shape.append(inshape[ii])
S
SunAhong1993 已提交
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
        size = points[i + 1] - points[i]
        shape[axis] = size
        output_shape.append(shape)
        if i == len(points) - 2:
            break
    return output_shape


def shape_prelu(layer, input_shape):
    return input_shape


def shape_sigmoid(layer, input_shape):
    return input_shape


def shape_absval(layer, input_shape):
    return input_shape


def shape_accuracy(layer, input_shape):
    return [[1]]


def shape_tanh(layer, input_shape):
    return input_shape


def shape_eltwise(layer, input_shape):
    return [input_shape[0]]


def shape_batchnorm(layer, input_shape):
    return input_shape


def shape_scale(layer, input_shape):
    return input_shape
S
SunAhong1993 已提交
224 225 226 227 228 229 230 231


def shape_reshape(layer, input_shape):
    def count(num_list):
        return reduce(lambda a, b: a * b, num_list)

    inshape = input_shape[0]
    params = layer.reshape_param
232 233
    axis = params.axis if hasattr(params, 'axis') else 0
    num_axes = params.num_axes if hasattr(params, 'num_axes') else -1
S
SunAhong1993 已提交
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
    if inshape[0] == -1:
        inshape[0] = 1
    input_count = count(inshape)

    input_num_axes = len(inshape)

    input_start_axis = axis
    start_axis = input_start_axis if input_start_axis >= 0 \
            else input_num_axes + input_start_axis + 1

    assert start_axis >= 0, "[Reshape]axis %d out of range" % (input_start_axis)
    assert start_axis <= input_num_axes, "[Reshape]axis %d out of range for %d-D input data"\
            % (input_start_axis, input_num_axes)

    assert num_axes >= -1, "[Reshape]num_axes must be >= 0, or -1 for all"

    end_axis = input_num_axes if num_axes == -1 else start_axis + num_axes
    assert end_axis <= input_num_axes, "end_axis[%d] = axis[%d] + num_axes[%d] is out of range"\
            % (end_axis, start_axis, num_axes)

    num_axes_replaced = end_axis - start_axis
    num_axes_retained = input_num_axes - num_axes_replaced
256
    num_new_axes = len(list(params.shape.dim))
S
SunAhong1993 已提交
257 258 259 260 261 262
    outshape = []

    for i in range(start_axis):
        outshape.append(inshape[i])

    for i in range(num_new_axes):
263
        outshape.append(params.shape.dim[i])
S
SunAhong1993 已提交
264 265 266 267 268 269 270 271 272 273 274

    for i in range(end_axis, input_num_axes):
        outshape.append(inshape[i])

    assert len(outshape) == num_axes_retained + num_new_axes,\
            "[Reshape]invalid dims of output shape[%s]" % (str(outshape))

    inferred_axis = -1
    copy_axes = []
    constant_count = 1
    for i in range(num_new_axes):
275
        top_dim = params.shape.dim[i]
S
SunAhong1993 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        if top_dim == 0:
            copy_axes.append(i)
            copy_axis_index = start_axis + i
            outshape[copy_axis_index] = inshape[copy_axis_index]
        elif top_dim == -1:
            assert inferred_axis == -1, "[Reshape]new shape contains multiple -1 dims"
            inferred_axis = i
        else:
            constant_count *= top_dim

    if inferred_axis >= 0:
        explicit_count = constant_count
        l = inshape[0:start_axis]
        if len(l) > 0:
            explicit_count *= count(l)
        l = inshape[end_axis:]
        if len(l) > 0:
            explicit_count *= count(l)
        for i in range(len(copy_axes)):
            explicit_count *= outshape[start_axis + copy_axes[i]]
S
SunAhong1993 已提交
296 297 298 299
        assert input_count % explicit_count == 0, "[Reshape]botom count[%d] "\
                "must be divisible by product of the specified dimensions[%d] "\
                % (input_count, explicit_count)
        outshape[start_axis + inferred_axis] = int(input_count / explicit_count)
S
SunAhong1993 已提交
300 301

    output_count = count(outshape)
S
SunAhong1993 已提交
302 303 304
    assert output_count == input_count, "[Reshape]output count[%d] must match input count[%d]" % (
        output_count, input_count)
    outshape[0] = -1
S
SunAhong1993 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
    return [outshape]


def shape_argmax(layer, input_shape):
    inshape = input_shape[0]
    params = layer.argmax_param
    out_max_val = params.out_max_val if hasattr(params, out_max_val) else False
    top_k = params.top_k if hasattr(params, top_k) else 1
    axis = parmas.axis if hasattr(params, axis) else -1
    if axis < 0:
        axis += len(inshape)
    assert (axis + 1 == len(inshape)
            ), 'only can be applied on the last dimension[axis:%d, %s] now,'\
                    'make sure you have set axis param in xxx.prototxt file' \
                    % (axis, str(inshape))

    outshape = inshape
    outshape[-1] = top_k
    if out_max_val is True:
        outshape[-1] *= 2
    return [outshape]


def shape_crop(layer, input_shape):
    assert len(input_shape) == 2, "the number of crop's inputs must be 2"
    return [input_shape[1]]


def shape_flatten(layer, input_shape):
    assert len(input_shape) == 1, "the number of flatten's inputs must be 1"
335
    inshape = input_shape[0]
S
SunAhong1993 已提交
336 337 338 339
    params = layer.flatten_param
    start_axis = params.axis
    end_axis = params.end_axis
    if start_axis < 0:
340
        start_axis += len(inshape)
S
SunAhong1993 已提交
341
    if end_axis < 0:
342
        end_axis += len(inshape) + 1
S
SunAhong1993 已提交
343 344
    assert start_axis <= end_axis, 'invalid axis[%d] or end_axis[%d] params'\
            % (start_axis, end_axis)
345 346 347 348 349
    output_shape = inshape[0:start_axis]
    if len(inshape[start_axis:end_axis]) != 0:
        flat_sz = reduce(lambda a, b: a * b, inshape[start_axis:end_axis])
        output_shape += [flat_sz]
    output_shape += inshape[end_axis:len(inshape)]
S
SunAhong1993 已提交
350
    output_shape[0] = -1
S
SunAhong1993 已提交
351 352 353 354 355 356 357 358 359 360 361 362 363 364
    return [output_shape]


def shape_power(layer, input_shape):
    return input_shape


def shape_reduction(layer, input_shape):
    params = layer.reduction_param
    axis = params.axis
    if axis < 0:
        axis += len(input_shape[0]) + 1
    assert axis <= len(input_shape[0]), 'invalid axis[%d] error' % (axis)
    return [input_shape[0:axis]]