Intel openvino(一)API使用

1、初始化

from openvino.runtime import Core

ie=Core()

2、查看设备型号(主要是Intel系列的CPU/GPU型号)

devices=ie.available_devices
print(devices)

for device in devices:
    device_name = ie.get_property(device, "FULL_DEVICE_NAME")
    print(f"{device}: {device_name}")

输出:

['CPU', 'GPU']
CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
GPU: Intel(R) UHD Graphics 630 (iGPU)

3、导出openvino模型

        OpenVINO IR(中间表示)模型由一个包含网络拓扑信息的.xml文件和一个包含权重和偏差二进制数据的.bin文件组成。

onnx_file="model/yolov5s.onnx"
onnx_model=ie.read_model(model=onnx_file)
complie_model_onnx=ie.compile_model(model=onnx_model,device_name="CPU")
from openvino.offline_transformations import serialize
serialize(model=onnx_model, model_path="model/yolov5s.xml", weights_path="model/yolov5s.bin")

生成xml和bin文件。

4、加载xml文件

xml文件:网络模型信息。

 

xml_file="model/yolov5s.xml"
model=ie.read_model(model=xml_file)
input_layer=model.input(0)
print("input_layer_name:",input_layer.any_name)
print(f"input precision: {input_layer.element_type}")
print(f"input shape: {input_layer.shape}")
output_layer=model.output(0)
print("output_layer_name:",output_layer.any_name)
print(f"output precision: {output_layer.element_type}")
print(f"output shape: {output_layer.shape}")

输出:

input_layer_name: images
input precision: <Type: 'float32'>
input shape: {1, 3, 640, 640}
output_layer_name: output
output precision: <Type: 'float32'>
output shape: {1, 25200, 85}

5、调整图像尺寸推理

onnx_file="model/yolov5s.onnx"
onnx_model=ie.read_model(model=onnx_file)
complie_model_onnx=ie.compile_model(model=onnx_model,device_name="CPU")

xml_file="model/yolov5s.xml"
model=ie.read_model(model=xml_file)
input_layer=model.input(0)
output_layer=model.output(0)

img=cv2.imread("data/bus.jpg")
print("img shape:",img.shape)

N, C, H, W = input_layer.shape
resized_image = cv2.resize(src=img, dsize=(W, H))
print("resize shape:",resized_image.shape)
input_data = np.expand_dims(np.transpose(resized_image, (2, 0, 1)), 0).astype(np.float32)
print("input shape:",input_data.shape)

request = complie_model_onnx.create_infer_request()
request.infer(inputs={input_layer.any_name: input_data})
result = request.get_output_tensor(output_layer.index).data
print("result shape:",result.shape)

输出:

img shape: (1080, 810, 3)
resize shape: (640, 640, 3)
input shape: (1, 3, 640, 640)
result shape: (1, 25200, 85)

参考:OpenVINO™ Runtime API Tutorial — OpenVINO™ documentation

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HySmiley

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值