Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / onnx / argmax.py
1 """
2  Copyright (c) 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 networkx as nx
18
19 from extensions.ops.argmax import ArgMaxOp
20 from mo.front.common.replacement import FrontReplacementSubgraph
21 from mo.front.onnx.extractors.utils import onnx_attr
22 from mo.graph.graph import Graph
23 from mo.ops.squeeze import Squeeze
24
25 class Argmax(FrontReplacementSubgraph):
26     enabled = True
27
28     def pattern(self):
29         return dict(
30             nodes=[('argmax', dict(op='ArgMax', keepdims=0))],
31             edges=[]
32         )
33
34     def replace_sub_graph(self, graph: Graph, match: dict):
35         """
36         In ONNX ArgMax operation has keepdims attribute that indicates
37         whether to stay a dimension along which maximum is computed or not.
38         In case of keepdims=0 this dimension should be removed but ArgMax operation in IR format
39         is not designed to cover this case. So we should additionally add Squeeze operation 
40         right after ArgMax for this case.
41         """
42         argmax_node = match['argmax']
43         axis = argmax_node.axis
44         squeeze_node = Squeeze(graph, {'squeeze_dims': [axis]}).create_node()
45         argmax_node.out_port(0).get_connection().set_source(squeeze_node.out_port(0))
46         squeeze_node.in_port(0).connect(argmax_node.out_port(0))