• Stars
    star
    750
  • Rank 60,067 (Top 2 %)
  • Language
    Jupyter Notebook
  • License
    GNU General Publi...
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

🎨 Pytorch YOLO v5 训练自己的数据集超详细教程!!! 🎨 (提供PDF训练教程下载)

YOLO v5在医疗领域中消化内镜目标检测的应用

YOLO v5训练自己数据集详细教程

🐛 🐛 现在YOLOv5 已经更新到6.0版本了,但是其训练方式同本Repo是一致的,只需要按照对应版本安装对应Python环境即可,其数据集的构建,配置文件的修改,训练方式等完全与本Repo一致!

🐛 🐛 我们提供了YOLOv5 TensorRT调用和INT8量化的C++和Python代码(其TensorRT加速方式不同于本Repo提供的TensorRT调用方式),有需要的大佬可在issues中留言!

Xu Jing


🔥 由于官方新版YOLO v5的backbone和部分参数调整,导致很多小伙伴下载最新官方预训练模型不可用,这里提供原版的YOLO v5的预训练模型的百度云盘下载地址

链接:https://pan.baidu.com/s/1SDwp6I_MnRLK45QdB3-yNw 提取码:423j


  • YOLOv4还没有退热,YOLOv5已经发布!

  • 6月9日,Ultralytics公司开源了YOLOv5,离上一次YOLOv4发布不到50天。而且这一次的YOLOv5是完全基于PyTorch实现的!

  • YOLO v5的主要贡献者是YOLO v4中重点介绍的马赛克数据增强的作者

本项目描述了如何基于自己的数据集训练YOLO v5

但是YOLO v4的二作提供给我们的信息和官方提供的还是有一些出入:

0.环境配置

安装必要的python package和配置相关环境

# python3.6
# torch==1.3.0
# torchvision==0.4.1

# git clone yolo v5 repo
git clone https://github.com/ultralytics/yolov5 # clone repo
# 下载官方的样例数据(这一步可以省略)
python3 -c "from yolov5.utils.google_utils import gdrive_download; gdrive_download('1n_oKgR81BJtqk75b00eAjdv03qVCQn2f','coco128.zip')" # download dataset
cd yolov5
# 安装必要的package
pip3 install -U -r requirements.txt

1.创建数据集的配置文件dataset.yaml

data/coco128.yaml来自于COCO train2017数据集的前128个训练图像,可以基于该yaml修改自己数据集的yaml文件

# train and val datasets (image directory or *.txt file with image paths)
train: ./datasets/score/images/train/
val: ./datasets/score/images/val/

# number of classes
nc: 3

# class names
names: ['QP', 'NY', 'QG']

2.创建标注文件

可以使用LabelImg,Labme,Labelbox, CVAT来标注数据,对于目标检测而言需要标注bounding box即可。然后需要将标注转换为和darknet format相同的标注形式,每一个图像生成一个*.txt的标注文件(如果该图像没有标注目标则不用创建*.txt文件)。创建的*.txt文件遵循如下规则:

  • 每一行存放一个标注类别
  • 每一行的内容包括class x_center y_center width height
  • Bounding box 的坐标信息是归一化之后的(0-1)
  • class label转化为index时计数是从0开始的
def convert(size, box):
    '''
    将标注的xml文件标注转换为darknet形的坐标
    '''
    dw = 1./(size[0])
    dh = 1./(size[1])
    x = (box[0] + box[1])/2.0 - 1
    y = (box[2] + box[3])/2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x*dw
    w = w*dw
    y = y*dh
    h = h*dh
    return (x,y,w,h)

每一个标注*.txt文件存放在和图像相似的文件目录下,只需要将/images/*.jpg替换为/lables/*.txt即可(这个在加载数据时代码内部的处理就是这样的,可以自行修改为VOC的数据格式进行加载)

例如:

datasets/score/images/train/000000109622.jpg  # image
datasets/score/labels/train/000000109622.txt  # label

如果一个标注文件包含5个person类别(person在coco数据集中是排在第一的类别因此index为0):

Screen Shot 2020-04-01 at 11 44 26 AM

3.组织训练集的目录

将训练集train和验证集val的images和labels文件夹按照如下的方式进行存放

Screen Shot 2020-04-01 at 11 44 26 AM

至此数据准备阶段已经完成,过程中我们假设算法工程师的数据清洗和数据集的划分过程已经自行完成。

4.选择模型backbone进行模型配置文件的修改

在项目的./models文件夹下选择一个需要训练的模型,这里我们选择yolov5x.yaml,最大的一个模型进行训练,参考官方README中的table,了解不同模型的大小和推断速度。如果你选定了一个模型,那么需要修改模型对应的yaml文件

# parameters
nc: 3  # number of classes   <------------------  UPDATE to match your dataset
depth_multiple: 1.33  # model depth multiple
width_multiple: 1.25  # layer channel multiple

# anchors
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

# yolov5 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Focus, [64, 3]],  # 1-P1/2
   [-1, 1, Conv, [128, 3, 2]],  # 2-P2/4
   [-1, 3, Bottleneck, [128]],
   [-1, 1, Conv, [256, 3, 2]],  # 4-P3/8
   [-1, 9, BottleneckCSP, [256]],
   [-1, 1, Conv, [512, 3, 2]],  # 6-P4/16
   [-1, 9, BottleneckCSP, [512]],
   [-1, 1, Conv, [1024, 3, 2]], # 8-P5/32
   [-1, 1, SPP, [1024, [5, 9, 13]]],
   [-1, 6, BottleneckCSP, [1024]],  # 10
  ]

# yolov5 head
head:
  [[-1, 3, BottleneckCSP, [1024, False]],  # 11
   [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]],  # 12 (P5/32-large)

   [-2, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4
   [-1, 1, Conv, [512, 1, 1]],
   [-1, 3, BottleneckCSP, [512, False]],
   [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]],  # 17 (P4/16-medium)

   [-2, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3
   [-1, 1, Conv, [256, 1, 1]],
   [-1, 3, BottleneckCSP, [256, False]],
   [-1, 1, nn.Conv2d, [na * (nc + 5), 1, 1, 0]],  # 22 (P3/8-small)

   [[], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]

5.Train

# Train yolov5x on score for 300 epochs

$ python3 train.py --img-size 640 --batch-size 16 --epochs 300 --data ./data/score.yaml --cfg ./models/score/yolov5x.yaml --weights weights/yolov5x.pt

6.Visualize

开始训练后,查看train*.jpg图片查看训练数据,标签和数据增强,如果你的图像显示标签或数据增强不正确,你应该查看你的数据集的构建过程是否有问题

Screen Shot 2020-04-01 at 11 44 26 AM

一个训练epoch完成后,查看test_batch0_gt.jpg查看batch 0 ground truth的labels

Screen Shot 2020-04-01 at 11 44 26 AM

查看test_batch0_pred.jpg查看test batch 0的预测

Screen Shot 2020-04-01 at 11 44 26 AM

训练的losses和评价指标被保存在Tensorboard和results.txtlog文件。results.txt在训练结束后会被可视化为results.png

>>> from utils.utils import plot_results
>>> plot_results()
# 如果你是用远程连接请安装配置Xming: https://blog.csdn.net/akuoma/article/details/82182913

Screen Shot 2020-04-01 at 11 44 26 AM

7.推断

$ python3 detect.py --source file.jpg  # image 
                            file.mp4  # video
                            ./dir  # directory
                            0  # webcam
                            rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa  # rtsp stream
                            http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8  # http stream
# inference  /home/myuser/xujing/EfficientDet-Pytorch/dataset/test/ 文件夹下的图像
$ python3 detect.py --source /home/myuser/xujing/EfficientDet-Pytorch/dataset/test/ --weights weights/best.pt --conf 0.1

$ python3 detect.py --source ./inference/images/ --weights weights/yolov5x.pt --conf 0.5

# inference  视频
$ python3 detect.py --source test.mp4 --weights weights/yolov5x.pt --conf 0.4

Screen Shot 2020-04-01 at 11 44 26 AM

Screen Shot 2020-04-01 at 11 44 26 AM

8.YOLOv5的TensorRT加速

请到这里来

Reference

[1].https://github.com/ultralytics/yolov5

[2].https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

More Repositories

1

vehicle-license-plate-recognition

🔥 🔥🔥基于Python的车牌检测和识别系统:
Python
608
star
2

YOLO-V3-Tensorflow

👷 👷👷 YOLO V3(Tensorflow 1.x) 安全帽 识别 | 提供数据集下载和与预训练模型
Python
213
star
3

CNN-paper2

🎨 🎨 深度学习 卷积神经网络教程 :图像识别,目标检测,语义分割,实例分割,人脸识别,神经风格转换,GAN等🎨🎨 https://dataxujing.github.io/CNN-paper2/
133
star
4

TensorRT_CV

🚀🚀🚀NVIDIA TensorRT 加速推断教程!
CSS
127
star
5

TensorRT-DETR

⚡⚡⚡NVIDIA-阿里2021 TRT比赛 `二等奖` 代码提交 团队:美迪康 AI Lab 🚀🚀🚀
Python
123
star
6

YOLOv8

🔥 Official YOLOv8模型训练和部署
Python
122
star
7

ScaledYOLOv4

🔥🔥🔥 Scaled-YOLOv4训练自己的数据集详细教程PDF,关于paper解读请联系小编获取PDF文档
Python
72
star
8

Faster-R-CNN-Keras

🚧🚧🚧 Faster R-CNN实现安防中安全帽佩戴目标检测
Python
65
star
9

xiaoX

flask+seq2seq【TensorFlow1.0, Pytorch】 🎨 🎨 在线聊天机器人 https://mp.weixin.qq.com/s/VpiAmVSTin3ALA8MnzhCJA 或 https://ask.hellobi.com/blog/python_shequ/14486
Python
61
star
10

yolact_pytorch

🔥 🔥 🔥Train Your Own DataSet for YOLACT and YOLACT++ Instance Segmentation Model!!!
Python
56
star
11

DIoU_YOLO_V3

📈📈📈【口罩佩戴检测数据训练 | 开源口罩检测数据集和预训练模型】Train D/CIoU_YOLO_V3 by darknet for object detection
Jupyter Notebook
55
star
12

YOLOR-

🔥🔥🔥 YOLOR 训练自己的数据集,详细教程
Python
44
star
13

detr_transformer

transformer used in object detection [DETR训练自己的数据集]
Python
39
star
14

YOLOX-

🔥 🔥 🔥 YOLOX 训练自己的数据集 TensorRT加速 详细教程
Python
37
star
15

OpenCV-Flask

🐛 🐛 Opencv视频流传输到网页浏览器并做目标检测 🐛 🐛
CSS
36
star
16

CornerNet-Lite-Pytorch

🚨🚨🚨 CornerNet:基于虚拟仿真环境下的自动驾驶交通标志识别
Python
34
star
17

YOLOv7

🔥🔥🔥 Official YOLOv7训练自己的数据集并实现端到端的TensorRT模型加速推断
Python
33
star
18

EfficientDet_pytorch

🎨 🎨 EfficientDet训练水下目标检测数据集🎨🎨
Python
30
star
19

create_apps_in_kivy

🔥🔥🔥 Kivy开发教程,附PDF文档书籍https://mp.weixin.qq.com/s/YKf8IzO76kjIMD5xMhEz-g
HTML
30
star
20

ncnn_android_yolov8

QT+NCNN 小米手机运行YOLOv8s
Python
29
star
21

Pytorch_YOLO-v4

🎨 Pytorch YOLO v4 训练自己的数据集超详细教程!!! 🎨(提供PDF训练教程下载)
Python
27
star
22

R_oop

🎨 R语言面向对象编程教程 https://dataxujing.github.io/R_oop/
CSS
27
star
23

NLP-paper

🎨 🎨NLP 自然语言处理教程 🎨🎨 https://dataxujing.github.io/NLP-paper/
26
star
24

YOLOv6

🌀 🌀 手摸手 美团 YOLOv6模型训练和TensorRT端到端部署方案教程
Python
23
star
25

tensorflow-serving-Wechat

🎨🎨 tensorflow serving and deep model online https://dataxujing.github.io/tensorflow-serving-Wechat/?transition=convex#/
JavaScript
19
star
26

Qt_NCNN_NanoDet

🔥🔥🔥 QT+NCNN实现安卓系统下的AI计算
C++
17
star
27

lanenet-tensorrt

🔥 🔥 🔥 车道线检测Lanenet TensorRT加速C++实现
C++
16
star
28

ExtremeNet-Pytorch

🐛🐛🐛 Train and inference ExtremeNet in Pytorch! Support Bounding Box and Instance Segmentation train data!!
Python
12
star
29

Cascade_RCNN_mmdetection

🐛🐛🐛Cascade RCNN 的训练基于mmdetection
Python
10
star
30

Bert_TensorRT

🐛 Bert TensorRT模型加速部署
Python
10
star
31

boston_model

🎨 🎨 https://mp.weixin.qq.com/s/7t0e_hfyDh1b2GPVlzXIMg 或 https://yq.aliyun.com/articles/636272
Python
10
star
32

ncnn_android_yolov6

ncnn qt yolov6
C++
10
star
33

ATSS_train_your_own_data

🔥🔥 用ATSS训练自己的目标检测模型!! 超详细教程和PDF教程下载!!!
Python
9
star
34

lane-detect-opencv

opencv车道线检测
Python
7
star
35

yolact_tensorrt_api

🔥 我的NVIDIA开发之旅--实例分割模型YOLACT的TensorRT API模型搭建与推断加速实战
7
star
36

tensorrt_deeposrt_yolov5

C++
6
star
37

R_online

https://dataxujing.github.io/R_online/
CSS
6
star
38

gcForest_r

🎨 🎨 R package for gcForest [ https://CRAN.R-project.org/package=gcForest ][ https://github.com/cran/gcForest ]
Python
6
star
39

Ricetl

身份证信息提取Gui R包,Ricetl()翻转你的身份证号: https://cran.r-project.org/web/packages/Ricetl/vignettes/Ricetl-doc.html
R
6
star
40

TensorRT-LLM-ChatGLM3

🔥 大模型部署实战:TensorRT-LLM, Triton Inference Server, vLLM
Python
6
star
41

DCGAN_pytorch

🎨🎨基于PyTorch的生成对抗网络DCGAN的训练
Python
5
star
42

MobileNet_V3_pytorch

A Pytorch implementation of MobileNet V3!
Python
5
star
43

dataxujing.github.io

🔥 🔥 🔥 xujing's home page https://dataxujing.github.io/
CSS
4
star
44

Mask-RCNN-TensorRT

🐛 TensorRT 实现Mask RCNN推断
C++
4
star
45

PNASNet_pytorch

🐛 Pytorch实现PNASNet的训练与测试
Python
4
star
46

plotbox2

🔥🔥🔥A python package for plot bounding box in object detection
Python
4
star
47

SSD-Tensorflow

🎨🎨🎨 SSD-Tensorflow目标检测基于医学影像的消化内镜活检钳
Python
3
star
48

libtorch_tutorials

🐛🐛🐛libtorch非官方教程🐛🐛🐛
3
star
49

Quick-Demo-from-Pytorch-to-Tensort-cpp

A simple and quick example shows how to convert the model of Pytoch to ONNX, and then deploy it with tensorrt in C++
C++
3
star
50

django-qr

Django web实现各种二维码生成
HTML
3
star
51

AI-Face-changing-in-Peking-Opera-

京剧换脸
Python
3
star
52

RetinaNet-Keras

✏️✏️✏️ RetinaNet目标检测模型训练和推断
Python
2
star
53

AIGC-paper

AIGC learning
2
star
54

VOT_paper

视觉目标跟踪(VOT)及ReID教程: https://dataxujing.github.io/VOT_paper/#/
2
star
55

ICSC

A R package for Inter-credit.
R
2
star
56

Mask_RCNN_keras

🐛🐛 Keras训练Mask RCNN教程,包括训练集构建,配置文件修改,训练,推断
Python
2
star
57

xcamera-android

kivy xcamera for android
Python
2
star
58

IncomeAnalysis

网络爬虫+数据可视化 🎨 🎨 https://dataxujing.github.io/IncomeAnalysis/
HTML
2
star
59

django2-ueditor

🔥🔥🔥Django2中集成xadmin和ueditor
Python
2
star
60

htmlwidgets_CN

🎨🎨 https://dataxujing.github.io/htmlwidgets_CN/
HTML
2
star
61

deepfakes

AI 图片视频换脸实现
2
star
62

DataXujing

1
star
63

Word_segment_CN

🎨 中文分词算法小结
Python
1
star
64

SpotM2-Jetson

🐶 【NVIDIA Jetson Edge AI开发者大赛】-- 美迪康 AI Lab团队代码提交
Python
1
star
65

idprep

A R packages to transmit ID number
R
1
star
66

IRSModel_py_shiny

IRSModel with Py and shiny
R
1
star
67

CNN-paper

🎨 CNN model learn : **该项目已停止更新**,最新项目请到链接:https://github.com/DataXujing/CNN-paper2
HTML
1
star
68

jsonr

A package about json string and R
R
1
star
69

detmetric

Python
1
star
70

Icics

A Python package for Inter-credit
Python
1
star
71

OpenCV_Multithreaded

🎨 🎨 🎨 多线程解决非实时深度学习模型与OpenCV结合的卡顿问题
Python
1
star
72

Install-R-rstudio-server-shiny-server-git

ubuntu 16.04 install some solft about open source R by XuJing
1
star
73

LN0SCIs3

A R package that used to compute simultaneous confidence intervals https://cran.r-project.org/web/packages/LN0SCIs/index.html 或 https://cran.r-project.org/web/packages/LN0SCIs/vignettes/LN0SCIs-tutorial.html
R
1
star
74

LN0SCIs

A R and Python Packages https://pypi.org/project/LN0SCIs/ and https://cran.r-project.org/web/packages/LN0SCIs/index.html
R
1
star