From f756d55dc6bb009727a7f054e4397ae7f6d55efa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ko=C5=BCykowski?= Date: Fri, 11 Mar 2022 19:19:16 +0100 Subject: [PATCH] Snippets for preprocessing migration page (#10917) * update preprocessing snippets * add missing file --- .../migration_ov_2_0/preprocessing.md | 155 +++++++++++++++++- docs/snippets/ov_preprocessing_migration.py | 107 ++++++++++++ 2 files changed, 254 insertions(+), 8 deletions(-) create mode 100644 docs/snippets/ov_preprocessing_migration.py diff --git a/docs/OV_Runtime_UG/migration_ov_2_0/preprocessing.md b/docs/OV_Runtime_UG/migration_ov_2_0/preprocessing.md index 9288249dbe9..a860ac261f6 100644 --- a/docs/OV_Runtime_UG/migration_ov_2_0/preprocessing.md +++ b/docs/OV_Runtime_UG/migration_ov_2_0/preprocessing.md @@ -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) diff --git a/docs/snippets/ov_preprocessing_migration.py b/docs/snippets/ov_preprocessing_migration.py new file mode 100644 index 00000000000..123d9f9bc5a --- /dev/null +++ b/docs/snippets/ov_preprocessing_migration.py @@ -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]