💡💡💡本文摘要:本文提供了YOLOv8 seg c++部署方式,ONNX Runtime CUDA和cpu部署
💡💡💡ONNX Runtime优点:通用性好,速度较快,适合各个平台复制;
💡💡💡下图为ONNX Runtime CUDA推理结果
ONNX Runtime
是将 ONNX 模型部署到生产环境的跨平台高性能运行引擎,主要对模型图应用了大量的图优化,然后基于可用的特定于硬件的加速器将其划分为子图(并行处理)。
ONNX的官方网站:https://onnx.ai/
ONXX的GitHub地址:https://github.com/onnx/onnx
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
Tensorrt 优点:在GPU上推理速度是最快的;缺点:不同显卡cuda版本可能存在不适用情况;
ONNX Runtime优点:通用性好,速度较快,适合各个平台复制;
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)
yolov8onnxruntime.cpp
#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;
}
详见:
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。