{"id":13642918,"url":"https://github.com/DataXujing/YOLOv7","last_synced_at":"2025-04-20T21:32:03.586Z","repository":{"id":44905232,"uuid":"512389900","full_name":"DataXujing/YOLOv7","owner":"DataXujing","description":":fire::fire::fire: Official YOLOv7训练自己的数据集并实现端到端的TensorRT模型加速推断","archived":false,"fork":false,"pushed_at":"2022-07-10T10:05:41.000Z","size":1601,"stargazers_count":45,"open_issues_count":9,"forks_count":10,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-08-02T01:17:41.197Z","etag":null,"topics":["object-detection","tensorrt","yolov7"],"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}},"created_at":"2022-07-10T09:05:12.000Z","updated_at":"2024-06-29T00:59:06.000Z","dependencies_parsed_at":"2022-08-12T11:40:25.251Z","dependency_job_id":null,"html_url":"https://github.com/DataXujing/YOLOv7","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv7","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv7/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv7/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLOv7/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DataXujing","download_url":"https://codeload.github.com/DataXujing/YOLOv7/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223839131,"owners_count":17211878,"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":["object-detection","tensorrt","yolov7"],"created_at":"2024-08-02T01:01:38.051Z","updated_at":"2024-11-09T14:31:06.459Z","avatar_url":"https://github.com/DataXujing.png","language":"Python","funding_links":[],"categories":["Other Versions of YOLO"],"sub_categories":[],"readme":"##  Official YOLOv7 训练自己的数据集（端到端TensorRT 模型部署）\n\n目前适用的版本： `v0.1` \n\n\u003e 我只能说太卷了!\n\nImplementation of paper - [YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors](https://arxiv.org/abs/2207.02696)\n\n\u003cimg src=\"./figure/performance.png\" height=\"480\"\u003e\n\n\n#### Performance \n\nMS COCO\n\n| Model | Test Size | AP\u003csup\u003etest\u003c/sup\u003e | AP\u003csub\u003e50\u003c/sub\u003e\u003csup\u003etest\u003c/sup\u003e | AP\u003csub\u003e75\u003c/sub\u003e\u003csup\u003etest\u003c/sup\u003e | batch 1 fps | batch 32 average time |\n| :-- | :-: | :-: | :-: | :-: | :-: | :-: |\n| [**YOLOv7**](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt) | 640 | **51.4%** | **69.7%** | **55.9%** | 161 *fps* | 2.8 *ms* |\n| [**YOLOv7-X**](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7x.pt) | 640 | **53.1%** | **71.2%** | **57.8%** | 114 *fps* | 4.3 *ms* |\n|  |  |  |  |  |  |  |\n| [**YOLOv7-W6**](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-w6.pt) | 1280 | **54.9%** | **72.6%** | **60.1%** | 84 *fps* | 7.6 *ms* |\n| [**YOLOv7-E6**](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6.pt) | 1280 | **56.0%** | **73.5%** | **61.2%** | 56 *fps* | 12.3 *ms* |\n| [**YOLOv7-D6**](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-d6.pt) | 1280 | **56.6%** | **74.0%** | **61.8%** | 44 *fps* | 15.0 *ms* |\n| [**YOLOv7-E6E**](https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-e6e.pt) | 1280 | **56.8%** | **74.4%** | **62.1%** | 36 *fps* | 18.7 *ms* |\n\n### 1.训练环境配置\n\n```shell\n\n# create the docker container, you can change the share memory size if you have more. # 注意这个share memory尽量设置的大一点，训练的话！\nsudo nvidia-docker run --name yolov7 -it -v /home/myuser/xujing/hackathon2022:/yolov7 --shm-size=64g nvcr.io/nvidia/pytorch:21.08-py3\n\n# apt install required packages\napt update\napt install -y zip htop screen libgl1-mesa-glx\n\n# pip install required packages\npip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/\n\n# go to code folder\ncd /yolov7\n```\n\n\n### 2.训练集准备\n\nYOLOv7类似于YOLOR支持YOLOv5类型的标注数据构建，如果你熟悉YOLOv5的训练集的构造过程，该部分可以直接跳过，这里我们提供了构建数据集的代码，将数据集存放在`./dataset`下：\n\n```shell\n./datasets/score   # 存放的文件，score是数据集的名称\n├─images           # 训练图像，每个文件夹下存放了具体的训练图像\n│  ├─train\n│  └─val\n└─labels           # label，每个文件夹下存放了具体的txt标注文件，格式满足YOLOv5\n    ├─train\n    └─val\n```\n\n我们提供了VOC标注数据格式转换为YOLOv5标注的具体代码，存放在`./dataset`下，关于YOLOv5的标注细节可以参考：\n+ https://github.com/DataXujing/YOLO-v5\n+ https://github.com/DataXujing/YOLOv6#2%E6%95%B0%E6%8D%AE%E5%87%86%E5%A4%87\n\n\n### 3.模型结构或配置文件修改\n\n+ 1.修改模型的配置文件\n\ni.训练数据的配置:`./data/score.yaml`\n\n```shell\ntrain: ./dataset/score/images/train # train images\nval: ./dataset/score/images/val # val images\n#test: ./dataset/score/images/test # test images (optional)\n\n# Classes\nnc: 4  # number of classes\nnames: ['person','cat','dog','horse']  # class names\n```\n\nii.模型结构的配置:`./cfg/training/yolov7_score.yaml`\n\n```shell\n# parameters\nnc: 4  # number of classes\ndepth_multiple: 1.0  # model depth multiple\nwidth_multiple: 1.0  # layer channel multiple\n\n# anchors\nanchors:\n  - [12,16, 19,36, 40,28]  # P3/8\n  - [36,75, 76,55, 72,146]  # P4/16\n  - [142,110, 192,243, 459,401]  # P5/32\n\n...\n\n```\n\n\n### 4.模型训练\n\n```shell\npython3 train.py --workers 8 --device 0 --batch-size 32 --data data/score.yaml --img 640 640 --cfg cfg/training/yolov7_score.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml\n\n# 一机多卡\npython3 -m torch.distributed.launch --nproc_per_node 4 --master_port 9527 train.py --workers 8 --device 0,1,2,3 --sync-bn --batch-size 128 --data data/score.yaml --img 640 640 --cfg cfg/training/yolov7_score.yaml --weights '' --name yolov7 --hyp data/hyp.scratch.p5.yaml\n\n```\n\n\n### 5.模型评估\n\n```shell\npython3 test.py --data data/score.yaml --img 640 --batch 32 --conf 0.001 --iou 0.45 --device 0 --weights ./runs/train/yolov7/weights/last.pt --name yolov7_640_val\n\n```\n\n### 6.模型推断\n\n```shell\npython3 inference.py\n\n```\n\n推断效果如下：\n\n\u003cdiv align=center\u003e\n\n|                                 |                                  |                                  |                                  \n| :-----------------------------: | :------------------------------: | :------------------------------: | \n| \u003cimg src=\"figure/image1.jpg\"  height=270 width=270\u003e | \u003cimg src=\"figure/image2.jpg\" width=270 height=270\u003e | \u003cimg src=\"figure/image3.jpg\" width=270 height=270\u003e |  \n\n\u003c/div\u003e\n\n\n\n### 7.TensorRT 模型加速\n\n我们实现了端到端的TensorRT模型加速，将NMS通过Plugin放入TensorRT Engine中。\n\n1. YOLOv7转ONNX\n\n注意：如果直接转ONNX会出现`scatterND`节点，可以做如下修改\n\n\u003cdiv align=center\u003e\n\u003cimg src=\"figure/onnx_0.png\" \u003e\n\u003c/div\u003e\n\n```shell\npython3 ./models/export.py --weights ./runs/train/yolov7/weights/last.pt --img-size 640 640 --grid\n\n```\n\n\u003cdiv align=center\u003e\n\u003cimg src=\"figure/onnx.png\" \u003e\n\u003c/div\u003e\n\n1. YOLOv7的前处理\n\nYOLOv6, YOLOv7的前处理和YOLOv5是相同的。\n\n其C++的实现如下：\n\n```c++\nvoid preprocess(cv::Mat\u0026 img, float data[]) {\n\tint w, h, x, y;\n\tfloat r_w = INPUT_W / (img.cols*1.0);\n\tfloat r_h = INPUT_H / (img.rows*1.0);\n\tif (r_h \u003e r_w) {\n\t\tw = INPUT_W;\n\t\th = r_w * img.rows;\n\t\tx = 0;\n\t\ty = (INPUT_H - h) / 2;\n\t}\n\telse {\n\t\tw = r_h * img.cols;\n\t\th = INPUT_H;\n\t\tx = (INPUT_W - w) / 2;\n\t\ty = 0;\n\t}\n\tcv::Mat re(h, w, CV_8UC3);\n\tcv::resize(img, re, re.size(), 0, 0, cv::INTER_LINEAR);\n\t//cudaResize(img, re);\n\tcv::Mat out(INPUT_H, INPUT_W, CV_8UC3, cv::Scalar(114, 114, 114));\n\tre.copyTo(out(cv::Rect(x, y, re.cols, re.rows)));\n\n\tint i = 0;\n\tfor (int row = 0; row \u003c INPUT_H; ++row) {\n\t\tuchar* uc_pixel = out.data + row * out.step;\n\t\tfor (int col = 0; col \u003c INPUT_W; ++col) {\n\t\t\tdata[i] = (float)uc_pixel[2] / 255.0;\n\t\t\tdata[i + INPUT_H * INPUT_W] = (float)uc_pixel[1] / 255.0;\n\t\t\tdata[i + 2 * INPUT_H * INPUT_W] = (float)uc_pixel[0] / 255.0;\n\t\t\tuc_pixel += 3;\n\t\t\t++i;\n\t\t}\n\t}\n\n}\n\n```\n\n\n3. YOLOv7的后处理\n\n不难发现，YOLOv5, YOLOv6, YOLOv7的后处理基本是完全相同的，TensorRT后处理的代码基本可以复用\n\n增加后处理和NMS节点：\n\n```shell\n\npython tensorrt/yolov7_add_postprocess.py\npython tensorrt/yolov7_add_nms.py\n\n```\n\nonnx中增加了如下节点：\n\n\u003cdiv align=center\u003e\n\u003cimg src=\"figure/onnx_1.png\" \u003e\n\u003c/div\u003e\n\n\n4. TRT序列化Engine\n\n```shell\ntrtexec --onnx=last_1_nms.onnx --saveEngine=yolov7.engine --workspace=3000 --verbose\n```\n\n5. `VS2017`下编译运行`./tensorrt/yolov7`\n\n\n\n\u003cdiv align=center\u003e\n\n|                                 |                                  |                                  |                                  \n| :-----------------------------: | :------------------------------: | :------------------------------: | \n| \u003cimg src=\"figure/image1_trt.jpg\"  height=270 width=270\u003e | \u003cimg src=\"figure/image2_trt.jpg\" width=270 height=270\u003e | \u003cimg src=\"figure/image3_trt.jpg\" width=270 height=270\u003e |  \n\n\u003c/div\u003e\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDataXujing%2FYOLOv7","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FDataXujing%2FYOLOv7","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FDataXujing%2FYOLOv7/lists"}