OpenVINO 转换并部署说明
1. OpenVINO优点
- 性能:OpenVINO 利用Intel CPU、集成和独立 GPU 以及 FPGA 的强大功能提供高性能推理。
- 支持异构执行:OpenVINO 提供 API,只需编写一次,即可在任何支持的Intel 硬件(CPU,GPU, FPGA, VPU 等)上部署。
- 模型优化器:OpenVINO 提供了一个模型优化器,可从PyTorch,TensorFlow,TensorFlow Lite、Keras、ONNX,PaddlePaddle 和 Caffe 等流行的深度学习框架导入、转换和优化模型。
- 易用性:工具包附带80 多本教程笔记本(包括YOLOv8 优化),教授工具包的不同方面。
2. 安装
使用pip安装所需的包。
pip install ultralytics
pip install openvino>=2024.0.0 -i Https://pypi.tuna.tsinghua.edu.cn/simple
根据[官方OpenVINO网站](https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit.html)上的指南安装OpenVINO。
3. 导出模型
1.使用提供的ultralytics包将YOLOv8分割模型导出为OpenVINO格式:
from ultralytics import YOLO
# Load a YOLOv8n PyTorch model
model = YOLO("best4.pt")
# Export the model
model.export(format="openvino") # creates 'yolov8n_openvino_model/'
# Load the exported OpenVINO model and specify the task
ov_model = YOLO("best4_openvino_model/", task="segment")
2.OpenVINO导出结构:
将模型导出为OpenVINO 格式时,会生成一个包含以下内容的目录:
- XML 文件:描述网络拓扑结构。
- BIN 文件:包含weights and biases 二进制数据。
- 映射文件:保存原始模型输出张量到OpenVINO tensor 名称的映射。
您可以使用这些文件通过OpenVINO 推理引擎运行推理。
3.测试推理OpenVINO代码如下:
import cv2
import numpy as np
from ultralytics import YOLO
class ModelInference:
def __init__(self, model_path, tracker_config='bytetrack.yaml', stop_event=None, use_gpu=False):
self .model = YOLO(model_path, task="segment")
self.tracker_config = tracker_config
self.stop_event = stop_event
self.use_gpu = use_gpu
self.device = "GPU" if use_gpu else "CPU"
self.iou_threshold = 0.7 # Set fixed IOU threshold to 0.7
def process_frame(self, frame):
# Resize the frame to the model's expected input size
input_image = cv2.resize(frame, (640, 640))
# Run inference on the model
results = self.model.track(source=input_image, tracker=self.tracker_config, show=False)
# Create a blank image to draw contours on
contours_image = np.zeros_like(input_image)
# Define the color for the contours (e.g., red)
contour_color = (0, 0, 255) # BGR format for red
# Extract and draw contours from the model results
for result in results:
if result.masks is not None:
masks = result.masks.data.numpy() # Assuming result.masks contains the mask data
for mask in masks:
mask = mask.astype(np.uint8)
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(contours_image, contours, -1, contour_color, 2) # Draw in red
# Resize the contours image back to the original frame size
contours_image = cv2.resize(contours_image, (frame.shape[1], frame.shape[0]))
# Combine the original frame with the contours image
combined_image = cv2.addWeighted(frame, 1, contours_image, 1, 0)
return combined_image
# Define paths
image_path = r'I:\openvino\frame_0627.png'
model_path = r'I:\openvino\best4_openvino_model'
# Load the image
input_image = cv2.imread(image_path)
# Initialize the ModelInference object
model_inference = ModelInference(model_path=model_path)
# Process the image
output_image = model_inference.process_frame(input_image)
# Save or display the result
output_path = r'I:\openvino\frame_0627_result.png'
cv2.imwrite(output_path, output_image)
cv2.imshow('Inference Result', output_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
使用OpenVINO测试推理图片如下: