You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需修改数据集,如何在YOLOv8训练、测试、验证时忽略指定类别?

在YOLOv8中忽略指定类别(0-11中忽略8-11)的操作方案

1. 修改数据集配置YAML文件

找到你的数据集配置文件(如data.yaml),调整nc(类别总数)和names(类别名称列表),只保留需要的前8个类别:

train: ./path/to/train/images
val: ./path/to/val/images
test: ./path/to/test/images

nc: 8  # 从12改为8,对应保留的类别数
names: ['类0名称', '类1名称', '类2名称', '类3名称', '类4名称', '类5名称', '类6名称', '类7名称']

2. 批量过滤标签文件中的目标(推荐操作)

如果你的标签文件包含要忽略的类别(8-11),可以用脚本批量清理这些无效标签:

import os

# 替换为你的标签文件夹路径
label_root = "./path/to/dataset/labels"
# 要忽略的类别ID
ignore_ids = {8, 9, 10, 11}

for txt_file in os.listdir(label_root):
    if not txt_file.endswith(".txt"):
        continue
    file_path = os.path.join(label_root, txt_file)
    with open(file_path, "r") as f:
        lines = f.readlines()
    # 过滤掉目标类别ID在忽略列表中的行
    filtered_lines = [line for line in lines if int(line.split()[0]) not in ignore_ids]
    with open(file_path, "w") as f:
        f.writelines(filtered_lines)

3. 训练/测试/验证时的参数指定

运行YOLOv8命令时,指定修改后的配置文件,并通过classes参数明确要使用的类别索引:

  • 训练命令示例:
yolo train data=./modified_data.yaml model=yolov8n.pt epochs=50 imgsz=640 classes=0,1,2,3,4,5,6,7
  • 验证/测试命令示例:
yolo val data=./modified_data.yaml model=runs/detect/train/weights/best.pt classes=0,1,2,3,4,5,6,7

通过以上三步,YOLOv8在训练、测试、验证阶段都会自动忽略最后4个类别,只处理前8个类的目标。

内容的提问来源于stack exchange,提问作者Samier Uddin Ahammad Shovo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.10 10:42:39