Publishing R3
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / pad_ext.py
1 """
2  Copyright (c) 2018 Intel Corporation
3
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7
8       http://www.apache.org/licenses/LICENSE-2.0
9
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 """
16
17 import logging as log
18
19 from mo.ops.pad import Pad
20 from mo.front.extractor import FrontExtractorOp
21 from mo.front.onnx.extractors.utils import onnx_attr
22 from mo.utils.error import Error
23
24 import numpy as np
25
26
27 class PadFrontExtractor(FrontExtractorOp):
28     op = 'Pad'
29     enabled = True
30
31     @staticmethod
32     def extract(node):
33         mode = onnx_attr(node, 'mode', 's', default='constant', dst_type=lambda x: x.decode())
34         pads = onnx_attr(node, 'pads', 'ints', dst_type=lambda x: np.array(x, dtype=np.int64))
35         value = onnx_attr(node, 'value', 'f', default=0)
36
37         assert pads is not None
38
39         if mode.lower() != 'constant':
40             log.error('Pad.mode != constant for node {}. It is not supported. '
41                 'Model conversion is not aborted but the final IR will be not correct.'.format(node.name))
42
43         if value != 0:
44             log.error('Pad.value == {} != 0 for node {}. It is not supported. '
45                 'MOdel conversion is not aborted but the final IR will be not correct.'.format(value, node.name))
46
47         # MO Pad op and ONNX Pad op have different format for pads values
48         # MO Pad has Dx2 where D is the total number of dimensions
49         # ONNX Pad pads flat layout, so
50         # need to reshape and transpose
51
52         pads = pads.reshape([2,-1])
53         pads = np.transpose(pads)
54
55         Pad.update_node_stat(node, {'mode': mode, 'pads': pads, 'fill_value': value})
56         return __class__.enabled