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

猜你喜欢

转载自blog.csdn.net/m0_37264397/article/details/127141178