YOLOv6训练自定义数据集报错:AssertionError无效目录路径
YOLOv6自定义数据集训练报错排查与解决
问题背景
严格遵循YOLOv6官方GitHub教程训练自定义数据集,数据集存放于YOLOv6父目录下,文件夹结构符合要求,训练/验证/测试图像齐全,但执行训练命令时持续报错。
报错信息
Using 1 GPU for training... training args are: Namespace(data_path='A/data.yaml', conf_file='configs/yolov6s.py', img_size=640, batch_size=16, epochs=50, workers=8, device='0', eval_interval=20, eval_final_only=False, heavy_eval_range=50, check_images=False, check_labels=False, output_dir='./runs/train', name='yolov6s-custom', dist_url='env://', gpu_count=0, local_rank=-1, resume=False, write_trainbatch_tb=False, stop_aug_last_n_epoch=15, save_ckpt_on_last_n_epoch=-1, distill=False, distill_feat=False, quant=False, calib=False, teacher_model_path=None, temperature=20, fuse_ab=False, bs_per_gpu=32, rank=-1, world_size=1, save_dir='runs/train/yolov6s-custom3') Traceback (most recent call last): File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/tools/train.py", line 128, in <module> main(args) File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/tools/train.py", line 113, in main trainer = Trainer(args, cfg, device) File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/yolov6/core/engine.py", line 54, in __init__ self.train_loader, self.val_loader = self.get_data_loader(args, cfg, self.data_dict) File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/yolov6/core/engine.py", line 361, in get_data_loader train_loader = create_dataloader(train_path, args.img_size, args.batch_size // args.world_size, grid_size, File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/yolov6/data/data_load.py", line 41, in create_dataloader dataset = TrainValDataset( File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/yolov6/data/datasets.py", line 68, in __init__ self.img_paths, self.labels = self.get_imgs_labels(self.img_dir) File "/content/gdrive/MyDrive/YOLO_Custom/YOLOv6/yolov6/data/datasets.py", line 220, in get_imgs_labels assert osp.exists(img_dir), f"{img_dir} is an invalid directory path!" AssertionError: ./images/train is an invalid directory path!
执行的训练命令
!python tools/train.py --batch-size 16 --conf configs/yolov6s.py --data data/data.yaml --device 0 --workers 8 --epochs 50 --img-size 640 --name yolov6s-custom
环境与配置说明
- data.yaml文件位于YOLOv6父目录的
data文件夹中 - 数据集文件夹名为
A,存放于YOLOv6父目录下 - data.yaml内容:
train: ./images/train val: ./images/val test: ./images/test nc: 1 names: ['xxx']
报错原因分析
报错核心是程序找不到./images/train路径,原因如下:
- 相对路径基准错误:YOLOv6训练时的工作目录是YOLOv6根目录,data.yaml中的相对路径基于该目录,因此
./images/train指向的是YOLOv6根目录下的images文件夹,而非父目录下数据集A内的对应路径。 - data.yaml路径配置错误:未正确指向父目录下数据集
A的图像文件夹。
解决步骤
方法1:使用绝对路径配置data.yaml
直接写入数据集的绝对路径,避免相对路径混淆:
train: /content/gdrive/MyDrive/YOLO_Custom/A/images/train val: /content/gdrive/MyDrive/YOLO_Custom/A/images/val test: /content/gdrive/MyDrive/YOLO_Custom/A/images/test nc: 1 names: ['xxx']
方法2:修正相对路径
利用../返回父目录,指向数据集A的对应路径(基于YOLOv6根目录):
train: ../A/images/train val: ../A/images/val test: ../A/images/test nc: 1 names: ['xxx']
方法3:调整数据集位置
将数据集文件夹A移动到YOLOv6根目录下,保持data.yaml原有路径配置不变。
验证方法
修改完成后,在YOLOv6根目录下执行以下命令测试路径有效性:
ls ../A/images/train
若能列出文件夹内文件,说明路径配置正确,重新执行训练命令即可。
内容的提问来源于stack exchange,提问作者tbensap18
相关产品推荐
相关产品推荐

