caffe_op_mapper.py 36.7 KB
Newer Older
J
jiangjiajun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
#   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.
S
SunAhong1993 已提交
14 15

import numbers
S
SunAhong1993 已提交
16
import numpy as np
J
jiangjiajun 已提交
17 18
from x2paddle.decoder.caffe_decoder import CaffeGraph
from x2paddle.core.op_mapper import OpMapper
S
SunAhong1993 已提交
19
from x2paddle.core.util import *
20
from x2paddle.op_mapper import caffe_shape
S
SunAhong1993 已提交
21
from x2paddle.op_mapper.caffe_custom_layer import *
S
SunAhong1993 已提交
22 23


J
jiangjiajun 已提交
24
class CaffeOpMapper(OpMapper):
S
SunAhong1993 已提交
25 26 27 28 29 30
    directly_map_ops = {
        'AbsVal': 'abs',
        'Sigmoid': 'sigmoid',
        'TanH': 'tanh',
    }

J
jiangjiajun 已提交
31 32 33
    def __init__(self, decoder):
        super(CaffeOpMapper, self).__init__()
        self.graph = decoder.caffe_graph
S
SunAhong1993 已提交
34
        self.weights = dict()
J
jiangjiajun 已提交
35
        resolver = decoder.resolver
J
jiangjiajun 已提交
36
        self.used_custom_layers = {}
S
SunAhong1993 已提交
37 38 39 40

        print("Total nodes: {}".format(len(self.graph.topo_sort)))
        for node_name in self.graph.topo_sort:
            node = self.graph.get_node(node_name)
S
SunAhong1993 已提交
41 42
            if node.layer_type == 'DepthwiseConvolution':
                node.layer_type = 'ConvolutionDepthwise'
S
SunAhong1993 已提交
43 44
            op = node.layer_type
            if hasattr(self, op):
45
                self.set_node_shape(node)
J
jiangjiajun 已提交
46 47
                func = getattr(self, op)
                func(node)
S
SunAhong1993 已提交
48
            elif op in custom_layers:
49
                self.set_node_shape(node, is_fluid_op=False)
S
SunAhong1993 已提交
50
                self.deal_custom_layer(node)
S
SunAhong1993 已提交
51 52 53
            elif op in self.directly_map_ops:
                self.set_node_shape(node)
                self.directly_map(node)
S
SunAhong1993 已提交
54
            else:
S
SunAhong1993 已提交
55 56
                raise Exception(
                    "The op {} in model is not supported yet.".format(op))
S
SunAhong1993 已提交
57

J
jiangjiajun 已提交
58 59 60
    def op_checker(self):
        unsupported_ops = set()
        for node_name in self.graph.topo_sort:
S
SunAhong1993 已提交
61
            node = self.graph.get_node(node_name)
J
jiangjiajun 已提交
62 63 64 65 66 67 68 69 70 71 72
            op = node.layer_type
            if not hasattr(self, op) and op not in custom_layers:
                unsupported_ops.add(op)
        if len(unsupported_ops) == 0:
            return True
        else:
            print("There are {} ops not supported yet, list as below".format(
                len(unsupported_ops)))
            for op in unsupported_ops:
                print(op)
            return False
S
SunAhong1993 已提交
73

74
    def set_node_shape(self, node, is_fluid_op=True):
S
SunAhong1993 已提交
75 76 77 78 79 80 81
        inputs = node.inputs
        input_shape = []
        for i, nm in enumerate(inputs):
            last_node = self.graph.get_node(nm)
            tmp = node.layer.bottom[i]
            idx = list(last_node.layer.top).index(tmp)
            input_shape.append(last_node.output_shape[idx])
82 83 84

        node.input_shape = input_shape
        func_name = 'shape_' + node.layer_type.lower()
S
SunAhong1993 已提交
85
        if is_fluid_op:
86 87
            node.output_shape = getattr(caffe_shape, func_name)(node.layer,
                                                                input_shape)
S
SunAhong1993 已提交
88
        else:
89
            node.output_shape = compute_output_shape(node)
S
SunAhong1993 已提交
90 91 92

    def adjust_parameters(self, node):
        data = node.data
S
SunAhong1993 已提交
93 94 95 96 97 98 99 100
        # When using the protobuf-backend, each parameter initially has four dimensions.
        # In certain cases (like FC layers), we want to eliminate the singleton dimensions.
        # This implementation takes care of the common cases. However, it does leave the
        # potential for future issues.
        # The Caffe-backend does not suffer from this problem.
        data = list(data)

        squeeze_indices = [1]  # Squeeze biases.
S
SunAhong1993 已提交
101
        if node.layer_type == 'InnerProduct':
S
SunAhong1993 已提交
102 103 104 105 106
            squeeze_indices.append(0)  # Squeeze FC.

        for idx in squeeze_indices:
            if idx >= len(data):
                continue
S
SunAhong1993 已提交
107

S
SunAhong1993 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
            d = data[idx]
            assert len(
                d.shape
            ) == 4, 'invalid shape[%s] from caffe when adjust_parameters' % (
                str(d.shape))

            shape_old = d.shape
            sq_axis = None
            if idx == 0:
                sq_axis = (0, 1)
            elif idx == 1:
                sq_axis = (0, 1, 2)
            else:
                continue

            data[idx] = np.squeeze(d, axis=sq_axis)
            shape_new = data[idx].shape
        return data
S
SunAhong1993 已提交
126

S
SunAhong1993 已提交
127
    def get_kernel_parameters(self, kind, params):
S
SunAhong1993 已提交
128
        assert kind in ['Convolution', 'Pooling', 'Deconvolution']
S
SunAhong1993 已提交
129 130 131
        [k_h, k_w] = [1, 1]
        if isinstance(params.kernel_size, numbers.Number):
            [k_h, k_w] = [params.kernel_size] * 2
S
SunAhong1993 已提交
132
        elif len(params.kernel_size) > 0:
C
channingss 已提交
133 134
            k_h = params.kernel_h if params.kernel_h > 0 else params.kernel_size[
                0]
S
SunAhong1993 已提交
135
            k_w = params.kernel_w if params.kernel_w > 0 else params.kernel_size[
S
SunAhong1993 已提交
136
                len(params.kernel_size) - 1]
S
SunAhong1993 已提交
137 138 139
        elif params.kernel_h > 0 or params.kernel_w > 0:
            k_h = params.kernel_h
            k_w = params.kernel_w
S
SunAhong1993 已提交
140 141 142
        [s_h, s_w] = [1, 1]
        if isinstance(params.stride, numbers.Number):
            [s_h, s_w] = [params.stride] * 2
S
SunAhong1993 已提交
143
        elif len(params.stride) > 0:
S
SunAhong1993 已提交
144
            s_h = params.stride_h if params.stride_h > 0 else params.stride[0]
J
jiangjiajun 已提交
145 146
            s_w = params.stride_w if params.stride_w > 0 else params.stride[len(
                params.stride) - 1]
S
SunAhong1993 已提交
147 148 149
        elif params.stride_h > 0 or params.stride_w > 0:
            s_h = params.stride_h
            s_w = params.stride_w
S
SunAhong1993 已提交
150 151 152
        [p_h, p_w] = [0, 0]
        if isinstance(params.pad, numbers.Number):
            [p_h, p_w] = [params.pad] * 2
S
SunAhong1993 已提交
153
        elif len(params.pad) > 0:
S
SunAhong1993 已提交
154
            p_h = params.pad_h if params.pad_h > 0 else params.pad[0]
J
jiangjiajun 已提交
155 156
            p_w = params.pad_w if params.pad_w > 0 else params.pad[len(
                params.pad) - 1]
S
SunAhong1993 已提交
157 158 159
        elif params.pad_h > 0 or params.pad_w > 0:
            p_h = params.pad_h
            p_w = params.pad_w
S
SunAhong1993 已提交
160 161 162
        dila_h = dila_w = 1
        group = 1
        c_o = 1
163
        if kind in ['Convolution', 'Deconvolution']:
S
SunAhong1993 已提交
164 165 166 167 168 169 170 171 172 173
            c_o = params.num_output
            dila_len = len(params.dilation)
            if dila_len == 2:
                dila_h = params.dilation[0]
                dila_w = params.dilation[1]
            elif dila_len == 1:
                dila_h = dila_w = params.dilation[0]
            else:
                assert dila_len == 0, "invalid length[%s] of dilation in convolution" % (
                    dila_len)
S
SunAhong1993 已提交
174 175
        if kind in ['Convolution', 'Deconvolution']:
            group = params.group
S
SunAhong1993 已提交
176 177 178 179 180 181
        kernel = [k_h, k_w]
        stride = [s_h, s_w]
        pad = [p_h, p_w]
        dilation = [dila_h, dila_w]
        return c_o, kernel, stride, pad, dilation, group

S
SunAhong1993 已提交
182 183 184 185 186 187
    def get_input_name(self, node):
        if hasattr(node, "index"):
            return node.layer_name + "[{}]".format(node.index)
        else:
            return node.layer_name

S
SunAhong1993 已提交
188 189 190 191 192 193 194 195
    def Input(self, node):
        shape = list(node.layer.input_param.shape[0].dim)[1:]
        dtype = 'float32'
        attr = {
            'dtype': string(dtype),
            'shape': shape,
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
196 197
        node.fluid_code.add_layer(
            "data", inputs=None, output=node, param_attr=attr)
S
SunAhong1993 已提交
198

S
SunAhong1993 已提交
199 200 201 202 203 204 205 206 207
    def MemoryData(self, node):
        # TODO(syf): Paddlepaddle can't fully support
        shape = node.output_shape[0][1:]
        dtype = 'float32'
        attr = {
            'dtype': string(dtype),
            'shape': shape,
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
208 209
        node.fluid_code.add_layer(
            "data", inputs=None, output=node.layer_name + '0', param_attr=attr)
S
SunAhong1993 已提交
210 211 212
        node.fluid_code.add_note('{} = [{}]'.format(node.layer_name,
                                                    node.layer_name + '0'))

S
SunAhong1993 已提交
213 214 215 216 217
    def Convolution(self, node):
        data = node.data
        params = node.layer.convolution_param
        channel, kernel, stride, pad, dilation, group = self.get_kernel_parameters(
            node.layer_type, params)
S
SunAhong1993 已提交
218 219
        if data is None:
            data = []
C
channingss 已提交
220 221 222
            print(
                'The parameter of {} (type is {}) is not set. So we set the parameters as 0'
                .format(node.layer_name, node.layer_type))
S
SunAhong1993 已提交
223 224
            input_c = node.input_shape[0][1]
            output_c = channel
C
channingss 已提交
225
            data.append(
J
jiangjiajun 已提交
226 227
                np.zeros([output_c, input_c, kernel[0], kernel[1]]).astype(
                    'float32'))
S
0.8.4  
sunyanfang01 已提交
228
            data.append(np.zeros([output_c, ]).astype('float32'))
S
SunAhong1993 已提交
229 230 231 232 233
        else:
            data = self.adjust_parameters(node)
        self.weights[node.layer_name + '_weights'] = data[0]
        if len(data) == 2:
            self.weights[node.layer_name + '_bias'] = data[1]
S
SunAhong1993 已提交
234 235
        assert len(node.inputs
                   ) == 1, 'The count of Convolution node\'s input is not 1.'
S
SunAhong1993 已提交
236
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
237

S
SunAhong1993 已提交
238
        attr = {
J
jiangjiajun 已提交
239 240 241 242 243 244 245 246 247 248
            'filter_size': kernel,
            'num_filters': channel,
            'stride': stride,
            'padding': pad,
            'dilation': dilation,
            'groups': group,
            'name': string(node.layer_name),
            'param_attr': string(node.layer_name + '_weights'),
            'bias_attr': False
            if len(data) == 1 else string(node.layer_name + '_bias'),
S
SunAhong1993 已提交
249
        }
J
jiangjiajun 已提交
250 251
        node.fluid_code.add_layer(
            "conv2d", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
252 253 254 255 256 257

    def Deconvolution(self, node):
        data = node.data
        params = node.layer.convolution_param
        channel, kernel, stride, pad, dilation, group = self.get_kernel_parameters(
            node.layer_type, params)
S
SunAhong1993 已提交
258 259
        if data is None:
            data = []
C
channingss 已提交
260 261 262
            print(
                'The parameter of {} (type is {}) is not set. So we set the parameters as 0'
                .format(node.layer_name, node.layer_type))
S
SunAhong1993 已提交
263 264
            input_c = node.input_shape[0][1]
            output_c = channel
C
channingss 已提交
265
            data.append(
J
jiangjiajun 已提交
266 267 268
                np.zeros([output_c, input_c, kernel[0], kernel[1]]).astype(
                    'float32'))
            data.append(np.zeros([output_c, ]).astype('float32'))
S
SunAhong1993 已提交
269 270 271 272 273
        else:
            data = self.adjust_parameters(node)
        self.weights[node.layer_name + '_weights'] = data[0]
        if len(data) == 2:
            self.weights[node.layer_name + '_bias'] = data[1]
S
SunAhong1993 已提交
274 275
        assert len(node.inputs
                   ) == 1, 'The count of Deconvolution node\'s input is not 1.'
S
SunAhong1993 已提交
276
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
277
        attr = {
J
jiangjiajun 已提交
278 279 280 281 282 283 284 285 286 287 288
            'output_size': None,
            'filter_size': kernel,
            'num_filters': channel,
            'stride': stride,
            'padding': pad,
            'dilation': dilation,
            'groups': group,
            'name': string(node.layer_name),
            'param_attr': string(node.layer_name + '_weights'),
            'bias_attr': False
            if len(data) == 1 else string(node.layer_name + '_bias')
S
SunAhong1993 已提交
289
        }
J
jiangjiajun 已提交
290 291
        node.fluid_code.add_layer(
            "conv2d_transpose", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
292 293 294

    def Pooling(self, node):
        params = node.layer.pooling_param
S
SunAhong1993 已提交
295
        ceil_mode = getattr(params, 'ceil_mode', True)
S
SunAhong1993 已提交
296 297
        global_pool = getattr(params, 'global_pooling', False)
        kernel_default = [1, 1]
S
SunAhong1993 已提交
298
        channel, kernel, stride, pad, dilation, group = self.get_kernel_parameters(
S
SunAhong1993 已提交
299
            node.layer_type, params)
S
SunAhong1993 已提交
300 301 302 303 304 305
        if params.pool == 0:
            pool_type = 'max'
        else:
            pool_type = 'avg'
        assert len(
            node.inputs) == 1, 'The count of Pooling node\'s input is not 1.'
S
SunAhong1993 已提交
306
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
307 308 309 310
        attr = {
            'pool_size': kernel,
            'pool_stride': stride,
            'pool_padding': pad,
S
SunAhong1993 已提交
311
            'ceil_mode': ceil_mode,
S
SunAhong1993 已提交
312
            'pool_type': string(pool_type),
S
SunAhong1993 已提交
313
            'exclusive': False,
S
SunAhong1993 已提交
314
            'global_pooling': global_pool,
S
SunAhong1993 已提交
315 316
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
317 318
        node.fluid_code.add_layer(
            "pool2d", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
319 320 321 322 323 324 325 326 327 328 329

    def LRN(self, node):
        assert len(node.inputs) == 1, 'The count of LRN node\'s input is not 1.'
        params = node.layer.lrn_param
        # The window size must be an odd value. For a window
        # size of (2*n+1), Paddle defines depth_radius = n.
        assert params.local_size % 2 == 1
        # Caffe scales by (alpha/(2*n+1)), whereas Paddle
        # just scales by alpha (as does Krizhevsky's paper).
        # We'll account for that here.
        alpha = params.alpha / float(params.local_size)
S
SunAhong1993 已提交
330
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
331 332
        attr = {
            'n': params.local_size,
S
SunAhong1993 已提交
333
            'k': params.k,
S
SunAhong1993 已提交
334 335 336 337
            'alpha': alpha,
            'beta': params.beta,
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
338 339
        node.fluid_code.add_layer(
            "lrn", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
340 341 342

    def InnerProduct(self, node):
        data = node.data
S
SunAhong1993 已提交
343 344
        params = node.layer.inner_product_param
        if data is None:
C
channingss 已提交
345 346 347
            print(
                'The parameter of {} (type is {}) is not set. So we set the parameters as 0.'
                .format(node.layer_name, node.layer_type))
S
SunAhong1993 已提交
348 349 350
            input_c = node.input_shape[0][1]
            output_c = params.num_output
            data = []
C
channingss 已提交
351
            data.append(
J
jiangjiajun 已提交
352 353
                np.zeros([input_c, output_c]).astype('float32').astype(
                    'float32'))
C
channingss 已提交
354 355
            data.append(
                np.zeros([output_c]).astype('float32').astype('float32'))
S
SunAhong1993 已提交
356 357 358 359 360 361 362 363 364 365
        else:
            data = self.adjust_parameters(node)
            # Reshape the parameters to Paddle's ordering
            transpose_order = (1, 0)
            w = data[0]
            fc_shape = w.shape
            output_channels = fc_shape[0]
            w = w.reshape((output_channels, -1))
            w = w.transpose(transpose_order)
            data[0] = w
S
SunAhong1993 已提交
366

S
SunAhong1993 已提交
367 368 369 370 371
        self.weights[node.layer_name + '_weights'] = data[0]
        if len(data) == 2:
            self.weights[node.layer_name + '_bias'] = data[1]
        assert len(node.inputs
                   ) == 1, 'The count of InnerProduct node\'s input is not 1.'
S
SunAhong1993 已提交
372
        #params = node.layer.inner_product_param
S
SunAhong1993 已提交
373 374
        assert params.axis == 1
        assert params.bias_term == True
S
SunAhong1993 已提交
375
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
376
        attr = {
J
jiangjiajun 已提交
377 378 379 380 381 382
            'size': params.num_output,
            'name': string(node.layer_name),
            'act': None,
            'param_attr': string(node.layer_name + '_weights'),
            'bias_attr': False
            if len(data) == 1 else string(node.layer_name + '_bias')
S
SunAhong1993 已提交
383
        }
J
jiangjiajun 已提交
384 385
        node.fluid_code.add_layer(
            "fc", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
386 387 388 389

    def Softmax(self, node):
        assert len(
            node.inputs) == 1, 'The count of Softmax node\'s input is not 1.'
S
SunAhong1993 已提交
390
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
391 392 393 394 395
        params = node.layer.softmax_param
        axis = params.axis
        shape = node.input_shape[0]
        dims = len(shape)
        axis = axis + dims if axis < 0 else axis
S
SunAhong1993 已提交
396
        attr = {'axis': axis, 'name': string(node.layer_name + '_softmax')}
J
jiangjiajun 已提交
397 398
        node.fluid_code.add_layer(
            "softmax", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
399 400 401 402 403

    def Slice(self, node):
        assert len(
            node.inputs) == 1, 'The count of Slice node\'s input is not 1.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
404
        top_len = len(node.layer.top)
S
SunAhong1993 已提交
405 406
        params = node.layer.slice_param
        axis = params.axis
S
SunAhong1993 已提交
407 408 409
        slice_dim = params.slice_dim
        if slice_dim != 1 and axis == 1:
            axis = slice_dim
S
SunAhong1993 已提交
410 411 412 413 414 415 416 417 418
        output_shape = node.output_shape
        sections_list = []
        for s in output_shape:
            sections_list.append(s[axis])
        attr = {
            'num_or_sections': sections_list,
            'dim': axis,
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
419 420
        node.fluid_code.add_layer(
            "split", inputs=input, output=node.layer_name, param_attr=attr)
C
channingss 已提交
421

S
SunAhong1993 已提交
422 423
    def Concat(self, node):
        assert len(
S
SunAhong1993 已提交
424
            node.inputs
S
sunyanfang01 已提交
425
        ) >= 1, 'The count of Concat node\'s input is not more than 1.'
S
SunAhong1993 已提交
426 427 428 429 430 431
        inputs = []
        for i in range(len(node.inputs)):
            input = self.graph.get_bottom_node(node, idx=i, copy=True)
            inputs.append(input)
        params = node.layer.concat_param
        axis = params.axis
S
SunAhong1993 已提交
432
        attr = {'axis': axis, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
433 434
        node.fluid_code.add_layer(
            "concat", inputs=inputs, output=node, param_attr=attr)
S
SunAhong1993 已提交
435

436 437 438 439 440 441 442 443 444 445 446 447 448 449
    def ReLU(self, node):
        """

        :param node:
        :return:
        """
        assert len(
            node.inputs) == 1, 'The count of ReLU node\'s input is not 1.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)

        params = node.layer.relu_param
        if params.HasField('negative_slope') and params.negative_slope != 0:
            negative_slope = float(params.negative_slope)

450
            attr = {'alpha': negative_slope}
451 452 453
            node.fluid_code.add_layer(
                'leaky_relu', inputs=input, output=node, param_attr=attr)
        else:
454
            node.fluid_code.add_layer('relu', inputs=input, output=node)
455

S
SunAhong1993 已提交
456 457 458 459 460 461 462 463 464 465 466 467 468
    def PReLU(self, node):
        assert len(
            node.inputs) == 1, 'The count of PReLU node\'s input is not 1.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        params = node.layer.prelu_param
        mode_bool = params.channel_shared
        if mode_bool:
            mode = 'all'
        else:
            mode = 'channel'
        data = node.data
        assert data is not None, 'The parameter of {} (type is {}) is not set. You need to use python package of caffe to set the default value.'.format(
            node.layer_name, node.layer_type)
C
channingss 已提交
469 470 471 472 473 474
        import paddle
        pd_version = paddle.__version__
        if pd_version.startswith("1.8.4") or pd_version.startswith("1.8.3"):
            self.weights[node.layer_name + '_weights'] = data[0].reshape(1, -1)
        else:
            self.weights[node.layer_name + '_weights'] = data[0]
S
SunAhong1993 已提交
475
        attr = {
S
SunAhong1993 已提交
476
            'mode': string(mode),
S
SunAhong1993 已提交
477 478
            'param_attr': string(node.layer_name + '_weights'),
            'name': string(node.layer_name)
S
SunAhong1993 已提交
479
        }
J
jiangjiajun 已提交
480 481
        node.fluid_code.add_layer(
            "prelu", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
482 483 484 485 486 487 488 489 490 491

    def Accuracy(self, node):
        assert len(
            node.inputs) == 2, 'The count of Accuracy node\'s input is not 2.'
        inputs = []
        inputs[0] = None
        inputs[1] = None
        i = 0
        for shape in node.input_shape:
            if shape[1] == 1:
S
SunAhong1993 已提交
492 493
                input = self.graph.get_bottom_node(node, idx=i, copy=True)
                inputs[1] = input
S
SunAhong1993 已提交
494
            else:
S
SunAhong1993 已提交
495 496
                input = self.graph.get_bottom_node(node, idx=i, copy=True)
                inputs[0] = input
S
SunAhong1993 已提交
497 498 499 500 501 502 503 504
            i += 1
        params = node.layer.accuracy_param
        top_k = params.top_k
        axis = params.axis
        ignore_label = params.ignore_label
        assert axis == 1, 'PaddlePaddle can not support the situation when the axis is not 1.'
        assert not ignore_label >= 0, 'PaddlePaddle can not support the situation when the model has ignore label.'
        attr = {'k': top_k}
J
jiangjiajun 已提交
505 506
        node.fluid_code.add_layer(
            "accuracy", inputs=inputs, output=node, param_attr=attr)
S
SunAhong1993 已提交
507 508 509 510 511 512 513

    def Eltwise(self, node):
        assert len(
            node.inputs) == 2, 'The count of TanH node\'s input is not 2.'
        params = node.layer.eltwise_param
        mode = params.operation
        inputs = []
S
SunAhong1993 已提交
514 515 516 517
        input0 = self.graph.get_bottom_node(node, idx=0, copy=True)
        inputs.append(input0)
        input1 = self.graph.get_bottom_node(node, idx=1, copy=True)
        inputs.append(input1)
S
SunAhong1993 已提交
518
        if mode == 0:
S
SunAhong1993 已提交
519 520 521
            inputs_dict = {}
            inputs_dict['x'] = inputs[0]
            inputs_dict['y'] = inputs[1]
S
SunAhong1993 已提交
522
            attr = {'act': None, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
523 524 525 526 527
            node.fluid_code.add_layer(
                "elementwise_mul",
                inputs=inputs_dict,
                output=node,
                param_attr=attr)
S
SunAhong1993 已提交
528 529 530 531 532 533 534 535 536
        elif mode == 1:
            if hasattr(params, 'coeff') and len(params.coeff) == 2:
                coeff = params.coeff
                input1_name = self.get_input_name(inputs[0])
                attr = {
                    'shape': [1],
                    'value': coeff[0],
                    'dtype': '{}.dtype'.format(input1_name)
                }
J
jiangjiajun 已提交
537 538 539 540 541
                node.fluid_code.add_layer(
                    "fill_constant",
                    inputs=None,
                    output=node.layer_name + '_const1',
                    param_attr=attr)
S
SunAhong1993 已提交
542
                attr = {'act': None, 'name': string(node.layer_name + '_mul1')}
J
jiangjiajun 已提交
543 544 545 546 547
                node.fluid_code.add_layer(
                    "elementwise_mul",
                    inputs=input1_name + ', ' + node.layer_name + '_const1',
                    output=node.layer_name + '_mul1',
                    param_attr=attr)
S
SunAhong1993 已提交
548 549 550 551 552 553
                input2_name = self.get_input_name(inputs[1])
                attr = {
                    'shape': [1],
                    'value': coeff[1],
                    'dtype': '{}.dtype'.format(input2_name)
                }
J
jiangjiajun 已提交
554 555 556 557 558
                node.fluid_code.add_layer(
                    "fill_constant",
                    inputs=None,
                    output=node.layer_name + '_const2',
                    param_attr=attr)
S
SunAhong1993 已提交
559
                attr = {'act': None, 'name': string(node.layer_name + '_mul2')}
J
jiangjiajun 已提交
560 561 562 563 564
                node.fluid_code.add_layer(
                    "elementwise_mul",
                    inputs=input2_name + ', ' + node.layer_name + '_const2',
                    output=node.layer_name + '_mul2',
                    param_attr=attr)
S
SunAhong1993 已提交
565 566

                attr = {'act': None, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
567 568 569 570 571 572
                node.fluid_code.add_layer(
                    "elementwise_add",
                    inputs='{}_mul1, {}_mul2'.format(node.layer_name,
                                                     node.layer_name),
                    output=node,
                    param_attr=attr)
S
SunAhong1993 已提交
573
            else:
S
SunAhong1993 已提交
574 575 576
                inputs_dict = {}
                inputs_dict['x'] = inputs[0]
                inputs_dict['y'] = inputs[1]
S
SunAhong1993 已提交
577
                attr = {'act': None, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
578 579 580 581 582
                node.fluid_code.add_layer(
                    "elementwise_add",
                    inputs=inputs_dict,
                    output=node,
                    param_attr=attr)
S
SunAhong1993 已提交
583
        else:
S
SunAhong1993 已提交
584 585 586
            inputs_dict = {}
            inputs_dict['x'] = inputs[0]
            inputs_dict['y'] = inputs[1]
S
SunAhong1993 已提交
587
            attr = {'act': None, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
588 589 590 591 592
            node.fluid_code.add_layer(
                "elementwise_max",
                inputs=inputs_dict,
                output=node,
                param_attr=attr)
S
SunAhong1993 已提交
593 594

    def BatchNorm(self, node):
C
channingss 已提交
595 596
        assert len(
            node.inputs) == 1, 'The count of BatchNorm node\'s input is not 1.'
S
SunAhong1993 已提交
597 598
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        params = node.layer.batch_norm_param
S
SunAhong1993 已提交
599
        if hasattr(params, 'eps'):
S
SunAhong1993 已提交
600 601 602
            eps = params.eps
        else:
            eps = 1e-5
S
SunAhong1993 已提交
603
        if node.data is None or len(node.data) != 3:
C
channingss 已提交
604 605 606
            print(
                'The parameter of {} (type is {}) is not set. So we set the parameters as 0'
                .format(node.layer_name, node.layer_type))
S
SunAhong1993 已提交
607
            input_c = node.input_shape[0][1]
J
jiangjiajun 已提交
608 609
            mean = np.zeros([input_c, ]).astype('float32')
            variance = np.zeros([input_c, ]).astype('float32')
S
SunAhong1993 已提交
610 611
            scale = 0
        else:
S
SunAhong1993 已提交
612

S
SunAhong1993 已提交
613
            node.data = [np.squeeze(i).astype('float32') for i in node.data]
S
SunAhong1993 已提交
614
            mean, variance, scale = node.data
S
SunAhong1993 已提交
615 616 617 618 619 620
        # Prescale the stats
        scaling_factor = 1.0 / scale if scale != 0 else 0
        mean *= scaling_factor
        variance *= scaling_factor
        self.weights[node.layer_name + '_mean'] = mean
        self.weights[node.layer_name + '_variance'] = variance
621 622 623 624 625 626 627 628 629
        attr = {
            'is_test': True,
            'param_attr': None,
            'bias_attr': None,
            'moving_mean_name': string(node.layer_name + '_mean'),
            'moving_variance_name': string(node.layer_name + '_variance'),
            'epsilon': eps,
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
630 631
        node.fluid_code.add_layer(
            "batch_norm", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
632 633

    def Scale(self, node):
S
SunAhong1993 已提交
634
        if node.data is None:
C
channingss 已提交
635 636 637
            print(
                'The parameter of {} (type is {}) is not set. So we set the parameters as 0'
                .format(node.layer_name, node.layer_type))
S
SunAhong1993 已提交
638
            input_c = node.input_shape[0][1]
C
channingss 已提交
639 640 641 642 643 644
            self.weights[node.layer_name + '_scale'] = np.zeros([
                input_c,
            ]).astype('float32')
            self.weights[node.layer_name + '_offset'] = np.zeros([
                input_c,
            ]).astype('float32')
S
SunAhong1993 已提交
645
        else:
J
jiangjiajun 已提交
646 647 648 649
            self.weights[node.layer_name + '_scale'] = np.squeeze(node.data[
                0]).astype('float32')
            self.weights[node.layer_name + '_offset'] = np.squeeze(node.data[
                1]).astype('float32')
650 651 652 653 654 655 656 657 658 659 660 661 662 663
        params = node.layer.scale_param
        axis = params.axis
        num_axes = params.num_axes
        inputs = []
        if len(node.inputs) == 2:
            # for two tensor, here resets axis to 1. Maybe there is a bug for unkown case.
            axis = 1
            bias_shape = node.input_shape[0][axis:axis + num_axes]
            input0 = self.graph.get_bottom_node(node, idx=0, copy=True)
            input1 = self.graph.get_bottom_node(node, idx=1, copy=True)
            inputs_dict = {}
            inputs_dict['x'] = input0
            inputs_dict['y'] = input1
            attr = {'axis': axis, 'name': string(node.layer_name + '_mul')}
J
jiangjiajun 已提交
664 665 666 667 668
            node.fluid_code.add_layer(
                "elementwise_mul",
                inputs=inputs_dict,
                output=node.layer_name + '_mul',
                param_attr=attr)
S
SunAhong1993 已提交
669
        else:
670 671
            bias_shape = node.input_shape[0][axis:axis + num_axes]
            input0 = self.graph.get_bottom_node(node, idx=0, copy=True)
S
SunAhong1993 已提交
672 673
            input0_name = self.get_input_name(input0)
            attr = {
674 675 676 677
                'dtype': '{}.dtype'.format(input0_name),
                'shape': bias_shape,
                'name': string(node.layer_name + '_cparam1'),
                'attr': string(node.layer_name + '_scale'),
S
SunAhong1993 已提交
678 679 680
                'is_bias': True,
                'default_initializer': 'Constant(value=1.0)'
            }
J
jiangjiajun 已提交
681 682
            node.fluid_code.add_layer(
                "create_parameter", inputs=None, output=node, param_attr=attr)
683 684 685 686
            inputs_dict = {}
            inputs_dict['x'] = input0
            inputs_dict['y'] = node
            attr = {'axis': axis, 'name': string(node.layer_name + '_mul')}
J
jiangjiajun 已提交
687 688 689 690 691
            node.fluid_code.add_layer(
                "elementwise_mul",
                inputs=inputs_dict,
                output=node.layer_name + '_mul',
                param_attr=attr)
692 693 694 695 696 697 698 699 700 701
        scale_shape = bias_shape
        input0_name = self.get_input_name(input0)
        attr = {
            'dtype': '{}.dtype'.format(input0_name),
            'shape': scale_shape,
            'name': string(node.layer_name + '_cparam2'),
            'attr': string(node.layer_name + '_offset'),
            'is_bias': True,
            'default_initializer': 'Constant(value=1.0)'
        }
J
jiangjiajun 已提交
702 703 704 705 706
        node.fluid_code.add_layer(
            "create_parameter",
            inputs=None,
            output=node.layer_name + '_offset_param',
            param_attr=attr)
707
        attr = {'axis': axis, 'name': string(node.layer_name + '_add')}
J
jiangjiajun 已提交
708 709 710 711 712 713
        node.fluid_code.add_layer(
            "elementwise_add",
            inputs='{}_mul, {}_offset_param'.format(node.layer_name,
                                                    node.layer_name),
            output=node,
            param_attr=attr)
S
SunAhong1993 已提交
714 715 716 717

    def Reshape(self, node):
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        top_count = len(input.layer.top)
718
        is_inplace = False if top_count == 1 else True
S
SunAhong1993 已提交
719 720 721 722
        output_shape = node.output_shape[0]
        attr = {
            'shape': output_shape,
            'inplace': is_inplace,
723
            'act': None,
S
SunAhong1993 已提交
724 725
            'name': string(node.layer_name)
        }
J
jiangjiajun 已提交
726 727
        node.fluid_code.add_layer(
            "reshape", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743

    def ArgMax(self, node):
        assert len(node.inputs) == 1 and len(
            node.outputs
        ) == 1, 'The count of ArgMax node\'s input and output is not 1.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        input_shape = node.input_shape[0]
        params = node.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(input_shape)
        if out_max_val is True:
            attr = {'k': top_k, 'name': string(node.layer_name + '_topk')}
J
jiangjiajun 已提交
744 745 746
            node.fluid_code.add_layer(
                "topk",
                inputs=input,
J
jiangjiajun 已提交
747 748
                output='{}_topk_var, {}_index_var'.format(node.layer_name,
                                                          node.layer_name),
J
jiangjiajun 已提交
749
                param_attr=attr)
S
SunAhong1993 已提交
750 751 752 753 754 755 756
            attr = {'dtype': '{}_topk_var.dtype'.format(node.layer_name)}
            node.fluid_code.add_layer(
                "cast",
                inputs='{}_index_var'.format(node.layer_name),
                output='{}_index_var'.format(node.layer_name),
                param_attr=attr)
            attr = {'axis': axis, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
757 758
            node.fluid_code.add_layer(
                "concat",
J
jiangjiajun 已提交
759 760
                inputs='{}_topk_var, {}_index_var'.format(node.layer_name,
                                                          node.layer_name),
J
jiangjiajun 已提交
761 762
                output=node,
                param_attr=attr)
S
SunAhong1993 已提交
763 764
        else:
            attr = {'k': top_k, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
765 766 767 768 769
            node.fluid_code.add_layer(
                "topk",
                inputs=input,
                output='_, {}'.format(node.layer_name),
                param_attr=attr)
S
SunAhong1993 已提交
770 771 772 773 774 775 776

    def Crop(self, node):
        assert len(
            node.inputs) == 2, 'The count of Crop node\'s input is not 2.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        example = self.graph.get_bottom_node(node, idx=1, copy=True)
        params = node.layer.crop_param
S
sonixixi 已提交
777
        axis = params.axis
S
SunAhong1993 已提交
778 779 780 781
        input_shape = node.input_shape[0]
        if axis < 0:
            axis += len(input_shape)
        offset_real = [0] * len(input_shape)
S
sonixixi 已提交
782
        if hasattr(params, "offset") and len(params.offset) > 0:
S
SunAhong1993 已提交
783
            offset = list(params.offset)
J
jiangjiajun 已提交
784 785 786
            assert (len(input_shape) - axis
                    ) == len(offset), "invalid offset[%s] in crop layer" % (
                        str(offset))
S
SunAhong1993 已提交
787
            offset_real = [0] * axis + offset
S
sonixixi 已提交
788
        attr = {'offsets': list(offset_real), 'name': string(node.layer_name)}
J
jiangjiajun 已提交
789 790
        node.fluid_code.add_layer(
            "crop",
J
jiangjiajun 已提交
791 792
            inputs={'x': input,
                    'shape': node.input_shape[1]},
J
jiangjiajun 已提交
793 794
            output=node,
            param_attr=attr)
S
SunAhong1993 已提交
795

S
SunAhong1993 已提交
796
    def Flatten(self, node):
S
SunAhong1993 已提交
797
        assert len(
J
jiangjiajun 已提交
798 799
            node.
            inputs) == 1, 'The count of DetectionOutput node\'s input is not 1.'
S
SunAhong1993 已提交
800 801 802
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        shape = node.output_shape[0]
        attr = {'shape': shape, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
803 804
        node.fluid_code.add_layer(
            "reshape", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
805 806 807 808 809 810 811 812 813 814 815 816 817 818 819

    def Power(self, node):
        assert len(
            node.inputs) == 1, 'The count of Permute node\'s input is not 1.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        params = node.layer.power_param
        power = params.power
        scale = params.scale
        shift = params.shift
        attr = {
            'scale': scale,
            'bias': shift,
            'bias_after_scale': True,
            'name': string(node.layer_name + '_scale')
        }
J
jiangjiajun 已提交
820 821
        node.fluid_code.add_layer(
            "scale", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
822
        attr = {'factor': power, 'name': string(node.layer_name)}
J
jiangjiajun 已提交
823 824
        node.fluid_code.add_layer(
            "pow", inputs=node, output=node, param_attr=attr)
S
SunAhong1993 已提交
825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845

    def Reduction(self, node):
        assert len(
            node.inputs) == 1, 'The count of Reduction node\'s input is not 1.'
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        params = node.layer.reduction_param
        operation = params.operation
        axis = params.axis
        coeff = params.coeff
        assert operation >= 1 and operation <= 4, "reduction reduction [%s] error" % (
            operation)
        input_len = len(node.input_shape[0])
        if axis < 0:
            axis += input_len + 1
        dim = list(range(input_len))
        if operation == 1:  ## operation = SUM
            attr = {
                'dim': dim[axis:],
                'keep_dim': False,
                'name': string(node.layer_name)
            }
J
jiangjiajun 已提交
846 847
            node.fluid_code.add_layer(
                "reduce_sum", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
848 849
        elif operation == 2:  ## operation = ASUM
            attr = {'name': string(node.layer_name + '_abs')}
J
jiangjiajun 已提交
850 851
            node.fluid_code.add_layer(
                "abs", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
852 853 854 855 856
            attr = {
                'dim': dim[axis:],
                'keep_dim': False,
                'name': string(node.layer_name)
            }
J
jiangjiajun 已提交
857 858
            node.fluid_code.add_layer(
                "reduce_sum", inputs=node, output=node, param_attr=attr)
S
SunAhong1993 已提交
859 860
        elif operation == 3:  ## operation = SUMSQ
            attr = {'factor': 2.0, 'name': string(node.layer_name + '_pow')}
J
jiangjiajun 已提交
861 862
            node.fluid_code.add_layer(
                "pow", inputs=input, output=node, param_attr=attr)
S
SunAhong1993 已提交
863 864 865 866 867
            attr = {
                'dim': dim[axis:],
                'keep_dim': False,
                'name': string(node.layer_name)
            }
J
jiangjiajun 已提交
868 869
            node.fluid_code.add_layer(
                "reduce_sum", inputs=node, output=node, param_attr=attr)
S
SunAhong1993 已提交
870 871 872 873 874 875
        else:  ## operation = MEAN
            attr = {
                'dim': dim[axis:],
                'keep_dim': False,
                'name': string(node.layer_name)
            }
J
jiangjiajun 已提交
876 877
            node.fluid_code.add_layer(
                "reduce_mean", inputs=node, output=node, param_attr=attr)
S
SunAhong1993 已提交
878
        attr = {'scale': coeff}
J
jiangjiajun 已提交
879 880
        node.fluid_code.add_layer(
            "scale", inputs=node, output=node, param_attr=attr)
S
SunAhong1993 已提交
881

S
SunAhong1993 已提交
882 883 884 885 886 887 888 889
    def deal_custom_layer(self, node):
        op = node.layer_type
        custom_code, func = make_custom_layer(node)
        params = get_params(node.layer, node.layer_type)
        arg_names, kwargs = set_args(func, params)
        kwargs['name'] = string(node.layer_name)
        kwargs['input_shape'] = node.input_shape
        data = node.data
S
SunAhong1993 已提交
890 891 892 893 894
        if data is not None:
            data = self.adjust_parameters(node)
            weights_name = deal_weights(node)
            for i in range(len(data)):
                self.weights[weights_name[i]] = data[i]
S
SunAhong1993 已提交
895 896 897
        inputs_node = []
        for i in range(len(node.inputs)):
            input = self.graph.get_bottom_node(node, idx=i, copy=True)
S
SunAhong1993 已提交
898 899
            if i == 1 and op == 'DetectionOutput':
                input = self.graph.get_bottom_node(node, idx=i, copy=True)
S
SunAhong1993 已提交
900 901
                while input is not None \
                      and input.layer_type != 'Softmax' \
S
SunAhong1993 已提交
902
                      and input.layer_type != 'Sigmoid':
S
SunAhong1993 已提交
903 904 905
                    input = self.graph.get_bottom_node(input, idx=0, copy=True)
                assert input is not None, 'This kind of DetectionOutput is not supported!'
                input = self.graph.get_bottom_node(input, idx=0, copy=True)
S
SunAhong1993 已提交
906
            inputs_node.append(input)
J
jiangjiajun 已提交
907 908 909 910 911 912
        node.fluid_code.add_layer(
            func.__code__.co_name,
            inputs=inputs_node,
            output=node,
            param_attr=kwargs,
            is_custom_layer=True)
J
jiangjiajun 已提交
913 914
        if op not in self.used_custom_layers:
            self.used_custom_layers[op] = custom_code
S
SunAhong1993 已提交
915 916 917 918 919 920

    def directly_map(self, node):
        assert node.layer_type in self.directly_map_ops
        op_info = self.directly_map_ops[node.layer_type]
        input = self.graph.get_bottom_node(node, idx=0, copy=True)
        attr = {'name': string(node.layer_name)}
J
jiangjiajun 已提交
921 922
        node.fluid_code.add_layer(
            op_info, inputs=input, output=node, param_attr=attr)