OpenVINO 2022.3之七:OpenVINO 预处理API提升模型推理性能

OpenVINO™ 2022.3 提供OpenVINO™ Runtime原生的用于数据预处理的API函数。
如果没有预处理API,那么输入数据的预处理操作只能放在CPU上实现,CPU完成数据预处理后,再将预处理后的数据传给iGPU、VPU等AI加速计算设备进行推理计算。
有了预处理API后,就能将预处理操作集成到在模型执行图中,这样iGPU、VPU 或即将发布的Intel独立显卡都能进行数据预处理,无需依赖CPU,提高了执行效率。

常见数据预处理的操作有:

Mean/Scale Normalization

c++:

// Suppose model's shape is {1, 3, 224, 224}
ppp.input("input").model().set_layout("NCHW"); // N=1, C=3, H=224, W=224
// Mean/Scale has 3 values which matches with C=3
ppp.input("input").preprocess().mean({103.94f, 116.78f, 123.68f}).scale({57.21f, 57.45f, 57.73f});

python:

# Suppose model's shape is {1, 3, 224, 224}
# N=1, C=3, H=224, W=224
ppp.input('input').model().set_layout(Layout('NCHW'))
# Mean/Scale has 3 values which matches with C=3
ppp.input('input').preprocess() \.mean([103.94, 116.78, 123.68]).scale([57.21, 57.45, 57.73])

Converting Precision

c++:

// First define data type for your tensor
ppp.input("input").tensor().set_element_type(ov::element::u8);// Then define preprocessing step
ppp.input("input").preprocess().convert_element_type(ov::element::f32);// If conversion is needed to `model's` element type, 'f32' can be omitted
ppp.input("input").preprocess().convert_element_type();

python:

# First define data type for your tensor
ppp.input('input').tensor().set_element_type(Type.u8)# Then define preprocessing step
ppp.input('input').preprocess().convert_element_type(Type.f32)# If conversion is needed to `model's` element type, 'f32' can be omitted
ppp.input('input').preprocess().convert_element_type()

Converting layout (transposing)

c++:

// First define layout for your tensor
ppp.input("input").tensor().set_layout("NHWC");// Then define layout of model
ppp.input("input").model().set_layout("NCHW");std::cout << ppp; // Will print 'implicit layout conversion step'

python:

# First define layout for your tensor
ppp.input('input').tensor().set_layout(Layout('NHWC'))# Then define layout of model
ppp.input('input').model().set_layout(Layout('NCHW'))print(ppp)  # Will print 'implicit layout conversion step'

Resizing Image

c++:

ppp.input("input").tensor().set_shape({1, 3, 960, 1280});
ppp.input("input").model().set_layout("??HW");
ppp.input("input").preprocess().resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR, 480, 640);

python:

ppp.input('input').tensor().set_shape([1, 3, 960, 1280])
ppp.input('input').model().set_layout(Layout('??HW'))
ppp.input('input').preprocess()\.resize(ResizeAlgorithm.RESIZE_LINEAR, 480, 640)

Color Conversion

c++:

ppp.input("input").tensor().set_color_format(ov::preprocess::ColorFormat::BGR);
ppp.input("input").preprocess().convert_color(ov::preprocess::ColorFormat::RGB);

python:

ppp.input('input').tensor().set_color_format(ColorFormat.BGR)
ppp.input('input').preprocess().convert_color(ColorFormat.RGB)

Color Conversion - NV12/I420

c++:

// This will split original `input` to 2 separate inputs: `input/y' and 'input/uv'
ppp.input("input").tensor().set_color_format(ov::preprocess::ColorFormat::NV12_TWO_PLANES);
ppp.input("input").preprocess().convert_color(ov::preprocess::ColorFormat::RGB);
std::cout << ppp;  // Dump preprocessing steps to see what will happen

python:

# This will split original `input` to 2 separate inputs: `input/y' and 'input/uv'
ppp.input('input').tensor()\.set_color_format(ColorFormat.NV12_TWO_PLANES)ppp.input('input').preprocess()\.convert_color(ColorFormat.RGB)
print(ppp)  # Dump preprocessing steps to see what will happen

示例:

  • 改变输入数据的形状:[720, 1280,3] → [1, 3, 640, 640]

  • 改变输入数据的精度:U8 → f32

  • 改变输入数据的颜色通道顺序:BGR → RGB

  • 改变输入数据的布局(layout):HWC → NCHW

  • 归一化数据:减去均值(mean),除以标准差(std)

C++代码:

#include <openvino/runtime/core.hpp>
#include <openvino/core/preprocess/pre_post_process.hpp>
#include <openvino/pass/serialize.hpp>// ========  Step 0: read original model =========
ov::Core core;
std::shared_ptr<ov::Model> model = core.read_model("/path/to/some_model.xml");// ======== Step 1: Preprocessing ================
ov::preprocess::PrePostProcessor prep(model);
// Declare section of desired application's input format
prep.input().tensor().set_element_type(ov::element::u8).set_layout("NHWC").set_color_format(ov::preprocess::ColorFormat::BGR).set_spatial_dynamic_shape();
// Specify actual model layout
prep.input().model().set_layout("NCHW");
// Explicit preprocessing steps. Layout conversion will be done automatically as last step
prep.input().preprocess().convert_element_type().convert_color(ov::preprocess::ColorFormat::RGB).resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR).mean({123.675f, 116.28f, 103.53f}) // Subtract mean after color conversion.scale({58.624f, 57.12f, 57.375f});
// Dump preprocessor
std::cout << "Preprocessor: " << prep << std::endl;
model = prep.build();// ======== Step 2: Change batch size ================
// In this example we also want to change batch size to increase throughput
ov::set_batch(model, 2);// ======== Step 3: Save the model ================
std::string xml = "/path/to/some_model_saved.xml";
std::string bin = "/path/to/some_model_saved.bin";
ov::serialize(model, xml, bin);

Python代码:

from openvino.preprocess import PrePostProcessor, ColorFormat, ResizeAlgorithm
from openvino.runtime import Core, Layout, Type, set_batch
# First method - imports
from openvino.runtime import serialize
# Second method - imports
from openvino.runtime.passes import Manager, Serialize# ========  Step 0: read original model =========
core = Core()
model = core.read_model(model='/path/to/some_model.xml')# ======== Step 1: Preprocessing ================
ppp = PrePostProcessor(model)
# Declare section of desired application's input format
ppp.input().tensor() \.set_element_type(Type.u8) \.set_spatial_dynamic_shape() \.set_layout(Layout('NHWC')) \.set_color_format(ColorFormat.BGR)# Specify actual model layout
ppp.input().model().set_layout(Layout('NCHW'))# Explicit preprocessing steps. Layout conversion will be done automatically as last step
ppp.input().preprocess() \.convert_element_type() \.convert_color(ColorFormat.RGB) \.resize(ResizeAlgorithm.RESIZE_LINEAR) \.mean([123.675, 116.28, 103.53]) \.scale([58.624, 57.12, 57.375])# Dump preprocessor
print(f'Dump preprocessor: {ppp}')
model = ppp.build()# ======== Step 2: Change batch size ================
# In this example we also want to change batch size to increase throughput
set_batch(model, 2)# ======== Step 3: Save the model ================
# First method - using serialize runtime wrapper
serialize(model, '/path/to/some_model_saved.xml', '/path/to/some_model_saved.bin')# Second method - using Manager and Serialize pass
manager = Manager()
manager.register_pass(Serialize('/path/to/some_model_saved.xml', '/path/to/some_model_saved.bin'))
manager.run_passes(model)

实战:

Opencv代码:

std::vector<float> paddings(3);       //scale, half_h, half_w
cv::Mat resized_img = letterbox(img, paddings); //resize to (640,640) by letterbox
// BGR->RGB, u8(0-255)->f32(0.0-1.0), HWC->NCHW
cv::Mat blob = cv::dnn::blobFromImage(resized_img, 1 / 255.0, cv::Size(640, 640), cv::Scalar(0, 0, 0), true);

OpenVINO Runtime API代码:

 // Step 3. Inizialize Preprocessing for the modelov::preprocess::PrePostProcessor ppp = ov::preprocess::PrePostProcessor(model);// Specify input image formatppp.input().tensor().set_element_type(ov::element::u8).set_layout("NHWC").set_color_format(ov::preprocess::ColorFormat::BGR);// Specify preprocess pipeline to input image without resizingppp.input().preprocess().convert_element_type(ov::element::f32).convert_color(ov::preprocess::ColorFormat::RGB).scale({ 255., 255., 255. });//  Specify model's input layoutppp.input().model().set_layout("NCHW");// Specify output results formatppp.output().tensor().set_element_type(ov::element::f32);// Embed above steps in the graphmodel = ppp.build();compiled_model = core.compile_model(model, "CPU");

速度对比能加速 10~20% 左右

OpenVINO 2022.3之七:OpenVINO 预处理API提升模型推理性能相关推荐

  1. 抓住训练集中真正有用的样本,提升模型整体性能!

    文 | Severus 编 | 小戏 在任务中寻找到真正有用的训练样本,可以说一直是机器学习研究者们共同的诉求.毕竟,找到了真正有用的训练样本,排除掉训练样本中的杂质,无论最终是提升训练模型的效率,还 ...

  2. 论文解读丨【CVPR 2022】不使用人工标注提升文字识别器性能

    摘要:本文提出了一种针对文字识别的半监督方法.区别于常见的半监督方法,本文的针对文字识别这类序列识别问题做出了特定的设计. 本文分享自华为云社区<[CVPR 2022] 不使用人工标注提升文字识 ...

  3. python工具方法35 实现SWA,再一次提升模型的性能

    SWA是论文Averaging Weights Leads to Wider Optima and Better Generalization所提出的一种无痛涨点的方式,只需要在模型训练的最后阶段保存 ...

  4. Python机器学习实战:掌握这四个特征选择方法,提升模型预测性能

    机器学习实战:这里没有艰深晦涩的数学理论,我们将用简单的案例和大量的示例代码,向大家介绍机器学习的核心概念.我们的目标是教会大家用Python构建机器学习模型,解决现实世界的难题. 当数据集包含很多特 ...

  5. pytorch 半精度,提升pytorch推理性能

    原生的torch是float32的,我们可以借鉴模型量化的思想将其变成float16,而且pytorch自身就定义了半精度的tensor 假设我训练的一个模型为model,我们在运算的时候直接将模型转 ...

  6. 数据异质性会影响深度学习变化检测模型的迁移能力,请列出提升模型迁移性的解决思路...

    数据异质性会导致深度学习变化检测模型的迁移能力降低.可以采用以下解决思路来提升模型的迁移性: 数据预处理: 对于不同类型的数据进行标准化处理,使得模型能够更好的适应不同的数据类型. 模型正则化: 通过 ...

  7. 模型调参:分步骤的提升模型的精度(cnn)

    机器学习AI算法工程 前天 机器学习AI算法工程  公众号: datayx https://mp.weixin.qq.com/s/Eu31_8E29msmwSmjiI57QQ 一.问题描述 当我们在处 ...

  8. 使用数据增强技术提升模型泛化能力

    在<提高模型性能,你可以尝试这几招...>一文中,我们给出了几种提高模型性能的方法,但这篇文章是在训练数据集不变的前提下提出的优化方案.其实对于深度学习而言,数据量的多寡通常对模型性能的影 ...

  9. 【OpenVINO】OpenVINO 2022.1 安装教程(Windows)

    OpenVINOTM2022.1 安装教程 Windows 1. OpenVINOTM介绍 2.OpenVINOTMTM 安装环境和安装特性介绍 2.1OpenVINOTMTM 安装环境 2.2 Op ...

最新文章

  1. 数据库原理与设计 P75作业 学号2013211466 班级0401302
  2. 应用层下的人脸识别(二):人脸库
  3. cocos2dx-Lua与Object的通讯机制
  4. 这些Android高级必会知识点你能答出来几个?含BATJM大厂
  5. 在线学习新编程 技巧全攻略
  6. 信号回勾产生的原因_燃气减压阀振动的原因及处理方案
  7. 重温C++之“strcpy_s与strcpy的比较”
  8. javax.servlet.http.HttpServlet was not found
  9. PostgreSQL环境变量
  10. 矩阵分析与应用学习总结目录
  11. 激活win10专业版
  12. SpringBoot爬虫
  13. 还有什么软件可以测试苹果真假,哪个软件可以检测苹果6s手机的真假
  14. WeLink互动直播:维护网课秩序,杜绝外人乱入
  15. matlab光学远轴光的折射,摄影光学基础知识-光的折射定律
  16. 《Total Commander:万能文件管理器》——12.6. 附录
  17. Titan数据库简介
  18. CVPR 2020 开幕!最佳论文奖等揭晓!
  19. Unity3D开发之折线图制作
  20. LTE网络中PDN,承载,IP的关系

热门文章

  1. 学习SEO过程中常遇见的问题
  2. java hashmap api_JAVA基础学习-集合三-Map、HashMap,TreeMap与常用API
  3. win10卸载git_打造 Win10 终极开发环境
  4. 【生活工作经验 六】招聘与应聘相关结论
  5. level2行情接口到底有没有必要用?
  6. 苹果x重启方法_iOS 13.4.1 Linux 简易越狱,重启就能打开
  7. springboot 整合 apache camel实现企业级数据集成和处理
  8. [绍棠] 应用内支付(IAP)详解
  9. Python爬取百度地图智慧交通-城市拥堵指数
  10. PHP开发API接口注意事项