2024/8/29
-
前期准备
数据集文件结构如下
VOCdevkit
————VOC2007
————Annotations # 存放图片对应的xml文件,与JPEGImages图片名称一一对应
————ImageSets
————Main # 存放trainval.txt、train.txt、val.txt、test.txt,开始为空
————JPEGImages # 存放所有图片文件
新建文件
2. 数据集划分
将以下代码放入split_train_val.py里面
# -*- coding: utf-8 -*-
"""
Author:Dehuan
简介:分训练集、验证集和测试集,按照 8:1:1 的比例来分,训练集9,验证集1
"""
import os
import random
import argparse
parser = argparse.ArgumentParser()
# xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
parser.add_argument('--xml_path', default='datasets/VOC2007/Annotations', type=str, help='input xml label path')
# 数据集的划分,地址选择自己数据下的ImageSets/Main
parser.add_argument('--txt_path', default='datasets/VOC2007/ImageSets/Main/', type=str, help='output txt label path')
opt = parser.parse_args()
train_percent = 0.9 # 训练集所占比例
val_percent = 0.1 # 验证集所占比例
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 = list(range(num))
t_train = int(num * train_percent)
t_val = int(num * val_percent)
train = random.sample(list, t_train)
num1 = len(train)
for i in range(num1):
list.remove(train[i])
val_test = [i for i in list if not i in train]
val = random.sample(val_test, t_val)
num2 = len(val)
for i in range(num2):
list.remove(val[i])
file_train = open(txtsavepath + '/train.txt', 'w')
file_val = open(txtsavepath + '/val.txt', 'w')
for i in train:
name = total_xml[i][:-4] + '\n'
file_train.write(name)
for i in val:
name = total_xml[i][:-4] + '\n'
file_val.write(name)
file_train.close()
file_val.close()
运行后
3 生成训练用的txt文件
将以下代码放入voc_label.py
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
sets = ['train', 'val'] # 如果你的Main文件夹没有test.txt,就删掉'test'
# classes = ["a", "b"] # 改成自己的类别,VOC数据集有以下20类别
classes = ['Paaper', 'Rock','Scissors'] # class names
abs_path = os.getcwd()
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(abs_path + '/datasets/VOC2007/Annotations/%s.xml' % (image_id), encoding='UTF-8')
out_file = open(abs_path + '/datasets/VOC2007/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
# 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')
for image_set in sets:
if not os.path.exists(abs_path + '/datasets/VOC2007/labels/'):
os.makedirs(abs_path + '/datasets/VOC2007/labels/')
image_ids = open(abs_path + '/datasets/VOC2007/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
list_file = open(abs_path + '/datasets/VOC2007/%s.txt' % (image_set), 'w')
for image_id in image_ids:
list_file.write(abs_path + '/datasets/VOC2007/JPEGImages/%s.jpg\n' % (image_id)) # 要么自己补全路径,只写一半可能会报错
convert_annotation(image_id)
list_file.close()
运行后
至此,数据集的样式是这样的
4.1 yaml文件参数修改
将以下内容复制到最开始创建的yaml文件里面,注意修改nc 和 names
train: VOC2007/train.txt
val: VOC2007/val.txt
# number of classes
nc: 1
# class names
names: ['Paaper']
train和val的路径看情况修改
4.2 utils.py修改参数
找到img2label_paths函数