{"id":23102675,"url":"https://github.com/dataxujing/yolo-v5","last_synced_at":"2025-04-12T19:50:07.493Z","repository":{"id":37648181,"uuid":"272313807","full_name":"DataXujing/YOLO-v5","owner":"DataXujing","description":":art: Pytorch YOLO v5 训练自己的数据集超详细教程！！！ :art: (提供PDF训练教程下载）","archived":false,"fork":false,"pushed_at":"2022-06-22T02:19:33.000Z","size":14292,"stargazers_count":940,"open_issues_count":38,"forks_count":195,"subscribers_count":12,"default_branch":"master","last_synced_at":"2025-04-12T19:49:59.827Z","etag":null,"topics":["object-detection","pytorch","yolov5"],"latest_commit_sha":null,"homepage":null,"language":"Jupyter Notebook","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","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-06-15T01:21:55.000Z","updated_at":"2025-04-12T03:43:55.000Z","dependencies_parsed_at":"2022-08-08T21:15:21.917Z","dependency_job_id":null,"html_url":"https://github.com/DataXujing/YOLO-v5","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%2FYOLO-v5","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLO-v5/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLO-v5/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DataXujing%2FYOLO-v5/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DataXujing","download_url":"https://codeload.github.com/DataXujing/YOLO-v5/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625501,"owners_count":21135513,"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","pytorch","yolov5"],"created_at":"2024-12-17T00:00:18.880Z","updated_at":"2025-04-12T19:50:07.467Z","avatar_url":"https://github.com/DataXujing.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"## [YOLO v5](https://github.com/ultralytics/yolov5)在医疗领域中消化内镜目标检测的应用\n\n### YOLO v5训练自己数据集详细教程\n\n:bug: :bug: 现在YOLOv5 已经更新到6.0版本了，但是其训练方式同本Repo是一致的，只需要按照对应版本安装对应Python环境即可，其数据集的构建，配置文件的修改，训练方式等完全与本Repo一致！\n\n:bug: :bug: 我们提供了YOLOv5 TensorRT调用和INT8量化的C++和Python代码（其TensorRT加速方式不同于本Repo提供的TensorRT调用方式），有需要的大佬可在issues中留言！\n\n**Xu Jing**\n\n------\n:fire: 由于官方新版YOLO v5的backbone和部分参数调整，导致很多小伙伴下载最新官方预训练模型不可用，这里提供原版的YOLO v5的预训练模型的百度云盘下载地址\n\n链接：https://pan.baidu.com/s/1SDwp6I_MnRLK45QdB3-yNw \n提取码：423j\n\n------\n\n+ YOLOv4还没有退热，YOLOv5已经发布！\n\n+ 6月9日，Ultralytics公司开源了YOLOv5，离上一次YOLOv4发布不到50天。而且这一次的YOLOv5是完全基于PyTorch实现的！\n\n+ YOLO v5的主要贡献者是YOLO v4中重点介绍的马赛克数据增强的作者\n\n\u003ca href=\"https://apps.apple.com/app/id1452689527\" target=\"_blank\"\u003e\n\u003cimg src=\"readmepic/readme1/82944393-f7644d80-9f4f-11ea-8b87-1a5b04f555f1.jpg\" width=\"1000\"\u003e\u003c/a\u003e\n\n\n\n\u003e 本项目描述了如何基于自己的数据集训练YOLO v5\n\n\u003cimg align=\"center\" src=\"readmepic/readme1/84200349-729f2680-aa5b-11ea-8f9a-604c9e01a658.png\" width=\"1000\"\u003e\n\n但是YOLO v4的二作提供给我们的信息和官方提供的还是有一些出入：\n\n\u003cimg align=\"center\" src=\"readmepic/readme1/YOLOv4_author2.jpg\" width=\"800\"\u003e\n\n\n#### 0.环境配置\n\n安装必要的python package和配置相关环境\n\n```\n# python3.6\n# torch==1.3.0\n# torchvision==0.4.1\n\n# git clone yolo v5 repo\ngit clone https://github.com/ultralytics/yolov5 # clone repo\n# 下载官方的样例数据（这一步可以省略）\npython3 -c \"from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')\" # download dataset\ncd yolov5\n# 安装必要的package\npip3 install -U -r requirements.txt\n```\n\n#### 1.创建数据集的配置文件`dataset.yaml`\n\n[data/coco128.yaml](https://github.com/ultralytics/yolov5/blob/master/data/coco128.yaml)来自于COCO train2017数据集的前128个训练图像，可以基于该`yaml`修改自己数据集的`yaml`文件\n\n ```ymal\n # train and val datasets (image directory or *.txt file with image paths)\ntrain: ./datasets/score/images/train/\nval: ./datasets/score/images/val/\n\n# number of classes\nnc: 3\n\n# class names\nnames: ['QP', 'NY', 'QG']\n ```\n\n#### 2.创建标注文件\n\n可以使用LabelImg,Labme,[Labelbox](https://labelbox.com/), [CVAT](https://github.com/opencv/cvat)来标注数据，对于目标检测而言需要标注bounding box即可。然后需要将标注转换为和**darknet format**相同的标注形式，每一个图像生成一个`*.txt`的标注文件（如果该图像没有标注目标则不用创建`*.txt`文件）。创建的`*.txt`文件遵循如下规则：\n\n- 每一行存放一个标注类别\n- 每一行的内容包括`class x_center y_center width height`\n- Bounding box 的坐标信息是归一化之后的（0-1）\n- class label转化为index时计数是从0开始的\n\n```python\ndef convert(size, box):\n    '''\n    将标注的xml文件标注转换为darknet形的坐标\n    '''\n    dw = 1./(size[0])\n    dh = 1./(size[1])\n    x = (box[0] + box[1])/2.0 - 1\n    y = (box[2] + box[3])/2.0 - 1\n    w = box[1] - box[0]\n    h = box[3] - box[2]\n    x = x*dw\n    w = w*dw\n    y = y*dh\n    h = h*dh\n    return (x,y,w,h)\n```\n\n每一个标注`*.txt`文件存放在和图像相似的文件目录下，只需要将`/images/*.jpg`替换为`/lables/*.txt`即可（这个在加载数据时代码内部的处理就是这样的，可以自行修改为VOC的数据格式进行加载）\n\n例如：\n\n```\ndatasets/score/images/train/000000109622.jpg  # image\ndatasets/score/labels/train/000000109622.txt  # label\n```\n如果一个标注文件包含5个person类别（person在coco数据集中是排在第一的类别因此index为0）：\n\n\u003cimg width=\"500\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/78174482-307bb800-740e-11ea-8b09-840693671042.png\"\u003e\n\n#### 3.组织训练集的目录\n\n将训练集train和验证集val的images和labels文件夹按照如下的方式进行存放\n\n\u003cimg width=\"500\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/datalist.png\"\u003e\n\n至此数据准备阶段已经完成，过程中我们假设算法工程师的数据清洗和数据集的划分过程已经自行完成。\n\n#### 4.选择模型backbone进行模型配置文件的修改\n\n在项目的`./models`文件夹下选择一个需要训练的模型，这里我们选择[yolov5x.yaml](https://github.com/ultralytics/yolov5/blob/master/models/yolov5x.yaml),最大的一个模型进行训练，参考官方README中的[table](https://github.com/ultralytics/yolov5#pretrained-checkpoints),了解不同模型的大小和推断速度。如果你选定了一个模型，那么需要修改模型对应的`yaml`文件\n\n```yaml\n\n# parameters\nnc: 3  # number of classes   \u003c------------------  UPDATE to match your dataset\ndepth_multiple: 1.33  # model depth multiple\nwidth_multiple: 1.25  # layer channel multiple\n\n# anchors\nanchors:\n  - [10,13, 16,30, 33,23]  # P3/8\n  - [30,61, 62,45, 59,119]  # P4/16\n  - [116,90, 156,198, 373,326]  # P5/32\n\n# yolov5 backbone\nbackbone:\n  # [from, number, module, args]\n  [[-1, 1, Focus, [64, 3]],  # 1-P1/2\n   [-1, 1, Conv, [128, 3, 2]],  # 2-P2/4\n   [-1, 3, Bottleneck, [128]],\n   [-1, 1, Conv, [256, 3, 2]],  # 4-P3/8\n   [-1, 9, BottleneckCSP, [256]],\n   [-1, 1, Conv, [512, 3, 2]],  # 6-P4/16\n   [-1, 9, BottleneckCSP, [512]],\n   [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32\n   [-1, 1, SPP, [1024, [5, 9, 13]]],\n   [-1, 6, BottleneckCSP, [1024]],  # 10\n  ]\n\n# yolov5 head\nhead:\n  [[-1, 3, BottleneckCSP, [1024, False]],  # 11\n   [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]],  # 12 (P5/32-large)\n\n   [-2, 1, nn.Upsample, [None, 2, 'nearest']],\n   [[-1, 6], 1, Concat, [1]],  # cat backbone P4\n   [-1, 1, Conv, [512, 1, 1]],\n   [-1, 3, BottleneckCSP, [512, False]],\n   [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]],  # 17 (P4/16-medium)\n\n   [-2, 1, nn.Upsample, [None, 2, 'nearest']],\n   [[-1, 4], 1, Concat, [1]],  # cat backbone P3\n   [-1, 1, Conv, [256, 1, 1]],\n   [-1, 3, BottleneckCSP, [256, False]],\n   [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]],  # 22 (P3/8-small)\n\n   [[], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)\n  ]\n\n```\n\n#### 5.Train\n\n```bash\n# Train yolov5x on score for 300 epochs\n\n$ python3 train.py --img-size 640 --batch-size 16 --epochs 300 --data ./data/score.yaml --cfg ./models/score/yolov5x.yaml --weights weights/yolov5x.pt\n\n```\n\n\n#### 6.Visualize\n\n开始训练后，查看`train*.jpg`图片查看训练数据，标签和数据增强，如果你的图像显示标签或数据增强不正确，你应该查看你的数据集的构建过程是否有问题\n\n\u003cimg width=\"1000\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/train_batch0.jpg\"\u003e\n\n一个训练epoch完成后，查看`test_batch0_gt.jpg`查看batch 0 ground truth的labels\n\n\n\u003cimg width=\"1000\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/test_batch0_gt.jpg\"\u003e\n\n查看`test_batch0_pred.jpg`查看test batch 0的预测\n\n\u003cimg width=\"1000\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/test_batch0_pred.jpg\"\u003e\n\n训练的losses和评价指标被保存在Tensorboard和`results.txt`log文件。`results.txt`在训练结束后会被可视化为`results.png`\n\n```python\n\u003e\u003e\u003e from utils.utils import plot_results\n\u003e\u003e\u003e plot_results()\n# 如果你是用远程连接请安装配置Xming: https://blog.csdn.net/akuoma/article/details/82182913\n```\n\n\u003cimg width=\"1000\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/results.png\"\u003e\n\n#### 7.推断\n\n```python\n$ python3 detect.py --source file.jpg  # image \n                            file.mp4  # video\n                            ./dir  # directory\n                            0  # webcam\n                            rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa  # rtsp stream\n                            http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8  # http stream\n````\n\n\n```python\n# inference  /home/myuser/xujing/EfficientDet-Pytorch/dataset/test/ 文件夹下的图像\n$ python3 detect.py --source /home/myuser/xujing/EfficientDet-Pytorch/dataset/test/ --weights weights/best.pt --conf 0.1\n\n$ python3 detect.py --source ./inference/images/ --weights weights/yolov5x.pt --conf 0.5\n\n# inference  视频\n$ python3 detect.py --source test.mp4 --weights weights/yolov5x.pt --conf 0.4\n```\n\n\u003cimg width=\"1000\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/t1.jpg\"\u003e\n\n\u003cimg width=\"1000\" align=\"center\" alt=\"Screen Shot 2020-04-01 at 11 44 26 AM\" src=\"./readmepic/readme2/pic/20200514_p6_5_247_one.jpg\"\u003e\n\n#### 8.YOLOv5的TensorRT加速\n\n[请到这里来](./README_v3.md)\n\n\n**Reference**\n\n[1].https://github.com/ultralytics/yolov5\n\n[2].https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdataxujing%2Fyolo-v5","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdataxujing%2Fyolo-v5","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdataxujing%2Fyolo-v5/lists"}