Snippets for preprocessing migration page (#10917)

* update preprocessing snippets

* add missing file
This commit is contained in:
Dawid Kożykowski
2022-03-11 19:19:16 +01:00
committed by GitHub
parent 81ffb7a3bc
commit f756d55dc6
2 changed files with 254 additions and 8 deletions

View File

@@ -19,45 +19,184 @@ It's also important to mention that since OpenVINO 2.0, the Runtime API does not
The steps below demonstrates how to migrate preprocessing scenarios from Inference Engine API to OpenVINO Runtime API 2.0.
The snippets suppose we need to preprocess a model input with tensor name `tensor_name`, in Inferenece Engine API using operation names to address the data, it's called `operation_name`.
#### Importing preprocessing in Python
In order to utilize preprocessing following imports must be added.
Inference Engine API:
@sphinxdirective
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [imports]
@endsphinxdirective
OpenVINO Runtime API 2.0:
@sphinxdirective
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [ov_imports]
@endsphinxdirective
There are two different namespaces `runtime`, which contains OpenVINO Runtime API classes and `preprocess` which provides Preprocessing API.
### Mean and scale values
Inference Engine API:
@snippet docs/snippets/ov_preprocessing_migration.cpp mean_scale
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [mean_scale]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [mean_scale]
@endsphinxdirective
OpenVINO Runtime API 2.0:
@snippet docs/snippets/ov_preprocessing_migration.cpp ov_mean_scale
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [ov_mean_scale]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [ov_mean_scale]
@endsphinxdirective
### Precision and layout conversions
Inference Engine API:
@snippet docs/snippets/ov_preprocessing_migration.cpp conversions
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [conversions]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [conversions]
@endsphinxdirective
OpenVINO Runtime API 2.0:
@snippet docs/snippets/ov_preprocessing_migration.cpp ov_conversions
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [ov_conversions]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [ov_conversions]
@endsphinxdirective
### Image scaling
Inference Engine API:
@snippet docs/snippets/ov_preprocessing_migration.cpp image_scale
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [image_scale]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [image_scale]
@endsphinxdirective
OpenVINO Runtime API 2.0:
@snippet docs/snippets/ov_preprocessing_migration.cpp ov_image_scale
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [ov_image_scale]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [ov_image_scale]
@endsphinxdirective
### Color space conversions
Inference Engine API:
@snippet docs/snippets/ov_preprocessing_migration.cpp color_space
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [color_space]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [color_space]
@endsphinxdirective
OpenVINO Runtime API 2.0:
@snippet docs/snippets/ov_preprocessing_migration.cpp ov_color_space
@sphinxdirective
.. tab:: C++
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.cpp
:language: cpp
:fragment: [ov_color_space]
.. tab:: Python
.. doxygensnippet:: docs/snippets/ov_preprocessing_migration.py
:language: python
:fragment: [ov_color_space]
@endsphinxdirective
**See also:**
- [Preprocessing details](../preprocessing_details.md)

View File

@@ -0,0 +1,107 @@
# Copyright (C) 2018-2022 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
#! [ov_imports]
from openvino.runtime import Core, Layout, Type
from openvino.preprocess import ColorFormat, PrePostProcessor, ResizeAlgorithm
#! [ov_imports]
#! [imports]
import openvino.inference_engine as ie
#! [imports]
#include "inference_engine.hpp"
model_path = ''
tensor_name = ''
core = Core()
model = core.read_model(model=model_path)
#! [ov_mean_scale]
ppp = PrePostProcessor(model)
input = ppp.input(tensor_name)
# we only need to know where is C dimension
input.model().set_layout(Layout('...C'))
# specify scale and mean values, order of operations is important
input.preprocess().mean([116.78]).scale([57.21, 57.45, 57.73])
# insert preprocessing operations to the 'model'
model = ppp.build()
#! [ov_mean_scale]
#! [ov_conversions]
ppp = PrePostProcessor(model)
input = ppp.input(tensor_name)
input.tensor().set_layout(Layout('NCHW')).set_element_type(Type.u8)
input.model().set_layout(Layout('NCHW'))
# layout and precision conversion is inserted automatically,
# because tensor format != model input format
model = ppp.build()
#! [ov_conversions]
#! [ov_color_space]
ppp = PrePostProcessor(model)
input = ppp.input(tensor_name)
input.tensor().set_color_format(ColorFormat.NV12_TWO_PLANES)
# add NV12 to BGR conversion
input.preprocess().convert_color(ColorFormat.BGR)
# and insert operations to the model
model = ppp.build()
#! [ov_color_space]
#! [ov_image_scale]
ppp = PrePostProcessor(model)
input = ppp.input(tensor_name)
# need to specify H and W dimensions in model, others are not important
input.model().set_layout(Layout('??HW'))
# scale to model shape
input.preprocess().resize(ResizeAlgorithm.RESIZE_LINEAR, 448, 448)
# and insert operations to the model
model = ppp.build()
#! [ov_image_scale]
model_path = ''
operation_name = ''
core = Core()
network = core.ReadNetwork(model_path)
#! [mean_scale]
preProcess = network.getInputsInfo()[operation_name].getPreProcess()
preProcess.init(3)
preProcess[0].meanValue = 116.78
preProcess[1].meanValue = 116.78
preProcess[2].meanValue = 116.78
preProcess[0].stdScale = 57.21
preProcess[1].stdScale = 57.45
preProcess[2].stdScale = 57.73
preProcess.setVariant(ie.MEAN_VALUE)
#! [mean_scale]
#! [conversions]
inputInfo = network.getInputsInfo()[operation_name]
inputInfo.setPrecision(ie.Precision.U8)
inputInfo.setLayout(ie.Layout.NHWC)
# model input layout is always NCHW in Inference Engine
# for shapes with 4 dimensions
#! [conversions]
#! [color_space]
preProcess = network.getInputsInfo()[operation_name].getPreProcess()
# Inference Engine supposes NV12 as two inputs which need to be passed
# as InferenceEngine::NV12Blob composed of two Y and UV planes
preProcess.setColorFormat(ie.NV12)
#! [color_space]
#! [image_scale]
preProcess = network.getInputsInfo()[operation_name].getPreProcess()
# Inference Engine supposes input for resize is always in NCHW layout
# while for OpenVINO Runtime API 2.0 `H` and `W` dimensions must be specified
# Also, current code snippet supposed resize from dynamic shapes
preProcess.setResizeAlgorithm(ie.ResizeAlgorithm.RESIZE_BILINEAR)
#! [image_scale]