Publishing 2019 R1 content
[platform/upstream/dldt.git] / model-optimizer / extensions / front / image_scaler_test.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
17 import unittest
18
19 import numpy as np
20
21 from extensions.front.image_scaler import ImageScaler
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23
24 nodes_attributes = {
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'},
30     # Test operation
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'},
42 }
43
44
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'),
54                              ],
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)
58
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'),
63                                  ('mul_1_w', 'mul_1'),
64                                  ('mul_1', 'mul_1_data'),
65                                  ('mul_1_data', 'add_1'),
66                                  ('const_add_1_w', 'add_1_w'),
67                                  ('add_1_w', 'add_1'),
68                                  ('add_1', 'add_1_data'),
69                                  ('add_1_data', 'last')
70                                  ],
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)
76
77         graph.graph['layout'] = 'NCHW'
78         graph.stage = 'middle'
79
80         replacer = ImageScaler()
81         replacer.find_and_replace_pattern(graph)
82
83         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
84         self.assertTrue(flag, resp)
85
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'),
93                              ],
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)
97
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')
105                                  ],
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)
110
111         graph.graph['layout'] = 'NCHW'
112         graph.stage = 'middle'
113
114         replacer = ImageScaler()
115         replacer.find_and_replace_pattern(graph)
116
117         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
118         self.assertTrue(flag, resp)
119
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'),
127                              ],
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)
131
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')
139                                  ],
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)
143
144         graph.graph['layout'] = 'NCHW'
145         graph.stage = 'middle'
146
147         replacer = ImageScaler()
148         replacer.find_and_replace_pattern(graph)
149
150         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
151         self.assertTrue(flag, resp)
152
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'),
160                              ],
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)
165
166         graph_ref = build_graph(nodes_attributes,
167                                 [('placeholder_1', 'placeholder_1_data'),
168                                  ('placeholder_1_data', 'last')
169                                  ],
170                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
171                                  }, nodes_with_edges_only=True)
172
173         graph.graph['layout'] = 'NCHW'
174         graph.stage = 'middle'
175
176         replacer = ImageScaler()
177         replacer.find_and_replace_pattern(graph)
178
179         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
180         self.assertTrue(flag, resp)
181
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'),
188                              ],
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)
192
193         graph_ref = build_graph(nodes_attributes,
194                                 [('placeholder_1', 'mul_1'),
195                                  ('const_mul_1_w', 'mul_1'),
196                                  ('mul_1', 'add_1'),
197                                  ('const_add_1_w', 'add_1'),
198                                  ('add_1', 'last')
199                                  ],
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)
205
206         graph.graph['layout'] = 'NCHW'
207         graph.stage = 'front'
208
209         replacer = ImageScaler()
210         replacer.find_and_replace_pattern(graph)
211
212         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
213         self.assertTrue(flag, resp)
214
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'),
220                              ],
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)
224
225         graph_ref = build_graph(nodes_attributes,
226                                 [('placeholder_1', 'add_1'),
227                                  ('const_add_1_w', 'add_1'),
228                                  ('add_1', 'last')
229                                  ],
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)
234
235         graph.graph['layout'] = 'NCHW'
236         graph.stage = 'front'
237
238         replacer = ImageScaler()
239         replacer.find_and_replace_pattern(graph)
240
241         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
242         self.assertTrue(flag, resp)
243
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'),
249                              ],
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)
253
254         graph_ref = build_graph(nodes_attributes,
255                                 [('placeholder_1', 'mul_1'),
256                                  ('const_mul_1_w', 'mul_1'),
257                                  ('mul_1', 'last')
258                                  ],
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)
262
263         graph.graph['layout'] = 'NCHW'
264         graph.stage = 'front'
265
266         replacer = ImageScaler()
267         replacer.find_and_replace_pattern(graph)
268
269         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
270         self.assertTrue(flag, resp)
271
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'),
277                              ],
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)
281
282         graph_ref = build_graph(nodes_attributes,
283                                 [('placeholder_1', 'last')
284                                  ],
285                                 {'placeholder_1': {'shape': np.array([1, 227, 227, 3])},
286                                  }, nodes_with_edges_only=True)
287
288         graph.graph['layout'] = 'NCHW'
289         graph.stage = 'front'
290
291         replacer = ImageScaler()
292         replacer.find_and_replace_pattern(graph)
293
294         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
295         self.assertTrue(flag, resp)