Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / extensions / ops / select_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 import numpy as np
19
20 from extensions.ops.select import Select
21 from mo.graph.graph import Node
22 from mo.utils.unittest.graph import build_graph_with_attrs, compare_graphs
23
24
25 class TestSelect(unittest.TestCase):
26     nodes = [
27         ('than', {'value': np.ones((2, 2)), 'kind': 'data', 'executable': True, 'shape': np.array([2, 2])}),
28         ('else', {'value': np.zeros((2, 2)), 'kind': 'data', 'executable': True, 'shape': np.array([2, 2])}),
29         ('condition', {'value': None, 'kind': 'data', 'executable': True}),
30         ('select', {'type': 'Select', 'kind': 'op', 'op': 'Select'}),
31         ('select_output', {'value': None, 'kind': 'data', 'executable': True, 'shape': None}),
32     ]
33     edges = [
34         ('condition', 'select', {'in': 0}),
35         ('than', 'select', {'in': 1}),
36         ('else', 'select', {'in': 2}),
37         ('select', 'select_output', {'out': 0}),
38     ]
39
40     def test_select_infer_no_condition(self):
41         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges)
42
43         # We should propagate only shapes
44         graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
45                                            edges_with_attrs=self.edges,
46                                            update_nodes_attributes=[('select_output', {'shape': np.array([2, 2])})])
47
48         tested_class = Select(graph=graph, attrs={})
49
50         node = Node(graph, 'select')
51         tested_class.infer(node)
52
53         (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
54         self.assertTrue(flag, resp)
55
56     def test_select_infer_condition_true(self):
57         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
58                                        update_nodes_attributes=[('condition', {'value': np.array([True])})])
59
60         # We should propagate shapes and values
61         graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
62                                            edges_with_attrs=self.edges,
63                                            update_nodes_attributes=[('select_output', {'shape': np.array([2, 2]),
64                                                                                        'value': np.ones((2,2))})])
65
66         tested_class = Select(graph=graph, attrs={})
67
68         node = Node(graph, 'select')
69         tested_class.infer(node)
70
71         (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
72         self.assertTrue(flag, resp)
73
74     def test_select_infer_condition_false(self):
75         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
76                                        update_nodes_attributes=[('condition', {'value': np.array([False])})])
77
78         # We should propagate shapes and values
79         graph_ref = build_graph_with_attrs(nodes_with_attrs=self.nodes,
80                                            edges_with_attrs=self.edges,
81                                            update_nodes_attributes=[('select_output', {'shape': np.array([2, 2]),
82                                                                                        'value': np.zeros((2, 2))})])
83
84         tested_class = Select(graph=graph, attrs={})
85
86         node = Node(graph, 'select')
87         tested_class.infer(node)
88
89         (flag, resp) = compare_graphs(graph, graph_ref, 'select_output', check_op_attrs=True)
90         self.assertTrue(flag, resp)
91
92     def test_select_infer_assert_shapes(self):
93         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
94                                        update_nodes_attributes=[('else', {'shape': np.array([3,3]), 'value':np.zeros((3,3))})])
95
96         tested_class = Select(graph=graph, attrs={})
97
98         node = Node(graph, 'select')
99         with self.assertRaisesRegex(AssertionError, "TensorFlow \'Select\' operation has 3 inputs: \'condition\',"
100                                                     " \'then\' and \'else\' tensors.\'then\' and \'else\' tensors"
101                                                     " must have the same shape by TensorFlow reference"):
102             tested_class.infer(node)
103
104     def test_select_infer_assert_condition_bool(self):
105         graph = build_graph_with_attrs(nodes_with_attrs=self.nodes, edges_with_attrs=self.edges,
106                                        update_nodes_attributes=[('condition', {'value': np.array([3])})])
107
108         tested_class = Select(graph=graph, attrs={})
109
110         node = Node(graph, 'select')
111         with self.assertRaisesRegex(AssertionError, "TensorFlow \'Select\' operation has 3 inputs: \'condition\',"
112                                                     " \'then\' and \'else\' tensors. Value of \'condition\' tensor"
113                                                     " must be boolen by TensorFlow reference"):
114             tested_class.infer(node)