一文彻底解决YOLOv5训练找不到标签问题

这篇具有很好参考价值的文章主要介绍了一文彻底解决YOLOv5训练找不到标签问题。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

YOLOv5 训练找不到标签, No labels found in /path/train.cache 问题的解决方法(亲测可用)

❤️ 网上绝大部分教程所述解决方法都不靠谱,也没有分析问题发生的原因,本文彻底解决了YOLOv5训练时找不到标签,出现 No labels found in /path/train.cache 的问题!希望通过本文,在配置环境的过程中,为各位解决一些不必要的麻烦。——©️ Sylvan Ding

版本 系统
YOLOv5 v6.1 Linux

出现 No labels found 的原因主要有两点,一方面是因为网上下载的数据集只提供了其专属的标签格式,需要转换成YOLOv5格式的标签;另一方面则是因为项目目录的组织问题。本文重点探讨后者,即由项目目录的组织问题而引起的找不到标签的问题,这类问题网上的解答较少。

标签格式有误

网上下载的数据集大多数只提供 VOC 格式的 .xml 标记文件,存放于 annotations 文件夹中,或是其他格式的标记文件。此时,就应当先编写程序,将标签转换成YOLOv5所需格式。

YOLOv5标签格式说明

After using a tool like Roboflow Annotate to label your images, export your labels to YOLO format, with one *.txt file per image (if no objects in image, no *.txt file is required). The *.txt file specifications are:

  • One row per object
  • Each row is class x_center y_center width height format.
  • Box coordinates must be in normalized xywh format (from 0 - 1). If your boxes are in pixels, divide x_center and width by image width, and y_center and height by image height.
  • Class numbers are zero-indexed (start from 0).

一文彻底解决YOLOv5训练找不到标签问题

一文彻底解决YOLOv5训练找不到标签问题

convert VOC to YOLOv5

VOC到YOLOv5格式转换,可以参考yolov5/data/VOC.yaml中,36行convert_label(),其中convert_box()提供了坐标转换功能。

def convert_label(path, lb_path, year, image_id):
    def convert_box(size, box):
        dw, dh = 1. / size[0], 1. / size[1]
        x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1, box[1] - box[0], box[3] - box[2]
        return x * dw, y * dh, w * dw, h * dh

    in_file = open(path / f'VOC{year}/Annotations/{image_id}.xml')
    out_file = open(lb_path, 'w')
    tree = ET.parse(in_file)
    root = tree.getroot()
    size = root.find('size')
    w = int(size.find('width').text)
    h = int(size.find('height').text)

    for obj in root.iter('object'):
        cls = obj.find('name').text
        if cls in yaml['names'] and not int(obj.find('difficult').text) == 1:
            xmlbox = obj.find('bndbox')
            bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
            cls_id = yaml['names'].index(cls)  # class id
            out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')

注:convert_box(size, box), bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])

❤️ 具体实现细节可以参考其他博主的文章,这类文章比较多。

其他格式转换成YOLOv5

对其他不同格式的标记文件,需要手动编写程序以转换成YOLOv5格式标记。

yolov5/utils/general.py中的一些函数也许能给您提供一些启发,如xyxy2xywh(), xywh2xyxy()… 他们负责坐标格式的转换。

项目目录的结构有误

在得到正确的标签格式后,仍出现No labels found错误,这时考虑项目目录组织结构出现错误。

正确的目录结构

coco

⭐️ 先放结论,以COCO为例,正确的目录结构应当为:

# path example
../datasets/coco128/images/im0.jpg  # image
../datasets/coco128/labels/im0.txt  # label
# yolov5/data/coco.yaml
path: ../datasets/coco  # dataset root dir
train: train2017.txt  # train images (relative to 'path')
val: val2017.txt  # val images
test: test-dev2017.txt

一文彻底解决YOLOv5训练找不到标签问题

  • datasets文件夹和yolov5文件夹同级,datasets下建立各个数据集文件。以coco为例,images文件夹直接存放所有图片数据labels文件夹直接存放图片对应的*.txt标记文件。

    .
    ├── images
    │   ├── 20151127_114556.jpg
    │   ├── 20151127_114946.jpg
    │   └── 20151127_115133.jpg
    ├── labels
    │   ├── 20151127_114556.txt
    │   ├── 20151127_114946.txt
    │   └── 20151127_115133.txt
    
  • 注意,imageslabels文件夹的命名均不能改成别的!原因稍后再说。

  • train2017.txt, val2017.txt,test-dev2017.txt中存放训练集、验证集、测试集的图片文件路径,其内容如下所示:

    ./images/20151127_114556.jpg
    ./images/20151127_114946.jpg
    ./images/20151127_115133.jpg
    
coco128

如果你想用coco128的文件组织形式:

# yolov5/data/coco128.yaml
path: ../datasets/coco128  # dataset root dir
train: images/train2017  # train images (relative to 'path') 128 images
val: images/train2017  # val images (relative to 'path') 128 images
test:  # test images (optional)

datasets目录结构应当为:

coco128
├── images
│   ├── test
│   │   └── 20151127_115133.jpg
│   └── train2017
│       └── 20151127_114556.jpg
└── labels
    ├── test
    │   └── 20151127_115133.txt
    └── train2017
        └── 20151127_114556.txt
  • 注意,imageslabels文件夹的命名均不能改成别的
  • imageslabels文件夹内,创建相互对应的文件夹用来存放训练集、验证集、测试集,文件夹名字要一致,没有要求,但需要在coco128.yaml中设置。

错误原因探究

yolov5/utils/datasets.py 391行 img2label_paths(img_paths) 定义了图片路径到标签路径的映射,447行 self.label_files = img2label_paths(self.im_files) # labels 调用 img2label_paths() 以生成 label_files.

def img2label_paths(img_paths):
    # Define label paths as a function of image paths
    sa, sb = os.sep + 'images' + os.sep, os.sep + 'labels' + os.sep  # /images/, /labels/ substrings
    return [sb.join(x.rsplit(sa, 1)).rsplit('.', 1)[0] + '.txt' for x in img_paths]

YOLOv5 locates labels automatically for each image by replacing the last instance of /images/ in each image path with /labels/.

即YOLOv5会自动将图片路径../datasets/coco128/images/*.jpg更改为../datasets/coco128/labels/*.txt,以寻找labels路径!

如何解决问题?

在上述 label_files 赋值后,打印 label_files,我们就能得到标记的路径,再根据打印出的路径修改自己项目的文件路径,方能解决一切问题!

一文彻底解决YOLOv5训练找不到标签问题

参考文献

Cannot find labels in train.cache custom dataset COCO #6158文章来源地址https://www.toymoban.com/news/detail-453232.html

到了这里,关于一文彻底解决YOLOv5训练找不到标签问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决YOLOv5训练自己的数据集出现No labels in path\train.cache问题

    不知道是第几次训练了,最开始跑也出现了这个问题,当时怎么解决的时隔了几个月又完全忘了,还好翻看了几个博客后回忆了起来 我自己的数据集的格式是VOC格式,如下图  若没有对数据集进行划分,则使用makeTXT.py对数据集进行划分,若数据集已经划分,则可忽略这一步

    2024年02月12日
    浏览(36)
  • YOLOv5训练过程中遇到该问题的解决方法ValueError: The requested array has an inhomogeneous shape after 1 dimensions

    YOLOv5训练时遇到问题ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions.可以参考以下解决方案 问题分析: 数组append时前后数组的shape不一致,当时我在自己遇到问题时也没有找到解决方法,最后发现是训练集中有一个图片名字太长导致

    2024年02月11日
    浏览(33)
  • YOLOV5 训练好模型测试时出现问题:AttributeError: ‘Upsample‘ object has no attribute ‘recompute_scale_factor‘的解决方法

    在使用YOLOV5 训练好模型测试时出现问题:AttributeError: ‘Upsample’ object has no attribute \\\'recompute_scale_factor’的快速解决方法。 解决方法一: 有些博主说降低torchhe和torchvision版本,比如上图所示我的torch版本1.11.0 torchvision版本0.10.2,torch版本降低到版本1.9.1,torchvision版本降低到版

    2024年02月12日
    浏览(36)
  • yolov5训练自己的数据集问题排除

    D:ProgramDataAnaconda3envsyolov5python.exe D:/yxt/yolov5-master/train.py Traceback (most recent call last):   File \\\"D:ProgramDataAnaconda3envsyolov5libsite-packagesgit__init__.py\\\", line 140, in module     refresh()   File \\\"D:ProgramDataAnaconda3envsyolov5libsite-packagesgit__init__.py\\\", line 127, in refresh     if not Git.refresh(p

    2024年04月11日
    浏览(40)
  • yolov5继续训练的方法,没解决sad

    目录 尝试1--唯一运行成功的 尝试2 尝试3 尝试4--希望最大 尝试5 后续成功! 前提 :虽然成功训练完了,但是想到以后万一训练轮数太少没收敛,怎么在已经训练好的模型基础上继续进行多轮epoch的训练。或者训练着突然中断,之前训练的岂不是功亏一篑。 起因 :在kaggle上训

    2024年02月05日
    浏览(27)
  • YOLOv5训练速度慢的一些解决方法

        博主电脑配置是AMD R5 3600,Nvidia RTX3060 12G,16G 3200MHz内存,训练数据集是自建数据集,大约1200张图片,3个检测目标。     训练YOLOv5-5.0版本的模型参数设置,模型是yolov5s,epoch 150(如果想要更好的mAP@0.5:0.95指标可以设置的更大,博主这个收敛的太快了就没设太多),bat

    2024年01月16日
    浏览(31)
  • 【YOLOv5】一些网上找不到答案的报错解决方案

    目录 AssertionError: Label class 4 exceeds nc=4 in /xxxxxx解决方法 原因 解决方法:(以我的情况为例) RuntimeError: result type Float can‘t be cast to the desired output type long int 原因 解决方法 ImportError: libgthread-2.0.so.0: cannot open shared object file: tensorboard :No dashboards are active for the current data set. 问题

    2024年02月12日
    浏览(24)
  • YOLOV5训练时P、R、mAP等值均为0的问题

    当YOLOv5的训练P、R、mAP等指标为0时,通常有以下一些原因: 数据集质量不佳:检查数据集中是否存在较大的类别不平衡或者太多的噪声。可能需要重新清理数据集以确保标签正确且具有可解释性。 学习率过高或过低:首先尝试将学习率降低到一个更合适的水平,并考虑使用

    2024年02月04日
    浏览(28)
  • 【Yolov5+Deepsort】训练自己的数据集(3)| 目标检测&追踪 | 轨迹绘制 | 报错分析&解决

    📢前言: 本篇是关于 如何使用YoloV5+Deepsort训练自己的数据集 ,从而实现目标检测与目标追踪,并绘制出物体的运动轨迹。本章讲解的为第三部分内容:数据集的制作、Deepsort模型的训练以及动物运动轨迹的绘制。本文中用到的数据集均为自采,实验动物为斑马鱼。 💻环境

    2024年02月10日
    浏览(34)
  • 训练yolov5的那些事之解决:AssertionError: Label class x exceeds nc=x in data/yolov5.yaml. Possible class label

    Yolov5报错: AssertionError: Label class x exceeds nc=x in data/yolov5.yaml. Possible class labels are 0-x-1 File “C:Users1Desktop水表识别YOLO5yolov5-mastertrain.py”, line 175, in train assert mlc nc, ‘Label class %g exceeds nc=%g in %s. Possible class labels are 0-%g’ % (mlc, nc, opt.data, nc - 1) 找到train文件的175行: 改成这样

    2024年02月11日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包