【bug记录】yolov7 AssertionError: train: No labels in ...train_list.cache. Can not train without

在尝试使用Yolov7训练VisDrone数据集时遇到警告,提示没有在train_list.cache中找到标签。解决方案是检查train_list.txt中的路径是否为绝对路径,并提供了一段Python代码来生成包含绝对路径的test_list.txt文件。
部署运行你感兴趣的模型镜像

问题描述

我在用yolov7跑自己的visdrone数据集时,遇到了如下报错:

train: WARNING: No labels found in datasets/VisDrone/train_list.cache. See https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data                                                 
train: New cache created: datasets/VisDrone/train_list.cache                                                                                                                            
Traceback (most recent call last):                                                                                                                                                      
  File "train.py", line 616, in <module>                                                                                                                                                
    train(hyp, opt, device, tb_writer)                                                                                                                                                  
  File "train.py", line 245, in train                                                                                                                                                   
    dataloader, dataset = create_dataloader(train_path, imgsz, batch_size, gs, opt,                                                                                                     
  File "/disk2/lxs/yolov7/utils/datasets.py", line 69, in create_dataloader                                                                                                             
    dataset = LoadImagesAndLabels(path, imgsz, batch_size,                                                                                                                              
  File "/disk2/lxs/yolov7/utils/datasets.py", line 403, in __init__                                                                                                                     
    assert nf > 0 or not augment, f'{prefix}No labels in {cache_path}. Can not train without labels. See {help_url}'                                                                    
AssertionError: train: No labels in datasets/VisDrone/train_list.cache. Can not train without labels. See https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data   


解决方案:

提示:如果train_list.txt中使用的是相对路径,建议全部改为绝对路径。生成train_list.txt代码如下

# -*-coding:utf-8-*-
# 生成文件夹中所有文件的路径到txt
import os


def listdir(path, list_name):  # 传入存储的list
    for file in os.listdir(path):
        file_path = os.path.join(path, file)
        if os.path.isdir(file_path):
            listdir(file_path, list_name)
        else:
            list_name.append(file_path)


list_name = []
path = '/disk2/lxs/yolov7/datasets/VisDrone/images/test/'  # 文件夹路径
listdir(path, list_name)
print(list_name)

with open('./datasets/VisDrone/test_list.txt', 'w') as f:  # 要存入的txt
    write = ''
    for i in list_name:
        write = write + str(i) + '\n'
    f.write(write)

您可能感兴趣的与本文相关的镜像

Yolo-v5

Yolo-v5

Yolo

YOLO(You Only Look Once)是一种流行的物体检测和图像分割模型,由华盛顿大学的Joseph Redmon 和Ali Farhadi 开发。 YOLO 于2015 年推出,因其高速和高精度而广受欢迎

### 问题分析 在运行YOLOv7时遇到的 `AssertionError: train: No labels in data\coco\train.cache. Can not train without labels.` 错误表明训练数据集中缺少标签文件(labels),这导致模型无法正常进行训练。此错误通常与数据集格式不正确或路径配置错误有关。 --- ### 解决方案 #### 1. 检查数据集格式 确保训练数据集严格遵循YOLOv7所要求的数据集格式。根据引用[^2],如果文件夹名称不符合规范(例如使用了大写字母 "Images" 而非 "images"),可能会导致标签文件未被正确加载。以下是正确的数据集目录结构: ``` dataset/ ├── images/ │ ├── train/ │ │ ├── image1.jpg │ │ ├── image2.jpg │ │ └── ... │ └── val/ │ ├── image1.jpg │ ├── image2.jpg │ └── ... └── labels/ ├── train/ │ ├── image1.txt │ ├── image2.txt │ └── ... └── val/ ├── image1.txt ├── image2.txt └── ... ``` - **images** 文件夹包含所有图像文件。 - **labels** 文件夹包含对应的标签文件(`.txt` 格式)。每个标签文件的内容应符合YOLO格式,即每行表示一个目标框: ``` <class_id> <x_center> <y_center> <width> <height> ``` --- #### 2. 验证 `train.cache` 文件 `train.cache` 是YOLOv7生成的一个缓存文件,用于加速数据加载。如果标签文件缺失或路径配置错误,可能导致该文件无法正确生成。以下步骤可验证并修复此问题: - **删除旧缓存文件**:在数据集目录中查找 `train.cache` 和 `val.cache` 文件,并将其删除。重新运行训练脚本时,YOLOv7会重新生成这些文件。 ```bash rm -f data/coco/train.cache rm -f data/coco/val.cache ``` - **检查数据集配置文件**:确保 `data.yaml` 文件中的路径配置正确。例如: ```yaml train: ../dataset/images/train val: ../dataset/images/val nc: 80 # 类别数量 names: ['class1', 'class2', ..., 'class80'] # 类别名称 ``` --- #### 3. 修改源码(不推荐) 如果确实需要自定义文件夹名称(如 "Images" 或其他),可以修改 YOLOv7 的源代码。根据引用[^2],进入 `utils/dataset.py` 文件,搜索相关代码片段(如 `define label` 或类似关键词),将默认的 `'images'` 替换为自定义名称。例如: ```python # 原始代码 image_dir = os.path.join(dataset_dir, 'images', phase) # 修改后的代码 image_dir = os.path.join(dataset_dir, 'Images', phase) ``` **注意**:这种方法可能会影响后续更新或与其他功能的兼容性,因此不建议使用。 --- #### 4. 检查标签文件内容 确保每个 `.txt` 标签文件的内容符合 YOLO 格式。例如,假设类别 ID 为 0 的目标框位于图像中心,宽度和高度分别为 0.5 和 0.3,则标签文件内容应为: ``` 0 0.5 0.5 0.5 0.3 ``` 如果标签文件为空或格式错误,也可能导致 `AssertionError`。 --- ### 示例代码 以下是一个简单的 Python 脚本,用于验证标签文件是否完整且格式正确: ```python import os def validate_labels(data_dir): label_dir = os.path.join(data_dir, 'labels', 'train') if not os.path.exists(label_dir): print(f"Error: Label directory '{label_dir}' does not exist.") return for label_file in os.listdir(label_dir): if not label_file.endswith('.txt'): continue file_path = os.path.join(label_dir, label_file) with open(file_path, 'r') as f: lines = f.readlines() if not lines: print(f"Warning: Empty label file: {file_path}") continue for line in lines: parts = line.strip().split() if len(parts) != 5: print(f"Error: Invalid format in {file_path}: {line.strip()}") break validate_labels('path/to/dataset') ``` --- ### 总结 通过检查数据集格式、验证 `train.cache` 文件、修改源码(若必要)以及确保标签文件内容正确,可以有效解决 `AssertionError: train: No labels in data\coco\train.cache` 错误。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值