From aecbd549f8dd52abf42308d01d19f1b45d41c67f Mon Sep 17 00:00:00 2001 From: Anastasia Popova Date: Mon, 10 Jan 2022 14:36:36 +0300 Subject: [PATCH] Support of partial shapes with boundaries in MO IR reader. (#9223) * Added support of partial shapes boundaries in MO IR reader. * Added comments. --- .../convert_model/Converting_Model.md | 5 +++- .../ir_reader/extenders/parameter_extender.py | 27 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/docs/MO_DG/prepare_model/convert_model/Converting_Model.md b/docs/MO_DG/prepare_model/convert_model/Converting_Model.md index a8a22189f18..f1ddbc33539 100644 --- a/docs/MO_DG/prepare_model/convert_model/Converting_Model.md +++ b/docs/MO_DG/prepare_model/convert_model/Converting_Model.md @@ -75,7 +75,10 @@ Framework-agnostic parameters: shape to the layout required by Inference Engine (N,C,H,W). The shape should not contain undefined dimensions (? or -1) and should fit the dimensions - defined in the input operation of the graph. If there + defined in the input operation of the graph. Boundaries + of undefined dimension can be specified with ellipsis, + for example [1,1..10,128,128]. One boundary can be undefined, + for example [1,..100] or [1,3,1..,1..]. If there are multiple inputs in the model, --input_shape should contain definition of shape for each input separated by a comma, for example: [1,3,227,227],[2,4] for a diff --git a/tools/mo/openvino/tools/mo/utils/ir_reader/extenders/parameter_extender.py b/tools/mo/openvino/tools/mo/utils/ir_reader/extenders/parameter_extender.py index 23a04c6e488..488c4a79b4e 100644 --- a/tools/mo/openvino/tools/mo/utils/ir_reader/extenders/parameter_extender.py +++ b/tools/mo/openvino/tools/mo/utils/ir_reader/extenders/parameter_extender.py @@ -3,6 +3,7 @@ from openvino.tools.mo.front.common.partial_infer.utils import int64_array, shape_array, dynamic_dimension_value from openvino.tools.mo.middle.passes.convert_data_type import destination_type_to_np_data_type +from openvino.tools.mo.utils.cli_parser import parse_dimension from openvino.tools.mo.utils.graph import Node from openvino.tools.mo.utils.ir_reader.extender import Extender @@ -18,7 +19,29 @@ class Parameter_extender(Extender): op.shape = int64_array([]) else: Extender.attr_to_list(op, 'shape') + shape = op.shape.copy() + has_shapes_with_boundaries = False for i, dim in enumerate(op.shape): if dim == -1 or (isinstance(dim, str) and ".." in dim): - op.shape[i] = -1 - op.shape = shape_array([d if d != -1 else dynamic_dimension_value for d in op.shape]) + shape[i] = -1 + if ".." in dim: + has_shapes_with_boundaries = True + shape = shape_array([d if d != -1 else dynamic_dimension_value for d in shape]) + + if has_shapes_with_boundaries: + shape_list = [] + for i, dim in enumerate(op.shape): + if not isinstance(dim, str): + shape_list.append(dim) + else: + shape_list.append(parse_dimension(dim)) + + # This value is used only for serialization of partial shapes with boundaries + # for Parameter node. + # 'user_shape' is not used in shape inference, as propagation of partial shapes with boundaries + # is not implemented in MO. + op['user_shape'] = tuple(shape_list) + + # If 'user_shape' is not set, 'shape' attribute is used for serialization. + # 'shape' is also used for shape inference. + op.shape = shape