使用YOLOv8训练自己的目标检测数据集(VOC格式/COCO格式)

2024-04-17 15:52

本文主要是介绍使用YOLOv8训练自己的目标检测数据集(VOC格式/COCO格式),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

yolov8训练自己的数据集

      • 1. 下载项目
      • 2. 搭建环境
      • 3. 数据集格式转换
        • 3.1 VOC格式转YOLO格式
        • 3.2 COCO格式转YOLO格式
      • 4. 训练数据
      • 5. 推理预测
      • 6. 模型导出

1. 下载项目

git clone https://github.com/ultralytics/ultralytics.git

2. 搭建环境

conda create --name ultralytics python==3.8
conda activate ultralytics
# 电脑是CUDA11.1的
pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.htmlpip install ultralytics

3. 数据集格式转换

3.1 VOC格式转YOLO格式
  • VOC格式
── VOCdevkit
└── VOC2007├── Annotations	# 存放图片对应的xml文件,与JPEGImages图片一一对应├── ImageSets│   └── Main	# 存放train.txt、val.txt└── JPEGImages	# 存放所有图片文件
  • YOLO格式
── VOCdevkit
├── images
│   ├── train	# 存放训练集图片
│   └── val	# 存放验证集图片
└── labels├── train	# 存放训练集标注文件└── val	# 存放验证集标注文件
  • 转换脚本
from tqdm import tqdm
import shutil
from pathlib import Path
import xml.etree.ElementTree as ETdef convert_label(path, lb_path, year, image_id, names):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 * dhin_file = open(path / f'VOC{year}/Annotations/{image_id}.xml')out_file = open(lb_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)for obj in root.iter('object'):cls = obj.find('name').textif 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 idout_file.write(" ".join(str(a) for a in (cls_id, *bb)) + '\n')else:print("category error: ", cls)year = "2007"
image_sets = ["train", "val"]
path = Path("F:/vsCode/ultralytics/datasets/VOCdevkit/")
class_names = ["apple"]for image_set in image_sets:imgs_path = path / 'images' / f'{image_set}'lbs_path = path / 'labels' / f'{image_set}'imgs_path.mkdir(exist_ok=True, parents=True)lbs_path.mkdir(exist_ok=True, parents=True)with open(path / f'VOC{year}/ImageSets/Main/{image_set}.txt') as f:image_ids = f.read().strip().split()for id in tqdm(image_ids, desc=f'{image_set}'):f = path / f'VOC{year}/JPEGImages/{id}.jpg'  # old img pathlb_path = (lbs_path / f.name).with_suffix('.txt')  # new label path# f.rename(imgs_path / f.name)  # move imageshutil.copyfile(f, imgs_path / f.name) # copy imageconvert_label(path, lb_path, year, id, class_names)  # convert labels to YOLO format

数据集文件夹目录如下
在这里插入图片描述

3.2 COCO格式转YOLO格式
  • COCO格式
── Apple
├── train
│   ├── _annotations.coco.json	# 训练集标注文件
│   ├── 00001.jpg
│   ├── 00003.jpg
│   └── ...
└── valid├── _annotations.coco.json	# 验证集标注文件├── 00002.jpg├── 00004.jpg└── ...
  • 转换脚本
import json
import os
import shutil
from tqdm import tqdmcoco_path = "F:/datasets/Apple_Detection_Swift-YOLO_192"
output_path = "F:/vsCode/ultralytics/datasets/Apple"os.makedirs(os.path.join(output_path, "images", "train"), exist_ok=True)
os.makedirs(os.path.join(output_path, "images", "val"), exist_ok=True)
os.makedirs(os.path.join(output_path, "labels", "train"), exist_ok=True)
os.makedirs(os.path.join(output_path, "labels", "val"), exist_ok=True)with open(os.path.join(coco_path, "train", "_annotations.coco.json"), "r") as f:train_annotations = json.load(f)with open(os.path.join(coco_path, "valid", "_annotations.coco.json"), "r") as f:val_annotations = json.load(f)# Iterate over the training images
for image in tqdm(train_annotations["images"]):width, height = image["width"], image["height"]scale_x = 1.0 / widthscale_y = 1.0 / heightlabel = ""for annotation in train_annotations["annotations"]:if annotation["image_id"] == image["id"]:# Convert the annotation to YOLO formatx, y, w, h = annotation["bbox"]x_center = x + w / 2.0y_center = y + h / 2.0x_center *= scale_xy_center *= scale_yw *= scale_xh *= scale_yclass_id = annotation["category_id"]label += "{} {} {} {} {}\n".format(class_id, x_center, y_center, w, h)# Save the image and labelshutil.copy(os.path.join(coco_path, "train", image["file_name"]), os.path.join(output_path, "images", "train", image["file_name"]))with open(os.path.join(output_path, "labels", "train", image["file_name"].replace(".jpg", ".txt")), "w") as f:f.write(label)# Iterate over the validation images
for image in tqdm(val_annotations["images"]):width, height = image["width"], image["height"]scale_x = 1.0 / widthscale_y = 1.0 / heightlabel = ""for annotation in val_annotations["annotations"]:if annotation["image_id"] == image["id"]:# Convert the annotation to YOLO formatx, y, w, h = annotation["bbox"]x_center = x + w / 2.0y_center = y + h / 2.0x_center *= scale_xy_center *= scale_yw *= scale_xh *= scale_yclass_id = annotation["category_id"]label += "{} {} {} {} {}\n".format(class_id, x_center, y_center, w, h)# Save the image and labelshutil.copy(os.path.join(coco_path, "valid", image["file_name"]), os.path.join(output_path, "images", "val", image["file_name"]))with open(os.path.join(output_path, "labels", "val", image["file_name"].replace(".jpg", ".txt")), "w") as f:f.write(label)

4. 训练数据

找到ultralytics/cfg/datasets/VOC.yaml,复制一份命名为VOC_self.yaml

# 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: F:/vsCode/ultralytics/datasets/VOCdevkit
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: apple

根据README文件选择预训练模型,最好先手动下载放置在项目主目录下。

在这里插入图片描述

训练命令:

yolo task=detect mode=train model=yolov8x.pt data=f:/ultralytics/ultralytics/cfg/datasets/VOC_self.yaml epochs=100 batch=4 device=0
  • 如果想从头开始构建新模型,则model参数设置为yolov8x.yaml
  • 使用自己的数据集,则data参数最好使用绝对路径
  • 如果数据集进行了修改,比如标注文件调整了、图片增多了等等,那么在训练前一定要先把labels文件夹下面的train.cache和val.cache删掉再运行训练命令

在这里插入图片描述

训练得到的模型保存到runs/detect/train文件夹下

5. 推理预测

yolo task=detect mode=predict model=runs\detect\train\weights\best.pt source=datasets\VOCdevkit\images\val device=0

6. 模型导出

将训练好的pt模型文件导出为onnx格式的

yolo task=detect mode=export model=runs\detect\train\weights\best.pt format=onnx

  • 遇到的问题

由于没提前安装onnx,运行后会自动下载最新版本的onnx,接着就会报错max() arg is an empty sequence
在这里插入图片描述

在这里插入图片描述


  • 解决方法

1)按照输出可以知道Ultralytics要求的onnx>=1.12.0,最好就是直接安装1.12.0版本的,所以pip install onnx==1.12.0

2)直接使用上方的CLI命令导出onnx还是会报max() arg is an empty sequence,需要改用python脚本来导出,并指定onnx的opset设置为13;

在这里插入图片描述

from ultralytics import YOLOmodel = YOLO('F:/vsCode/ultralytics/runs/detect/train2/weights/best.pt')
model.export(format='onnx', opset=13)

3)运行该导出脚本即可导出成功
在这里插入图片描述

这篇关于使用YOLOv8训练自己的目标检测数据集(VOC格式/COCO格式)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/912226

相关文章

Qt spdlog日志模块的使用详解

《Qtspdlog日志模块的使用详解》在Qt应用程序开发中,良好的日志系统至关重要,本文将介绍如何使用spdlog1.5.0创建满足以下要求的日志系统,感兴趣的朋友一起看看吧... 目录版本摘要例子logmanager.cpp文件main.cpp文件版本spdlog版本:1.5.0采用1.5.0版本主要

Java中使用Hutool进行AES加密解密的方法举例

《Java中使用Hutool进行AES加密解密的方法举例》AES是一种对称加密,所谓对称加密就是加密与解密使用的秘钥是一个,下面:本文主要介绍Java中使用Hutool进行AES加密解密的相关资料... 目录前言一、Hutool简介与引入1.1 Hutool简介1.2 引入Hutool二、AES加密解密基础

使用Python将JSON,XML和YAML数据写入Excel文件

《使用Python将JSON,XML和YAML数据写入Excel文件》JSON、XML和YAML作为主流结构化数据格式,因其层次化表达能力和跨平台兼容性,已成为系统间数据交换的通用载体,本文将介绍如何... 目录如何使用python写入数据到Excel工作表用Python导入jsON数据到Excel工作表用

Mysql如何将数据按照年月分组的统计

《Mysql如何将数据按照年月分组的统计》:本文主要介绍Mysql如何将数据按照年月分组的统计方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录mysql将数据按照年月分组的统计要的效果方案总结Mysql将数据按照年月分组的统计要的效果方案① 使用 DA

鸿蒙中Axios数据请求的封装和配置方法

《鸿蒙中Axios数据请求的封装和配置方法》:本文主要介绍鸿蒙中Axios数据请求的封装和配置方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.配置权限 应用级权限和系统级权限2.配置网络请求的代码3.下载在Entry中 下载AxIOS4.封装Htt

鸿蒙中@State的原理使用详解(HarmonyOS 5)

《鸿蒙中@State的原理使用详解(HarmonyOS5)》@State是HarmonyOSArkTS框架中用于管理组件状态的核心装饰器,其核心作用是实现数据驱动UI的响应式编程模式,本文给大家介绍... 目录一、@State在鸿蒙中是做什么的?二、@Spythontate的基本原理1. 依赖关系的收集2.

Python基础语法中defaultdict的使用小结

《Python基础语法中defaultdict的使用小结》Python的defaultdict是collections模块中提供的一种特殊的字典类型,它与普通的字典(dict)有着相似的功能,本文主要... 目录示例1示例2python的defaultdict是collections模块中提供的一种特殊的字

C++ Sort函数使用场景分析

《C++Sort函数使用场景分析》sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变,如果某些场景需要保持相同元素间的相对顺序,可使... 目录C++ Sort函数详解一、sort函数调用的两种方式二、sort函数使用场景三、sort函数排序

Java String字符串的常用使用方法

《JavaString字符串的常用使用方法》String是JDK提供的一个类,是引用类型,并不是基本的数据类型,String用于字符串操作,在之前学习c语言的时候,对于一些字符串,会初始化字符数组表... 目录一、什么是String二、如何定义一个String1. 用双引号定义2. 通过构造函数定义三、St

Pydantic中Optional 和Union类型的使用

《Pydantic中Optional和Union类型的使用》本文主要介绍了Pydantic中Optional和Union类型的使用,这两者在处理可选字段和多类型字段时尤为重要,文中通过示例代码介绍的... 目录简介Optional 类型Union 类型Optional 和 Union 的组合总结简介Pyd