训练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问题

问题详情

Yolov5报错:
AssertionError: Label class x exceeds nc=x in data/yolov5.yaml. Possible class labels are 0-x-1
File “C:\Users\1\Desktop\水表识别\YOLO5\yolov5-master\train.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行:

assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Possible class labels are 0-%g' % (mlc, nc, opt.data, nc - 1)

改成这样

#assert mlc < nc, 'Label class %g exceeds nc=%g in %s. Possible class labels are 0-%g' % (mlc, nc, opt.data, nc - 1)

注释掉

我的解决方法

这个问题从原理上来说,是你的检测框xml文件转到(yolo)txt后,类别编号没有从0开始。导致类别的索引超出了范围,yolov5中默认是从0开始到x-1,x是你的检测类别。解决方法很简单,先检查你的txt标签文件中是否从0开始,若不是则使用以下代码,classes中的类别应与您检测的类别对应:

# 缺陷坐标xml转txt
import os
import xml.etree.ElementTree as ET
import os
import random
classes = ["fire","smoke"]  # 输入类别名称,必须与xml标注名称一致

def convert(size, box):
    print(size, box)
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    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)


def convert_annotation(image_id):
    if not os.path.exists('data/labels/'):
        os.makedirs('data/labels/')
    in_file = open(r'./data/Annotations/%s' % (image_id), 'rb')  # 读取xml文件路径
    out_file = open('./data/labels/%s.txt' % (image_id.split('.')[0]), 'w')  # 需要保存的txt格式文件路径
    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 not in classes:
            continue
        cls_id = classes.index(cls)
        xmlbox = obj.find('bndbox')
        b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
             float(xmlbox.find('ymax').text))
        bb = convert((w, h), b)
        out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')


image_ids_train = os.listdir('./data/Annotations')  # 读取xml文件名索引
for image_id in image_ids_train:
    print(image_id)
    convert_annotation(image_id)


trainval_percent = 0.1  # 可自行进行调节
train_percent = 1
xmlfilepath = './data/Images'

total_xml = os.listdir(xmlfilepath)
num = len(total_xml)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)
ftest = open('./data/test.txt', 'w')
ftrain = open('./data/train.txt', 'w')

for i in list:
    name = total_xml[i] + '\n'
    if i in trainval:
        if i in train:
            ftest.write('data/Images/' + name)
    else:
        ftrain.write('data/Images/' + name)
ftrain.close()
ftest.close()

如果没有解决的话可能是因为您的txt(标签文件)中的类别数与训练的yaml文件中的类别数不对应导致,注意nc的数目并查看txt(标签文件中)是否从0开始到nc-1。
其他解决方法:txt格式的labels每个种类标的是有序号的,由xml或者json格式转化为xml时会在每个坐标前生成一个序号,从0开始。(如果是删除了几个种类之后,种类数减少,但序号不会改变) 打开labels标签的每个文件,把序号从0开始再改过一遍。或者在xml格式删除种类之后再转化为txt格式

总结

在使用yolov5框架时这个问题是非常常见的,好在yolo系列比较成熟,对小白特别友好。在遇到问题时多查阅资料,只要保证一一对应,训练行容易上手,后续想要发一下yolov5网络中添加即插即用模块的博客,还望多多关注!当然如果您的代码仍有问题,也可私信我抽空给您远程协助,谢谢观看!

参考资料

参考的链接: https://blog.csdn.net/qq_45714906/article/details/120528631

  • 17
    点赞
  • 50
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
引用\[1\]:Yolov5报错: AssertionError: Label class x exceeds nc=x in data/yolov5.yaml. Possible class labels are 0-x-1 File “C:\Users\1\Desktop\水表识别\YOLO5\yolov5-master\train.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) 大多博客给出的方法 。 引用\[2\]:问题一:在运行train.py(只有1类)程序过程中出现“AssertionError:Label class 1 exceeds nc=1 in yolo/dataset.ymal Possible class labels are 0-0”情况。 。 引用\[3\]: 训练yolov5的那些解决AssertionError: Label class x exceeds nc=x in data/yolov5.yaml. Possible class labels are 0-x-1问题 问题详情大多博客给出的方法我的解决方法 总结参考资料 。 问题: AssertionError: Label class 1 exceeds nc=1 in /home/suanfa-2/lz/yolov5_tag6.1/yolov5-6.1/data/scq_v4.yaml. Possible class labels are 0-0是什么问题?如何解决? 回答: 这个错误是由于在训练yolov5模型时,标签类别数超过了配置文件中设置的类别数所导致的。具体来说,错误信息中的"Label class 1 exceeds nc=1"表示标签类别1超过了配置文件中设置的类别数1。而"Possible class labels are 0-0"表示可能的类别标签只有0。\[1\]\[2\]\[3\]要解决这个问题,你需要检查你的数据集和配置文件,确保标签类别数与配置文件中设置的类别数一致。你可以检查数据集的标签文件和配置文件中的类别数是否匹配,以及是否有重复的类别标签。另外,还要确保你的数据集中的标签类别数不超过配置文件中设置的类别数。如果有需要,你可以修改配置文件中的类别数,使其与你的数据集相匹配。 #### 引用[.reference_title] - *1* *3* [训练yolov5的那些解决AssertionError: Label class x exceeds nc=x in data/yolov5.yaml. Possible ...](https://blog.csdn.net/qq_43725659/article/details/127867867)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [YOLOv5报错AssertionError:Label class 1 exceeds nc=1 in yolo/dataset.ymal Possible class labels are 0...](https://blog.csdn.net/weixin_56524592/article/details/129178934)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

致命扼腕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值