Skip to content

Commit

Permalink
Model Optimizer Python API documentaion. (#14696)
Browse files Browse the repository at this point in the history
  • Loading branch information
popovaan committed Dec 16, 2022
1 parent 1a72966 commit 756b0de
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/MO_DG/Deep_Learning_Model_Optimizer_DevGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
openvino_docs_MO_DG_prepare_model_convert_model_Cutting_Model
openvino_docs_MO_DG_Additional_Optimization_Use_Cases
openvino_docs_MO_DG_FP16_Compression
openvino_docs_MO_DG_Python_API
openvino_docs_MO_DG_prepare_model_Model_Optimizer_FAQ

@endsphinxdirective
Expand Down
72 changes: 72 additions & 0 deletions docs/MO_DG/prepare_model/MO_Python_API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
## Model Optimizer Python API {#openvino_docs_MO_DG_Python_API}

Model Optimizer (MO) has a Python API for model conversion, which is represented by the `convert_model()` method in the openvino.tools.mo namespace.
`convert_model()` has all the functionality available from the command-line tool.
`convert_model()` returns an openvino.runtime.Model object which can be compiled and inferred or serialized to IR.

```sh
from openvino.tools.mo import convert_model

ov_model = convert_model("resnet.onnx")
```

`convert_model()` accepts all parameters available in the MO command-line tool. Parameters can be specified by Python classes or string analogs, similar to the command-line tool.
Example 1:

```sh
from openvino.runtime import PartialShape, Layout

ov_model = convert_model(model, input_shape=PartialShape([1,3,100,100]), mean_values=[127, 127, 127], layout=Layout("NCHW"))
```

Example 2:

```sh
ov_model = convert_model(model, input_shape="[1,3,100,100]", mean_values="[127,127,127]", layout="NCHW")
```

Command-line flags, like `--compress_to_fp16`, can be set in the Python API by providing a boolean value (`True` or `False`).

```sh
ov_model = convert_model(model, compress_to_fp16=True)
```

The `input` parameter can be set by a `tuple` with a name, shape, and type. The input name of the type string is required in the tuple. The shape and type are optional.
The shape can be a `list` or `tuple` of dimensions (`int` or `openvino.runtime.Dimension`), or `openvino.runtime.PartialShape`, or `openvino.runtime.Shape`. The type can be of numpy type or `openvino.runtime.Type`.

```sh
ov_model = convert_model(model, input=("input_name", [3], np.float32))
```

For complex cases, when a value needs to be set in the `input` parameter, the `InputCutInfo` class can be used. `InputCutInfo` accepts four parameters: `name`, `shape`, `type`, and `value`.

`InputCutInfo("input_name", [3], np.float32, [0.5, 2.1, 3.4])` is equivalent of `InputCutInfo(name="input_name", shape=[3], type=np.float32, value=[0.5, 2.1, 3.4])`.
Supported types for `InputCutInfo`:
- name: `string`.
- shape: `list` or `tuple` of dimensions (`int` or `openvino.runtime.Dimension`), `openvino.runtime.PartialShape`,` openvino.runtime.Shape`.
- type: `numpy type`, `openvino.runtime.Type`.
- value: `numpy.ndarray`, `list` of numeric values, `bool`.

```sh
from openvino.tools.mo import convert_model, InputCutInfo

ov_model = convert_model(model, input=InputCutInfo("input_name", [3], np.float32, [0.5, 2.1, 3.4]))
```

`layout`, `source_layout` and `dest_layout` accept an `openvino.runtime.Layout` object or `string`.

```sh
from openvino.runtime import Layout
from openvino.tools.mo import convert_model

ov_model = convert_model(model, source_layout=Layout("NCHW"))
```

To set both source and destination layouts in the `layout` parameter, the `LayoutMap` class can be used. `LayoutMap` accepts two parameters: `source_layout` and `target_layout`.
`LayoutMap("NCHW", "NHWC")` is equivalent to `LayoutMap(source_layout="NCHW", target_layout="NHWC")`.

```sh
from openvino.tools.mo import convert_model, LayoutMap

ov_model = convert_model(model, layout=LayoutMap("NCHW", "NHWC"))
```

0 comments on commit 756b0de

Please sign in to comment.