{"id":13642931,"url":"https://github.com/DataXujing/YOLOv9","last_synced_at":"2025-04-20T21:32:11.490Z","repository":{"id":225332484,"uuid":"765695776","full_name":"DataXujing/YOLOv9","owner":"DataXujing","description":":fire: YOLOv9 paper解析，训练自己的数据集，TensorRT端到端部署， NCNN安卓手机部署","archived":false,"fork":false,"pushed_at":"2024-03-07T13:05:53.000Z","size":42526,"stargazers_count":65,"open_issues_count":1,"forks_count":13,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-04T13:37:27.804Z","etag":null,"topics":["ncnn","tensorrt","yolov9"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DataXujing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-03-01T12:42:25.000Z","updated_at":"2025-01-12T12:28:02.000Z","dependencies_parsed_at":"2024-03-07T14:39:10.285Z","dependency_job_id":null,"html_url":"https://github.com/DataXujing/YOLOv9","commit_stats":null,"previous_names":["dataxujing/yolov9"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv9","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv9/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv9/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv9/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DataXujing","download_url":"https://codeload.github.com/DataXujing/YOLOv9/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249965546,"owners_count":21352925,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ncnn","tensorrt","yolov9"],"created_at":"2024-08-02T01:01:38.228Z","updated_at":"2025-04-20T21:32:11.463Z","avatar_url":"https://github.com/DataXujing.png","language":"Python","funding_links":[],"categories":["Other Versions of YOLO"],"sub_categories":[],"readme":"## Official YOLOv9 训练自己的数据集并基于NVIDIA TensorRT和及安卓手机端部署\n\n### 1.YOLOv9算法解读\n\n\u003e YOLOv9的改进\n\n+ YOLOv9从可逆函数角度理论上分析了现有的CNN架构，基于这种分析，YOLOv9作者还设计了PGI和辅助可逆分支，并取得了优秀的结果；\n+ YOLOv9用到的PGI解决了深度监督只能用于极深的神经网络架构的问题，因此使得新的轻量级架构才更适合落地；\n+ YOLOv9中设计的GELAN仅使用传统卷积，就能实现比基于最先进技术的深度可分卷积设计更高的参数使用率，同时展现出轻量级、快速和精确的巨大优势；\n+ 基于所提出的PGI和GELAN，YOLOv9在MS COCO数据集上的性能在所有方面都大大超过了现有的实时目标检测器。\n\n\n\u003e PGI(可编程梯度信息)\n\nPGI主要包括三个组成部分，即：\n\n+ 主分支\n+ 辅助可逆分支\n+ 多级辅助信息\n\n![](docs/PGI.png)\n\n\u003e GELAN模块\n\nYOLOv9提出了新网络架构——GELAN。GELAN通过结合两种神经网络架构，即结合用梯度路径规划（CSPNet）和（ELAN）设计了一种广义的高效层聚合网络（GELAN）；GELAN综合考虑了轻量级、推理速度和准确度。\n\n![](docs/GELAN.png)\n\nGELAN整体架构如上图所示。YOLOv9将ELAN的能力进行了泛化，原始ELAN仅使用卷积层的堆叠，而GELAN可以使用任何计算块作为基础Module。\n\n\u003e 损失函数与样本匹配\n\n![](docs/loss.png)\n\n通过上图代码可以看到:\n+ 样本匹配依旧使用的是TaskAlign样本匹配。和YOLOv8、YOLOE、YOLOv6等算法保持一致；\n+ 分类损失：BCE Loss 回归损失：DFL Loss + CIoU Loss\n\n\u003e 模型结构\n\n![](docs/yolov9.png)\n\n\n\n### 2.构建自己的训练数据集训练YOLOv9\n\n假设我们有NVIDIA的计算卡，同时配置好了YOLOv9运行需要的环境！\n\n+ 训练数据的构建\n\nYOLOv9遵循YOLOv5-YOLOv8的训练数据构建方式，可以参考：\u003chttps://github.com/DataXujing/YOLO-v5\u003e, 这里以肺炎X-ray数据集作为训练YOLOv9-c模型的数据集。\n\n+ 构建数据集配置文件:`./data/xray.yaml`\n\n```yaml\npath: ./datasets/xray  # dataset root dir\ntrain: images/train/   # train images (relative to 'path') 118287 images\nval: images/val/  # val images (relative to 'path') 5000 images\ntest: images/test/   # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794\n\n# Classes\nnames:\n  0: pneumonia\n\n```\n\n+ 构建YOLOv9-c模型配置文件：`models/detect/yolov9-c.yaml`\n\n```yaml\n# YOLOv9\n\n# parameters\nnc: 1  # number of classes\ndepth_multiple: 1.0  # model depth multiple\nwidth_multiple: 1.0  # layer channel multiple\n#activation: nn.LeakyReLU(0.1)\n#activation: nn.ReLU()\n\n# anchors\nanchors: 3\n\n......\n```\n\n+ 下载预训练的模型：\n\n```shell\nwget https://github.com/WongKinYiu/yolov9/releases/download/v0.1/yolov9-c.pt\n```\n\n+ 训练YOLOv9-c\n\n```shell\npython3 train_dual.py  --weights=./pretrain/yolov9-c.pt --cfg=./models/detect/yolov9-c.yaml --data=./data/xray.yaml --epoch=100 --batch-size=16 --imgsz=640 --hyp=data/hyps/hyp.scratch-high.yaml\n```\n\n### 3.YOLOv9推理Demo\n\n```python\npython inference.py\n```\n\n|  Pytorch| Pytorch | Pytorch |\n| ------ | ------ | ------ |\n| ![](docs/6.jpg) | ![](docs/58.jpg)  | ![](docs/150.jpg)  |\n\n\n### 4.YOLOv9 端到端TensorRT加速推理C++实现\n\n+ Pytorch转ONNX\n\n```shell\npython export.py --data=./data/xray.yaml --weights=./runs/train/exp/weights/last.pt --opset=13 --include=onnx --simplify\n```\n\n+ 添加EfficientNMS plugin\n\n```shell\npython onnx_add_nms_op.py\n```\n\n在原onnx模型中插入EfficientNMS Plugin节点：\n![](docs/add_nms.png)\n\n+ TensorRT序列化Engine\n\n```shell\ntrtexec --onnx=last_nms.onnx --saveEngine=yolov9-c.plan --workspace=3000 --verbose\n```\n![](docs/trtexec.png)\n\n恭喜你，TensorRT序列化Engine成功!\n\n+ Win10下C++实现端到端的TensorRT推断\n\n我们的代码存放在`tensorrt`文件夹下(在TensorRT 8.2和TensorRT8.6测试），相同图片在TensorRT C++的推理结果基本一致\n\n\n| TensorRT | TensorRT |  TensorRT|\n| ------ | ------ | ------ |\n| ![](docs/trt_6.jpg) | ![](docs/trt-58.jpg)  | ![](docs/trt-150.jpg)  |\n\n\n### 5.YOLOv9安卓手机部署\n\n+ 首先我们在windows下使用ncnn进行调用，该部分所有的代码在`ncnn`文件夹下，关于安卓手机端部署的代码，可以参考我们的另一个项目：\u003chttps://github.com/DataXujing/ncnn_android_yolov9\u003e\n\n| NCNN-FP32                 | NCNN-FP32                  | NCNN-FP32                   |\n| ------------------------- | -------------------------- | --------------------------- |\n| ![](docs/ncnn_fp32_6.jpg) | ![](docs/ncnn_fp32_58.jpg) | ![](docs/ncnn_fp32_150.jpg) |\n\n| NCNN-FP16                 | NCNN-FP16                  | NCNN-FP16                   |\n| ------------------------- | -------------------------- | --------------------------- |\n| ![](docs/ncnn_fp16_6.jpg) | ![](docs/ncnn_fp16_58.jpg) | ![](docs/ncnn_fp16_150.jpg) |\n\n\u003e 我们也实现了YOLOv9-c的ncnn下的int8量化，但是目前还存在问题：\u003chttps://github.com/Tencent/ncnn/issues/5362\u003e\n\n小米手机下的部署：\n\n![](docs/video1.gif)\n\n### 参考\n\n1. [YOLOv9开源 | 架构图\u0026模块改进\u0026正负样本匹配\u0026损失函数解读，5分钟即可理解YOLOv9](https://mp.weixin.qq.com/s/31NlBknx4PcXipfuV2w6hw)\n\n2. [YOLOv9: Learning What You Want to Learn Using Programmable Gradient Information](https://arxiv.org/abs/2402.13616)\n\n3. https://github.com/WongKinYiu/yolov9\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDataXujing%2FYOLOv9","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDataXujing%2FYOLOv9","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDataXujing%2FYOLOv9/lists"}