Giter VIP home page Giter VIP logo

yolo-v5's Introduction

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

yolo-v5's People

Contributors

dataxujing avatar glenn-jocher avatar pcgeek86 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

yolo-v5's Issues

pytorch 版本?

python3 train.py --img-size 640 --batch-size 16 --epochs 300 --data ./data/my.yaml --cfg ./models/my_yolov5x.yaml --weights weights/yolov5s.pt --device 1

错误1:AttributeError: module 'torch.nn' has no attribute 'Hardswish',这是只有torch 1.6.0 才有的模块
错误2:torch.nn.modules.module.ModuleAttributeError: 'BatchNorm2d' object has no attribute '_non_persistent_buffers_set' 根据 https://github.com/ultralytics/yolov5/issues/58,得装torch 1.5.1

所以我要装哪个版本?

无法使用GPU训练

您好,我似乎无法使用GPU进行训练,我做了一些尝试但都没有奏效,很抱歉麻烦您,这或许是一个简单的问题,可他确实困扰了很久,希望可以得到您的帮助,告诉我我应该做什么更改呢?感谢您!

When I train with CPU, the error message shows that the parameter types are inconsistent

I'm trying to train my network with CPU,and the command is"python train.py --img 640 --batch 2 --epochs 2 --data coco.yaml --weights yolov5s.pt --device cpu".

But I received an error:

Traceback (most recent call last):
File "train.py", line 518, in
train(hyp, opt, device, tb_writer, wandb)
File "train.py", line 412, in train
results, _, _ = test.test(opt.data,
File "D:\Algorithm\algorithm_lin\PANDA-project-tianchi-zl\yolov5-master\test.py", line 118, in test
inf_out, train_out = model(img, augment=augment) # inference and training outputs
File "D:\SoftwareInstallation\Anaconda\envs\yolov5_env\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Algorithm\algorithm_lin\PANDA-project-tianchi-zl\yolov5-master\models\yolo.py", line 119, in forward
return self.forward_once(x, profile) # single-scale inference, train
File "D:\Algorithm\algorithm_lin\PANDA-project-tianchi-zl\yolov5-master\models\yolo.py", line 135, in forward_once
x = m(x) # run
File "D:\SoftwareInstallation\Anaconda\envs\yolov5_env\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Algorithm\algorithm_lin\PANDA-project-tianchi-zl\yolov5-master\models\common.py", line 112, in forward
return self.conv(torch.cat([x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))
File "D:\SoftwareInstallation\Anaconda\envs\yolov5_env\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\Algorithm\algorithm_lin\PANDA-project-tianchi-zl\yolov5-master\models\common.py", line 40, in fuseforward
return self.act(self.conv(x))
File "D:\SoftwareInstallation\Anaconda\envs\yolov5_env\lib\site-packages\torch\nn\modules\module.py", line 727, in _call_impl
result = self.forward(*input, **kwargs)
File "D:\SoftwareInstallation\Anaconda\envs\yolov5_env\lib\site-packages\torch\nn\modules\conv.py", line 431, in forward
return self._conv_forward(input, self.weight)
File "D:\SoftwareInstallation\Anaconda\envs\yolov5_env\lib\site-packages\torch\nn\modules\conv.py", line 427, in _conv_forward
return F.conv2d(input, weight, self.bias, self.stride,
RuntimeError: Input type (torch.FloatTensor) and weight type (torch.HalfTensor) should be the same

How should I solve this problem?Thanks!

the version of the yolov5

Hi, because the author of the yolov5 published the v5 4.0 in January.
And can you tell me what's your version number?

pytorch版本

你好!我想知道你对于作者原版的代码做了哪些修改,使得它能够在pytorch1.3和torchvision0.4.1上工作呢?我看到作者现在的代码要求torch>1.5。

训练一开始报错MemoryError

当我执行train的时候,系统报错:Can't pickle <class 'MemoryError'>: it's not the same object as builtins.MemoryError

xml转换txt文件失败

你好xujiang,非常感谢你为yolov5中文社区创造的这篇实例教程,awesome!但是我在制作自己的数据时遇到了问题:我在使用./datasets/路径下,01_xxx.py、02_xxx.py、03_xxx.py、04_xxx.py程序时遇到了困难,比如annotation文件夹,myJPJE文件夹我都没有创建,我在想是不是您在制作自己的数据时运用的路径,(但是我clonenin的项目中,或者教程中没有发现类似描述)如果是的话,可否告知下您在制作阶段是如何组织文件夹、数据结构的。万分感谢!!!

AttributeError: 'function' object has no attribute 'names'

在我已经训练完自己的数据集推理的时候,使用的是best.pt,结果出现了这个问题
File "detect.py", line 164, in
detect()
File "detect.py", line 48, in detect
names = model.names if hasattr(model, 'names') else model.modules.names
AttributeError: 'function' object has no attribute 'names'

你能告诉我怎么解决吗?谢谢

标签

请问没有目标物体的图片怎么转换数据格式,label.txt,图片上一个物体都没有,加入训练

训练

请问你尝试过调参吗,有没有大的提升?

error

where is resnet101.pt ???

出现未知变量

Traceback (most recent call last):
File "G:/YOLO-v5-master/train.py", line 399, in
train(hyp)
File "G:/YOLO-v5-master/train.py", line 201, in train
tb_writer.add_histogram('classes', c, 0)
NameError: name 'tb_writer' is not defined
这是报错的原因,'tb_writer'哪来的呢,没有定义

数据加载问题

博主你好,请问我想要在yaml文件中改网络格式,想要输入的是两个模态的数据(红外和可见光模态)。现在需要有两个输入的数据集要被读到,博主可以给一些建议吗,十分感谢了。

训练时出现segmentation fault

您好,我在使用coco17数据集训练yolov5s时,训练出现segmentation fault,如下图所示:
image
在训练中,出现segmentation fault的时间点是随机的,我曾经尝试了多次,每一次都不一样。

我的环境是pytorch==1.3.0,torchvision==0.4.1,cuda10.2。

补充:使用cuda 10.0也出现了同样的问题

多GPU训练

您好作者,您的这篇复现对我很有帮助。我已经训练完成并在自己的数据集上得到了很好的效果。

遇到了一些问题,但目前不知如何解决。
但我尝试使用 --device 1,2 这个参数,稍微调大batch_size的时候,
提示out-of-memory。我是有三块3090的,如果同时调用两块gpu应该不会报超出显存的错误。
我使用 --device 1,2 时候,只调用gpu1,另外一块无法同时调用。不知道是什么问题。

我使用了pytorch=1.10,cuda11.3,python3.6

RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.

Traceback (most recent call last):
File "/home/hqu/yolov5/models/yolo.py", line 274, in
model = Model(opt.cfg).to(device)
File "/home/hqu/yolov5/models/yolo.py", line 95, in init
self._initialize_biases() # only run once
File "/home/hqu/yolov5/models/yolo.py", line 150, in _initialize_biases
b[:,4] = b[:,4]+math.log(8 / (640 / s) ** 2) # obj (8 objects per 640 image)
RuntimeError: a view of a leaf Variable that requires grad is being used in an in-place operation.
博主,我在train过程中报这个错误,不知道怎么解决了,我的环境是RTX3090+CUDA11+pytorch1.8。跑ultralytics的yolo.py也报这个错误。

yolov5 版本

您好 当将训练好的模型用tensorrt加速时出现了网络不匹配的问题,请问您当时是用的哪个版本的yolov5呢 ? 是v2.0还是3.0,或者是其他版本的?

test

您好!
image
test使用GPU,bug,是代码不支持吗~

AttributeError: 'function' object has no attribute 'names'

您好,非常感谢您提供的项目代码。最近我使用训练好的模型进行检测时,显示如下报错:
Traceback (most recent call last):
File "detect.py", line 164, in
detect()
File "detect.py", line 48, in detect
names = model.names if hasattr(model, 'names') else model.modules.names
AttributeError: 'function' object has no attribute 'names'

但是前几天我用另外一个数据集做训练和检测时,没有发现类似的问题,我在yaml中把相关内容都改成了符合我数据集的参数。而且我发现models/yolov5x.yaml 和 models/score/yolov5x.yaml这两个文件中的参数不一样。我参考了issue9 也没找到答案,请问您知道这是什么原因吗?期待您的回复。☺

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.