用OpenVINO进行推理,通常需要以下几个步骤
文章目录
官网有详细的介绍,这里只是搬运工
https://docs.openvino.ai/latest/openvino_2_0_inference_pipeline.html
一、创建一个Core对象
创建一个openvino的初始化引擎
import openvino.runtime as ov
core = ov.Core()
1.temp 加载拓展算子(可选)
加载自定义的op算子,强烈推荐使用OpenVINO的拓展接口来写自定义的算子
core.add_extension("path_to_extension_library.so")
二、读取模型
这里只是从本地磁盘上读取模型,还没有对模型进行编译
model = core.read_model("model.xml")
三、从设备上加载模型
二中只是读取模型文件,还没有编译,所以需要从设备上编译加载模型
compiled_model = core.compile_model(model, "CPU")
3.1 查看模型的输入和输出(可选)
for i in range(len(compiled_model.inputs)):
print(compiled_model.inputs[i].names, compiled_model.inputs[i].shape)
# print(compiled_model.outputs[1].names, compiled_model.outputs[1].shape)
for i in range(len(compiled_model.outputs)):
print(compiled_model.outputs[i].names, compiled_model.outputs[i].shape)
四、创建推理请求
模型加载成功后,还需要创建推理请求,才可以执行推理
infer_request = compiled_model.create_infer_request()
五、输入数据,执行推理
5.1 通过tensor排序的下标获取tensor
input_tensor1 = infer_request.get_input_tensor(0)
print(input_tensor1)
# assert input_tensor1.data.dtype == np.int64
5.2 也可通过tensor name获取tensor
这里因为用的网络只有一个输入,且输入的节点名为 inputs。所以5.2中的tensor与5.1中的tensor是一样的
# 因此,两种方法获取的tensor应该是相等的
input_tensor2 = infer_request.get_tensor("inputs")
print(input_tensor2)
assert input_tensor2 != input_tensor1, "两个Tensor不相等"
print("-------------------------------------")
5.3 opencv读取数据,进行预处理等操作
# 6 读取数据,预处理为float32的格式,转换通道等
images = cv2.imread("./mobilenetv3_FP32/0.bmp")
images = cv2.resize(images, (224, 224))
images = images.transpose((2, 0, 1))
images = np.array(images, np.float32)
x = []
# 因为网络的batchsize是24,所以需要扩展24下
for i in range(24):
x.append(images)
x = np.array(x)
六、 执行推理,
获取结果的方式有两种,一种是直接通过返回值获取
还有一种是通过infer_request.get_output_tensor() 获取
# 获取结果方式1:直接通过返回值, res是一个dict
res = infer_request.infer(inputs={"inputs": x})
for key in res.keys():
print(res[key].shape)
# 6.2 获取结果方式2:通过get_output_tensor()
output_tensor = infer_request.get_output_tensor()
print(output_tensor.data.shape)
print("hello world")
七、完整代码示例
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
* * *** * * * *
* * * ** * *
**** * ** * *
* * * ** * *
* * ** * * ****
@File : ClassifierOpenVINO.py
@Date : 2022/10/9/009
@Require :
@Author : https://blog.csdn.net/hjxu2016
@Funtion : 基于分类网络,走通openvino推理推理流程
"""
import openvino.runtime as ov
import cv2
import numpy as np
if __name__ == "__main__":
# 1、创建openvino初始化的引擎
core = ov.Core()
# 1-temp、加载自定义的op算子,强烈推荐使用OpenVINO的拓展接口来写自定义的算子
# 通常用不到
# core.add_extension("path_to_extension_library.so")
# 2、从本地磁盘上读取模型
model_path = "./mobilenetv3_FP32/mobilenetv3.xml"
model = core.read_model(model_path)
# 3.1 从设备上,加载模型
# 1.2中只是读取模型文件,还没有编译,所以需要经过编译加载模型
compiled_model = core.compile_model(model, "CPU")
print(compiled_model)
print("-------------------------------------")
for i in range(len(compiled_model.inputs)):
print(compiled_model.inputs[i].names, compiled_model.inputs[i].shape)
# print(compiled_model.outputs[1].names, compiled_model.outputs[1].shape)
for i in range(len(compiled_model.outputs)):
print(compiled_model.outputs[i].names, compiled_model.outputs[i].shape)
print("-------------------------------------")
# 4. 创建推理请求
infer_request = compiled_model.create_infer_request()
# 5.填充数据
# 5.1 通过tensor排序的下标获取tensor
input_tensor1 = infer_request.get_input_tensor(0)
print(input_tensor1)
# assert input_tensor1.data.dtype == np.int64
# 5.2 也可通过tensor name获取tensor,因为用的网络只有一个输入,且输入的节点名为 inputs
# 因此,两种方法获取的tensor应该是相等的
input_tensor2 = infer_request.get_tensor("inputs")
print(input_tensor2)
assert input_tensor2 != input_tensor1, "两个Tensor不相等"
print("-------------------------------------")
# 6 读取数据,预处理为float32的格式,转换通道等
images = cv2.imread("./mobilenetv3_FP32/0.bmp")
images = cv2.resize(images, (224, 224))
images = images.transpose((2, 0, 1))
images = np.array(images, np.float32)
x = []
# 因为网络的batchsize是24,所以需要扩展24下
for i in range(24):
x.append(images)
x = np.array(x)
print("-------------------------------------")
# 6.1 执行推理,获取结果方式1:直接通过返回值, res是一个dict
res = infer_request.infer(inputs={"inputs": x})
for key in res.keys():
print(res[key].shape)
# 6.2 获取结果方式2:通过get_output_tensor()
output_tensor = infer_request.get_output_tensor()
print(output_tensor.data.shape)
print("hello world")
End、Reference:
https://docs.openvino.ai/latest/openvino_2_0_inference_pipeline.html