Added unit tests and readme for model optimizer (#79)
[platform/upstream/dldt.git] / model-optimizer / extensions / middle / SliceConvert_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 import unittest
17
18 import numpy as np
19
20 from extensions.middle.SliceConverter import ConvertSlice
21 from mo.graph.graph import Node
22 from mo.utils.unittest.graph import build_graph, compare_graphs
23 from mo.ops.slice import Slice
24
25 nodes_attributes = {
26     # input data
27     'placeholder_1': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
28     'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
29     # Slice layer
30     'slice': {'type': 'Slice', 'kind': 'op', 'op': 'Slice'},
31     'slice_data': {'value': None, 'shape': None, 'kind': 'data'},
32     # Output operation
33     'output_op': {'type': 'Const', 'value': None, 'kind': 'op', 'op': 'Const'},
34     'output_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
35     # Crop layer
36     'crop': {'type': 'Crop', 'kind': 'op', 'op': 'Crop', 'axis': None, 'offset': None, 'dim': None},
37     'dim': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
38     # StridedSlice layer
39     'strided_slice': {'type': 'StridedSlice', 'kind': 'op', 'op': 'StridedSlice', 'slices': None,
40                       'shrink_axis_mask': None}
41 }
42
43
44 class ConvertSliceTests(unittest.TestCase):
45     def test_1(self):
46         """
47         Testing case with non-constant path and multiple
48         slicing dimensions
49         :return:
50         """
51         graph = build_graph(nodes_attributes,
52                             [('placeholder_1', 'placeholder_1_data'),
53                              ('placeholder_1_data', 'slice'),
54                              ('slice', 'slice_data'),
55                              ('slice_data', 'output_op'),
56                              ('output_op', 'output_data')
57                              ],
58                             {'placeholder_1_data': {'shape': np.array([4, 5, 6])},
59                              'slice': {'start': np.array([1, 2, 3]), 'end': np.array([3, 4, 4]), 'axis': None},
60                              'output_op': {'is_output': True},
61                              }
62                             )
63         slice_node = Node(graph, 'slice')
64         Slice.infer(slice_node)
65
66         pattern = ConvertSlice()
67         pattern.find_and_replace_pattern(graph)
68
69         graph_ref = build_graph(nodes_attributes,
70                                 [('placeholder_1', 'placeholder_1_data'),
71                                  ('placeholder_1_data', 'crop'),
72                                  ('crop', 'slice_data'),
73                                  ('slice_data', 'output_op'),
74                                  ('output_op', 'output_data')
75                                  ],
76                                 {'placeholder_1_data': {'shape': np.array([4, 5, 6])},
77                                  'crop': {'axis': np.array([0, 1, 2]), 'offset': np.array([1, 2, 3]),
78                                           },
79                                  'output_op': {'is_output': True},
80                                  'dim': {'dim': np.array([2, 2, 1])},
81                                  }
82                                 )
83         (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
84         self.assertTrue(flag, resp)
85
86     def test_2(self):
87         """
88         Testing case with constant path and one
89          slicing dimension
90         """
91         graph = build_graph(nodes_attributes,
92                             [('placeholder_1', 'placeholder_1_data'),
93                              ('placeholder_1_data', 'slice'),
94                              ('slice', 'slice_data'),
95                              ('slice_data', 'output_op'),
96                              ('output_op', 'output_data')
97                              ],
98                             {'placeholder_1_data': {'shape': np.array([4, 5, 6])},
99                              'slice': {'start': np.array([1]), 'end': np.array([3]), 'axis': None},
100                              'output_op': {'is_output': True}
101                              }
102                             )
103         slice_node = Node(graph, 'slice')
104         Slice.infer(slice_node)
105
106         pattern = ConvertSlice()
107         pattern.find_and_replace_pattern(graph)
108
109         graph_ref = build_graph(nodes_attributes,
110                                 [('placeholder_1', 'placeholder_1_data'),
111                                  ('placeholder_1_data', 'strided_slice'),
112                                  ('strided_slice', 'slice_data'),
113                                  ('slice_data', 'output_op'),
114                                  ('output_op', 'output_data')
115                                  ],
116                                 {'placeholder_1_data': {'shape': np.array([4, 5, 6])},
117                                  'strided_slice': {'slices': np.array([slice(1, 3, 1),slice(0, 5, 1),slice(0, 6, 1)]),
118                                                    'shrink_axis_mask': np.array([False, False, False])},
119                                  'output_op': {'is_output': True}
120                                  }
121                                 )
122
123         (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
124         self.assertTrue(flag, resp)