Files
openvino/model-optimizer/mo/front/mxnet/extractors/slice_axis.py
T
Pavel Esir 22169a05b9 [MO] StridedSlice improvements (#4139)
* fix ss

* successfully converted

* successfully run moved infer and normalizer unit-tests

* successfully rewritten StridedSlice infer unittests

* int64 array

* Successfully converter crash-when-loading, xj_feauture and toy nets (cherry-picked maxpoolV4 and tf_broadcast_ext)

* successfully moved PermuteAttrs to general mechanism

* successfully converted xj_feauture and crash when loading with the new rewritten SS infer

* fixed get_shape_from_slice and moved to common utils

* fixed extending masks and some other

* some refactoring

* fixed extending masks in extractor, fixed licence year and some other code clearing

* corrected a couple of unittests

* fox permute for 5 rank slice and 4 rank inputs/

* WIP

* Added comments

* fixed StridedSlice in ProposalMutation.py

* rechecked shape_infer unittests added some new cases

* added shape_infer unit-tests after StridedSliceNormalizer pass and Permute unit-tests

* corrected unittests

* Applied review comments

* general permutations for inputs implemented, corrected ellipsis unrolling when shrink_axis is at the beginning, some other corrections

* removed code duplication in infer and normalizer, moved 'slices' attr normalizing to StridedSliceNormalizer.py

* removed some code duplication and other minor improvements

* Added tests

* minor corrections

* wider range of unittests added (froze the number)

* review comments applied

* enabled skipped unit-test

* comment corrections

* applied review comments: changed op -> type, added some asserts, corrected comments and other minor corrections

* sorted inputs, updated Supported_Frameworks_Layers.md, some minor
2021-02-16 11:48:49 +03:00

72 lines
2.3 KiB
Python

"""
Copyright (C) 2018-2021 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from mo.front.caffe.extractors.utils import get_canonical_axis_index
import numpy as np
from mo.utils.error import Error
def slice_axis_ext(attrs):
axis = attrs.int("axis", 0)
begin = attrs.int("begin", 0)
end = attrs.int("end", None)
node_attrs = {
'type': 'Crop',
'axis': axis,
'offset': begin,
'dim': end,
'infer': mxnet_slice_axis_infer
}
return node_attrs
def mxnet_slice_axis_infer(node):
in_shape = node.in_port(0).data.get_shape()
node.axis = get_canonical_axis_index(in_shape, node.axis)
slice_axis = node.axis
new_shape = np.array(in_shape, dtype=np.int64)
new_shape[slice_axis] = new_shape[slice_axis] / len(node.out_nodes())
axis_size = in_shape[slice_axis]
if node.offset < 0:
node.offset += axis_size
if not node.dim:
node.dim = axis_size
elif node.dim < 0:
node.dim += axis_size
input_dim = in_shape.size
node.dim = (node.dim - node.offset)
if node.dim > in_shape[slice_axis]:
raise Error(
'{0} node dimension value is bigger than the corresponding value in the input shape {1}. ' +
'\nIn particular {2} is bigger than {3}. The Model Optimizer does not support this case. ' +
'\nTo overcome, try to edit the original model "end" property of the {0} layer.',
node.name, ','.join(str(i) for i in in_shape), str(node.dim), str(in_shape[slice_axis])
)
for i in range(0, input_dim):
if i == slice_axis:
new_shape[i] = node.dim
else:
new_shape[i] = in_shape[i]
for i in range(0, len(node.out_nodes())):
node.out_node(i)['shape'] = new_shape