YOLOv10目标检测算法的使用

2024-06-23 10:36

本文主要是介绍YOLOv10目标检测算法的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

一、环境安装

1、创建虚拟环境

2、安装依赖

二、数据集准备

1、预训练权重

2、数据划分

3、建立数据集的yaml文件

 三、训练

1、终端运行指令

2、建立一个 python 文件运行

四、验证

1、终端运行指令

2、建立一个 python 文件运行

五、模型推理

1、单张图片推理

2、视频推理

六、导出报告

七、报错处理

1、提示数据集.yaml文件错误:RuntimeError: Dataset 'datasets/fire.yaml' error

八、附录

1、xml转txt脚本


一、环境安装

1、创建虚拟环境

conda create -n yolov10 python=3.8# 激活yolov9 env
conda activate yolov10

2、安装依赖

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

注意:如果需要终端运行命令,即yolo xxx,还需要运行下面命令进行额外安装:

pip install -e .

二、数据集准备

1、预训练权重

预训练权重下载:

import os
import urllib.request# Create a directory for the weights in the current working directory
weights_dir = os.path.join(os.getcwd(), "weights")
os.makedirs(weights_dir, exist_ok=True)# URLs of the weight files
urls = ["https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10n.pt","https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10s.pt","https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10m.pt","https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10b.pt","https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10x.pt","https://github.com/jameslahm/yolov10/releases/download/v1.0/yolov10l.pt"
]# Download each file
for url in urls:file_name = os.path.join(weights_dir, os.path.basename(url))urllib.request.urlretrieve(url, file_name)print(f"Downloaded {file_name}")

2、数据划分

 将需要训练的数据集,放入项目目录下,格式如下(目前的图片和标签是这个样子的):

Moon_Cake├─images└─all└─labels└─all

现在通过脚本将数据集进行划分,格式如下:

├── yolov10_dataset└── train└── images (folder including all training images)└── labels (folder including all training labels)└── test└── images (folder including all testing images)└── labels (folder including all testing labels)└── valid└── images (folder including all testing images)└── labels (folder including all testing labels)

划分代码:

import os
import random
import shutildef copy_files(src_dir, dst_dir, filenames, extension):os.makedirs(dst_dir, exist_ok=True)missing_files = 0for filename in filenames:src_path = os.path.join(src_dir, filename + extension)dst_path = os.path.join(dst_dir, filename + extension)# Check if the file exists before copyingif os.path.exists(src_path):shutil.copy(src_path, dst_path)else:print(f"Warning: File not found for {filename}")missing_files += 1return missing_filesdef split_and_copy_dataset(image_dir, label_dir, output_dir, train_ratio=0.7, valid_ratio=0.15, test_ratio=0.15):# 获取所有图像文件的文件名(不包括文件扩展名)image_filenames = [os.path.splitext(f)[0] for f in os.listdir(image_dir)]# 随机打乱文件名列表random.shuffle(image_filenames)# 计算训练集、验证集和测试集的数量total_count = len(image_filenames)train_count = int(total_count * train_ratio)valid_count = int(total_count * valid_ratio)test_count = total_count - train_count - valid_count# 定义输出文件夹路径train_image_dir = os.path.join(output_dir, 'train', 'images')train_label_dir = os.path.join(output_dir, 'train', 'labels')valid_image_dir = os.path.join(output_dir, 'valid', 'images')valid_label_dir = os.path.join(output_dir, 'valid', 'labels')test_image_dir = os.path.join(output_dir, 'test', 'images')test_label_dir = os.path.join(output_dir, 'test', 'labels')# 复制图像和标签文件到对应的文件夹train_missing_files = copy_files(image_dir, train_image_dir, image_filenames[:train_count], '.jpg')train_missing_files += copy_files(label_dir, train_label_dir, image_filenames[:train_count], '.txt')valid_missing_files = copy_files(image_dir, valid_image_dir, image_filenames[train_count:train_count + valid_count], '.jpg')valid_missing_files += copy_files(label_dir, valid_label_dir, image_filenames[train_count:train_count + valid_count], '.txt')test_missing_files = copy_files(image_dir, test_image_dir, image_filenames[train_count + valid_count:], '.jpg')test_missing_files += copy_files(label_dir, test_label_dir, image_filenames[train_count + valid_count:], '.txt')# Print the count of each datasetprint(f"Train dataset count: {train_count}, Missing files: {train_missing_files}")print(f"Validation dataset count: {valid_count}, Missing files: {valid_missing_files}")print(f"Test dataset count: {test_count}, Missing files: {test_missing_files}")# 使用例子
image_dir = 'datasets/coco128/images/train2017'
label_dir = 'datasets/coco128/labels/train2017'
output_dir = './my_dataset'split_and_copy_dataset(image_dir, label_dir, output_dir)

划分后的样子:

3、建立数据集的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: /hy-tmp/yolov10/datasets/dataset_split
train: train # train images
val: val # val images
test: test # test images (optional) # number of classes
nc: 2# Classes,这个类别也可以这样写:names: [ 'sleep' , 'phone' ]
names:0: sleep1: phone

下面的自带的:

path: ../datasets/coco128  # dataset root dir
train: images/train2017  # train images (relative to 'path') 128 images
val: images/train2017  # val images (relative to 'path') 128 images
test:  # test images (optional)# Classes
names:0: person1: bicycle2: car''''''79: toothbrush

 三、训练

YOLov10 提供两种训练方式,终端运行指令和新建一个 python 文件运行

1、终端运行指令

通过命令直接进行训练在其中指定参数,命令如下(注意:如果是Windows系统,Workers最好设置成0,否则容易报线程的错误):

# 从YAML构建一个新模型,从头开始训练
yolo task=detect mode=train model=yolov10n.yaml data=替换你数据集的yaml文件地址 batch=16 epochs=100 imgsz=640 workers=0 device=0# 从预训练的*.pt模型开始训练
yolo task=detect mode=train model=yolov10n.pt data=替换你数据集的yaml文件地址 batch=16 epochs=100 imgsz=640 workers=0 device=0# 从YAML中构建一个新模型,将预训练的权重转移到它并开始训练
yolo task=detect mode=train model=yolov10n.yaml pretrained=yolov10n.pt data=替换你数据集的yaml文件地址 batch=16 epochs=100 imgsz=640 workers=0 device=0

通过指定cfg直接进行训练,配置好ultralytics/cfg/default.yaml这个文件之后,可以直接执行这个文件进行训练,这样就不用在命令行输入其它的参数了:

yolo cfg=ultralytics/cfg/default.yaml

2、建立一个 python 文件运行

有些教程写的是,需要导入YOLOv10模块,而不是YOLO模块(参考),但是实际上也没啥问题(推荐这个,注意修改yolov10n.yaml模型配置文件的类别数)。

from ultralytics import YOLO# 模型配置文件
model_yaml_path = "ultralytics/cfg/models/v10/yolov10n.yaml"
#数据集配置文件
data_yaml_path = 'datasets/fire.yaml'
#预训练模型
pre_model_name = 'yolov10n.pt'if __name__ == '__main__':#加载预训练模型model = YOLO(model_yaml_path).load(pre_model_name)#训练模型results = model.train(data=data_yaml_path,epochs=20,batch=4,name='train_v10')

而应对指令命令的三种形式如下:

from ultralytics import YOLOv10# Load a model # 三选一
model = YOLOv10('yolov10n.yaml')  # build a new model from YAML
model = YOLOv10('yolov10n.pt')  # load a pretrained model (recommended for training)
model = YOLOv10('yolov10n.yaml').load('yolov10n.pt')  # build from YAML and transfer weights# Train the model
model.train(data='coco128.yaml', epochs=100, imgsz=640)

可以通过tensorboard查看实时训练效果:

tensorboard --logdir runs\detect\train2

四、验证

1、终端运行指令

yolo task=detect mode=val model=yolov10n.pt
# mode=val 就是看验证集
yolo task=detect mode=val split=val model=runs/detect/train2/weights/best.pt  data=ultralytics/datasets/MoonCake.yaml # 替换你数据集的yaml文件地址
# mode=test 就是看测试集
yolo task=detect mode=val split=test model=runs/detect/train2/weights/best.pt  data=ultraly

这篇关于YOLOv10目标检测算法的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C++11右值引用与Lambda表达式的使用

《C++11右值引用与Lambda表达式的使用》C++11引入右值引用,实现移动语义提升性能,支持资源转移与完美转发;同时引入Lambda表达式,简化匿名函数定义,通过捕获列表和参数列表灵活处理变量... 目录C++11新特性右值引用和移动语义左值 / 右值常见的左值和右值移动语义移动构造函数移动复制运算符

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

C#中lock关键字的使用小结

《C#中lock关键字的使用小结》在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时,其他线程无法访问同一实例的该代码块,下面就来介绍一下lock关键字的使用... 目录使用方式工作原理注意事项示例代码为什么不能lock值类型在C#中,lock关键字用于确保当一个线程位于给定实例的代码块中时

MySQL 强制使用特定索引的操作

《MySQL强制使用特定索引的操作》MySQL可通过FORCEINDEX、USEINDEX等语法强制查询使用特定索引,但优化器可能不采纳,需结合EXPLAIN分析执行计划,避免性能下降,注意版本差异... 目录1. 使用FORCE INDEX语法2. 使用USE INDEX语法3. 使用IGNORE IND

C# $字符串插值的使用

《C#$字符串插值的使用》本文介绍了C#中的字符串插值功能,详细介绍了使用$符号的实现方式,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录$ 字符使用方式创建内插字符串包含不同的数据类型控制内插表达式的格式控制内插表达式的对齐方式内插表达式中使用转义序列内插表达式中使用

flask库中sessions.py的使用小结

《flask库中sessions.py的使用小结》在Flask中Session是一种用于在不同请求之间存储用户数据的机制,Session默认是基于客户端Cookie的,但数据会经过加密签名,防止篡改,... 目录1. Flask Session 的基本使用(1) 启用 Session(2) 存储和读取 Se

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原

Spring AI使用tool Calling和MCP的示例详解

《SpringAI使用toolCalling和MCP的示例详解》SpringAI1.0.0.M6引入ToolCalling与MCP协议,提升AI与工具交互的扩展性与标准化,支持信息检索、行动执行等... 目录深入探索 Spring AI聊天接口示例Function CallingMCPSTDIOSSE结束语

Linux系统之lvcreate命令使用解读

《Linux系统之lvcreate命令使用解读》lvcreate是LVM中创建逻辑卷的核心命令,支持线性、条带化、RAID、镜像、快照、瘦池和缓存池等多种类型,实现灵活存储资源管理,需注意空间分配、R... 目录lvcreate命令详解一、命令概述二、语法格式三、核心功能四、选项详解五、使用示例1. 创建逻

在Java中使用OpenCV实践

《在Java中使用OpenCV实践》用户分享了在Java项目中集成OpenCV4.10.0的实践经验,涵盖库简介、Windows安装、依赖配置及灰度图测试,强调其在图像处理领域的多功能性,并计划后续探... 目录前言一 、OpenCV1.简介2.下载与安装3.目录说明二、在Java项目中使用三 、测试1.测