YOLOv8分割模型转换OpenVINO并部署推理

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 格式时,会生成一个包含以下内容的目录:

  1. XML 文件:描述网络拓扑结构。
  2. BIN 文件:包含weights and biases 二进制数据。
  3. 映射文件:保存原始模型输出张量到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测试推理图片如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值