yolo训练voc数据集划分

1、划分数据集比例split_train_val.py

import os
import random
import argparse

parser = argparse.ArgumentParser()
#xml文件的地址或者label的地址,根据自己的数据进行修改 xml一般存放在Annotations下 主要是获取每个数据的地址名字
parser.add_argument('--xml_path', default='/home/jovyan/exp_2607/dataset-445/yolo_labels', type=str, help='input xml label path')
#数据集的划分保存的位置,地址一般选择自己数据下的ImageSets/Main
parser.add_argument('--txt_path', default='/home/jovyan/exp_2607/data/mydata/ImageSets/Main', type=str, help='output txt label path')
opt = parser.parse_args()

trainval_percent = 1.0  # 训练集和验证集所占比例。 这里没有划分测试集
train_percent = 0.7     # 训练集所占比例,可自己进行调整
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()

2.xml_to_yolo

import xml.etree.ElementTree as ET
import os
from os import getcwd

sets = ['train', 'val', 'test']
classes = ["door_close", "door_open", "billboard", "tear up","person", "forklift", "shovel loader", "nothing forklift","conveyer belt"]  # 改成自己的类别
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('/home/jovyan/exp_2529/data/mydata/xml/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open('/home/jovyan/exp_2529/data/mydata/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')
        
wd = getcwd()
for image_set in sets:
    image_ids = open('/home/jovyan/exp_2607/data/mydata/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()

    if not os.path.exists('/home/jovyan/exp_2607/dataset_copy/'):
        os.makedirs('/home/jovyan/exp_2607/dataset_copy/')

    if not os.path.exists('/home/jovyan/exp_2607/dataset_copy/images'):
        os.symlink('/home/jovyan/exp_2607/dataset-445/images', '/home/jovyan/exp_2607/dataset_copy/images')

    if not os.path.exists('/home/jovyan/exp_2607/dataset_copy/labels'):
        os.symlink('/home/jovyan/exp_2607/dataset-445/yolo_labels', '/home/jovyan/exp_2607/dataset_copy/labels')

    if not os.path.exists('/home/jovyan/exp_2607/data/mydata/dataSet_path/'):
        os.makedirs('/home/jovyan/exp_2607/data/mydata/dataSet_path/')

    list_file = open('/home/jovyan/exp_2607/data/mydata/dataSet_path/%s.txt' % (image_set), 'w')
    # 这行路径不需更改,这是相对路径
    for image_id in image_ids:
        list_file.write('/home/jovyan/exp_2607/dataset_copy/images/%s.png\n' % (image_id))
        # convert_annotation(image_id)  存在xml转txt文件的时候才使用  如果数据有现成的txt文件就不用运行
    list_file.close()

3.mydata.yaml

train: /home/jovyan/exp_2607/data/mydata/dataSet_path/train.txt  
val: /home/jovyan/exp_2607/data/mydata/dataSet_path/val.txt  
test: /home/jovyan/exp_2607/data/mydata/dataSet_path/test.txt  

# number of classes
nc: 9

# class names
names: [ 'door_close', 'door_open', 'billboard', 'tear up', 'person', 'forklift', 'shovel loader', 'nothing forklift', 'conveyer belt']

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

云雨、

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

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

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

打赏作者

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

抵扣说明:

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

余额充值