updated readme file due to moving CMake scripts to the root folder
[platform/upstream/dldt.git] / model-optimizer / mo / ops / unsqueeze_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 from generator import generator
21
22 from mo.front.common.partial_infer.utils import int64_array
23 from mo.graph.graph import Node
24 from mo.ops.unsqueeze import Unsqueeze
25 from mo.utils.unittest.graph import build_graph, compare_graphs
26
27
28 @generator
29 class TestUnsqueezeOp(unittest.TestCase):
30     nodes_attributes = {
31         'data_1': {
32             'kind': 'data',
33             'shape': None,
34             'value': None,
35         },
36         'unsq': {
37             'op': 'Unsqueeze',
38             'kind': 'op',
39         },
40         'unsq_dims_const': {
41             'op': 'Const',
42             'kind': 'op',
43         },
44         'unsq_dims': {
45             'kind': 'data',
46         },
47         'data_2': {
48             'kind': 'data',
49             'shape': None,
50             'value': None,
51         }
52     }
53
54     def test_unsqueeze_infer(self):
55         unsq_dims = np.array([0, 4])
56         graph = build_graph(self.nodes_attributes,
57                             [('data_1', 'unsq'),
58                              ('unsq_dims_const', 'unsq_dims'),
59                              ('unsq_dims', 'unsq'),
60                              ('unsq', 'data_2')],
61                             {'data_1': {'shape': np.array([1, 3, 64, 64])},
62                              'unsq_dims': {'value': unsq_dims, 'shape': unsq_dims.shape},
63                              'unsq_dims_const': {'value': unsq_dims, 'shape': unsq_dims.shape},
64                              })
65
66         graph_ref = build_graph(self.nodes_attributes,
67                                 [('data_1', 'unsq'),
68                                  ('unsq_dims_const', 'unsq_dims'),
69                                  ('unsq_dims', 'unsq'),
70                                  ('unsq', 'data_2')],
71                                 {'data_1': {'shape': np.array([1, 3, 64, 64])},
72                                  'unsq_dims': {'value': unsq_dims, 'shape': unsq_dims.shape},
73                                  'unsq_dims_const': {'value': unsq_dims, 'shape': unsq_dims.shape},
74                                  'data_2': {'shape': np.array([1, 1, 3, 64, 1, 64])},
75                                  })
76
77         unsqueeze_node = Node(graph, 'unsq')
78         Unsqueeze.infer(unsqueeze_node)
79
80         (flag, resp) = compare_graphs(graph, graph_ref, 'data_2')
81         self.assertTrue(flag, resp)
82
83     def test_unsqueeze_infer_negative_indices(self):
84         unsq_dims = np.array([-1])
85         graph = build_graph(self.nodes_attributes,
86                             [('data_1', 'unsq'),
87                              ('unsq_dims_const', 'unsq_dims'),
88                              ('unsq_dims', 'unsq'),
89                              ('unsq', 'data_2')],
90                             {'data_1': {'shape': np.array([2, 3, 64, 64])},
91                              'unsq_dims': {'value': unsq_dims, 'shape': unsq_dims.shape},
92                              'unsq_dims_const': {'value': unsq_dims, 'shape': unsq_dims.shape},
93                              })
94
95         graph_ref = build_graph(self.nodes_attributes,
96                                 [('data_1', 'unsq'),
97                                  ('unsq_dims_const', 'unsq_dims'),
98                                  ('unsq_dims', 'unsq'),
99                                  ('unsq', 'data_2')],
100                                 {'data_1': {'shape': np.array([2, 3, 64, 64])},
101                                  'unsq_dims': {'value': int64_array([4]), 'shape': unsq_dims.shape},
102                                  'unsq_dims_const': {'value': int64_array([4]), 'shape': unsq_dims.shape},
103                                  'data_2': {'shape': np.array([2, 3, 64, 64, 1])},
104                                  })
105
106         unsqueeze_node = Node(graph, 'unsq')
107         Unsqueeze.infer(unsqueeze_node)
108
109         (flag, resp) = compare_graphs(graph, graph_ref, 'data_2')
110         self.assertTrue(flag, resp)