Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / back / ShufflenetReLUReorder.py
1 """
2  Copyright (c) 2018-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 numpy as np
18
19 from mo.back.replacement import BackReplacementPattern
20 from mo.graph.graph import Graph
21
22
23 class ShufflenetReLUReorder(BackReplacementPattern):
24     """
25     This pass is workaround for GPU plugin
26     """
27     enabled = False
28
29     def pattern(self):
30         return dict(
31             nodes=[
32                 ('relu', dict(kind='op', type='ReLU')),
33                 ('relu_data', dict(kind='data')),
34                 ('reshape1', dict(kind='op', type='Reshape')),
35                 ('reshape1_data', dict(kind='data')),
36                 ('transpose', dict(kind='op', type='Permute')),
37                 ('transpose_data', dict(kind='data')),
38                 ('reshape2', dict(kind='op', type='Reshape')),
39                 ('reshape2_data', dict(kind='data')),
40                 ('conv', dict(kind='op', type='Convolution'))
41             ],
42             edges=[('relu', 'relu_data'),
43                    ('relu_data', 'reshape1'),
44                    ('reshape1', 'reshape1_data'),
45                    ('reshape1_data', 'transpose'),
46                    ('transpose', 'transpose_data'),
47                    ('transpose_data', 'reshape2'),
48                    ('reshape2', 'reshape2_data'),
49                    ('reshape2_data', 'conv'),
50                    ]
51         )
52
53     def replace_pattern(self, graph: Graph, match: dict):
54         relu = match['relu']
55         reshape1 = match['reshape1']
56         reshape2_data = match['reshape2_data']
57         conv = match['conv']
58
59         if np.max(conv.pad) == 0:
60             return
61
62         relu_input = relu.in_node()
63
64         # Disconnect InputData-x->ReLU->Data-x->Reshape1
65         edge_attrs = graph.get_edge_data(relu.out_node().id, reshape1.id)[0]
66         graph.remove_edge(relu_input.id, relu.id)
67         graph.remove_edge(relu.out_node().id, reshape1.id)
68
69         # Connect InputData-->Reshape1
70         graph.add_edges_from([(relu_input.id, reshape1.id, edge_attrs)])
71
72         # Insert ReLU:  Reshape2Data->ReLU->Data->Convolution
73         edge_attrs = graph.get_edge_data(reshape2_data.id, conv.id)[0]
74         graph.remove_edge(reshape2_data.id, conv.id)
75         graph.add_edges_from([(reshape2_data.id, relu.id, {'in': 0}), (relu.out_node().id, conv.id, edge_attrs)])