在网上查了很多资料,大部分人说是数据集的标签有问题。有的说是标签的数据的小数点位数不一样导致的。然后我就将标签的数据小数点位数统一了一下。
结果还是报错
一直没搞懂,这个错误的原因。最开始一直盯着ValueError: not enough values to unpack (expected 3, got 0)改。一直报错。
后来我开始想为什么我总端输入命令可以训练。但是我自己写的脚本一直报错。后来就针对它为什么找不到这个图片路径。
train: WARNING ⚠️ mydatas/images/IMG20231223152215_z9.jpg: ignoring corrupt image/label: [Errno 2] No such file or directory: 'mydatas/images/IMG20231223152215_z9.jpg'可以看到这个是相对路径,然后我就想着重新生成,生成绝对路径。
import xml.etree.ElementTree as ET
import pickle
import numpy as np
import os
from os import listdir, getcwd
from os.path import join
sets = ['train', 'test', 'val']
classes = ['craze','white mark','joint line', 'bump', 'off-centered']
def convert(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
x = format(x, '.6f')
w = w * dw
w = format(w, '.6f')
y = y * dh
y = format(y, '.6f')
h = h * dh
h = format(h, '.6f')
return (x, y, w, h)
def convert_annotation(image_id):
in_file = open('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/Annotations/%s.xml' % (image_id),encoding='utf-8')
out_file = open('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/labels/%s.txt' % (image_id), 'w',encoding='utf-8')
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'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
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')
wd = getcwd()
print(wd)
for image_set in sets:
if not os.path.exists('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/labels/'):
os.makedirs('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/labels/')
image_ids = open('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/ImageSets/%s.txt' % (image_set)).read().strip().split()
list_file = open('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write('/home/gzz/zhoulin/model-sound code/yolov8/mydatas/images/%s.jpg\n' % (image_id))
convert_annotation(image_id)
list_file.close()
这个是我的标签生成代码。可以看到都改成了绝对路径。
然后在运行train文件
#coding:utf-8
# 替换主干网络,训练
from ultralytics import YOLO
if __name__ == '__main__':
model = YOLO('yolov8_qinglianghua.yaml')
#model.load('yolov8n.pt') # loading pretrain weights
model.train(data='111.yaml', epochs=400, batch=8)
可以看到它不报错了,ValueError: not enough values to unpack (expected 3, got 0)这个问题也解决了。