VOC2007数据集转yolo格式

第一步:划分数据集

训练集:验证集:测试集=8:1:1

新建一个split_data.py文件,用来划分数据集,这里是直接根据图片进行划分的数据集

import os
import random

img_path = 'D:/pycharm/PythonProject/datasets/VOC2007/images/'  # 图片文件夹路径(最后斜杠不要漏)
label_path = 'D:/pycharm/PythonProject/datasets/VOC2007/'  # 生成划分数据路径
img_list = os.listdir(img_path)
train_ratio = 0.8  # 训练集比例
val_ratio = 0.1  # 验证集比例
shuffle = True  # 是否随机划分


def data_split(full_list, train_ratio, val_ratio, shuffle=True):
    n_total = len(full_list)
    train_set_num = int(n_total * train_ratio)
    val_set_num = int(n_total * val_ratio)
    if shuffle:
        random.shuffle(full_list)
    train_set = full_list[:train_set_num]
    val_set = full_list[train_set_num:(train_set_num + val_set_num)]
    test_set = full_list[(train_set_num + val_set_num):]
    return train_set, val_set, test_set


if __name__ == '__main__':
    train_set, val_set, test_set = data_split(img_list, train_ratio, val_ratio, shuffle=True)

    with open(label_path + 'train.txt', 'w') as f:
        for img_name in train_set:
            f.write(img_name.split('.jpg')[0] + '\n')

    with open(label_path + 'val.txt', 'w') as f:
        for img_name in val_set:
            f.write(img_name.split('.jpg')[0] + '\n')

    with open(label_path + 'test.txt', 'w') as f:
        for img_name in test_set:
            f.write(img_name.split('.jpg')[0] + '\n')

第二步:标签转换:voc-yolo

import xml.etree.ElementTree as ET
import os

sets = ['train', 'test', 'val']
Imgpath = 'D:/pycharm/PythonProject/datasets/VOC2007/images/'
xmlfilepath = 'D:/pycharm/PythonProject/datasets/VOC2007/Annotations/'
ImageSets_path = 'D:/pycharm/PythonProject/datasets/VOC2007/'
Label_path = 'D:/pycharm/PythonProject/datasets/VOC2007/'
classes = ["aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"]



def convert(size, box):
    dw = 1. / size[0]
    dh = 1. / size[1]
    x = (box[0] + box[1]) / 2.0
    y = (box[2] + box[3]) / 2.0
    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(xmlfilepath + '%s.xml' % (image_id))
    out_file = open(Label_path + '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))
        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(Label_path + 'labels/'):
        os.makedirs(Label_path + 'labels/')
    image_ids = open(ImageSets_path + '%s.txt' % (image_set)).read().strip().split()
    list_file = open(Label_path + '%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()

第三步:

训练准备

在data文件夹下新建voc2007.yaml

train: D:/Dataset/VOC2007/train.txt
val: D:/Dataset/VOC2007/val.txt
test: D:/Dataset/VOC2007/test.txt

# number of classes
nc: 20

# class names
names: [ 'aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor' ]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

zmysunshine

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

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

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

打赏作者

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

抵扣说明:

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

余额充值