Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / mo / front / onnx / extractor.py
1 """
2  Copyright (c) 2018-2019 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 numpy as np
18
19 from mo.front.onnx.extractors.concat import concat_ext
20 from mo.front.onnx.extractors.const import onnx_const_ext
21 from mo.front.onnx.extractors.constant import onnx_constant_ext
22 from mo.front.onnx.extractors.eltwise import make_tf_eltwise
23 from mo.front.onnx.extractors.fused_bn import tf_fused_bn_extractor
24 from mo.front.onnx.extractors.matmul import onnx_gemm_ext
25 from mo.front.onnx.extractors.placeholder import onnx_placeholder_ext
26 from mo.front.onnx.extractors.reshape import onnx_reshape_ext
27 from mo.graph.graph import Node
28
29
30 def node_pb_arg(pb_extractor: callable):
31     return lambda node: pb_extractor(node.pb)
32
33
34 onnx_op_extractors = {
35     'BatchNormalization': tf_fused_bn_extractor,
36     'Gemm': onnx_gemm_ext,
37     'Placeholder': onnx_placeholder_ext,
38     'Concat': concat_ext,
39     'Const': onnx_const_ext,
40     'Constant': onnx_constant_ext,
41     'Identity': node_pb_arg(make_tf_eltwise(lambda v: v, attrs={'identity': True})),
42     'Sum': node_pb_arg(
43         make_tf_eltwise(lambda a, b: a + b, attrs={'type': 'Eltwise', 'operation': 'sum', 'can_be_bias': True})),
44     'Relu': node_pb_arg(make_tf_eltwise(lambda v: np.maximum(0, v), attrs={'type': 'ReLU'})),  # 0 is an integer
45     'Reshape': onnx_reshape_ext,
46 }
47
48
49 def common_onnx_fields(node: Node):
50     return {
51         'kind': 'op',
52         'name': node.id,
53     # no reliable name for an onnx node, name can be empty, so we use that surrogate built as ID in the loaader
54         'op': node.op if node.has_valid('op') else node.pb.op_type,
55         'precision': 'FP32'  # TODO use real precision derived from the model
56     }
57
58
59 def onnx_op_extractor(node: Node, lowered_keys_map: dict):
60     if not node.has_valid('pb'):
61         return True, node.graph.node[node.id]
62
63     result = common_onnx_fields(node)
64     node.graph.node[node.id].update(result)
65     supported = False
66     op = result['op'].lower()
67     if op in lowered_keys_map:
68         op = lowered_keys_map[op]
69         assert op in onnx_op_extractors
70         attrs = onnx_op_extractors[op](node)
71         if attrs:
72             result.update(attrs)
73             supported = True
74     return supported, result