Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / mxnet / ssd_reorder_detection_out_inputs.py
1 """
2  Copyright (c) 2017-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 mo.graph.graph import Graph
20 from mo.front.common.replacement import FrontReplacementPattern
21 from extensions.front.mxnet.ssd_pattern_remove_transpose import SsdPatternRemoveTranspose
22 from extensions.front.mxnet.ssd_pattern_flatten_softmax_activation import SsdPatternFlattenSoftmaxActivation
23
24
25 class SsdReorderDetectionOutInputs(FrontReplacementPattern):
26
27     enabled = True
28
29     def run_before(self):
30         return [SsdPatternFlattenSoftmaxActivation, SsdPatternRemoveTranspose]
31
32     @staticmethod
33     def pattern():
34         return dict(
35             nodes=[
36                 ('multi_box_detection', dict(op='_contrib_MultiBoxDetection'))
37             ],
38             edges=[])
39
40     @staticmethod
41     def replace_pattern(graph: Graph, match: dict):
42         """
43         DetectionOutput layer has another order of inputs unlike mxnet.
44         Need to reorder _contrib_MultiBoxDetection inputs
45         for correct conversion to DetectionOutput layer.
46
47         Parameters
48         ----------
49         graph : Graph
50            Graph with loaded model.
51         """
52         multi_box_detection_node = match['multi_box_detection']
53         conf_node = multi_box_detection_node.in_node(0)
54         loc_node = multi_box_detection_node.in_node(1)
55
56         conf_edge_data = graph.get_edge_data(conf_node.id, multi_box_detection_node.id)
57         conf_out_port = conf_edge_data[0]['out']
58         conf_in_port = conf_edge_data[0]['in']
59
60         loc_edge_data = graph.get_edge_data(loc_node.id, multi_box_detection_node.id)
61         loc_out_port = loc_edge_data[0]['out']
62         loc_in_port = loc_edge_data[0]['in']
63
64         graph.remove_edge(conf_node.id, multi_box_detection_node.id)
65         graph.remove_edge(loc_node.id, multi_box_detection_node.id)
66
67         graph.create_edge(loc_node, multi_box_detection_node, in_port=conf_in_port, out_port=conf_out_port)
68         graph.create_edge(conf_node, multi_box_detection_node, in_port=loc_in_port, out_port=loc_out_port)