diff --git a/docs/IE_DG/ShapeInference.md b/docs/IE_DG/ShapeInference.md index 58203a3f841..34feaed02bb 100644 --- a/docs/IE_DG/ShapeInference.md +++ b/docs/IE_DG/ShapeInference.md @@ -1,38 +1,61 @@ Using Shape Inference {#openvino_docs_IE_DG_ShapeInference} ========================================== -Inference Engine takes two kinds of model description as an input: [Intermediate Representation (IR)](../MO_DG/IR_and_opsets.md) and [nGraph::Function](nGraph_Flow.md) objects. -Both should have fixed input shapes to be successfully loaded to the Inference Engine. -To feed input data of a shape that is different from the model input shape, resize the model first. +Inference Engine takes three kinds of a model description as an input, which are converted into an `InferenceEngine::CNNNetwork` object: +1. [Intermediate Representation (IR)](../MO_DG/IR_and_opsets.md) through `InferenceEngine::Core::ReadNetwork` +2. [ONNX model](../IE_DG/OnnxImporterTutorial.md) through `InferenceEngine::Core::ReadNetwork` +3. [nGraph::Function](../IE_DG/nGraph_Flow.md) through the constructor of `InferenceEngine::CNNNetwork` -Model resizing on the stage of IR generation or [nGraph::Function creation](nGraphTutorial.md) is the recommended approach. -OpenVINO™ provides the following experimental methods for runtime model reshaping: +`InferenceEngine::CNNNetwork` keeps an `ngraph::Function` object with the model description internally. +The object should have fully defined input shapes to be successfully loaded to the Inference Engine plugins. +To resolve undefined input dimensions of a model, call the `CNNNetwork::reshape` method providing new input shapes before loading to the Inference Engine plugin. -1. Setting a new input shape with the `InferenceEngine::CNNNetwork::reshape` method - - `InferenceEngine::CNNNetwork::reshape` method updates input shapes and propagates them down to the outputs of the model through all intermediate layers. - - Shape propagation for `InferenceEngine::CNNNetwork` objects created from `nGraph::Function` or IR of the version 10 works through the `nGraph` shape inference mechanism. - `InferenceEngine::CNNNetwork` objects created from lower IR versions are considered deprecated and may be reshaped incorrectly or give unexpected results. - - To keep the v10 IR resizable by the `InferenceEngine::CNNNetwork::reshape` method, convert the model with the additional Model Optimizer key `--keep_shape_ops`. - -2. Setting a new batch dimension value with the `InferenceEngine::CNNNetwork::setBatchSize` method - - The meaning of a model batch may vary depending on choices you made during the model designing. - The `InferenceEngine::CNNNetwork::setBatchSize` method deduces index of batch dimension relying only on the input rank. - This method does not work for models with a non-zero index batch placement or models with inputs without a batch dimension. +Run the following code right after `InferenceEngine::CNNNetwork` creation to explicitly check for model input names and shapes: +```cpp +CNNNetwork network = ... // read IR / ONNX model or create from nGraph::Function explicitly +const auto parameters = network.getFunction()->get_parameters(); +for (const auto & parameter : parameters) { + std::cout << "name: " << parameter->get_friendly_name() << " shape: " << parameter->get_partial_shape() << std::endl; + if (parameter->get_partial_shape().is_dynamic()) + std::cout << "ATTENTION: Input shape is not fully defined. Use the CNNNetwork::reshape method to resolve it." << std::endl; +} +``` - Batch-setting algorithm does not involve shape inference mechanism. - Batch of input and output shapes for all layers is set to a new batch value without layer validation. - It may cause both positive and negative side effects. - - Due to the limitations described above, the current method is recommended for simple image processing models only. +To feed input data of a shape that is different from the model input shape, reshape the model first. +OpenVINO™ provides the following methods for runtime model reshaping: -Practically, some models are not ready to be resized. In this case, a new input shape cannot be set with the Model Optimizer or the `InferenceEngine::CNNNetwork::reshape` method. +* **Set a new input shape** with the `InferenceEngine::CNNNetwork::reshape` method.
+ The `InferenceEngine::CNNNetwork::reshape` method updates input shapes and propagates them down to the outputs of the model through all intermediate layers. + You can reshape a model multiple times like in this application scheme: + ``` + ReadNetwork -> reshape(input_1_shape) -> LoadNetwork -> infer(input_1) + \ + -> reshape(input_2_shape) -> LoadNetwork -> infer(input_2) + ``` + > **NOTES**: + > - Starting with the 2021.1 release, the Model Optimizer converts topologies keeping shape-calculating sub-graphs by default, which enables correct shape propagation during reshaping. + > - Older versions of IRs are not guaranteed to reshape successfully. Please regenerate them with the Model Optimizer of the latest version of OpenVINO™.
+ > - If an ONNX model does not have a fully defined input shape and the model was imported with the ONNX importer, reshape the model before loading it to the plugin. +* **Set a new batch dimension value** with the `InferenceEngine::CNNNetwork::setBatchSize` method.
+ The meaning of a model batch may vary depending on the model design. + The `InferenceEngine::CNNNetwork::setBatchSize` method deduces the index of a batch dimension based only on the input rank. + This method does not work for models with a non-zero index batch placement or models with inputs without a batch dimension. + The batch-setting algorithm does not involve the shape inference mechanism. + Batch of input and output shapes for all layers is set to a new batch value without layer validation. + It may cause both positive and negative side effects. + Due to the limitations described above, the current method is not recommended to use. + If you need to set a new batch size for the model, use the `CNNNetwork::reshape` method instead. -## Troubleshooting Resize Errors +Do not use runtime reshaping methods simultaneously, especially do not call the `CNNNetwork::reshape` method after you use `InferenceEngine::CNNNetwork::setBatchSize`. +The `InferenceEngine::CNNNetwork::setBatchSize` method causes irreversible conversion of the internal model representation into the legacy model representation. +The method does not use nGraph for shape inference which leads to reduced reshape opportunities and may affect the performance of the model. + +There are other approaches to reshape the model during the stage of IR generation or [nGraph::Function creation](../IE_DG/nGraphTutorial.md). + +Practically, some models are not ready to be reshaped. In this case, a new input shape cannot be set with the Model Optimizer or the `InferenceEngine::CNNNetwork::reshape` method. + +## Troubleshooting Reshape Errors Operation semantics may impose restrictions on input shapes of the operation. Shape collision during shape propagation may be a sign that a new shape does not satisfy the restrictions. @@ -42,7 +65,7 @@ Examples of such operations: - `Reshape` operation with a hard-coded output shape value - `MatMul` operation with the `Const` second input cannot be resized by spatial dimensions due to operation semantics -Model structure and logic should not change significantly after resizing. +Model structure and logic should not change significantly after model reshaping. - The Global Pooling operation is commonly used to reduce output feature map of classification models output. Having the input of the shape [N, C, H, W], Global Pooling returns the output of the shape [N, C, 1, 1]. Model architects usually express Global Pooling with the help of the `Pooling` operation with the fixed kernel size [H, W]. @@ -50,12 +73,12 @@ During spatial reshape, having the input of the shape [N, C, H1, W1], Pooling wi It breaks the classification model structure. For example, [publicly available Inception family models from TensorFlow*](https://github.com/tensorflow/models/tree/master/research/slim#pre-trained-models) have this issue. -- Resizing the model input shape may significantly affect its accuracy. +- Changing the model input shape may significantly affect its accuracy. For example, Object Detection models from TensorFlow have resizing restrictions by design. To keep the model valid after the reshape, choose a new input shape that satisfies conditions listed in the `pipeline.config` file. For details, refer to the Tensorflow Object Detection API models resizing techniques. -## Usage of Reshape Method +## Usage of Reshape Method The primary method of the feature is `InferenceEngine::CNNNetwork::reshape`. It gets new input shapes and propagates it from input to output for all intermediates layers of the given network. diff --git a/docs/MO_DG/prepare_model/convert_model/Converting_Model_General.md b/docs/MO_DG/prepare_model/convert_model/Converting_Model_General.md index 5bdbf7b9dc4..82bcd133bb8 100644 --- a/docs/MO_DG/prepare_model/convert_model/Converting_Model_General.md +++ b/docs/MO_DG/prepare_model/convert_model/Converting_Model_General.md @@ -126,8 +126,10 @@ Framework-agnostic parameters: value, for example: "node_name->True". It will be DEPRECATED in future releases. Use --input option to specify a value for freezing. - --static_shape Enables `ShapeOf` operation with all children folding to `Constant`. - This option makes model not reshapable in Inference Engine + --static_shape Enables IR generation for fixed input shape (folding + `ShapeOf` operations and shape-calculating sub-graphs + to `Constant`). Changing model input shape using + the Inference Engine API in runtime may fail for such an IR. --disable_weights_compression Disable compression and store weights with original precision. diff --git a/docs/MO_DG/prepare_model/convert_model/tf_specific/Convert_Object_Detection_API_Models.md b/docs/MO_DG/prepare_model/convert_model/tf_specific/Convert_Object_Detection_API_Models.md index 0f14e8f2cd9..d0e310088e0 100644 --- a/docs/MO_DG/prepare_model/convert_model/tf_specific/Convert_Object_Detection_API_Models.md +++ b/docs/MO_DG/prepare_model/convert_model/tf_specific/Convert_Object_Detection_API_Models.md @@ -1,9 +1,7 @@ # Converting TensorFlow* Object Detection API Models {#openvino_docs_MO_DG_prepare_model_convert_model_tf_specific_Convert_Object_Detection_API_Models} > **NOTES**: -> -> * Starting with the 2019 R1 release, the Model Optimizer supports the `--keep_shape_ops` command line parameter that allows you to convert the TensorFlow\* Object Detection API Faster and Mask RCNNs topologies so they can be re-shaped in the Inference Engine using dedicated reshape API. Refer to [Using Shape Inference](../../../../IE_DG/ShapeInference.md) for more information on how to use this feature. It is possible to change the both spatial dimensions of the input image and batch size. -> * Starting with the 2018 R4 release, the Model Optimizer supports the `--input_shape` command line parameter for the TensorFlow\* Object Detection API topologies. Refer to the [Custom Input Shape](#tf_od_custom_input_shape) for more information. +> * Starting with the 2021.1 release, the Model Optimizer converts the TensorFlow\* Object Detection API SSDs, Faster and Mask RCNNs topologies keeping shape-calculating sub-graphs by default, so topologies can be re-shaped in the Inference Engine using dedicated reshape API. Refer to [Using Shape Inference](../../../../IE_DG/ShapeInference.md) for more information on how to use this feature. It is possible to change the both spatial dimensions of the input image and batch size. > * To generate IRs for SSD topologies, the Model Optimizer creates a number of `PriorBoxClustered` layers instead of a constant node with prior boxes calculated for the particular input image size. This change allows you to reshape the topology in the Inference Engine using dedicated Inference Engine API. The reshaping is supported for all SSD topologies except FPNs which contain hardcoded shapes for some operations preventing from changing topology input shape. ## How to Convert a Model diff --git a/model-optimizer/mo/utils/cli_parser.py b/model-optimizer/mo/utils/cli_parser.py index e8b55a10e66..bb2918db462 100644 --- a/model-optimizer/mo/utils/cli_parser.py +++ b/model-optimizer/mo/utils/cli_parser.py @@ -318,12 +318,12 @@ def get_common_cli_parser(parser: argparse.ArgumentParser = None): action=IgnoredAction, default=False) common_group.add_argument('--static_shape', - help='Enables `ShapeOf` operation with all children folding to `Constant`. ' - 'This option makes model not reshapable in Inference Engine', + help='Enables IR generation for fixed input shape (folding `ShapeOf` operations and ' + 'shape-calculating sub-graphs to `Constant`). Changing model input shape using ' + 'the Inference Engine API in runtime may fail for such an IR.', action='store_true', default=False) common_group.add_argument('--keep_shape_ops', - help='[ Experimental feature ] Enables `Shape` operation with all children keeping. ' - 'This feature makes model reshapable in Inference Engine', + help='The option is ignored. Expected behavior is enabled by default.', action=IgnoredAction, default=True) common_group.add_argument('--disable_weights_compression', help='Disable compression and store weights with original precision.',