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.
18 from extensions.middle.ShufflenetReshape import FeatureShuffleReshape
19 from mo.graph.graph import Graph
20 from mo.middle.replacement import MiddleReplacementPattern
21 from mo.ops.permute import Permute
22 from mo.ops.reshape import Reshape
23 from mo.utils.error import Error
26 class ShuffleChannel(MiddleReplacementPattern):
28 Replaces Caffe ShuffleChannel with Reshapes and Permute layers
34 return [FeatureShuffleReshape]
37 from extensions.middle.pass_separator import MiddleFinish
43 ('op', dict(op='ShuffleChannel')),
48 def replace_pattern(self, graph: Graph, match: dict):
49 if graph.graph['layout'] != "NCHW":
54 in_node = node.in_node(0)
55 out_node = node.out_node(0)
56 group = int(node['group'])
58 graph.remove_edge(in_node.id, node.id)
59 graph.remove_edge(node.id, out_node.id)
62 cols = in_node.shape[1] // group
64 if rows * cols != in_node.shape[1]:
65 raise Error("Group {} should divide input channels number {} without reminder for node {}"
66 "".format(group, in_node.shape[1], node.id))
68 reshape_split = Reshape(graph, attrs={'name': node.id + '/Reshape_split_',
69 'dim': np.array([in_node.shape[0], rows, cols, -1])})
70 reshape_split_node = reshape_split.create_node_with_data([in_node])
71 transpose = Permute(graph, attrs={'name': node.id + '/Transpose_',
72 'order': np.array([0, 2, 1, 3])})
73 transpose_node = transpose.create_node_with_data([reshape_split_node])
74 reshape_concat = Reshape(graph, attrs={'name': node.id + '/Reshape_concat_',
75 'dim': out_node.shape})
76 reshape_concat.create_node_with_data([transpose_node], data_nodes=[out_node])