Publishing R3
[platform/upstream/dldt.git] / model-optimizer / extensions / front / mxnet / ssd_pattern_flatten_softmax_activation.py
1 """
2  Copyright (c) 2017-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 networkx as nx
18 from mo.graph.graph import create_edge
19 from mo.front.common.replacement import FrontReplacementSubgraph
20 from extensions.front.mxnet.ssd_pattern_remove_flatten import SsdPatternRemoveFlatten
21 from extensions.front.mxnet.ssd_pattern_remove_reshape import SsdPatternRemoveReshape
22
23 from mo.ops.reshape import Reshape
24
25
26 class SsdPatternFlattenSoftmaxActivation(FrontReplacementSubgraph):
27
28     enabled = True
29
30     def run_before(self):
31         return [SsdPatternRemoveFlatten, SsdPatternRemoveReshape]
32
33     def pattern(self):
34         return dict(
35             nodes=[
36                 ('softmax_activation', dict(op='SoftmaxActivation')),
37                 ('multi_box_detection', dict(op='_contrib_MultiBoxDetection'))
38             ],
39             edges=[
40                 ('softmax_activation', 'multi_box_detection', {'in': 1})
41             ],
42             node_attrs=['op'],
43             edge_attrs=['in'])
44
45     def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
46         """
47         Need to find the pattern: SoftmaxActivation -> DetectionOutput
48         DetectionOutput in IE expects flattened input from SoftMax, that is why there is the need to add
49         Flatten layer
50
51         Parameters
52         ----------
53         graph : nx.MultiDiGraph
54            Graph with loaded model.
55          match : dict
56            Patterns which were found in graph structure.
57         """
58         softmax_activation = match['softmax_activation']
59         multi_box_detection = match['multi_box_detection']
60         edge_data = graph.get_edge_data(softmax_activation.id, multi_box_detection.id)
61         out_port = edge_data[0]['out']
62         in_port = edge_data[0]['in']
63         graph.remove_edge(softmax_activation.id, multi_box_detection.id)
64         symbol_node = dict(
65             op='Flatten',
66             name=multi_box_detection.name + '/Reshape_',
67             dim=[0,-1]
68         )
69         new_reshape_op = Reshape(graph, {'symbol_dict': symbol_node} )
70         new_reshape_node = new_reshape_op.create_node([softmax_activation])
71         create_edge(new_reshape_node, multi_box_detection, in_port=in_port, out_port=out_port)