yolo pretrain convert


原文链接: yolo pretrain convert

1.voc数据集转换

数据集目录结构

└── VOCdevkit

└── VOC2012
    ├── Annotations
    │   ├── 20190615163323424.xml
    │   ├── .......还有很多xml
    ├── ImageSets
    │   └── Main #标签的训练和验证集文本
    │       ├── biji_train.txt
    │       ├── biji_val.txt
    │       ├── dian _train.txt
    │       ├── dian _val.txt
    │       ├── henji_train.txt
    │       ├── henji_val.txt
    │       ├── yahen_train.txt
    │       └── yahen_val.txt
    └── JPEGImages
        ├── 20190615163323424.bmp
        ├── .......还有很多图片

转换脚本

import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join

sets=[('2012', 'biji_train'), ('2012', 'biji_val'), ('2012', 'dian _train'), ('2012', 'dian _val'), ('2012', 'henji_train'),('2012', 'henji_val'), ('2012', 'yahen_train'), ('2012', 'yahen_val')]

classes = ["biji", "dian ", "henji", "yahen"]


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(year, image_id):
    print('%s'%(image_id))
    in_file = open('VOCdevkit/VOC%s/Annotations/%s.xml'%(year, image_id))
    out_file = open('VOCdevkit/VOC%s/labels/%s.txt'%(year, 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')

if __name__ == '__main__':
    wd = getcwd()

    for year, image_set in sets:
        if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)):
            os.makedirs('VOCdevkit/VOC%s/labels/'%(year))
        image_ids = open('VOCdevkit/VOC%s/ImageSets/Main/%s.txt'%(year, image_set)).read().strip().split()
        list_file = open('%s_%s.txt'%(year, image_set), 'w')
        for image_id in image_ids:
            filename = os.path.splitext(image_id)[0]
            if filename == '1' or filename == '-1':
                continue
            list_file.write('%s/VOCdevkit/VOC%s/JPEGImages/%s.bmp\n'%(wd, year, filename))
            convert_annotation(year, filename)
        list_file.close()

    os.system("cat *_train.txt > train.txt")
    os.system("cat *_val.txt > val.txt")

转换脚本执行后会生成train.txt 和val.txt 和数据集目录下会有lables文件夹,train.txt 和val.txt 需要再整合到voc.data
classes= 4
train = /darknet/mupian-demo/data/train.txt
valid = /darknet/mupian-demo/data/val.txt
names = /darknet/mupian-demo/data/voc.names
backup = /darknet/mupian-demo/backup
voc.names

biji
dian
henji
yahen

lables文件夹需要放置在train.txt(val.txt)中图片所在的文件夹的同级目录,也就是train.txt(val.txt)中的图片的上级目录

作者:baymin_
链接:https://www.jianshu.com/p/af2d292da518
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

`