Skip to content

Commit

Permalink
206, 207 Python API convert_model() used instead of console MO (ope…
Browse files Browse the repository at this point in the history
…nvinotoolkit#935)

* 206 mo Python API used

Signed-off-by: igor-davidyuk <[email protected]>

* add model convert info message

Signed-off-by: igor-davidyuk <[email protected]>

* 207 use MO python API

Signed-off-by: igor-davidyuk <[email protected]>

* Update notebooks/207-vision-paddlegan-superresolution/207-vision-paddlegan-superresolution.ipynb

* add links to MO API docs

Signed-off-by: igor-davidyuk <[email protected]>

---------

Signed-off-by: igor-davidyuk <[email protected]>
  • Loading branch information
igor-davidyuk authored Mar 20, 2023
1 parent 5311789 commit bc033ea
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install ppgan --no-deps\n",
"!pip install \"imageio==2.9.0\" \"imageio-ffmpeg\" \"numba>=0.53.1\" \"easydict\" \"munch\" \"natsort\""
"!pip install \"imageio==2.9.0\" \"imageio-ffmpeg\" \"numba>=0.53.1\" \"easydict\" \"munch\" \"natsort\"\n",
"!pip install ppgan --no-deps"
]
},
{
Expand Down Expand Up @@ -345,23 +345,36 @@
]
},
{
"attachments": {},
"cell_type": "markdown",
"id": "41c3cd0b",
"id": "63ecd375",
"metadata": {},
"source": [
"**Convert Model to OpenVINO IR with Model Optimizer**"
"**Convert ONNX Model to OpenVINO IR with [Model Optimizer Python API](https://docs.openvino.ai/latest/openvino_docs_MO_DG_Python_API.html)**"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81a64832",
"id": "21e58ce1",
"metadata": {},
"outputs": [],
"source": [
"onnx_path = model_path.with_suffix(\".onnx\")\n",
"from openvino.tools import mo\n",
"from openvino.runtime import serialize\n",
"\n",
"print(\"Exporting ONNX model to OpenVINO IR... This may take a few minutes.\")\n",
"!mo --input_model $onnx_path --output_dir $MODEL_DIR --input_shape \"[1,3,$target_height,$target_width]\" --model_name $MODEL_NAME --compress_to_fp16 --mean_values=\"[127.5,127.5,127.5]\" --scale_values=\"[127.5,127.5,127.5]\" --log_level \"CRITICAL\""
"\n",
"model = mo.convert_model(\n",
" onnx_path,\n",
" input_shape=[1, 3, target_height, target_width],\n",
" mean_values=[127.5,127.5,127.5],\n",
" scale_values=[127.5,127.5,127.5],\n",
" compress_to_fp16=True\n",
")\n",
"\n",
"# Serialize model in IR format\n",
"serialize(model, str(ir_path))"
]
},
{
Expand Down Expand Up @@ -628,7 +641,7 @@
"hash": "ae617ccb002f72b3ab6d0069d721eac67ac2a969e83c083c4321cfcab0437cd1"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -642,7 +655,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.6"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
"metadata": {},
"outputs": [],
"source": [
"!pip install ppgan --no-deps\n",
"!pip install \"imageio==2.9.0\" \"imageio-ffmpeg\" \"numba>=0.53.1\" \"easydict\" \"munch\" \"natsort\""
"!pip install \"imageio==2.9.0\" \"imageio-ffmpeg\" \"numba>=0.53.1\" \"easydict\" \"munch\" \"natsort\"\n",
"!pip install ppgan --no-deps"
]
},
{
Expand Down Expand Up @@ -285,7 +285,7 @@
"id": "93f8c13a",
"metadata": {},
"source": [
"### Convert ONNX Model to OpenVINO IR"
"### Convert ONNX Model to OpenVINO IR with [Model Optimizer Python API](https://docs.openvino.ai/latest/openvino_docs_MO_DG_Python_API.html)"
]
},
{
Expand All @@ -297,22 +297,30 @@
},
"outputs": [],
"source": [
"from openvino.tools import mo\n",
"from openvino.runtime import serialize\n",
"\n",
"## Uncomment the command below to show Model Optimizer help, which shows the possible arguments for Model Optimizer.\n",
"# ! mo --help"
"# mo.convert_model(help=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ed6cc19f-4b8c-4754-8473-1d905b89d476",
"metadata": {
"tags": []
},
"id": "af0d8b16",
"metadata": {},
"outputs": [],
"source": [
"if not ir_path.exists():\n",
" print(\"Exporting ONNX model to OpenVINO IR... This may take a few minutes.\")\n",
" ! mo --input_model $onnx_path --input_shape \"[1,3,299,299]\" --model_name $MODEL_NAME --output_dir \"$MODEL_DIR\" --compress_to_fp16 --log_level \"CRITICAL\""
"print(\"Exporting ONNX model to OpenVINO IR... This may take a few minutes.\")\n",
"\n",
"model = mo.convert_model(\n",
" onnx_path,\n",
" input_shape=[1,3,299,299],\n",
" compress_to_fp16=True\n",
")\n",
"\n",
"# Serialize model in IR format\n",
"serialize(model, str(ir_path))"
]
},
{
Expand Down Expand Up @@ -342,6 +350,7 @@
"source": [
"# Read the network and get input and output names.\n",
"ie = Core()\n",
"# Alternatively, the model obtained from `mo.convert_model()` may be used here\n",
"model = ie.read_model(model=ir_path)\n",
"input_layer = model.input(0)"
]
Expand Down Expand Up @@ -549,7 +558,7 @@
"hash": "bb0b397daf458ed78ef5a7e21732498aa92824cb15d3098f5341da903a887e15"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -563,7 +572,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
"version": "3.10.6"
}
},
"nbformat": 4,
Expand Down

0 comments on commit bc033ea

Please sign in to comment.