Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / extensions / front / image_scaler_test.py
1 """
2  Copyright (c) 2018 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     'mul_1_w': {'value': None, 'shape': None, 'kind': 'op', 'op': 'Const'},
36     'mul_1_data': {'value': None, 'shape': None, 'kind': 'data'},
37     'add_1': {'type': None, 'value': None, 'kind': 'op', 'op': 'Add'},
38     'add_1_w': {'value': None, 'shape': None, 'kind': 'op', 'op': 'Const'},
39     'add_1_data': {'value': None, 'shape': None, 'kind': 'data'},
40 }
41
42
43 class ImageScalerTest(unittest.TestCase):
44     def test_image_scaler_test1(self):
45         graph = build_graph(nodes_attributes,
46                             [('placeholder_1', 'placeholder_1_data'),
47                              ('placeholder_1_data', 'im_scaler'),
48                              ('im_scaler', 'im_scaler_data'),
49                              ('im_scaler_data', 'last'),
50                              ],
51                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
52                              'im_scaler': {'scale': np.array(1.0), 'bias': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
53                              }, nodes_with_edges_only=True)
54
55         graph_ref = build_graph(nodes_attributes,
56                                 [('placeholder_1', 'placeholder_1_data'),
57                                  ('placeholder_1_data', 'add_1'),
58                                  ('add_1_w', 'add_1'),
59                                  ('add_1', 'add_1_data'),
60                                  ('add_1_data', 'last')
61                                  ],
62                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
63                                  'add_1_w': {'shape': np.array([3, 1, 1]),
64                                              'value': np.reshape(np.array([1, 2, 3]), [3, 1, 1])},
65                                  }, nodes_with_edges_only=True)
66
67         graph.graph['layout'] = 'NCHW'
68
69         replacer = ImageScaler()
70         replacer.find_and_replace_pattern(graph)
71
72         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
73         self.assertTrue(flag, resp)
74
75     def test_image_scaler_test2(self):
76         graph = build_graph(nodes_attributes,
77                             [('placeholder_1', 'placeholder_1_data'),
78                              ('placeholder_1_data', 'im_scaler'),
79                              ('im_scaler', 'im_scaler_data'),
80                              ('im_scaler_data', 'last'),
81                              ],
82                             {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
83                              'im_scaler': {'scale': np.array(2.0), 'bias': np.reshape(np.array([0, 0, 0]), [3, 1, 1])},
84                              }, nodes_with_edges_only=True)
85
86         graph_ref = build_graph(nodes_attributes,
87                                 [('placeholder_1', 'placeholder_1_data'),
88                                  ('placeholder_1_data', 'mul_1'),
89                                  ('mul_1_w', 'mul_1'),
90                                  ('mul_1', 'mul_1_data'),
91                                  ('mul_1_data', 'last')
92                                  ],
93                                 {'placeholder_1_data': {'shape': np.array([1, 227, 227, 3])},
94                                  'mul_1_w': {'shape': np.array(2.0).shape, 'value': np.array(2.0)},
95                                  }, nodes_with_edges_only=True)
96
97         graph.graph['layout'] = 'NCHW'
98
99         replacer = ImageScaler()
100         replacer.find_and_replace_pattern(graph)
101
102         (flag, resp) = compare_graphs(graph, graph_ref, 'last')
103         self.assertTrue(flag, resp)