【深度学习】YOLOv8使用自己的VOC数据集

本文介绍了如何使用UltralyticsYOLO进行PASCALVOC数据集的预处理,包括从XML标注文件转换为文本文件,划分训练集和验证集,并提供了必要的脚本和路径设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# Ultralytics YOLO 🚀, AGPL-3.0 license
# PASCAL VOC dataset http://host.robots.ox.ac.uk/pascal/VOC by University of Oxford
# Documentation: # Documentation: https://docs.ultralytics.com/datasets/detect/voc/
# Example usage: yolo train data=VOC.yaml
# parent
# ├── ultralytics
# └── datasets
#     └── VOC  ← downloads here (2.8 GB)


# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/VOC
train: # train images (relative to 'path')  16551 images
  - images/train
val: # val images (relative to 'path')  4952 images
  - images/val
test: # test images (optional)
  - images/val

# Classes
names:
  0: JYZPS


# Download script/URL (optional) ---------------------------------------------------------------------------------------
download: |
  import shutil
  import xml.etree.ElementTree as ET
  import os
  import random
  from tqdm import tqdm
  
  
  def convert_label(xml_file_path, txt_file_path):
    def convert_box(size, box):
      dw, dh = 1. / size[0], 1. / size[1]
      x, y, w, h = (box[0] + box[1]) / 2.0 - 1, (box[2] + box[3]) / 2.0 - 1,\
        box[1] - box[0], box[3] - box[2]
      return x * dw, y * dh, w * dw, h * dh
  
    in_file = open(xml_file_path)
    out_file = open(txt_file_path, '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)
    
    names = list(yaml['names'].values())
    for obj in root.iter('object'):
      cls = obj.find('name').text
      if cls in names:
        xmlbox = obj.find('bndbox')
        bb = convert_box((w, h), [float(xmlbox.find(x).text) for x in ('xmin', 'xmax', 'ymin', 'ymax')])
        cls_id = names.index(cls)  # class id
        out_file.write(" ".join([str(a) for a in (cls_id, *bb)]) + '\n')
    out_file.close()
  
  
  # 1.path
  root_dir = yaml['path']  # need
  imgs_dir = os.path.join(root_dir, 'JPEGImages')  # need
  xmls_dir = os.path.join(root_dir, 'Annotations')  # need
  txts_dir = os.path.join(root_dir, 'Labels')
  images_dir = os.path.join(root_dir, 'images')
  labels_dir = os.path.join(root_dir, 'labels')
  train_img_dir = os.path.join(images_dir, 'train')
  train_txt_dir = os.path.join(labels_dir, 'train')
  val_img_dir = os.path.join(images_dir, 'val')
  val_txt_dir = os.path.join(labels_dir, 'val')
  # 2.dir
  shutil.rmtree(txts_dir, ignore_errors=True)
  os.mkdir(txts_dir)
  shutil.rmtree(images_dir, ignore_errors=True)
  os.mkdir(images_dir)
  os.mkdir(train_img_dir)
  os.mkdir(val_img_dir)
  shutil.rmtree(labels_dir, ignore_errors=True)
  os.mkdir(labels_dir)
  os.mkdir(train_txt_dir)
  os.mkdir(val_txt_dir)
  # 3.convert xml to txt
  imgs_list = os.listdir(imgs_dir)
  for img_file in tqdm(imgs_list, desc='convert xml to txt'):
    if img_file.split('.')[-1] in ['JPG', 'jpg']:
      name, _ = os.path.splitext(img_file)
      txt_file = name + '.txt'
      xml_file = name + '.xml'
      xml_path = os.path.join(xmls_dir, xml_file)
      txt_path = os.path.join(txts_dir, txt_file)
      convert_label(xml_path, txt_path)
  # 4.split train and val
  random.seed(0)
  random.shuffle(imgs_list)
  data_len = len(imgs_list)
  train_list, val_list = imgs_list[:int(0.8*data_len)], imgs_list[int(0.8*data_len):]
  # 5.copy file
  for img_file in tqdm(train_list, desc='copy train file'):
    if img_file.split('.')[-1] in ['JPG', 'jpg']:
      src_path = os.path.join(imgs_dir, img_file)
      dst_path = os.path.join(train_img_dir, img_file)
      shutil.copyfile(src=src_path, dst=dst_path)
      name, _ = os.path.splitext(img_file)
      txt_file = name + '.txt'
      src_path = os.path.join(txts_dir, txt_file)
      dst_path = os.path.join(train_txt_dir, txt_file)
      shutil.copyfile(src=src_path, dst=dst_path) 
  
  for img_file in tqdm(val_list, desc='copy val file'):
    src_path = os.path.join(imgs_dir, img_file)
    dst_path = os.path.join(val_img_dir, img_file)
    shutil.copyfile(src=src_path, dst=dst_path)
    name, _ = os.path.splitext(img_file)
    txt_file = name + '.txt'
    src_path = os.path.join(txts_dir, txt_file)
    dst_path = os.path.join(val_txt_dir, txt_file)
    shutil.copyfile(src=src_path, dst=dst_path)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值