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.front.image_scaler import ImageScaler
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},
27 # ImageScaler operation
28 'im_scaler': {'type': None, 'kind': 'op', 'op': 'ImageScaler'},
29 'im_scaler_data': {'value': None, 'shape': None, 'kind': 'data'},
31 'last': {'type': None, 'value': None, 'kind': 'op', 'op': None},
32 'last_data': {'value': None, 'shape': None, 'kind': 'data'},
33 # Mul and Add operations
34 'mul_1': {'type': None, 'value': None, 'kind': 'op', 'op': 'Mul'},
35 'const_mul_1_w': {'type': None, 'value': None, 'kind': 'op', 'op': 'Const'},
36 'mul_1_w': {'value': None, 'shape': None, 'kind': 'data'},
37 'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
38 'add_1': {'type': None, 'value': None, 'kind': 'op', 'op': 'Add'},
39 'const_add_1_w': {'type': None, 'value': None, 'kind': 'op', 'op': 'Const'},
40 'add_1_w': {'value': None, 'shape': None, 'kind': 'data'},
41 'add_1_data': {'value': None, 'shape': None, 'kind': 'data'},
45 class ImageScalerTest(unittest.TestCase):
46 # Tests for MIDDLE stage
47 # Graph with Mul and Add operations
48 def test_image_scaler_test_1(self):
49 graph = build_graph(nodes_attributes,
50 [('placeholder_1', 'placeholder_1_data'),
51 ('placeholder_1_data', 'im_scaler'),
52 ('im_scaler', 'im_scaler_data'),
53 ('im_scaler_data', 'last'),
55 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
56 'im_scaler': {'scale': np.array(2.0), 'bias': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
57 }, nodes_with_edges_only=True)
59 graph_ref = build_graph(nodes_attributes,
60 [('placeholder_1', 'placeholder_1_data'),
61 ('placeholder_1_data', 'mul_1'),
62 ('const_mul_1_w', 'mul_1_w'),
64 ('mul_1', 'mul_1_data'),
65 ('mul_1_data', 'add_1'),
66 ('const_add_1_w', 'add_1_w'),
68 ('add_1', 'add_1_data'),
69 ('add_1_data', 'last')
71 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
72 'const_mul_1_w': {'shape': np.array(2.0).shape, 'value': np.array(2.0)},
73 'const_add_1_w': {'shape': np.array([3, 1, 1]),
74 'value': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
75 }, nodes_with_edges_only=True)
77 graph.graph['layout'] = 'NCHW'
78 graph.stage = 'middle'
80 replacer = ImageScaler()
81 replacer.find_and_replace_pattern(graph)
83 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
84 self.assertTrue(flag, resp)
86 # Graph with Add operation
87 def test_image_scaler_test_2(self):
88 graph = build_graph(nodes_attributes,
89 [('placeholder_1', 'placeholder_1_data'),
90 ('placeholder_1_data', 'im_scaler'),
91 ('im_scaler', 'im_scaler_data'),
92 ('im_scaler_data', 'last'),
94 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
95 'im_scaler': {'scale': np.array(1.0), 'bias': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
96 }, nodes_with_edges_only=True)
98 graph_ref = build_graph(nodes_attributes,
99 [('placeholder_1', 'placeholder_1_data'),
100 ('placeholder_1_data', 'add_1'),
101 ('const_add_1_w', 'add_1_w'),
102 ('add_1_w', 'add_1'),
103 ('add_1', 'add_1_data'),
104 ('add_1_data', 'last')
106 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
107 'const_add_1_w': {'shape': np.array([3, 1, 1]),
108 'value': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
109 }, nodes_with_edges_only=True)
111 graph.graph['layout'] = 'NCHW'
112 graph.stage = 'middle'
114 replacer = ImageScaler()
115 replacer.find_and_replace_pattern(graph)
117 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
118 self.assertTrue(flag, resp)
120 # Graph with Mul operation
121 def test_image_scaler_test_3(self):
122 graph = build_graph(nodes_attributes,
123 [('placeholder_1', 'placeholder_1_data'),
124 ('placeholder_1_data', 'im_scaler'),
125 ('im_scaler', 'im_scaler_data'),
126 ('im_scaler_data', 'last'),
128 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
129 'im_scaler': {'scale': np.array(2.0), 'bias': np.reshape(np.array([0, 0, 0]), [3, 1, 1])},
130 }, nodes_with_edges_only=True)
132 graph_ref = build_graph(nodes_attributes,
133 [('placeholder_1', 'placeholder_1_data'),
134 ('placeholder_1_data', 'mul_1'),
135 ('const_mul_1_w', 'mul_1_w'),
136 ('mul_1_w', 'mul_1'),
137 ('mul_1', 'mul_1_data'),
138 ('mul_1_data', 'last')
140 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
141 'const_mul_1_w': {'shape': np.array(2.0).shape, 'value': np.array(2.0)},
142 }, nodes_with_edges_only=True)
144 graph.graph['layout'] = 'NCHW'
145 graph.stage = 'middle'
147 replacer = ImageScaler()
148 replacer.find_and_replace_pattern(graph)
150 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
151 self.assertTrue(flag, resp)
153 # Graph without Mul and Add operations
154 def test_image_scaler_test_4(self):
155 graph = build_graph(nodes_attributes,
156 [('placeholder_1', 'placeholder_1_data'),
157 ('placeholder_1_data', 'im_scaler'),
158 ('im_scaler', 'im_scaler_data'),
159 ('im_scaler_data', 'last'),
161 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
162 'im_scaler_data': {'shape': np.array([1, 227, 227, 3])},
163 'im_scaler': {'scale': np.array(1.0), 'bias': np.reshape(np.array([0, 0, 0]), [3, 1, 1])},
164 }, nodes_with_edges_only=True)
166 graph_ref = build_graph(nodes_attributes,
167 [('placeholder_1', 'placeholder_1_data'),
168 ('placeholder_1_data', 'last')
170 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
171 }, nodes_with_edges_only=True)
173 graph.graph['layout'] = 'NCHW'
174 graph.stage = 'middle'
176 replacer = ImageScaler()
177 replacer.find_and_replace_pattern(graph)
179 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
180 self.assertTrue(flag, resp)
182 # Tests for FRONT stage
183 # Graph with Mul and Add operations
184 def test_image_scaler_test_5(self):
185 graph = build_graph(nodes_attributes,
186 [('placeholder_1', 'im_scaler'),
187 ('im_scaler', 'last'),
189 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
190 'im_scaler': {'scale': np.array(2.0), 'bias': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
191 }, nodes_with_edges_only=True)
193 graph_ref = build_graph(nodes_attributes,
194 [('placeholder_1', 'mul_1'),
195 ('const_mul_1_w', 'mul_1'),
197 ('const_add_1_w', 'add_1'),
200 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
201 'const_mul_1_w': {'shape': np.array(2.0).shape, 'value': np.array(2.0)},
202 'const_add_1_w': {'shape': np.array([3, 1, 1]),
203 'value': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
204 }, nodes_with_edges_only=True)
206 graph.graph['layout'] = 'NCHW'
207 graph.stage = 'front'
209 replacer = ImageScaler()
210 replacer.find_and_replace_pattern(graph)
212 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
213 self.assertTrue(flag, resp)
215 # Graph with Add operation
216 def test_image_scaler_test_6(self):
217 graph = build_graph(nodes_attributes,
218 [('placeholder_1', 'im_scaler'),
219 ('im_scaler', 'last'),
221 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
222 'im_scaler': {'scale': np.array(1.0), 'bias': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
223 }, nodes_with_edges_only=True)
225 graph_ref = build_graph(nodes_attributes,
226 [('placeholder_1', 'add_1'),
227 ('const_add_1_w', 'add_1'),
230 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
231 'const_add_1_w': {'shape': np.array([3, 1, 1]),
232 'value': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
233 }, nodes_with_edges_only=True)
235 graph.graph['layout'] = 'NCHW'
236 graph.stage = 'front'
238 replacer = ImageScaler()
239 replacer.find_and_replace_pattern(graph)
241 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
242 self.assertTrue(flag, resp)
244 # Graph with Mul operation
245 def test_image_scaler_test_7(self):
246 graph = build_graph(nodes_attributes,
247 [('placeholder_1', 'im_scaler'),
248 ('im_scaler', 'last'),
250 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
251 'im_scaler': {'scale': np.array(2.0), 'bias': np.reshape(np.array([0, 0, 0]), [3, 1, 1])},
252 }, nodes_with_edges_only=True)
254 graph_ref = build_graph(nodes_attributes,
255 [('placeholder_1', 'mul_1'),
256 ('const_mul_1_w', 'mul_1'),
259 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
260 'const_mul_1_w': {'shape': np.array(2.0).shape, 'value': np.array(2.0)},
261 }, nodes_with_edges_only=True)
263 graph.graph['layout'] = 'NCHW'
264 graph.stage = 'front'
266 replacer = ImageScaler()
267 replacer.find_and_replace_pattern(graph)
269 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
270 self.assertTrue(flag, resp)
272 # Graph without Mul and Add operations
273 def test_image_scaler_test_8(self):
274 graph = build_graph(nodes_attributes,
275 [('placeholder_1', 'im_scaler'),
276 ('im_scaler', 'last'),
278 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
279 'im_scaler': {'scale': np.array(1.0), 'bias': np.reshape(np.array([0, 0, 0]), [3, 1, 1])},
280 }, nodes_with_edges_only=True)
282 graph_ref = build_graph(nodes_attributes,
283 [('placeholder_1', 'last')
285 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
286 }, nodes_with_edges_only=True)
288 graph.graph['layout'] = 'NCHW'
289 graph.stage = 'front'
291 replacer = ImageScaler()
292 replacer.find_and_replace_pattern(graph)
294 (flag, resp) = compare_graphs(graph, graph_ref, 'last')
295 self.assertTrue(flag, resp)