问 题:有不少学YOLOv5算法的朋友咨询我,发现部分朋友犯了一个很大的错误,就是只是划分了训练集和验证集,没有测试集,并且没有意识到自己的实验设置是错误的、不科学的,这是非常可怕的,意味着可能前期的工作都白做了,浪费了宝贵的时间和精力,部分没有服务器的朋友,还浪费了大量的金钱。所以有必要更新一下YOLOv5算法数据集的随机划分。一般训练集:验证集:测试集=6:2:2(参考一本人工智能的书籍划分的比例,也根据个人数据集的大小灵活把握)。
方 法:
首先对数据集的文件进行划分,trainval_percent = 0.8,train_percent = 3/4,这个参数设置就是按照6:2:2的比例进行了划分。然后再根据前面写的博客进行索引。学习经验分享之三:YOLOv5训练数据集路径索引_人工智能算法研究院的博客-CSDN博客
import os
import random
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--xml_path', default=r'D:\AI\NWPU-10\yolo-format', type=str, help='input txt label path')
parser.add_argument('--txt_path', default=r'D:\AI\NWPU-10\ImageSets\Main', type=str, help='output txt label path')
opt = parser.parse_args()
trainval_percent = 0.8
train_percent = 3/4
xmlfilepath = opt.xml_path
txtsavepath = opt.txt_path
total_xml = os.listdir(xmlfilepath)
if not os.path.exists(txtsavepath):
os.makedirs(txtsavepath)
num = len(total_xml)
list_index = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list_index, tv)
train = random.sample(trainval, tr)
file_trainval = open(txtsavepath + '/trainval.txt', 'w')
file_test = open(txtsavepath + '/test.txt', 'w')
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/val.txt', 'w')
for i in list_index:
name = total_xml[i][:-4] + '\n'
if i in trainval:
file_trainval.write(name)
if i in train:
file_train.write(name)
else:
file_val.write(name)
else:
file_test.write(name)
file_trainval.close()
file_train.close()
file_val.close()
file_test.close()
很多朋友都是自己标注的,格式基本为XML格式,故附上yolo格式的转换代码。需要的自取。
# -*- codeing = utf-8 -*-
# @Time : 2021/9/30 10:21
# @Auther : zqk
# @File : voc_labelhrsc.py
# @Software: PyCharm
import xml.etree.ElementTree as ET
import os
from os import getcwd
sets = ['train', 'val', 'test']
classes = ["airplane","airport","baseballfield","basketballcourt","bridge","chimney","dam","expresswayservicearea",
"expresswaytollstation","golfcourse","groundtrackfield","harbor","overpass","ship","stadium","storagetank",
"tenniscourt","trainstation","vehicle","windmill"] # 改成自己的类别
abs_path = os.getcwd()
print(abs_path)
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('ZQK_data/Annotations/%s.xml' % (image_id), encoding='UTF-8')
out_file = open('ZQK_data/labels/%s.txt' % (image_id), 'w')
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))
b1, b2, b3, b4 = b
# 标注越界修正
if b2 > w:
b2 = w
if b4 > h:
b4 = h
b = (b1, b2, b3, b4)
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('ZQK_data/labels/'):
# os.makedirs('ZQK_data/labels/')
image_ids = open('RSOD/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
list_file = open('RSOD/ImageSets/%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write(abs_path + '/RSOD/JPEGImages/%s.jpg\n' % (image_id))
# convert_annotation(image_id)
list_file.close()
最后,希望有兴趣的可以关注一下,后续会出更多相关内容,感谢支持。另外有问题可以进行留言或者私信,我将抽时间进行更新博客统一回答!