YOLOV8 训练自己的数据集
第一步:数据准备
在yolov8中建立datasets文件夹,然后建立数据集文件夹mydata。
在mydata文件夹中建立Annotations文件夹(存放VOC格式下的自己的xml文件)、images文件夹(存放自己的原始标记图像)、ImageSets文件夹(空)。
打开pycharm在mydata目录下建立一个名为makeTxt.py文件、一个名为voc_label.py文件。
makeTxt.py给数据分类trian val test。运行后ImageSets文件夹生成四个txt。
voc_label.py运行后生成labels文件夹和三个txt。修改代码中的classes使用自己的类。
voc_label.py代码:
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
sets = [('train'), ('test'), ('val')]
classes = ["zebra", "panda", "elephant", "tiger", "cat", "dolphin", "whale", "shark", "goldfish", "starfish",
"muskmelon", "lemon", "mango", "strawberry", "Chinese cabbage", "eggplant", "potato", "apple", "balsam pear",
"pumpkin"]
def convert(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = (box[0] + box[1]) / 2.0 - 1
y = (box[2] + box[3]) / 2.0 - 1
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):
in_file = open('Annotations/%s.xml' % (image_id), encoding='utf-8')
out_file = open('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:
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')
wd = getcwd()
for image_set in sets:
if not os.path.exists('labels/'):
os.makedirs('labels/')
image_ids = open('ImageSets/%s.txt' % (image_set)).read().strip().split()
list_file = open('%s.txt' % (image_set), 'w', encoding='utf-8')
for image_id in image_ids:
list_file.write('%s/images/%s.jpg\n' % (wd, image_id))
convert_annotation(image_id)
list_file.close()
makeTxt.py代码:
import os
import random
trainval_percent = 0.1
train_percent = 0.9
xmlfilepath = './Annotations'
txtsavepath = './ImageSets'
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)
ftrainval = open('./ImageSets/trainval.txt', 'w')
ftest = open('./ImageSets/test.txt', 'w')
ftrain = open('./ImageSets/train.txt', 'w')
fval = open('./ImageSets/val.txt', 'w')
for i in list:
name = total_xml[i][:-4] + '\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftest.write(name)
else:
fval.write(name)
else:
ftrain.write(name)
ftrainval.close()
ftrain.close()
fval.close()
ftest.close()
shu.yaml代码:
train: C:/Users/Administrator/Desktop/ultralytics-main/datasets/mydata/train.txt
val: C:/Users/Administrator/Desktop/ultralytics-main/datasets/mydata/val.txt
# Classes
names:
0: zebra
1: panda
2: elephant
3: tiger
labels文件里的txt就是我们使用labelimg标注时数据为YOLO格式。至此数据准备工作完成,开始训练。
第二步:训练
建立一个yaml文件
shu.yaml
train和val都写绝对路径
打开pycharm找到终端terminal命令行输入命令:yolo train data=C:\Users\Administrator\Desktop\ultralytics-main\datasets\shu.yaml model=yolov8n.pt epochs=100 lr0=0.01
至此训练完成。 在runs中生成训练结果和训练模型。
训练结果分析
第三步、测试
使用训练后的模型进行测试。
测试指令:yolo predict model=runs/detect/train26/weights/best.pt source=datasets/mydata/images/0214.jpg