YOLOv11 目标检测

本文章不再赘述anaconda的下载以及虚拟环境的配置,博主使用的python版本为3.8

1.获取YOLOv11的源工程文件

链接:GitHub - ultralytics/ultralytics: Ultralytics YOLO11 🚀

直接下载解压

2.需要自己准备的文件

文件结构如下:红框部分的文件需要自己补充(见下文)

yolo11n.pt (还是在同一个链接下)

my_train.py

import warnings
warnings.filterwarnings('ignore')
from ultralytics import YOLOif __name__ == '__main__':model = YOLO('ultralytics/cfg/models/11/yolo11.yaml')model.load('yolo11n.pt') # loading pretrain weightsmodel.train(data='E:/pytroch/YOLO/yolov11/ultralytics-main-250314/ultralytics-main/my_Data.yaml',imgsz=640,epochs=100,batch=32,workers=8,#close_mosaic=10,device='cpu',#device='0',optimizer='SGD', # using SGD#project='runs/train',#name='exp',amp=False,cache=False,  # 服务器可设置为True,训练速度变快)

.yaml的路径要使用绝对路径

my_predict图像推理.py (之后可换成自己训练的模型)

from ultralytics import YOLO# Load a pre-trained YOLO model (adjust model type as needed)
model = YOLO("yolo11n.pt")  # n, s, m, l, x versions available# Perform object detection on an image
results = model.predict(source="1.jpg")  # Can also use video, directory, URL, etc.# Display the results
results[0].show()  # Show the first image results

my_predict实时推理.py (之后可换成自己训练的模型)

from ultralytics import YOLO
import cv2
import numpy as np
import time# 加载模型
model = YOLO("yolo11n.pt")# 打开视频文件
#video_path = "720p.mp4"  # 替换为你的视频路径
#cap = cv2.VideoCapture(video_path)
cap = cv2.VideoCapture(0) #使用外置USB摄像头# 检查视频是否打开成功
if not cap.isOpened():print("Error: Cannot open video file.")exit()# 获取视频信息
fps = int(cap.get(cv2.CAP_PROP_FPS))
print(f"Video FPS: {fps}")# 获取视频宽高
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))# 实时推理并显示结果
while True:start_time = time.time()  # 记录帧处理开始时间ret, frame = cap.read()if not ret:break  # 视频结束# 模型推理results = model(frame)  # 对当前帧进行推理for r in results:# 绘制检测结果图像im_array = r.plot()  # 带预测结果的图像 (BGR 格式)# 检查是否有实例分割掩码if r.masks is not None:# 计算总像素占比total_pixels = frame_width * frame_height  # 图像总像素数masks = r.masks.data.cpu().numpy()  # 获取实例分割的掩码total_object_pixels = np.sum(masks)  # 所有物体的像素总和total_percentage = (total_object_pixels / total_pixels) * 100  # 百分比计算# 在图像左上角绘制百分比信息text = f"Total percentage: {total_percentage:.2f}%"else:text = "No objects detected"  # 没有检测到物体时的提示信息font = cv2.FONT_HERSHEY_SIMPLEXfont_scale = 1.2color = (0, 0, 255)thickness = 2position = (20, 40)  # 文本位置 (x, y)im_array = cv2.putText(im_array, text, position, font, font_scale, color, thickness)# 显示当前帧的推理结果cv2.imshow("Inference", im_array)# 计算帧率elapsed_time = time.time() - start_timeprint(f"Processing time per frame: {elapsed_time:.2f} seconds ({1/elapsed_time:.2f} FPS)")# 按 'q' 键退出if cv2.waitKey(1) & 0xFF == ord('q'):break# 释放资源
cap.release()
cv2.destroyAllWindows()

my_Data.yaml

train: my_Dataset\train # train images (relative to 'path') 128 images
val: my_Dataset\val  # val images (relative to 'path') 128 images
test: my_Dataset\testnc: 4# Classes
names: ['recycle','hazardous','foodWaste','other']

路径为数据集的路径(见下文)

names的类别顺序:与labels标签一致即可

my_Dataset存放数据集(数据集文件结构如下)

my_Dataset
|
|
|
----> test---->images1.jpg2.jpg……---->labels1.txt2.txt……----> train---->images3.jpg4.jpg……---->labels3.txt4.txt……----> val---->images5.jpg6.jpg……---->labels5.txt6.txt……

3.模型训练

在所有文件配置好后运行my_train.py

关于训练参数的设置:

翻译置:Configuration - Ultralytics YOLO Docs

YOLO 模型的训练设置包含训练过程中使用的各种超参数和配置。这些设置会影响模型的性能、速度和准确性。关键训练设置包括批量大小、学习率、动量和权重衰减。此外,优化器、损失函数和训练数据集组合的选择也会影响训练过程。仔细调整和试验这些设置对于优化性能至关重要。

参数Argument

类型

Type

默认值

Default

描述

Description

modelstrNone

指定用于训练的模型文件。接受 .pt 预训练模型或 .yaml 配置文件的路径。对于定义模型结构或初始化权重至关重要。

Specifies the model file for training. Accepts a path to either a .pt pretrained model or a .yaml configuration file. Essential for defining the model structure or initializing weights.

datastrNone

数据集配置文件的路径(例如,coco8.yaml)。该文件包含特定于数据集的参数,包括训练和验证数据的路径、类名和类数。

Path to the dataset configuration file (e.g., coco8.yaml). This file contains dataset-specific parameters, including paths to training and validation data, class names, and number of classes.

epochsint100

​训练 epoch 的总数。每个 epoch 表示整个数据集的完整传递。调整此值可能会影响训练持续时间和模型性能。

Total number of training epochs. Each epoch represents a full pass over the entire dataset. Adjusting this value can affect training duration and model performance.

timefloatNone

最大训练时间(小时)。如果设置,这将覆盖 epochs 参数,从而允许训练在指定的持续时间后自动停止。适用于时间受限的训练场景。

Maximum training time in hours. If set, this overrides the epochs argument, allowing training to automatically stop after the specified duration. Useful for time-constrained training scenarios.

patienceint100

​在提前停止训练之前,在验证指标没有改善的情况下等待的epochs数。通过在性能停滞时停止训练来帮助防止过度拟合。

Number of epochs to wait without improvement in validation metrics before early stopping the training. Helps prevent overfitting by stopping training when performance plateaus.

batchint16

具有三种模式:设置为整数(例如,batch=16)、GPU 内存利用率为 60% 的自动模式 (batch=-1) 或具有指定利用率分数的自动模式 (batch=0.70)。

Batch size, with three modes: set as an integer (e.g., batch=16), auto mode for 60% GPU memory utilization (batch=-1), or auto mode with specified utilization fraction (batch=0.70).

imgszint or list640

用于训练的目标图像大小。在输入到模型之前,所有图像都会调整到此尺寸。影响模型精度和计算复杂度。

Target image size for training. All images are resized to this dimension before being fed into the model. Affects model accuracy and computational complexity.

saveboolTrue

支持保存训练检查点和最终模型权重。对于恢复训练或模型部署非常有用。

Enables saving of training checkpoints and final model weights. Useful for resuming training or model deployment.

save_periodint-1

保存模型检查点的频率,以 epoch 为单位指定。值为 -1 将禁用此功能。对于在长时间训练期间保存临时模型很有用。

Frequency of saving model checkpoints, specified in epochs. A value of -1 disables this feature. Useful for saving interim models during long training sessions.

cacheboolFalse

启用数据集图像在内存 (True/ram)、磁盘 (disk) 上的缓存,或禁用数据集图像缓存 (False)。通过减少磁盘 I/O 来提高训练速度,但代价是内存使用量增加。

Enables caching of dataset images in memory (True/ram), on disk (disk), or disables it (False). Improves training speed by reducing disk I/O at the cost of increased memory usage.

deviceint or str or listNone

指定用于训练的计算设备:单个 GPU (device=0)、多个 GPU (device=0,1)、CPU (device=cpu) 或 Apple Silicon 的 MPS (device=mps)。

Specifies the computational device(s) for training: a single GPU (device=0), multiple GPUs (device=0,1), CPU (device=cpu), or MPS for Apple silicon (device=mps).

workersint8

用于数据加载的工作线程数(如果是多 GPU 训练,则按 RANK 计算)。影响数据预处理和馈送到模型的速度,在多 GPU 设置中特别有用。

Number of worker threads for data loading (per RANK if Multi-GPU training). Influences the speed of data preprocessing and feeding into the model, especially useful in multi-GPU setups.

projectstrNone

保存训练输出的项目目录的名称。允许有序地存储不同的实验。

Name of the project directory where training outputs are saved. Allows for organized storage of different experiments.

namestrNone

训练运行的名称。用于在项目文件夹中创建子目录,用于存储训练日志和输出。

Name of the training run. Used for creating a subdirectory within the project folder, where training logs and outputs are stored.

exist_okboolFalse

如果为 True,则允许覆盖现有 project/name 目录。对于迭代实验很有用,无需手动清除以前的输出。

If True, allows overwriting of an existing project/name directory. Useful for iterative experimentation without needing to manually clear previous outputs.

pretrainedboolTrue

确定是否从预训练的模型开始训练。可以是布尔值或指向要从中加载权重的特定模型的字符串路径。提高训练效率和模型性能。

Determines whether to start training from a pretrained model. Can be a boolean value or a string path to a specific model from which to load weights. Enhances training efficiency and model performance.

optimizerstr'auto'

选择用于训练的优化器。选项包括 SGD、Adam、AdamW、NAdam、RAdam、RMSProp 等,或 auto,用于根据模型配置自动选择。影响收敛速度和稳定性。

Choice of optimizer for training. Options include SGDAdamAdamWNAdamRAdamRMSProp etc., or auto for automatic selection based on model configuration. Affects convergence speed and stability.

seedint0

设置用于训练的随机种子,确保结果在具有相同配置的运行中的可重复性。

Sets the random seed for training, ensuring reproducibility of results across runs with the same configurations.

deterministicboolTrue

强制使用确定性算法,确保可重现性,但由于对非确定性算法的限制,可能会影响性能和速度。

Forces deterministic algorithm use, ensuring reproducibility but may affect performance and speed due to the restriction on non-deterministic algorithms.

single_clsboolFalse

在训练期间,将多类数据集中的所有类视为单个类。对于二元分类任务或专注于对象存在而不是分类时非常有用。

Treats all classes in multi-class datasets as a single class during training. Useful for binary classification tasks or when focusing on object presence rather than classification.

classeslist[int]None

指定要训练的类 ID 列表。对于在训练期间筛选并仅关注某些类很有用。

Specifies a list of class IDs to train on. Useful for filtering out and focusing only on certain classes during training.

rectboolFalse

启用矩形训练,优化批量合成以实现最少的填充。可以提高效率和速度,但可能会影响模型准确性。

Enables rectangular training, optimizing batch composition for minimal padding. Can improve efficiency and speed but may affect model accuracy.

multi_scaleboolFalse

通过在训练期间将 imgsz 增加/减少 0.5 倍来实现多尺度训练。在推理过程中使用多个 imgsz 训练模型以使其更准确。

Enables multi-scale training by increasing/decreasing imgsz by upto a factor of 0.5 during training. Trains the model to be more accurate with multiple imgsz during inference.

cos_lrboolFalse

利用余弦学习速率调度器,在历元上按照余弦曲线调整学习速率。有助于管理学习率以实现更好的收敛。

Utilizes a cosine learning rate scheduler, adjusting the learning rate following a cosine curve over epochs. Helps in managing learning rate for better convergence.

close_mosaicint10在最后N个时期禁用马赛克数据增强,以在完成前稳定训练。设置为0将禁用此功能。
Disables mosaic data augmentation in the last N epochs to stabilize training before completion. Setting to 0 disables this feature.
resumeboolFalse

从上次保存的检查点恢复训练。自动加载模型权重、优化器状态和 epoch 计数,无缝继续训练。

Resumes training from the last saved checkpoint. Automatically loads model weights, optimizer state, and epoch count, continuing training seamlessly.

ampboolTrue

支持自动混合精度(AMP)训练,减少内存使用,并可能加快训练速度,同时对准确性的影响最小。

Enables Automatic Mixed Precision (AMP) training, reducing memory usage and possibly speeding up training with minimal impact on accuracy.

fractionfloat1.0

指定用于训练的数据集的分数。允许对完整数据集的子集进行训练,这对于实验或资源有限时非常有用。

Specifies the fraction of the dataset to use for training. Allows for training on a subset of the full dataset, useful for experiments or when resources are limited.

profileboolFalse

在训练期间启用 ONNX 和 TensorRT 速度分析,有助于优化模型部署。

Enables profiling of ONNX and TensorRT speeds during training, useful for optimizing model deployment.

freezeint or listNone

冻结模型的前N层或按索引指定的层,减少可训练参数的数量。有助于微调或迁移学习。

Freezes the first N layers of the model or specified layers by index, reducing the number of trainable parameters. Useful for fine-tuning or transfer learning.

lr0float0.01

初始学习率(即 SGD=1E-2,Adam=1E-3)。调整此值对于优化过程至关重要,它会影响模型权重的更新速度。

Initial learning rate (i.e. SGD=1E-2Adam=1E-3) . Adjusting this value is crucial for the optimization process, influencing how rapidly model weights are updated.

lrffloat0.01

最终学习率占初始学习率的分数 = (lr0 * lrf),与调度器结合使用,以随时间调整学习率。

Final learning rate as a fraction of the initial rate = (lr0 * lrf), used in conjunction with schedulers to adjust the learning rate over time.

momentumfloat0.937

​SGD 的动量因子或 Adam 优化器的 beta1,影响当前更新中过去梯度的合并。

Momentum factor for SGD or beta1 for Adam optimizers, influencing the incorporation of past gradients in the current update.

weight_decayfloat0.0005

L2正则化项,惩罚大权重以防止过度拟合。

L2 regularization term, penalizing large weights to prevent overfitting.

warmup_epochsfloat3.0

学习率预热的 epoch 数,逐渐将学习率从低值提高到初始学习率,以尽早稳定训练。

Number of epochs for learning rate warmup, gradually increasing the learning rate from a low value to the initial learning rate to stabilize training early on.

warmup_momentumfloat0.8

预热阶段的初始动量,在预热期间逐渐适应设定的动量。

Initial momentum for warmup phase, gradually adjusting to the set momentum over the warmup period.

warmup_bias_lrfloat0.1

预热阶段偏差参数的学习率,有助于稳定初始 epoch 中的模型训练。

Learning rate for bias parameters during the warmup phase, helping stabilize model training in the initial epochs.

boxfloat7.5

损失函数中 box loss 分量的权重,影响对准确预测边界框坐标的重视程度。

Weight of the box loss component in the loss function, influencing how much emphasis is placed on accurately predicting bounding box coordinates.

clsfloat0.5

分类损失在总损失函数中的权重,影响相对于其他分量的正确类别预测的重要性。

Weight of the classification loss in the total loss function, affecting the importance of correct class prediction relative to other components.

dflfloat1.5

分布焦点损失的权重,用于某些 YOLO 版本以进行精细分类。

Weight of the distribution focal loss, used in certain YOLO versions for fine-grained classification.

posefloat12.0

为姿势估计训练的模型中姿势损失的权重,影响对准确预测姿势关键点的重视。

Weight of the pose loss in models trained for pose estimation, influencing the emphasis on accurately predicting pose keypoints.

kobjfloat2.0

姿态估计模型中关键点客体损失的权重,平衡检测置信度和姿态准确性。

Weight of the keypoint objectness loss in pose estimation models, balancing detection confidence with pose accuracy.

nbsint64

损失标准化的标称批量。

Nominal batch size for normalization of loss.

overlap_maskboolTrue

确定是应将对象掩码合并到单个掩码中进行训练,还是为每个对象保持单独。在重叠的情况下,在合并过程中,较小的蒙版将覆盖在较大的蒙版之上。

Determines whether object masks should be merged into a single mask for training, or kept separate for each object. In case of overlap, the smaller mask is overlaid on top of the larger mask during merge.

mask_ratioint4

分割掩码的下采样率,影响训练期间使用的掩码的分辨率。

Downsample ratio for segmentation masks, affecting the resolution of masks used during training.

dropoutfloat0.0

分类任务中正则化的 dropout rate,防止在训练期间通过随机省略单元来过度拟合。

Dropout rate for regularization in classification tasks, preventing overfitting by randomly omitting units during training.

valboolTrue

在训练期间启用验证,允许在单独的数据集上定期评估模型性能。

Enables validation during training, allowing for periodic evaluation of model performance on a separate dataset.

plotsboolFalse

生成并保存训练和验证指标的图以及预测示例,提供对模型性能和学习进度的可视化见解。

Generates and saves plots of training and validation metrics, as well as prediction examples, providing visual insights into model performance and learning progression.

4.模型推理

训练结果保存在文件夹runs中

将my_predict实时推理.py 的模型换成自己训练的模型并运行

5.在树莓派4b上推理

训练后的模型大小有10Mb以上(不进行量化),树莓派光用cpu跑只有可怜的1帧

树莓派4b上运行yolov11n

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.rhkb.cn/news/38527.html

如若内容造成侵权/违法违规/事实不符,请联系长河编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Linux】——环境变量与进程地址空间

文章目录 环境变量环境变量的概念常见的环境变量PATH相关指令 main的三个参数前两个参数第三个参数 程序地址空间进程地址空间 环境变量 环境变量的概念 环境变量一般是指在操作系统中用来指定操作系统运行环境的一些参数,将来会以shell的形式传递给所有进程&…

Kafka--常见问题

1.为什么要使用 Kafka,起到什么作用 Kafka是一个高吞吐量、分布式、基于发布订阅的消息系统,它主要用于处理实时数据流 Kafka 设计上支持高吞吐量的消息传输,每秒可以处理数百万条消息。它能够在处理大量并发请求时,保持低延迟和…

Flutter:页面滚动,导航栏背景颜色过渡动画

记录:导航默认透明,页面发生滚动后,导航背景色由0-1,过渡到白色背景。 view import package:ducafe_ui_core/ducafe_ui_core.dart; import package:flutter/material.dart; import package:get/get.dart; import package:redo…

探秘格式化:数据危机与恢复之道

引言 在数字化飞速发展的当下,数据已然成为我们生活中不可或缺的一部分。无论是珍贵的家庭照片、重要的工作文档,还是企业关键的业务数据,都承载着我们的回忆、努力和希望。然而,格式化这一操作却如同隐藏在数字世界中的“幽灵”…

人工智能 - 通用 AI Agent 之 LangManus、Manus、OpenManus 和 OWL 技术选型

一、核心项目概览 1. Manus(闭源通用 AI Agent) 定位 :全球首个全流程自动化通用 AI Agent,GAIA 基准测试 SOTA 水平。核心能力 : 全流程自动化 :从任务规划(如撰写报告)到执行(代码生成、表格制作)的端到端处理。智能纠错机制 :基于沙箱环境的实时错误反思与调整…

封装一个分割线组件

最终样式 Vue2代码 <template><div class"sep-line"><div class"sep-label"><span class"sep-box-text"><slot>{{ title }}</slot> <!-- 默认插槽内容&#xff0c;如果没有传递内容则使用title -->&…

走进Java:String字符串的基本使用

❀❀❀ 大佬求个关注吧~祝您开心每一天 ❀❀❀ 目录 一、什么是String 二、如何定义一个String 1. 用双引号定义 2. 通过构造函数定义 三、String中的一些常用方法 1 字符串比较 1.1 字符串使用 1.2 字符串使用equals() 1.3 使用 equalsIgnoreCase() 1.4 cpmpareTo…

第2.2节 Android Jacoco插件覆盖率采集

JaCoCo&#xff08;Java Code Coverage&#xff09;是一款开源的代码覆盖率分析工具&#xff0c;适用于Java和Android项目。它通过插桩技术统计测试过程中代码的执行情况&#xff0c;生成可视化报告&#xff0c;帮助开发者评估测试用例的有效性。在github上开源的项目&#xff…

OpenGL ES ->乒乓缓冲,计算只用两个帧缓冲对象(Frame Buffer Object)+叠加多个滤镜作用后的Bitmap

乒乓缓冲核心思想 不使用乒乓缓冲&#xff0c;如果要每个滤镜作用下的绘制内容&#xff0c;也就是这个滤镜作用下的帧缓冲&#xff0c;需要创建一个Frame Buffer Object加上对应的Frame Buffer Object Texture使用乒乓缓冲&#xff0c;只用两个Frame Buffer Object加上对应的F…

Unity导出WebGL,无法加载,data文件无法找到 404(NotFound)

问题&#xff1a;data文件无法找到404Not found 示例是使用IIS托管启动 F12可以看到not found 的报错 解决办法&#xff1a; iis无法识别data文件&#xff0c;在MIME类型中增加data 类型&#xff1a;application/octet-stream 添加之后&#xff0c;会在根目录下生产一个…

C++与OO思想的联系

一、C与OO思想的联系 C&#xff1a;OO思想&#xff08;面向对象--属性和行为&#xff09; 任何事务都可以被看做一个个对象&#xff0c;一个再复杂的模型结构都是由千千万万个对象组成。 OO思想两个要素&#xff1a;属性和行为(方法)。 OO思想的特点&#xff1a; 封装&#x…

单表达式倒计时工具:datetime的极度优雅(DeepSeek)

一个简单表达式&#xff0c;也可以优雅自成工具。 笔记模板由python脚本于2025-03-22 20:25:49创建&#xff0c;本篇笔记适合任意喜欢学习的coder翻阅。 【学习的细节是欢悦的历程】 博客的核心价值&#xff1a;在于输出思考与经验&#xff0c;而不仅仅是知识的简单复述。 Pyth…

Kubernetes的Replica Set和ReplicaController有什么区别

ReplicaSet 和 ReplicationController 是 Kubernetes 中用于管理应用程序副本的两种资源&#xff0c;它们有类似的功能&#xff0c;但 ReplicaSet 是 ReplicationController 的增强版本。 以下是它们的主要区别&#xff1a; 1. 功能的演进 ReplicationController 是 Kubernete…

CSS基础知识一览

持续维护 选择器 display 常用属性 浮动 弹性布局

IS-IS原理与配置

一、IS-IS概述 IS-IS&#xff08;Intermediate System to Intermediate System&#xff0c;中间系统到中间系统&#xff09;是ISO&#xff08;International Organization for Standardization&#xff0c;国际标准化组织&#xff09;为它的CLNP&#xff08;ConnectionLessNet…

【前端】Visual Studio Code安装配置教程:下载、汉化、常用组件、基本操作

文章目录 一、Visual Studio Code下载二、汉化三、常用组件1、Auto Rename Tag2、view-in-browser3、Live Server 四、基本操作五、感谢观看&#xff01; 一、Visual Studio Code下载 下载官网&#xff1a;https://code.visualstudio.com/ 进入官网后点击右上角的Download &…

git推送代码相关学习——(一)

推荐去阅读一下廖老师的git相关的教程https://liaoxuefeng.com/books/git/introduction/index.html 这个系列就来学习一下git操作。 第一步&#xff0c;新建项目 去github中新建一个项目&#xff0c;然后依据项目来进行本地的开发工作。 第二步&#xff0c;拉取项目 git c…

CMS网站模板设计与用户定制化实战评测

内容概要 在数字化转型背景下&#xff0c;CMS平台作为企业内容管理的核心载体&#xff0c;其模板架构的灵活性与用户定制能力直接影响运营效率。通过对WordPress、Baklib等主流系统的技术解构发现&#xff0c;模块化设计理念已成为行业基准——WordPress依托超过6万款主题库实…

Maya基本操作

基本操作 按住ALT键&#xff0c;左键旋转视角&#xff0c;中键平移视角&#xff0c;右键放大缩小视角。 按空格键切换4格视图。 导入FBX格式文件后&#xff0c;无贴图显示。 按6键开启。着色纹理显示 坐标轴相关 修改菜单-左键最上面的虚线。固定修改选项窗口。 选中物体…

政安晨【超级AI工作流】—— 使用Dify通过工作流对接ComfyUI实现多工作流协同

政安晨的个人主页&#xff1a;政安晨 欢迎 &#x1f44d;点赞✍评论⭐收藏 希望政安晨的博客能够对您有所裨益&#xff0c;如有不足之处&#xff0c;欢迎在评论区提出指正&#xff01; 目录 一、准备工作 Dify跑起来 ollama局域网化配置 Dify配置并验证 启动ComfyUI 二、…