EADST

YOLOv5: Data Preparation for Training

YOLOv5: Data Preparation for Training, from VOC Format to YOLO Format

First, split the data to train and val, and get txt files

import os
import random

train_percent = 0.95 xmlfilepath = 'bucket_v1/Annotations' txtsavepath = 'bucket_v1/ImageSets' total_xml = os.listdir(xmlfilepath)

num = len(total_xml) trainval = range(num) tr = int(num * train_percent) train = random.sample(trainval, tr)

ftrain = open('bucket_v1/ImageSets/Main/train.txt', 'w') fval = open('bucket_v1/ImageSets/Main/val.txt', 'w')

for i in trainval: name = total_xml[i][:-4] + '\n' if i in train: ftrain.write(name) else: fval.write(name)

Second, convert VOC format data (xml) to YOLO format data (txt)

import os
from tqdm import tqdm
from lxml import etree
import json
import shutil

voc_root = "/dfs/data/others/byolov5/dataset/bucket_v1" voc_version = "bucket_v1"

train_txt = "train.txt" val_txt = "val.txt" save_file_root = "/dfs/data/others/byolov5/dataset/yolo_data"

voc_images_path = os.path.join(voc_root, "JPEGImages") voc_xml_path = os.path.join(voc_root, "Annotations") train_txt_path = os.path.join(voc_root, "ImageSets", "Main", train_txt) val_txt_path = os.path.join(voc_root, "ImageSets", "Main", val_txt)

def parse_xml_to_dict(xml): if len(xml) == 0: return {xml.tag: xml.text} result = {} for child in xml: child_result = parse_xml_to_dict(child)
if child.tag != 'object': result[child.tag] = child_result[child.tag] else: if child.tag not in result:
result[child.tag] = [] result[child.tag].append(child_result[child.tag]) return {xml.tag: result}

def translate_info(file_names: list, save_root: str, class_dict: dict, train_val='train'): save_txt_path = os.path.join(save_root, train_val, "labels") if os.path.exists(save_txt_path) is False: os.makedirs(save_txt_path) save_images_path = os.path.join(save_root, train_val, "images") if os.path.exists(save_images_path) is False: os.makedirs(save_images_path)

for file in tqdm(file_names, desc="translate {} file...".format(train_val)):
    img_path = os.path.join(voc_images_path, file + ".jpg")
    assert os.path.exists(img_path), "file:{} not exist...".format(img_path)

    xml_path = os.path.join(voc_xml_path, file + ".xml")
    assert os.path.exists(xml_path), "file:{} not exist...".format(xml_path)

    # read xml
    with open(xml_path, encoding='UTF-8') as fid:
        xml_str = fid.read()
    xml = etree.fromstring(xml_str)
    data = parse_xml_to_dict(xml)["annotation"]
    img_height = int(data["size"]["height"])
    img_width = int(data["size"]["width"])
    # write object info into txt
    # assert "object" in data.keys(), "file: '{}' lack of object key.".format(xml_path)
    if "object" not in data.keys():
        print("Warning: in '{}' xml, there are no objects.".format(xml_path))
        continue

    with open(os.path.join(save_txt_path, file + ".txt"), "w") as f:
        for index, obj in enumerate(data["object"]):
            xmin = float(obj["bndbox"]["xmin"])
            xmax = float(obj["bndbox"]["xmax"])
            ymin = float(obj["bndbox"]["ymin"])
            ymax = float(obj["bndbox"]["ymax"])
            class_name = obj["name"]
            class_index = class_dict[class_name] - 1

            if xmax <= xmin or ymax <= ymin:
                print("Warning: in '{}' xml, there are some bbox w/h <=0".format(xml_path))
                continue

            xcenter = xmin + (xmax - xmin) / 2
            ycenter = ymin + (ymax - ymin) / 2
            w = xmax - xmin
            h = ymax - ymin

            xcenter = round(xcenter / img_width, 6)
            ycenter = round(ycenter / img_height, 6)
            w = round(w / img_width, 6)
            h = round(h / img_height, 6)

            info = [str(i) for i in [class_index, xcenter, ycenter, w, h]]

            if index == 0:
                f.write(" ".join(info))
            else:
                f.write("\n" + " ".join(info))

    # copy image into save_images_path
    path_copy_to = os.path.join(save_images_path, img_path.split(os.sep)[-1])
    if os.path.exists(path_copy_to) is False:
        shutil.copyfile(img_path, path_copy_to)

def main(): class_dict = {"b": 1, "t": 2} with open(train_txt_path, "r") as r: train_file_names = [i for i in r.read().splitlines() if len(i.strip()) > 0] translate_info(train_file_names, save_file_root, class_dict, "train") with open(val_txt_path, "r") as r: val_file_names = [i for i in r.read().splitlines() if len(i.strip()) > 0] translate_info(val_file_names, save_file_root, class_dict, "val")

if name == "main": main()

Reference:

Train Custom Data

相关标签
About Me
XD
Goals determine what you are going to be.
Category
标签云
Tensor AI Proxy 飞书 第一性原理 FastAPI Breakpoint GPT4 Search HuggingFace RGB InvalidArgumentError GoogLeNet Knowledge Linux Datetime Jetson Permission FP8 Heatmap IndexTTS2 Animate TensorRT tar Translation Anaconda NameSilo scipy 图形思考法 COCO LeetCode XGBoost PyTorch mmap SQL 强化学习 Card Paper OCR Domain Pickle v0.dev git Freesound FP16 NLTK 公式 Mixtral Llama 多进程 Sklearn logger Food Michelin FP64 Plate Diagram 财报 报税 Bipartite diffusers 顶会 RAR Input CC 签证 Qwen2.5 Django CV Github DeepStream JSON Hilton Distillation Image2Text CAM XML Markdown Paddle Git Data CTC Vim WAN Windows Interview 多线程 Password Random Docker Firewall BTC Qwen GGML Website Algorithm PDF Excel Tracking Use Disk uwsgi Jupyter VSCode Hotel Cloudreve TSV Bert HaggingFace SPIE Review 算法题 Clash ONNX Quantization Video LLAMA VPN Shortcut Attention Web FlashAttention LaTeX Qwen2 Streamlit Hungarian Base64 torchinfo CEIR DeepSeek 净利润 MD5 Statistics VGG-16 GPTQ llama.cpp BF16 ModelScope 腾讯云 NLP Vmess Template CLAP BeautifulSoup Safetensors Nginx Bin OpenAI PIP API GIT Ptyhon Logo Pytorch Conda WebCrawler 域名 Quantize Gemma YOLO hf Crawler Tiktoken PyCharm FP32 EXCEL Google Pandas Ubuntu Zip 证件照 C++ Pillow Dataset ChatGPT Agent News 递归学习法 Augmentation Plotly 版权 Claude uWSGI QWEN Transformers TensorFlow Numpy Python CSV Math ResNet-50 云服务器 UNIX OpenCV Color git-lfs Magnet Land Miniforge Bitcoin LLM SAM 关于博主 搞笑 SVR PDB SQLite CUDA 阿里云 TTS v2ray 音频 transformers LoRA tqdm UI 继承 Baidu printf
站点统计

本站现有博文321篇,共被浏览764702

本站已经建立2442天!

热门文章
文章归档
回到顶部