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