Publishing R3
[platform/upstream/dldt.git] / model-optimizer / extensions / front / mxnet / ssd_pattern_remove_transpose.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 extensions.front.mxnet.ssd_pattern_remove_flatten import SsdPatternRemoveFlatten
19 from extensions.front.mxnet.ssd_pattern_remove_reshape import SsdPatternRemoveReshape
20 from extensions.front.mxnet.ssd_pattern_flatten_softmax_activation import SsdPatternFlattenSoftmaxActivation
21 from mo.graph.graph import create_edge
22 from mo.front.common.replacement import FrontReplacementSubgraph
23
24
25 class SsdPatternRemoveTranspose(FrontReplacementSubgraph):
26
27     enabled = True
28
29     def run_before(self):
30         return [SsdPatternFlattenSoftmaxActivation, SsdPatternRemoveFlatten, SsdPatternRemoveReshape]
31
32     def pattern(self):
33         return dict(
34             nodes=[
35                 ('transpose', dict(op='transpose')),
36                 ('softmax_activation', dict(op='SoftmaxActivation')),
37                 ('multi_box_detection', dict(op='_contrib_MultiBoxDetection'))
38             ],
39             edges=[
40                 ('transpose', 'softmax_activation', {'in': 0}),
41                 ('softmax_activation', 'multi_box_detection', {'in': 1}),
42             ],
43             node_attrs=['op'],
44             edge_attrs=['in'])
45
46     def replace_sub_graph(self, graph: nx.MultiDiGraph, match: dict):
47         """
48         Need to find each occurrence of pattern:
49         transpose -> SoftmaxActivation -> _contrib_MultiBoxDetection
50         remove transpose layer to secure the order of weights in SoftMax to be the same as IE expects
51         IE expects weights to be in following order: class-wise values for each priorbox.
52         priorboxes change the quickest
53
54         Parameters
55         ----------
56         graph : nx.MultiDiGraph
57            Graph with loaded model.
58          match : dict
59            Patterns which were found in graph structure.
60         """
61         transpose_node = match['transpose']
62         softmax_activation = match['softmax_activation']
63         transpose_in_node = transpose_node.in_node(0)
64
65         graph.remove_edge(transpose_in_node.id, transpose_node.id)
66         graph.remove_edge(transpose_node.id, softmax_activation.id)
67         graph.remove_node(transpose_node.id)
68         create_edge(transpose_in_node, softmax_activation)