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

本文介绍了如何使用UltralyticsYOLO进行PASCALVOC数据集的预处理,包括从XML标注文件转换为文本文件,划分训练集和验证集,并提供了必要的脚本和路径设置。
摘要由CSDN通过智能技术生成
# 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)
  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于使用YOLOv8训练自己的VOC数据集,您需要按照以下步骤操作: 1. 数据集准备:首先,您需要准备VOC格式的数据集,该数据集包含图像和相应的标注文件。每个图像应该有一个XML或TXT文件,其中包含对象的边界框坐标和类别标签。 2. 配置文件修改:YOLOv8模型需要一个配置文件来定义模型的结构和超参数。您可以在YOLOv8的代码库中找到示例配置文件,并根据您的需求进行修改。其中,您需要更改类别数目和训练和测试数据集的路径。 3. 权重文件下载:YOLOv8模型的训练通常需要在ImageNet预训练权重的基础上进行微调。您可以从Darknet官方网站下载预训练权重文件,然后将其转换为PyTorch格式。 4. 训练模型:使用准备好的数据集、配置文件和权重文件,您可以开始训练YOLOv8模型。您可以使用PyTorch等深度学习框架来实现训练过程。训练过程中,您可以调整学习率、批大小、迭代次数等超参数,以获得更好的性能。 5. 模型评估:完成训练后,您可以使用测试集对训练得到的模型进行评估。评估指标可以包括平均精度(mAP)、召回率等。 6. 模型应用:训练好的YOLOv8模型可以用于目标检测任务。您可以使用模型对新的图像进行推理,并输出检测到的目标类别和边界框。 请注意,YOLOv8是一个较为复杂的模型,对计算资源要求较高。在训练过程中,您可能需要使用GPU来加速计算。此外,还可以通过数据增强、调整超参数等方式来改进模型性能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值