2022-04-01 15:06:55 +03:00
# Use Case - Integrate and Save Preprocessing Steps Into IR {#openvino_docs_OV_UG_Preprocess_Usecase_save}
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
@sphinxdirective
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
Previous sections covered the topic of the :doc:`preprocessing steps <openvino_docs_OV_UG_Preprocessing_Details>`
and the overview of :doc:`Layout <openvino_docs_OV_UG_Layout_Overview>` API.
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
For many applications, it is also important to minimize read/load time of a model.
Therefore, performing integration of preprocessing steps every time on application
startup, after ``ov::runtime::Core::read_model``, may seem inconvenient. In such cases,
once pre and postprocessing steps have been added, it can be useful to store new execution
model to OpenVINO Intermediate Representation (OpenVINO IR, ` .xml` format).
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
Most available preprocessing steps can also be performed via command-line options,
using Model Optimizer. For details on such command-line options, refer to the
:doc:` Optimizing Preprocessing Computation <openvino_docs_MO_DG_Additional_Optimization_Use_Cases>`.
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
Code example - Saving Model with Preprocessing to OpenVINO IR
#############################################################
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
When some preprocessing steps cannot be integrated into the execution graph using
Model Optimizer command-line options (for example, ``YUV``->``RGB`` color space conversion,
``Resize``, etc.), it is possible to write a simple code which:
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
* Reads the original model (OpenVINO IR, TensorFlow, ONNX, PaddlePaddle).
* Adds the preprocessing/postprocessing steps.
* Saves resulting model as IR (``.xml`` and ``.bin``).
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
Consider the example, where an original ONNX model takes one ``float32`` input with the
``{1, 3, 224, 224}`` shape, the ``RGB`` channel order, and mean/scale values applied.
In contrast, the application provides ``BGR`` image buffer with a non-fixed size and
input images as batches of two. Below is the model conversion code that can be applied
in the model preparation script for such a case.
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
* Includes / Imports
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. tab-set::
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. tab-item:: C++
:sync: cpp
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
:language: cpp
:fragment: ov:preprocess:save_headers
2022-03-24 22:27:29 +03:00
2023-03-14 09:31:44 +01:00
.. tab-item:: Python
:sync: py
2022-03-24 22:27:29 +03:00
2023-03-14 09:31:44 +01:00
.. doxygensnippet:: docs/snippets/ov_preprocessing.py
:language: Python
:fragment: ov:preprocess:save_headers
2022-03-24 22:27:29 +03:00
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
* Preprocessing & Saving to the OpenVINO IR code.
2022-03-14 12:12:20 +03:00
2022-03-24 22:27:29 +03:00
2023-03-14 09:31:44 +01:00
.. tab-set::
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. tab-item:: C++
:sync: cpp
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
:language: cpp
:fragment: ov:preprocess:save
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. tab-item:: Python
:sync: py
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. doxygensnippet:: docs/snippets/ov_preprocessing.py
:language: Python
:fragment: ov:preprocess:save
2022-03-24 22:27:29 +03:00
2023-03-14 09:31:44 +01:00
Application Code - Load Model to Target Device
##############################################
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
After this, the application code can load a saved file and stop preprocessing. In this case, enable
:doc:` model caching <openvino_docs_OV_UG_Model_caching_overview>` to minimize load
time when the cached model is available.
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. tab-set::
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. tab-item:: C++
:sync: cpp
2022-03-24 22:27:29 +03:00
2023-03-14 09:31:44 +01:00
.. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
:language: cpp
:fragment: ov:preprocess:save_load
2022-03-24 22:27:29 +03:00
2023-03-14 09:31:44 +01:00
.. tab-item:: Python
:sync: py
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
.. doxygensnippet:: docs/snippets/ov_preprocessing.py
:language: Python
:fragment: ov:preprocess:save_load
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
Additional Resources
####################
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
* :doc:` Preprocessing Details <openvino_docs_OV_UG_Preprocessing_Details>`
* :doc:` Layout API overview <openvino_docs_OV_UG_Layout_Overview>`
* :doc:` Model Optimizer - Optimize Preprocessing Computation <openvino_docs_MO_DG_Additional_Optimization_Use_Cases>`
* :doc:` Model Caching Overview<openvino_docs_OV_UG_Model_caching_overview>`
* The ` ov::preprocess::PrePostProcessor <classov_1_1preprocess_1_1PrePostProcessor.html#doxid -classov-1-1preprocess-1-1-pre-post-processor>` C++ class documentation
* The ` ov::pass::Serialize <classov_1_1pass_1_1Serialize.html#doxid -classov-1-1pass-1-1-serialize>` - pass to serialize model to XML/BIN
* The ` ov::set_batch <namespaceov.html#doxid -namespaceov-1a3314e2ff91fcc9ffec05b1a77c37862b>` - update batch dimension for a given model
2022-03-14 12:12:20 +03:00
2023-03-14 09:31:44 +01:00
@endsphinxdirective