Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / DepthToSpace.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 from mo.front.common.partial_infer.utils import int64_array
17 from mo.graph.graph import Graph
18 from mo.middle.replacement import MiddleReplacementPattern
19 from mo.ops.permute import Permute
20 from mo.ops.reshape import Reshape
21
22
23 class DepthToSpace(MiddleReplacementPattern):
24     """
25     Replaces DepthToSpace with 6D_Reshape->Permute->4D_Reshape sequence
26     """
27
28     enabled = True
29
30     def run_after(self):
31         from extensions.middle.pass_separator import MiddleStart
32         return [MiddleStart]
33
34     def run_before(self):
35         from extensions.middle.pass_separator import MiddleFinish
36         return [MiddleFinish]
37
38     def pattern(self):
39         return dict(
40             nodes=[
41                 ('in_data', dict(kind='data')),
42                 ('op', dict(op='DepthToSpace', data_format='NHWC')),
43                 ('out_data', dict(kind='data'))
44             ],
45             edges=[
46                 ('in_data', 'op'),
47                 ('op', 'out_data')
48             ])
49
50     def replace_pattern(self, graph: Graph, match: dict):
51         node = match['op']
52
53         N, H, W, C = match['in_data'].shape
54         block_size = node['block_size']
55
56         graph.remove_edge(match['in_data'].id, match['op'].id)
57         graph.remove_edge(match['op'].id, match['out_data'].id)
58
59         dim_6D = int64_array([N, block_size, block_size, int(C / (block_size ** 2)), H, W])
60         order_6D = int64_array([0, 3, 4, 1, 5, 2])
61         dim_4D = int64_array([N, int(H * block_size), int(W * block_size), int(C / (block_size ** 2))])
62
63         reshape_data_node = Reshape(graph=graph, attrs={'name': match['op'].id + '/Reshape_to_6D',
64                                                         'dim': dim_6D}).create_node_with_data([match['in_data']])
65         permute_data_node = Permute(graph=graph, attrs={'name': match['op'].id + '/Permute',
66                                                         'order': order_6D}).create_node_with_data([reshape_data_node])
67         reshape_node = Reshape(graph=graph, attrs={'name': match['op'].id + '/Reshape_to_4D',
68                                                    'dim': dim_4D}).create_node_with_data([permute_data_node],
69                                                                                          data_nodes=[match['out_data']])
70
71         reshape_data_node.in_node()['nchw_layout'] = True
72         reshape_data_node['nchw_layout'] = True
73         permute_data_node.in_node()['nchw_layout'] = True
74         permute_data_node['nchw_layout'] = True