2 Copyright (c) 2018-2019 Intel Corporation
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
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
23 class DepthToSpace(MiddleReplacementPattern):
25 Replaces DepthToSpace with 6D_Reshape->Permute->4D_Reshape sequence
31 from extensions.middle.pass_separator import MiddleStart
35 from extensions.middle.pass_separator import MiddleFinish
41 ('in_data', dict(kind='data')),
42 ('op', dict(op='DepthToSpace', data_format='NHWC')),
43 ('out_data', dict(kind='data'))
50 def replace_pattern(self, graph: Graph, match: dict):
53 N, H, W, C = match['in_data'].shape
54 block_size = node['block_size']
56 graph.remove_edge(match['in_data'].id, match['op'].id)
57 graph.remove_edge(match['op'].id, match['out_data'].id)
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))])
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']])
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