2 Copyright (c) 2018 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 mo.front.common.partial_infer.expand_dims import tf_expand_dims_infer
22 from mo.graph.graph import Node
23 from mo.utils.unittest.graph import build_graph
25 nodes_attributes = {'input_1': {'kind': 'data', 'value': None},
26 'input_2': {'kind': 'data', 'value': None},
27 'expand_dims': {'kind': 'op'},
28 'out': {'value': None, 'shape': None, 'kind': 'data'}
32 class TestExpandDimsInfer(unittest.TestCase):
33 def test_expand_dims_infer_two_inputs(self):
34 graph = build_graph(nodes_attributes,
35 [('input_1', 'expand_dims'),
36 ('input_2', 'expand_dims'),
37 ('expand_dims', 'out')],
38 {'input_1': {'shape': np.array([3, 256, 256])},
39 'input_2': {'shape': np.array([1]), 'value': np.array([1], dtype=np.int32)},
42 expand_dims_node = Node(graph, 'expand_dims')
44 tf_expand_dims_infer(expand_dims_node)
45 exp_shape = np.array([3, 1, 256, 256])
46 res_shape = expand_dims_node.out_node().shape
47 self.assertEqual(len(exp_shape), len(res_shape))
48 for i in range(0, len(exp_shape)):
49 self.assertEqual(exp_shape[i], res_shape[i])
51 def test_expand_dims_infer_two_inputs_2(self):
52 graph = build_graph(nodes_attributes,
53 [('input_1', 'expand_dims'),
54 ('input_2', 'expand_dims'),
55 ('expand_dims', 'out')],
56 {'input_1': {'shape': np.array([3, 256, 256])},
57 'input_2': {'shape': np.array([1]), 'value': np.array([2], dtype=np.int32)},
60 expand_dims_node = Node(graph, 'expand_dims')
62 tf_expand_dims_infer(expand_dims_node)
63 exp_shape = np.array([3, 256, 1, 256])
64 res_shape = expand_dims_node.out_node().shape
65 self.assertEqual(len(exp_shape), len(res_shape))
66 for i in range(0, len(exp_shape)):
67 self.assertEqual(exp_shape[i], res_shape[i])
69 def test_expand_dims_infer_two_inputs_3(self):
70 graph = build_graph(nodes_attributes,
71 [('input_1', 'expand_dims'),
72 ('input_2', 'expand_dims'),
73 ('expand_dims', 'out')],
74 {'input_1': {'shape': np.array([3, 256, 256])},
75 'input_2': {'shape': np.array([]), 'value': np.array(3, dtype=np.int32)},
78 expand_dims_node = Node(graph, 'expand_dims')
80 tf_expand_dims_infer(expand_dims_node)
81 exp_shape = np.array([3, 256, 256, 1])
82 res_shape = expand_dims_node.out_node().shape
83 self.assertEqual(len(exp_shape), len(res_shape))
84 for i in range(0, len(exp_shape)):
85 self.assertEqual(exp_shape[i], res_shape[i])
87 def test_expand_dims_infer_two_inputs_negative(self):
88 graph = build_graph(nodes_attributes,
89 [('input_1', 'expand_dims'),
90 ('input_2', 'expand_dims'),
91 ('expand_dims', 'out')],
92 {'input_1': {'shape': np.array([3, 256, 256])},
93 'input_2': {'shape': np.array([1]), 'value': np.array([2, 3], dtype=np.int32)},
96 expand_dims_node = Node(graph, 'expand_dims')
98 tf_expand_dims_infer(expand_dims_node)
99 self.assertIsNone(expand_dims_node.out_node().shape)
101 def test_expand_dims_infer_two_inputs_negative_2(self):
102 graph = build_graph(nodes_attributes,
103 [('input_1', 'expand_dims'),
104 ('input_2', 'expand_dims'),
105 ('expand_dims', 'out')],
106 {'input_1': {'shape': None},
107 'input_2': {'shape': np.array([1]), 'value': np.array([2, 3], dtype=np.int32)},
110 expand_dims_node = Node(graph, 'expand_dims')
112 tf_expand_dims_infer(expand_dims_node)
113 self.assertIsNone(expand_dims_node.out_node().shape)
115 def test_expand_dims_infer_one_input(self):
116 graph = build_graph(nodes_attributes,
117 [('input_1', 'expand_dims'),
118 ('expand_dims', 'out')],
119 {'input_1': {'shape': np.array([3, 256, 256])},
120 'expand_dims': {'expand_axis': 1}
123 expand_dims_node = Node(graph, 'expand_dims')
125 tf_expand_dims_infer(expand_dims_node)
126 exp_shape = np.array([3, 1, 256, 256])
127 res_shape = expand_dims_node.out_node().shape
128 self.assertEqual(len(exp_shape), len(res_shape))
129 for i in range(0, len(exp_shape)):
130 self.assertEqual(exp_shape[i], res_shape[i])
132 def test_expand_dims_infer_one_input_2(self):
133 graph = build_graph(nodes_attributes,
134 [('input_1', 'expand_dims'),
135 ('expand_dims', 'out')],
136 {'input_1': {'shape': np.array([3, 256, 256])},
137 'expand_dims': {'expand_axis': 2}
140 expand_dims_node = Node(graph, 'expand_dims')
142 tf_expand_dims_infer(expand_dims_node)
143 exp_shape = np.array([3, 256, 1, 256])
144 res_shape = expand_dims_node.out_node().shape
145 self.assertEqual(len(exp_shape), len(res_shape))
146 for i in range(0, len(exp_shape)):
147 self.assertEqual(exp_shape[i], res_shape[i])
149 def test_expand_dims_infer_one_input_negative(self):
150 graph = build_graph(nodes_attributes,
151 [('input_1', 'expand_dims'),
152 ('expand_dims', 'out')],
153 {'input_1': {'shape': np.array([3, 256, 256])},
154 'expand_dims': {'expand_axis': None}
157 expand_dims_node = Node(graph, 'expand_dims')
159 tf_expand_dims_infer(expand_dims_node)
160 self.assertIsNone(expand_dims_node.out_node().shape)