【目标检测-数据集准备】DIOR转为yolo训练所需格式

时间:2024-03-13 07:24:10

【目标检测】DIOR遥感影像数据集,转为yolo系列模型训练所需格式。

在这里插入图片描述
标签文件位于Annotations下,格式为xml,yolo系列模型训练所需格式为txt,格式为

class_id x_center,y_center,w,h

其中,train,text,val按照官方方式划分(DIOR/ImageSets/Main/train.txt),分别含影像5062,5063,11738张。
在DIOR/ImageSets/Main/xx.txt 路径中,txt文件为不包含影像后缀的影像名称,如下图
在这里插入图片描述
yolo训练中需要的train.txt文件内容需要是包括后缀的绝对路径:
在这里插入图片描述

转换代码:
转换中的outpath可以自定义,为后续配置文件中的路径

注意:
(1)将DIOR的影像文件夹改名为images,注意全小写,字母要对
(2)转换后的标签位于影像文件夹下的labels下,不要修改
**images和labels两个文件夹名称不要修改,不要修改,否则会报错:No labels in xx./train.cache

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os
from os import getcwd

sets = ['train', 'val', 'test']


# class names
classes =  ['airplane', 'airport', 'baseballfield', 'basketballcourt', 'bridge', 'chimney', 'dam',
        'Expressway-Service-area', 'Expressway-toll-station', 'golffield', 'groundtrackfield', 'harbor',
        'overpass', 'ship', 'stadium', 'storagetank', 'tenniscourt', 'trainstation', 'vehicle', 'windmill']   # 改成自己的类别
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

#修改路径-----------------------------
datasetpath="E:/dataset/DIOR"
imgpath="E:/dataset/DIOR/images"
outpath="E:/dataset/DIOR/myyolo"

def convert_annotation(image_id):
    in_file = open(datasetpath+'/Annotations/%s.xml' % (image_id), encoding='UTF-8')
    out_file = open(datasetpath+'/labels/%s.txt' % (image_id), 'w')   #不要修改labels文件夹名称
    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 = obj.find('name').text
        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))
        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(datasetpath+'/labels/'):
        os.makedirs(datasetpath+'/labels/')
    image_ids = open(datasetpath+'/ImageSets/Main/%s.txt' % (image_set)).read().strip().split()

    if not os.path.exists(outpath):
        os.makedirs(outpath)
    list_file = open(outpath+'/%s.txt' % (image_set), 'w')

    for image_id in image_ids:
        list_file.write(imgpath+'/%s.jpg\n' % (image_id))
        convert_annotation(image_id)
    list_file.close()

转换后的text文件:
在这里插入图片描述
建立数据集配置文件DIOR.yaml,路径修改为outpath,

train: E:/dataset/DIOR/myyolo/train.txt
val: E:/dataset/DIOR/myyolo/val.txt

# number of classes
nc: 20

# class names
names: ['airplane', 'airport', 'baseballfield', 'basketballcourt', 'bridge', 'chimney', 'dam',
        'Expressway-Service-area', 'Expressway-toll-station', 'golffield', 'groundtrackfield', 'harbor',
        'overpass', 'ship', 'stadium', 'storagetank', 'tenniscourt', 'trainstation', 'vehicle', 'windmill']

在训练时将data参数设置为DIOR.yaml即可使用yolo系列模型训练DIOR。YOLOv5,v7,v8通用。

parser.add_argument('--data', type=str, default='data/DIOR.yaml', help='data.yaml path')