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.
21 from extensions.middle.ShufflenetReshape import FeatureShuffleReshape, ReshapeSoftmaxReshape
22 from mo.utils.unittest.graph import build_graph, compare_graphs
25 'placeholder_1': {'shape': None, 'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
26 'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
28 'reshape_1': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape', 'dim': None},
29 'reshape_1_data': {'name': 'reshape_1_data', 'value': None, 'shape': None, 'kind': 'data'},
30 'reshape_2': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
31 'reshape_2_data': {'name': 'reshape_2_data', 'value': None, 'shape': None, 'kind': 'data'},
32 'reshape_3': {'type': 'Reshape', 'kind': 'op', 'op': 'Reshape'},
33 'reshape_3_data': {'name': 'reshape_3_data', 'value': None, 'shape': None, 'kind': 'data'},
35 'transpose_1': {'type': 'Permute', 'kind': 'op', 'op': 'Transpose'},
36 'transpose_1_data': {'value': None, 'shape': None, 'kind': 'data'},
38 'softmax_1': {'type': 'SoftMax', 'kind': 'op', 'op': 'SoftMax'},
39 'softmax_1_data': {'value': None, 'shape': None, 'kind': 'data'},
43 class FeatureShuffleReshapeTests(unittest.TestCase):
45 graph = build_graph(nodes_attributes,
46 [('placeholder_1', 'placeholder_1_data'),
47 ('placeholder_1_data', 'reshape_1'),
48 ('reshape_1', 'reshape_1_data'),
49 ('reshape_1_data', 'transpose_1'),
50 ('transpose_1', 'transpose_1_data'),
51 ('transpose_1_data', 'reshape_2'),
52 ('reshape_2', 'reshape_2_data')
54 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 112])},
55 'reshape_1_data': {'shape': np.array([227, 227, 4, 28])},
56 'transpose_1': {'order': np.array([0, 1, 3, 2])},
57 'transpose_1_data': {'shape': np.array([227, 227, 28, 4])},
58 'reshape_2_data': {'shape': np.array([1, 227, 227, 112])},
60 graph.graph['layout'] = 'NHWC'
62 graph_ref = build_graph(nodes_attributes,
63 [('placeholder_1', 'placeholder_1_data'),
64 ('placeholder_1_data', 'reshape_1'),
65 ('reshape_1', 'reshape_1_data'),
66 ('reshape_1_data', 'transpose_1'),
67 ('transpose_1', 'transpose_1_data'),
68 ('transpose_1_data', 'reshape_2'),
69 ('reshape_2', 'reshape_2_data')
71 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 112])},
72 'reshape_1_data': {'shape': np.array([1, 4, 28, 227 * 227])},
73 'transpose_1': {'order': np.array([0, 2, 1, 3])},
74 'transpose_1_data': {'shape': np.array([1, 28, 4, 227 * 227])},
75 'reshape_2_data': {'shape': np.array([1, 227, 227, 112])},
76 'reshape_3_data': {'shape': np.array([1, 227, 227, 112])},
79 pattern = FeatureShuffleReshape()
80 pattern.find_and_replace_pattern(graph)
82 (flag, resp) = compare_graphs(graph, graph_ref, 'reshape_2_data', check_op_attrs=True)
83 self.assertTrue(flag, resp)
86 graph = build_graph(nodes_attributes,
87 [('placeholder_1', 'placeholder_1_data'),
88 ('placeholder_1_data', 'reshape_1'),
89 ('reshape_1', 'reshape_1_data'),
90 ('reshape_1_data', 'transpose_1'),
91 ('transpose_1', 'transpose_1_data'),
92 ('transpose_1_data', 'reshape_2'),
93 ('reshape_2', 'reshape_2_data')
95 {'placeholder_1_data': {'shape': np.array([1, 112, 227, 227])},
96 'reshape_1_data': {'shape': np.array([1, 4, 28, 227, 227])},
97 'transpose_1': {'order': np.array([0, 2, 1, 3, 4])},
98 'transpose_1_data': {'shape': np.array([1, 28, 4, 227, 227])},
99 'reshape_2_data': {'shape': np.array([1, 112, 227, 227])},
101 graph.graph['layout'] = 'NCHW'
103 graph_ref = build_graph(nodes_attributes,
104 [('placeholder_1', 'placeholder_1_data'),
105 ('placeholder_1_data', 'reshape_1'),
106 ('reshape_1', 'reshape_1_data'),
107 ('reshape_1_data', 'transpose_1'),
108 ('transpose_1', 'transpose_1_data'),
109 ('transpose_1_data', 'reshape_2'),
110 ('reshape_2', 'reshape_2_data')
112 {'placeholder_1_data': {'shape': np.array([1, 112, 227, 227])},
113 'reshape_1_data': {'shape': np.array([1, 4, 28, 227 * 227])},
114 'transpose_1': {'order': np.array([0, 2, 1, 3])},
115 'transpose_1_data': {'shape': np.array([1, 28, 4, 227 * 227])},
116 'reshape_2_data': {'shape': np.array([1, 112, 227, 227])},
119 pattern = FeatureShuffleReshape()
120 pattern.find_and_replace_pattern(graph)
122 (flag, resp) = compare_graphs(graph, graph_ref, 'reshape_2_data', check_op_attrs=True)
123 self.assertTrue(flag, resp)
126 class ReshapeSoftmaxReshapeTests(unittest.TestCase):
128 graph = build_graph(nodes_attributes,
129 [('placeholder_1', 'placeholder_1_data'),
130 ('placeholder_1_data', 'reshape_1'),
131 ('reshape_1', 'reshape_1_data'),
132 ('reshape_1_data', 'softmax_1'),
133 ('softmax_1', 'softmax_1_data'),
134 ('softmax_1_data', 'reshape_2'),
135 ('reshape_2', 'reshape_2_data')
137 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 2])},
138 'reshape_1': {'dim': np.array([1, 227 * 227, 2])},
139 'reshape_1_data': {'shape': np.array([1 * 227 * 227, 2])},
140 'reshape_2_data': {'shape': np.array([1, 227, 227, 2])},
142 graph.graph['layout'] = 'NHWC'
144 graph_ref = build_graph(nodes_attributes,
145 [('placeholder_1', 'placeholder_1_data'),
146 ('placeholder_1_data', 'reshape_1'),
147 ('reshape_1', 'reshape_1_data'),
148 ('reshape_1_data', 'softmax_1'),
149 ('softmax_1', 'softmax_1_data'),
150 ('softmax_1_data', 'reshape_3'),
151 ('reshape_3', 'reshape_3_data'),
152 ('reshape_3_data', 'reshape_2'),
153 ('reshape_2', 'reshape_2_data')
155 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 2])},
156 'reshape_1_data': {'shape': np.array([1, 2, 227 * 227])},
157 'reshape_2_data': {'shape': np.array([1, 227, 227, 2])},
160 pattern = ReshapeSoftmaxReshape()
161 pattern.find_and_replace_pattern(graph)
163 (flag, resp) = compare_graphs(graph, graph_ref, 'reshape_2_data', check_op_attrs=True)
164 self.assertTrue(flag, resp)
167 graph = build_graph(nodes_attributes,
168 [('placeholder_1', 'placeholder_1_data'),
169 ('placeholder_1_data', 'reshape_1'),
170 ('reshape_1', 'reshape_1_data'),
171 ('reshape_1_data', 'softmax_1'),
172 ('softmax_1', 'softmax_1_data'),
173 ('softmax_1_data', 'reshape_2'),
174 ('reshape_2', 'reshape_2_data')
176 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 2])},
177 'reshape_1_data': {'shape': np.array([1 * 227 * 227, 2])},
178 'reshape_2_data': {'shape': np.array([1, 227, 227, 2])},
180 graph.graph['layout'] = 'NCHW'
182 graph_ref = build_graph(nodes_attributes,
183 [('placeholder_1', 'placeholder_1_data'),
184 ('placeholder_1_data', 'reshape_1'),
185 ('reshape_1', 'reshape_1_data'),
186 ('reshape_1_data', 'softmax_1'),
187 ('softmax_1', 'softmax_1_data'),
188 ('softmax_1_data', 'reshape_2'),
189 ('reshape_2', 'reshape_2_data')
191 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 2])},
192 'reshape_1_data': {'shape': np.array([1 * 227 * 227, 2])},
193 'reshape_2_data': {'shape': np.array([1, 227, 227, 2])},
196 pattern = ReshapeSoftmaxReshape()
197 pattern.find_and_replace_pattern(graph)
199 (flag, resp) = compare_graphs(graph, graph_ref, 'reshape_2_data', check_op_attrs=True)
200 self.assertTrue(flag, resp)