2 Copyright (c) 2018-2019 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.
19 from mo.graph.graph import Graph
20 from mo.middle.replacement import MiddleReplacementPattern
21 from mo.ops.const import Const
22 from mo.ops.crop import Crop
23 from mo.ops.strided_slice import StridedSlice
26 def convert_negative_indices(indices: np.array, shape: np.array):
27 for ind, value in enumerate(indices):
29 indices[ind] += shape[ind]
32 class ConvertSlice(MiddleReplacementPattern):
34 This class convert Slice operation to Crop or Split depends on parameters
41 from extensions.middle.pass_separator import MiddleStart
47 ('slice', dict(kind='op', op='Slice'))
52 def replace_pattern(self, graph: Graph, match: dict):
55 if not node.has_valid('start') or not node.has_valid('end'):
60 axis = node.axis if node.has_valid('axis') else range(begin.size)
63 input = node.in_node(0)
64 output_data = node.out_node()
66 # Check whether operation use only one axis or not
67 axes_begin = np.zeros(len(input.shape), dtype=np.int32)
68 axes_end = np.zeros(len(input.shape), dtype=np.int32)
69 begin_ext = np.zeros(len(input.shape), dtype=np.int32)
70 end_ext = np.zeros(len(input.shape), dtype=np.int32)
72 axes = np.zeros(begin.size)
73 for i in range(len(axis)):
74 if begin[i] != 0 or end[i] < input.shape[i]:
78 axes_begin[axis[i]] = 1
79 begin_ext[axis[i]] = begin[i]
80 if end[i] < input.shape[i]:
82 end_ext[axis[i]] = end[i]
83 axes = np.array(axes, dtype=bool)
85 if dims == 1 or dims == 0:
86 # If Slice use only one axis or no axis, than
87 # convert Slice to StridedSlice
88 ss = StridedSlice(graph, dict(new_axis_mask=np.zeros(len(output_data.shape), dtype=np.int32),
89 shrink_axis_mask=np.zeros(len(output_data.shape), dtype=np.int32),
90 ellipsis_mask=np.zeros(len(output_data.shape), dtype=np.int32),
91 begin_mask=axes_begin,
94 convert_negative_indices(begin_ext, input.shape)
95 convert_negative_indices(end_ext, input.shape)
97 begin_node = Const(graph, {'name': 'begin', 'value': begin_ext, 'force_precision': 'I32'}).create_node_with_data()
98 end_node = Const(graph, {'name': 'end', 'value': end_ext, 'force_precision': 'I32'}).create_node_with_data()
100 ss.create_node_with_data(inputs=[input, begin_node, end_node], data_nodes=[output_data])
101 # Remove unnecessary edges from and to to Slice vertex
102 graph.remove_edge(input.id, node.id)
103 graph.remove_edge(node.id, output_data.id)
105 # If Slice use more than one axis use Crop layer
106 crop = Crop(graph, dict(axis=np.arange(begin.size)[axes],
108 # creating node with data
109 crop.create_node_with_data(inputs=[input], data_nodes=[output_data])
111 # Remove unnecessary edges from and to to Slice vertex
112 graph.remove_edge(input.id, node.id)
113 graph.remove_edge(node.id, output_data.id)