【目标检测】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 getcwdsets = ['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 - 1y = (box[2] + box[3]) / 2.0 - 1w = box[1] - box[0]h = box[3] - box[2]x = x * dww = w * dwy = y * dhh = h * dhreturn 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:# continuecls = obj.find('name').textif cls not in classes:continuecls_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 = wif b4 > h:b4 = hb = (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')