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.
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
27 'placeholder_1': {'type': 'Placeholder', 'kind': 'op', 'op': 'Placeholder'},
28 'placeholder_1_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
30 'slice': {'type': 'Slice', 'kind': 'op', 'op': 'Slice'},
31 'slice_data': {'value': None, 'shape': None, 'kind': 'data'},
33 'output_op': {'type': 'Const', 'value': None, 'kind': 'op', 'op': 'Const'},
34 'output_data': {'value': None, 'shape': None, 'kind': 'data', 'data_type': None},
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},
39 'strided_slice': {'type': 'StridedSlice', 'kind': 'op', 'op': 'StridedSlice', 'slices': None,
40 'shrink_axis_mask': None}
44 class ConvertSliceTests(unittest.TestCase):
47 Testing case with non-constant path and multiple
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')
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},
63 slice_node = Node(graph, 'slice')
64 Slice.infer(slice_node)
66 pattern = ConvertSlice()
67 pattern.find_and_replace_pattern(graph)
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')
76 {'placeholder_1_data': {'shape': np.array([4, 5, 6])},
77 'crop': {'axis': np.array([0, 1, 2]), 'offset': np.array([1, 2, 3]),
79 'output_op': {'is_output': True},
80 'dim': {'dim': np.array([2, 2, 1])},
83 (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
84 self.assertTrue(flag, resp)
88 Testing case with constant path and one
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')
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}
103 slice_node = Node(graph, 'slice')
104 Slice.infer(slice_node)
106 pattern = ConvertSlice()
107 pattern.find_and_replace_pattern(graph)
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')
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}
123 (flag, resp) = compare_graphs(graph, graph_ref, 'output_op', check_op_attrs=True)
124 self.assertTrue(flag, resp)