yolov8 模型部署--TensorRT部署-c++服务化部署
写目录
- yolov8 模型部署--TensorRT部署
- 1、模型导出为onnx格式
- 2、模型onnx格式转engine 部署
yolov8 模型部署–TensorRT部署
1、模型导出为onnx格式
-
如果要用TensorRT部署YOLOv8,需要先使用下面的命令将模型导出为onnx格式:
yolo export model=yolov8n.pt format=onnx -
YOLOv8的3个检测头一共有80x80+40x40+20x20=8400个输出单元格,每个单元格包含x,y,w,h这4项再加80个类别的置信度总共84项内容,所以通过上面命令导出的onnx模型的输出维度为
1x84x8400。 -
模型输出维度

-
这样的通道排列顺序有个问题,那就是后处理的时候会造成内存访问不连续。
-
为了解决这个问题,我们可以修改一下代码,具体做法是把
ultralytics/nn/modules.py文件中的代码做如下修改,交换一下张量y的通道顺序:
def forward(self, x):shape = x[0].shape # BCHWfor i in range(self.nl):x[i] = torch.cat((self.cv2[i](x[i]), self.cv3[i](x[i])), 1)if self.training:return xelif self.dynamic or self.shape != shape:self.anchors, self.strides = (x.transpose(0, 1) for x in make_anchors(x, self.stride, 0.5))self.shape = shapex_cat = torch.cat([xi.view(shape[0], self.no, -1) for xi in x], 2)if self.export and self.format in ('saved_model', 'pb', 'tflite', 'edgetpu', 'tfjs'): # avoid TF FlexSplitV opsbox = x_cat[:, :self.reg_max * 4]cls = x_cat[:, self.reg_max * 4:]else:box, cls = x_cat.split((self.reg_max * 4, self.nc), 1)dbox = dist2bbox(self.dfl(box), self.anchors.unsqueeze(0), xywh=True, dim=1) * self.stridesy = torch.cat((dbox, cls.sigmoid()), 1)# 修改模型输出维度y=y.permute(0,2,1)return y if self.export else (y, x)

- 这样修改后再执行上面的模型导出命令,模型的输出维度变为
1x8400x84

2、模型onnx格式转engine 部署
- 配置好
TensorRT和NVIDIA环境 - 使用
trtexec转换格式trtexec --onnx=coco/best.onnx --saveEngine=coco/best.onnx.engine --workspace=32 - 模型部署部分代码-c++
#ifndef MyController_hpp #define MyController_hpp#include <ctime> #include <chrono> #include <sstream> #include <iomanip>#include <iostream> #include <numeric> #include <vector>#include "oatpp/web/server/api/ApiController.hpp" #include "oatpp/core/macro/codegen.hpp" #include "oatpp/core/macro/component.hpp"#include "opencv2/opencv.hpp" #include "../dto/DTOs.hpp" // 定义数据格式,用于在不同组件之间传输数据#include "../yoloApp/simple_yolo.hpp" #include "../byteTrackApp/logging.h" #include "../byteTrackApp/BYTETracker.h"// high performance #include "../yoloHighPer/cpm.hpp" #include "../yoloHighPer/infer.hpp" #include "../yoloHighPer/yolo.hpp"# include <dirent.h> # include <sys/types.h> # include <sys/stat.h> # include <unistd.h> # include <stdarg.h>using namespace std; using namespace cv;#include OATPP_CODEGEN_BEGIN(ApiController) //<-- Begin Codegenstatic bool exists(const string& path){#ifdef _WIN32return ::PathFileExistsA(path.c_str()); #elsereturn access(path.c_str(), R_OK) == 0; #endif }static std::vector<std::string> cocolabels = {"car", "excavator", "loader", "dumpTruck", "person" };class InferInstance{ public:InferInstance(std::string onnx_model_path, std::string trt_model_path){onnx_model = onnx_model_path;trt_model = trt_model_path;startup();}bool startup(){// if(!exists(trt_model)){// SimpleYolo::compile(// SimpleYolo::Mode::FP32, // FP32、FP16、INT8// SimpleYolo::Type::V8, // 1, // max batch size// onnx_model, // source // trt_model, // save to// 1 << 30,// "inference"// );// }infer_ = yolo::load(trt_model, yolo::Type::V8);return infer_ != nullptr;}int inference(const Mat& image_input, yolo::BoxArray& boxarray){if(infer_ == nullptr){// INFOE("Not Initialize.");return 1;}if(image_input.empty()){// INFOE("Image is empty.");return 1;}boxarray = infer_->forward(cvimg(image_input));return 0;}private:yolo::Image cvimg(const cv::Mat &image) { return yolo::Image(image.data, image.cols, image.rows);}private:std::string onnx_model = "best.onnx";std::string trt_model = "best.onnx.engine";shared_ptr<yolo::Infer> infer_; };/// std::string onnx_model = "coco/best.onnx"; std::string engine_label = "coco/best.onnx.engine"; std::unique_ptr<InferInstance> infer_instance1(new InferInstance(onnx_model, engine_label));int frame_rate = 10; int track_buffer = 30; std::unique_ptr<BYTETracker> tracker_instance1(new BYTETracker(frame_rate, track_buffer));////*** 建议使用 Api 控制器,而不是使用裸 HttpRequestHandler 为每个新端点创建新的请求处理程序。* API 控制器通过为您生成样板代码,使添加新端点的过程变得更加容易。 它还有助于组织您的端点,* 将它们分组到不同的 API 控制器中。*//*** Sample Api Controller.*/ class MyController : public oatpp::web::server::api::ApiController { protected:/*** Constructor with object mapper.* @param objectMapper - default object mapper used to serialize/deserialize DTOs.*/MyController(const std::shared_ptr<ObjectMapper>& objectMapper): oatpp::web::server::api::ApiController(objectMapper){}public: static std::shared_ptr<MyController> createShared(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>, objectMapper)){return std::shared_ptr<MyController>(new MyController(objectMapper));}// TODO Insert Your endpoints here !!!/--data--// 多目标追踪ENDPOINT_ASYNC("POST", "/car1", tracker1){ENDPOINT_ASYNC_INIT(tracker1)Action act() override {return request->readBodyToStringAsync().callbackTo(&tracker1::returnResponse);}Action returnResponse(const oatpp::String& body_){auto response = tracker_inference(*infer_instance1, *tracker_instance1, body_, controller);return _return(response);}};//public:// 多目标追踪static std::shared_ptr<OutgoingResponse> tracker_inference(InferInstance& infer_, BYTETracker& track_infer, std::string body_, auto* controller){auto base64Image = base64_decode(body_);if(base64Image.empty()){return controller->createResponse(Status::CODE_400, "The image is empty!");}std::vector<char> base64_img(base64Image.begin(), base64Image.end());cv::Mat image = cv::imdecode(base64_img, 1);// 获取程序开始时间点auto start_time = std::chrono::high_resolution_clock::now();// 推理yolo::BoxArray boxarray;CV_Assert(0 == infer_.inference(image, boxarray));// 获取程序结束时间点auto end_time = std::chrono::high_resolution_clock::now();// 计算运行时间auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time);// 打印运行时间(以微秒为单位)// std::cout << "程序运行时间: " << duration.count() << " 毫秒" << std::endl;// 结果处理vector<Objects> objects;objects.resize(boxarray.size());int index = 0;for(auto& box : boxarray) {objects[index].rect.x = box.left;;objects[index].rect.y = box.top;objects[index].rect.width = box.right - box.left;objects[index].rect.height = box.bottom - box.top;objects[index].prob = box.confidence;objects[index].label = box.class_label;index++;std::cout << "left: " << box.left << ", top: " << box.top<< ", right: " << box.right << ", bottom: " << box.bottom<< ", confidence: " << box.confidence << ", class_label: " << box.class_label << std::endl;}auto yoloDto = TrackYoloDto::createShared();auto boxList = TrackBoxList::createShared();std::vector<STrack> output_stracks = track_infer.update(objects);for (int i = 0; i < output_stracks.size(); i++){auto trackBoxDto = TrackerBboxes::createShared();vector<float> tlwh = output_stracks[i].tlwh; // 方框的位置trackBoxDto->class_id = cocolabels[output_stracks[i].class_id];trackBoxDto->track_id = output_stracks[i].track_id;trackBoxDto->x = tlwh[0];trackBoxDto->y = tlwh[1];trackBoxDto->width = tlwh[2];trackBoxDto->height = tlwh[3];boxList->push_back(trackBoxDto);}output_stracks.clear();yoloDto->data = boxList;yoloDto->status = "successful";yoloDto->time = currentDateTime();return controller->createDtoResponse(Status::CODE_200, yoloDto);}static std::string currentDateTime(){auto now = std::chrono::system_clock::now();auto now_c = std::chrono::system_clock::to_time_t(now);auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;std::stringstream ss;ss << std::put_time(std::localtime(&now_c), "%Y-%m-%d %H:%M:%S") << '.' << std::setfill('0') << std::setw(3) << now_ms.count();return ss.str();}static unsigned char from_b64(unsigned char ch){/* Inverse lookup map */static const unsigned char tab[128] = {255, 255, 255, 255,255, 255, 255, 255, /* 0 */255, 255, 255, 255,255, 255, 255, 255, /* 8 */255, 255, 255, 255,255, 255, 255, 255, /* 16 */255, 255, 255, 255,255, 255, 255, 255, /* 24 */255, 255, 255, 255,255, 255, 255, 255, /* 32 */255, 255, 255, 62,255, 255, 255, 63, /* 40 */52, 53, 54, 55,56, 57, 58, 59, /* 48 */60, 61, 255, 255,255, 200, 255, 255, /* 56 '=' is 200, on index 61 */255, 0, 1, 2,3, 4, 5, 6, /* 64 */7, 8, 9, 10,11, 12, 13, 14, /* 72 */15, 16, 17, 18,19, 20, 21, 22, /* 80 */23, 24, 25, 255,255, 255, 255, 255, /* 88 */255, 26, 27, 28,29, 30, 31, 32, /* 96 */33, 34, 35, 36,37, 38, 39, 40, /* 104 */41, 42, 43, 44,45, 46, 47, 48, /* 112 */49, 50, 51, 255,255, 255, 255, 255, /* 120 */};return tab[ch & 127];}static std::string base64_decode(const std::string& base64){if(base64.empty())return "";int len = base64.size();auto s = (const unsigned char*)base64.data();unsigned char a, b, c, d;int orig_len = len;int dec_len = 0;string out_data;auto end_s = s + base64.size();int count_eq = 0;while(*--end_s == '='){count_eq ++;}out_data.resize(len / 4 * 3 - count_eq);char *dst = const_cast<char*>(out_data.data());char *orig_dst = dst;while (len >= 4 && (a = from_b64(s[0])) != 255 &&(b = from_b64(s[1])) != 255 && (c = from_b64(s[2])) != 255 &&(d = from_b64(s[3])) != 255) {s += 4;len -= 4;if (a == 200 || b == 200) break; /* '=' can't be there */*dst++ = a << 2 | b >> 4;if (c == 200) break;*dst++ = b << 4 | c >> 2;if (d == 200) break;*dst++ = c << 6 | d;}dec_len = (dst - orig_dst);// dec_len必定等于out_data.size()return out_data;} };#include OATPP_CODEGEN_END(ApiController) //<-- End Codegen#endif /* MyController_hpp */ - 启动模型

- 请求接口进行推理
yolov8 模型部署测试
相关文章:
yolov8 模型部署--TensorRT部署-c++服务化部署
写目录 yolov8 模型部署--TensorRT部署1、模型导出为onnx格式2、模型onnx格式转engine 部署 yolov8 模型部署–TensorRT部署 1、模型导出为onnx格式 如果要用TensorRT部署YOLOv8,需要先使用下面的命令将模型导出为onnx格式: yolo export modelyolov8n.p…...
自适应迭代扩展卡尔曼滤波算法AIEKF估计SOC VS 扩展卡尔曼估计SOC
自适应迭代扩展卡尔曼滤波算法(AIEK) 自适应迭代扩展卡尔曼滤波算法(AIEK)是一种滤波算法,其目的是通过迭代过程来逐渐适应不同的状态和环境,从而优化滤波效果。 该算法的基本思路是在每一步迭代过程中&a…...
2023-亲测有效-git clone失败怎么办?用代理?加git?
git 克隆不下来,超时 用以下格式: git clone https://ghproxy.com/https://github.com/Tencent/ncnn.git 你的网站前面加上 https://ghproxy.com/ 刷的一下就下完了!!...
An Empirical Study of GPT-3 for Few-Shot Knowledge-Based VQA
本文是LLM系列文章,针对《An Empirical Study of GPT-3 for Few-Shot Knowledge-Based VQA》的翻译。 GPT-3对基于小样本知识的VQA的实证研究 摘要引言相关工作方法OK-VQA上的实验VQAv2上的实验结论 摘要 基于知识的视觉问答(VQA)涉及回答需…...
2023高教社杯数学建模B题思路分析 - 多波束测线问题
# 1 赛题 B 题 多波束测线问题 单波束测深是利用声波在水中的传播特性来测量水体深度的技术。声波在均匀介质中作匀 速直线传播, 在不同界面上产生反射, 利用这一原理,从测量船换能器垂直向海底发射声波信 号,并记录从声波发射到…...
02-docker network
Docker网络 Docker网络是什么 Docker 网络是 Docker 容器之间进行通信和连接的网络环境。在 Docker 中,每个容器都有自己的网络命名空间,这意味着每个容器都有自己的网络接口、IP 地址和网络配置 Docker网络启动后,会在宿主机中建立一个名…...
栈和队列经典笔试题
文章目录 栈和队列的回顾💻栈🩳队列👟 栈和队列经典笔试题🔋有效的括号🎸用队列实现栈 🕯用栈实现队列🔭设计循环队列🧼 安静的夜晚 你在想谁吗 栈和队列的回顾💻 栈&am…...
No5.9:多边形内角和公式
#!/usr/bin/python # -*- coding: UTF-8 -*-#指定了编码,中文就能正常展示 # codingutf-8def calc_degree(n):#n代表边形的总数degree (n - 2) * 180#多边形内角和公式return degreeprint(calc_degree(3))#三角形的内角和 print(calc_degree(4))#四边形的内角和【小…...
EditPlus 配置python 及Anaconda中的python
若不是pycharm vscode 太大,太占内存,谁会想到用Notepad,EdirPlus 配置python呢!!! 话不多说,首先你自己安装好EditPlus。开始 菜单栏 选择 工具 -> 配置自定义工具 组名:python 命令:d:\*…...
linux 编译 llvm + clang
1. 需要下载以下三个压缩包,下载源码:Release LLVM 15.0.7 llvm/llvm-project GitHub clang-15.0.7.src.tar.xzcmake-15.0.7.src.tar.xzllvm-15.0.7.src.tar.xz 2. 解压后将 clang 源码放入 llvm/tools/ 下 3. 将解压后的 cmake-15.0.7…...
Mybatis 框架 ( 四 ) QueryWrapper
4.5.Wrapper条件构造器 Wrapper : 条件构造抽象类,最顶端父类 AbstractWrapper : 用于查询条件封装,生成 sql 的 where 条件 QueryWrapper : Entity 对象封装操作类,不是用lambda语法 UpdateWrapper &am…...
数据结构和算法之二分法查找
二分法查找,也称作二分查找或折半查找,是一种在有序数组中快速查找特定元素的算法。它采用分治法思想,通过将问题划分为规模更小的子问题,并且通过对子问题的查找来解决原问题。 二分法查找的思路是不断地将数组一分为二…...
系统日期如何在页面展示,框架是react或者vue3
安装插件dayjs或者moment.js 2.使用setInterval(useInterval)或者requestAnimationFrame react项目中useInterval的代码示例: import React, {useState } from react; import { useInterval } from "ahooks"; import moment fro…...
(二十二)大数据实战——Flume数据采集之故障转移案例实战
前言 本节内容我们完成Flume数据采集的故障转移案例,使用三台服务器,一台服务器负责采集nc数据,通过使用failover模式的Sink处理器完成监控数据的故障转移,使用Avro的方式完成flume之间采集数据的传输。整体架构如下:…...
前端小案例3:Flex弹性布局行内元素宽度自适应
前端小案例3:Flex弹性布局行内元素宽度自适应 项目背景:需要在一行上展示空调设备的三个模式(制冷、制热、通风)或者两个模式(制冷、制热);因为不同产品的模式数量不同,因此需要让模…...
纳尼?小说还要用看的?这可以听!无广!
这是一款听书软件,可以自定义书源,自己设置书架,页面简单易操作,无广告。 支持直接搜索书名,链接,图文,本地文件等方式听书 拥有30多主播声音,分类细致 支持倍速、添加BGM等...
【微服务部署】四、Jenkins一键打包部署NodeJS(Vue)前端项目步骤详解
本文介绍使用Jenkins一键将NodeJS(Vue)前端项目打包并上传到生产环境服务器,这里使用的是直接打包静态页面,发送到远程服务器Nginx配置目录的方式,首先确保服务器环境配置好,安装Nginx,运行目录…...
【前端】禁止别人调试自己的前端页面代码
无限debugger 前端页面防止调试的方法主要是通过不断 debugger 来疯狂输出断点,因为 debugger 在控制台被打开的时候就会执行由于程序被 debugger 阻止,所以无法进行断点调试,所以网页的请求也是看不到的代码如下: /** * 基础禁止…...
UDP的可靠性传输
UDP系列文章目录 第一章 UDP的可靠性传输-理论篇(一) 第二章 UDP的可靠性传输-理论篇(二) 文章目录 UDP系列文章目录前言1.TCP 和UDP格式对比2.UDP分片原理3.UDP 传输层应该注意问题4.MTU5.UDP 分片机制设计重点 一、ARQ协议什么…...
科研笔记:TPAMI submission guideline
1 author information Author Information - IEEE Transactions on Pattern Analysis and Machine Intelligence | IEEE Computer Society Digital Library 1.1 会议期刊extension 当一个TPAMI的提交基于之前的会议论文时,IEEE要求期刊论文是之前出版物的“实质…...
三维GIS开发cesium智慧地铁教程(5)Cesium相机控制
一、环境搭建 <script src"../cesium1.99/Build/Cesium/Cesium.js"></script> <link rel"stylesheet" href"../cesium1.99/Build/Cesium/Widgets/widgets.css"> 关键配置点: 路径验证:确保相对路径.…...
线程同步:确保多线程程序的安全与高效!
全文目录: 开篇语前序前言第一部分:线程同步的概念与问题1.1 线程同步的概念1.2 线程同步的问题1.3 线程同步的解决方案 第二部分:synchronized关键字的使用2.1 使用 synchronized修饰方法2.2 使用 synchronized修饰代码块 第三部分ÿ…...
vscode(仍待补充)
写于2025 6.9 主包将加入vscode这个更权威的圈子 vscode的基本使用 侧边栏 vscode还能连接ssh? debug时使用的launch文件 1.task.json {"tasks": [{"type": "cppbuild","label": "C/C: gcc.exe 生成活动文件"…...
Linux相关概念和易错知识点(42)(TCP的连接管理、可靠性、面临复杂网络的处理)
目录 1.TCP的连接管理机制(1)三次握手①握手过程②对握手过程的理解 (2)四次挥手(3)握手和挥手的触发(4)状态切换①挥手过程中状态的切换②握手过程中状态的切换 2.TCP的可靠性&…...
dedecms 织梦自定义表单留言增加ajax验证码功能
增加ajax功能模块,用户不点击提交按钮,只要输入框失去焦点,就会提前提示验证码是否正确。 一,模板上增加验证码 <input name"vdcode"id"vdcode" placeholder"请输入验证码" type"text&quo…...
MVC 数据库
MVC 数据库 引言 在软件开发领域,Model-View-Controller(MVC)是一种流行的软件架构模式,它将应用程序分为三个核心组件:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于提高代码的可维护性和可扩展性。本文将深入探讨MVC架构与数据库之间的关系,以…...
多模态大语言模型arxiv论文略读(108)
CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文标题:CROME: Cross-Modal Adapters for Efficient Multimodal LLM ➡️ 论文作者:Sayna Ebrahimi, Sercan O. Arik, Tejas Nama, Tomas Pfister ➡️ 研究机构: Google Cloud AI Re…...
Go 并发编程基础:通道(Channel)的使用
在 Go 中,Channel 是 Goroutine 之间通信的核心机制。它提供了一个线程安全的通信方式,用于在多个 Goroutine 之间传递数据,从而实现高效的并发编程。 本章将介绍 Channel 的基本概念、用法、缓冲、关闭机制以及 select 的使用。 一、Channel…...
华为OD机考-机房布局
import java.util.*;public class DemoTest5 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseSystem.out.println(solve(in.nextLine()));}}priv…...
基于PHP的连锁酒店管理系统
有需要请加文章底部Q哦 可远程调试 基于PHP的连锁酒店管理系统 一 介绍 连锁酒店管理系统基于原生PHP开发,数据库mysql,前端bootstrap。系统角色分为用户和管理员。 技术栈 phpmysqlbootstrapphpstudyvscode 二 功能 用户 1 注册/登录/注销 2 个人中…...
