Files
openvino/docs/OV_Runtime_UG/preprocessing_details.md
Mikhail Nosov f82533005b [OV2.0] Preprocessing documentation (#10451)
* [OV2.0] Preprocessing documentation - first draft

* Small update

* Added ov::Layout overview

* Fix code style

* Preprocessing details - ~50% done

* Corrected links

* Fixed comments, added more docs

* Minor updates

* Couple more links

* Fixed comments

* Remove 'future' link
2022-02-21 19:20:23 +03:00

11 KiB

Preprocessing API - details

Preprocessing capabilities

Addressing particular input/output

If your model has only one input, then simple ov::preprocess::PrePostProcessor::input() will get a reference to preprocessing builder for this input (tensor, steps, model):

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:input_1]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:input_1]

@endsphinxdirective

In general, when model has multiple inputs/outputs, each one can be addressed by tensor name

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:input_name]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:input_name]

@endsphinxdirective

Or by it's index

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:input_index]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:input_index]

@endsphinxdirective

C++ references:

  • ov::preprocess::InputTensorInfo
  • ov::preprocess::OutputTensorInfo
  • ov::preprocess::PrePostProcessor

Supported preprocessing operations

C++ references:

  • ov::preprocess::PreProcessSteps

Mean/Scale normalization

Typical data normalization includes 2 operations for each data item: subtract mean value and divide to standard deviation. This can be done with the following code:

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:mean_scale]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:mean_scale]

@endsphinxdirective

In Computer Vision area normalization is usually done separately for R, G, B values. To do this, layout with 'C' dimension shall be defined. Example:

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:mean_scale_array]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:mean_scale_array]

@endsphinxdirective

C++ references:

  • ov::preprocess::PreProcessSteps::mean()
  • ov::preprocess::PreProcessSteps::scale()

Convert precision

In Computer Vision, image is represented by array of unsigned 8-but integer values (for each color), but model accepts floating point tensors

To integrate precision conversion into execution graph as a preprocessing step, just do:

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:convert_element_type]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:convert_element_type]

@endsphinxdirective

C++ references:

  • ov::preprocess::InputTensorInfo::set_element_type()
  • ov::preprocess::PreProcessSteps::convert_element_type()

Convert layout (transpose)

Transposing of matrices/tensors is a typical operation in Deep Learning - you may have a BMP image 640x480 which is an array of {480, 640, 3} elements, but Deep Learning model can require input with shape {1, 3, 480, 640}

Using layout of user's tensor and layout of original model conversion can be done implicitly

@sphinxdirective .. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:convert_layout]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:convert_layout]

@endsphinxdirective

Or if you prefer manual transpose of axes without usage of layout in your code, just do:

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:convert_layout_2]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:convert_layout_2]

@endsphinxdirective

It performs the same transpose, but we believe that approach using source and destination layout can be easier to read and understand

C++ references:

  • ov::preprocess::PreProcessSteps::convert_layout()
  • ov::preprocess::InputTensorInfo::set_layout()
  • ov::preprocess::InputModelInfo::set_layout()
  • ov::Layout

Resize image

Resizing of image is a typical preprocessing step for computer vision tasks. With preprocessing API this step can also be integrated into execution graph and performed on target device.

To resize the input image, it is needed to define H and W dimensions of layout

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:resize_1]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:resize_1]

@endsphinxdirective

Or in case if original model has known spatial dimensions (widht+height), target width/height can be omitted

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:resize_2]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:resize_2]

@endsphinxdirective

C++ references:

  • ov::preprocess::PreProcessSteps::resize()
  • ov::preprocess::ResizeAlgorithm

Color conversion

Typical use case is to reverse color channels from RGB to BGR and wise versa. To do this, specify source color format in tensor section and perform convert_color preprocessing operation. In example below, user has BGR image and needs to convert it to RGB as required for model's input

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:convert_color_1]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:convert_color_1]

@endsphinxdirective

Color conversion - NV12/I420

Preprocessing also support YUV-family source color formats, i.e. NV12 and I420. In advanced cases such YUV images can be splitted into separate planes, e.g. for NV12 images Y-component may come from one source and UV-component comes from another source. Concatenating such components in user's application manually is not a perfect solution from performance and device utilization perspectives, so there is a way to use Preprocessing API. For such cases there is NV12_TWO_PLANES and I420_THREE_PLANES source color formats, which will split original input to 2 or 3 inputs

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:convert_color_2]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:convert_color_2]

@endsphinxdirective

In this example, original input is being split to input/y and input/uv inputs. You can fill input/y from one source, and input/uv from another source. Color conversion to RGB will be performed using these sources, it is more optimal as there will be no additional copies of NV12 buffers.

C++ references:

  • ov::preprocess::ColorFormat
  • ov::preprocess::PreProcessSteps::convert_color

Custom operations

Preprocessing API also allows adding custom preprocessing steps into execution graph. Custom step is a function which accepts current 'input' node and returns new node after adding preprocessing step

Note: Custom preprocessing function shall only insert node(s) after input, it will be done during model compilation. This function will NOT be called during execution phase. This may look not trivial and require some knowledge of OpenVINO™ operations

If there is a need to insert some additional operations to execution graph right after input, like some specific crops and/or resizes - Preprocessing API can be a good choice to implement this

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:custom]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:custom]

@endsphinxdirective

C++ references:

Postprocessing

Postprocessing steps can be added to model outputs. As for preprocessing, these steps will be also integrated into graph and executed on selected device.

Preprocessing uses flow User tensor -> Steps -> Model input

Postprocessing is wise versa: Model output -> Steps -> User tensor

Comparing to preprocessing, there is not so much operations needed to do in post-processing stage, so right now only following postprocessing operations are supported:

  • Convert layout
  • Convert element type
  • Custom operations

Usage of these operations is similar to Preprocessing. Some example is shown below:

@sphinxdirective

.. tab:: C++

  .. doxygensnippet:: docs/snippets/ov_preprocessing.cpp
     :language: cpp
     :fragment: [ov:preprocess:postprocess]

.. tab:: Python

  .. doxygensnippet:: docs/snippets/ov_preprocessing.py
     :language: python
     :fragment: [ov:preprocess:postprocess]

@endsphinxdirective

C++ references:

  • ov::preprocess::PostProcessSteps
  • ov::preprocess::OutputModelInfo
  • ov::preprocess::OutputTensorInfo