2023.10.24 Openvino

本文详细介绍了在Windows环境下安装OpenVINODevelopment环境,包括创建虚拟环境、升级pip、安装OpenVINODev及其extras选项,以及如何使用ONNX模型进行推理,展示了模型输入输出调整和内存中模型加载的方法。
摘要由CSDN通过智能技术生成

 openvino官网

Get Started — OpenVINO™ documentationicon-default.png?t=N7T8https://docs.openvino.ai/2022.3/get_started.html

在windows下尝试安装(python):

1. 安装OpenVINO Development同时会安装OpenVINO Runtime作为依赖

python -m venv openvino_env    #创建虚拟环境
openvino_env\Scripts\activate  #激活虚拟环境
python -m pip install --upgrade pip  #更新pip,必须保证pip的版本最新

2. 安装合适的openvino-dev:

extras填这些caffe, kaldi, mxnet, onnx, pytorch, tensorflow, tensorflow2

pip install openvino-dev[extras]
#such as:pip install openvino-dev[tensorflow2,onnx]

3. 检查是否安装成功,没有成功会有相应的提示(成功会返回绿色的字)

mo -h

Started with OpenVINO:

#初始化with Core
from openvino.runtime import Core
ie = Core()

devices = ie.available_devices #获取可用设备{CPU, GPU0, GPU1等}
#get_property获取设备名称
for device in devices:
    device_name = ie.get_property(device, "FULL_DEVICE_NAME")
    print(f"{device}: {device_name}")

ONNX无需转换成ir,也可以直接运行:

from openvino.runtime import Core

ie = Core()
onnx_model_path = "model/segmentation.onnx"

model_onnx = ie.read_model(model=onnx_model_path)
compiled_model_onnx = ie.compile_model(model=model_onnx, device_name="CPU")
result = compiled_model_onnx(input_data)
###""若要转成ir,运行下面的代码即可""###
from openvino.runtime import serialize
serialize(model_onnx, xml_path="model/exported_onnx_model.xml")

查看输入名和输入格式:

from openvino.runtime import Core

ie = Core()
classification_model_xml = "model/classification.xml"
model = ie.read_model(model=classification_model_xml)
print(model.inputs)
#如果有多个输入,则model.inputs(index) index是期望输入层的索引值

开始推理:

from openvino.runtime import Core

ie = Core()
classification_model_xml = "model/classification.xml"
model = ie.read_model(model=classification_model_xml)
compiled_model = ie.compile_model(model=model, device_name="CPU")
input_layer = compiled_model.input(0)
output_layer = compiled_model.output(0)
#可用input_layer.shape获取输入的图片形状做适当调整
result = compiled_model(input_data)[output_layer]#单输入
#result = compiled_model([input_data])[output_layer]#多输入

模型可以改变输入输出:

new_shape = PartialShape([2, 3, 544, 544])
segmentation_model.reshape({segmentation_input_layer.any_name: new_shape})
segmentation_compiled_model = ie.compile_model(model=segmentation_model, device_name="CPU")

还有一个把模型放在内存中的,但是感觉用处不大。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值