Files
openvino/docs/snippets/ov_preprocessing_migration.py
Anastasia Kuporosova 2bf8d910f6 [Docs][PyOV] update python snippets (#19367)
* [Docs][PyOV] update python snippets

* first snippet

* Fix samples debug

* Fix linter

* part1

* Fix speech sample

* update model state snippet

* add serialize

* add temp dir

* CPU snippets update (#134)

* snippets CPU 1/6

* snippets CPU 2/6

* snippets CPU 3/6

* snippets CPU 4/6

* snippets CPU 5/6

* snippets CPU 6/6

* make  module TODO: REMEMBER ABOUT EXPORTING PYTONPATH ON CIs ETC

* Add static model creation in snippets for CPU

* export_comp_model done

* leftovers

* apply comments

* apply comments -- properties

* small fixes

* rempve debug info

* return IENetwork instead of Function

* apply comments

* revert precision change in common snippets

* update opset

* [PyOV] Edit docs for the rest of plugins (#136)

* modify main.py

* GNA snippets

* GPU snippets

* AUTO snippets

* MULTI snippets

* HETERO snippets

* Added properties

* update gna

* more samples

* Update docs/OV_Runtime_UG/model_state_intro.md

* Update docs/OV_Runtime_UG/model_state_intro.md

* attempt1 fix ci

* new approach to test

* temporary remove some files from run

* revert cmake changes

* fix ci

* fix snippet

* fix py_exclusive snippet

* fix preprocessing snippet

* clean-up main

* remove numpy installation in gha

* check for GPU

* add logger

* iexclude main

* main update

* temp

* Temp2

* Temp2

* temp

* Revert temp

* add property execution devices

* hide output from samples

---------

Co-authored-by: p-wysocki <przemyslaw.wysocki@intel.com>
Co-authored-by: Jan Iwaszkiewicz <jan.iwaszkiewicz@intel.com>
Co-authored-by: Karol Blaszczak <karol.blaszczak@intel.com>
2023-09-13 21:05:24 +02:00

103 lines
3.0 KiB
Python

# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
from utils import get_model, get_ngraph_model
#! [ov_imports]
from openvino 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"
tensor_name="input"
core = Core()
model = get_model([1,32,32,3])
#! [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]
model = get_model()
#! [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]
model = get_model([1, 3, 448, 448])
#! [ov_image_scale]
ppp = PrePostProcessor(model)
input = ppp.input("input")
# 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]
import openvino.inference_engine as ie
import ngraph as ng
operation_name = "data"
core = ie.IECore()
network = get_ngraph_model()
#! [mean_scale]
preprocess_info = network.input_info[operation_name].preprocess_info
preprocess_info.init(3)
preprocess_info[0].mean_value = 116.78
preprocess_info[1].mean_value = 116.78
preprocess_info[2].mean_value = 116.78
preprocess_info[0].std_scale = 57.21
preprocess_info[1].std_scale = 57.45
preprocess_info[2].std_scale = 57.73
preprocess_info.mean_variant = ie.MeanVariant.MEAN_VALUE
#! [mean_scale]
#! [conversions]
input_info = network.input_info[operation_name]
input_info.precision = "U8"
input_info.layout = "NHWC"
# model input layout is always NCHW in Inference Engine
# for shapes with 4 dimensions
#! [conversions]
#! [image_scale]
preprocess_info = network.input_info[operation_name].preprocess_info
# 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_info.resize_algorithm = ie.ResizeAlgorithm.RESIZE_BILINEAR