前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Yolov8 Seg分割 C++ GPU部署:ONNXRuntime cuda部署

Yolov8 Seg分割 C++ GPU部署:ONNXRuntime cuda部署

原创
作者头像
AI小怪兽
发布2024-08-13 15:50:14
2590
发布2024-08-13 15:50:14
举报
文章被收录于专栏:YOLO大作战

💡💡💡本文摘要:本文提供了YOLOv8 seg c++部署方式,ONNX Runtime CUDA和cpu部署

💡💡💡ONNX Runtime优点:通用性好,速度较快,适合各个平台复制;

💡💡💡下图为ONNX Runtime CUDA推理结果

1. ONNX和Tensorrt区别

ONNX Runtime 是将 ONNX 模型部署到生产环境的跨平台高性能运行引擎,主要对模型图应用了大量的图优化,然后基于可用的特定于硬件的加速器将其划分为子图(并行处理)。

ONNX的官方网站:https://onnx.ai/

ONXX的GitHub地址:https://github.com/onnx/onnx

1.2 Tensorrt介绍

C++ 库,用于加速 NVIDIA 的 GPU,可以为深度学习应用提供低延迟、高吞吐率的部署推理,支持 TensorFlow,Pytorch,Caffe2 ,Paddle等框架训练出的神经网络,可以优化网络计算TensorRT官网下载地址:https://developer.nvidia.com/zh-cn/tensorrt

开发者指南:https://docs.nvidia.com/deeplearning/tensorrt/developer-guide/index.html

Github地址:https://github.com/NVIDIA/TensorRT

1.3 Yolov8两种部署方式比较:

Tensorrt 优点:在GPU上推理速度是最快的;缺点:不同显卡cuda版本可能存在不适用情况;

ONNX Runtime优点:通用性好,速度较快,适合各个平台复制;

2.Yolov8 seg ONNX Runtime部署

2.1 如何得到 .onnx

代码语言:javascript
复制
from ultralytics import YOLO

# Load a YOLOv8 model
model = YOLO("runs/segment/train/weights/best.pt")

# Export the model
model.export(format="onnx", opset=12, dynamic=False, imgsz=640)

2.2 主函数代码:

yolov8onnxruntime.cpp

代码语言:c
复制
#include <iostream>
#include<opencv2/opencv.hpp>

#include<math.h>
#include "yolov8.h"
#include "yolov8_onnx.h"

#include "yolov8_seg_onnx.h"

#include<time.h>
//#define  VIDEO_OPENCV //if define, use opencv for video.

using namespace std;
using namespace cv;
using namespace dnn;

template<typename _Tp>
int yolov8(_Tp& task, cv::Mat& img, std::string& model_path)
{


	cv::dnn::Net net;
	if (task.ReadModel(net, model_path, true)) {
		std::cout << "read net ok!" << std::endl;
	}
	else {
		return -1;
	}
	//生成随机颜色
	std::vector<cv::Scalar> color;
	srand(time(0));
	for (int i = 0; i < 80; i++) {
		int b = rand() % 256;
		int g = rand() % 256;
		int r = rand() % 256;
		color.push_back(cv::Scalar(b, g, r));
	}
	std::vector<OutputParams> result;

	bool isPose = false;

	PoseParams poseParams;
	if (task.Detect(img, net, result)) {

		if (isPose)
			DrawPredPose(img, result, poseParams);
		else
			DrawPred(img, result, task._className, color);

	}
	else {
		std::cout << "Detect Failed!" << std::endl;
	}
	system("pause");
	return 0;
}

template<typename _Tp>
int yolov8_onnx(_Tp& task, cv::Mat& img, std::string& model_path)
{

	if (task.ReadModel(model_path, true)) {
		std::cout << "read net ok!" << std::endl;
	}
	else {
		return -1;
	}
	//生成随机颜色
	std::vector<cv::Scalar> color;
	srand(time(0));
	for (int i = 0; i < 80; i++) {
		int b = rand() % 256;
		int g = rand() % 256;
		int r = rand() % 256;
		color.push_back(cv::Scalar(b, g, r));
	}
	bool isPose = false;

	PoseParams poseParams;

	std::vector<OutputParams> result;
	if (task.OnnxDetect(img, result)) {
		if (isPose)
			DrawPredPose(img, result, poseParams);
		else
			DrawPred(img, result, task._className, color);
	}
	else {
		std::cout << "Detect Failed!" << std::endl;
	}
	//system("pause");
	return 0;
}




int main() {

	//std::string img_path = "D:/DL/AIDeploy/YOLOv8-Deploy/yolov8onnxruntime/model/1.jpg";

	//std::string model_path_detect = "D:/DL/AIDeploy/YOLOv8-Deploy/yolov8onnxruntime/model/yolov8n.onnx";


	std::string model_path_seg = "D:/DL/AIDeploy/YOLOv8-Deploy/yolov8onnxruntime/model/yolov8n-seg.onnx";


	Yolov8SegOnnx		task_segment_ort;

	cv::Mat src = imread(img_path);
	cv::Mat img = src.clone();

	//Yolov8				task_detect_ocv;
	//Yolov8Onnx			task_detect_ort;


	//yolov8_onnx(task_detect_ort, img, model_path_detect);  //yoolov8 onnxruntime detect


	yolov8_onnx(task_segment_ort, img, model_path_seg); //yolov8 onnxruntime segment

	return 0;
}


详见:

https://blog.csdn.net/m0_63774211/article/details/133184240

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. ONNX和Tensorrt区别
    • 1.2 Tensorrt介绍
      • 1.3 Yolov8两种部署方式比较:
      • 2.Yolov8 seg ONNX Runtime部署
        • 2.1 如何得到 .onnx
        • 2.2 主函数代码:
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档