跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客GitHub 精选镜像工具UI配色美学隐私政策关于联系
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
PythonAI算法

YOLOv12 环境配置、训练与推理实战详解

YOLOv12 环境搭建涉及 Conda 虚拟环境配置、PyTorch 与 CUDA 版本适配及依赖库安装。教程涵盖 VOC 数据集转 YOLO 格式、训练验证集划分策略及配置文件编写。提供推理与训练代码示例,解析模型参数设置与断点续训方法,助力开发者高效完成目标检测任务部署与模型优化。

DevOpsTeam发布于 2026/1/13更新于 2026/6/1330 浏览
YOLOv12 环境配置、训练与推理实战详解

YOLOv12 环境配置、训练与推理实战详解

YOLOv12 是近期发布的目标检测模型,由纽约州立大学联合中科院推出。相比前代版本,它在网络结构上进行了优化,引入了残差高效层聚合网络 (R-ELAN) 和区域注意力机制 (Area-Attention),在保持高精度的同时提升了效率。

一、代码获取与模型结构

官方源码地址:https://github.com/sunsmarterjie/yolov12

1. 模型结构图

根据 yolov12.yaml 配置文件绘制的整体结构图显示,YOLOv12 对比 YOLOv11 减少了总层数,网络结构更加精简。核心改进在于 A2C2f 模块中引入的区域注意力机制,通过十字形窗口自我注意机制计算纵横交错的注意力,以较少的计算量获得更大的感受野。

YOLOv12 性能表现

理论细节可参考论文:https://arxiv.org/pdf/2502.12524

二、环境配置教程

YOLOv12 的环境搭建与 v11/v10 等版本通用,建议创建独立的虚拟环境以避免依赖冲突。如果遇到 ImportError: cannot import name 'scaled_dot_product_attention' 错误,通常意味着 PyTorch 版本过低,需重新配置环境。

1. 创建虚拟环境

推荐使用 Python 3.9 至 3.11 版本。这里以 3.11 为例:

conda create -n yolov12 python=3.11

输入 y 确认安装并等待下载完成。

2. 激活虚拟环境

conda activate yolov12

激活成功后,命令行左侧会显示 (yolov12) 标识。

3. 查询 CUDA 支持版本

无显卡用户可跳过此步。有显卡用户可通过终端输入 nvidia-smi 查看支持的最高 CUDA 版本。例如显示 CUDA 12.5,则安装 PyTorch 时可选择向下兼容的版本(如 cu121 或 cu118)。

若驱动过旧导致 CUDA 版本低,可前往 NVIDIA 官网更新驱动后重启电脑再次检查。

4. PyTorch 安装

根据官网推荐及兼容性,建议安装 PyTorch 2.2.2 版本。有 GPU 的用户选择带 CUDA 的版本,无 GPU 则选 CPU 版。

在线安装: 访问 PyTorch 官网复制对应命令,注意 -c 后面的内容无需复制。

离线安装: 若在线安装失败,可下载 .whl 文件进行本地安装。文件名包含关键信息:

  • cu118 / cu102:CUDA 版本
  • cp311 / cp39:Python 版本
  • win:操作系统

下载完成后,进入文件所在目录执行:

pip install torch-xxx.whl
pip install torchvision-xxx.whl
pip install torchaudio-xxx.whl

顺序依次为 torch、torchvision、torchaudio。

5. 验证 GPU 可用性

安装完成后,在终端运行 Python 测试:

import torch
print(torch.__version__)
print(torch.cuda.is_available())
print(torch.cuda.device_count())

输出 True 表示 GPU 可用。若为 False,CPU 训练速度会较慢,但推理功能正常。

6. 安装其他依赖

直接安装 requirements.txt 可能会因版本冲突报错。建议先清理该文件,保留基础依赖,缺什么再补什么。

matplotlib>=3.3.0
numpy==1.24.4
opencv-python>=4.6.0
pillow>=7.1.2
pyyaml>=5.3.1
requests>=2.23.0
scipy>=1.4.1
tqdm>=4.64.0
pandas>=1.1.4
seaborn>=0.11.0
thop>=0.1.1
psutil

安装命令:

pip install -r requirements.txt

若运行推理时报错缺少库,按需补充,例如:

pip install huggingface-hub==0.23.2

7. Flash Attention 补充

部分配置中包含 flash_attn 库,主要用于 Linux 环境加速。Windows 下若无特殊需求可不安装,代码内部已做兼容处理:优先使用 flash_attn_func,未启用时自动回退到标准缩放点积注意力 (sdpa)。

三、数据集准备

1. 标注工具

推荐使用 LabelImg 或 LabelMe。安装方式:

pip install labelimg

启动后设置自动保存路径即可开始标注。

2. VOC 格式转换

若标注结果为 VOC 格式(XML),需转换为 YOLO 格式(TXT)。以下脚本用于批量转换:

# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import os, cv2
import numpy as np

classes = []

def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = (box[0] + box[1]) / 2.0 - 1
    y = (box[2] + box[3]) / 2.0 - 1
    w = box[1] - box[0]
    h = box[3] - box[2]
    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)

def convert_annotation(xmlpath, xmlname):
    with open(xmlpath, "r", encoding='utf-8') as in_file:
        txtname = xmlname[:-4] + '.txt'
        txtfile = os.path.join(txtpath, txtname)
        tree = ET.parse(in_file)
        root = tree.getroot()
        filename = root.find('filename')
        img = cv2.imdecode(np.fromfile('{}/{}.{}'.format(imgpath, xmlname[:-4], postfix), np.uint8), cv2.IMREAD_COLOR)
        h, w = img.shape[:2]
        res = []
        for obj in root.iter('object'):
            cls = obj.find('name').text
            if cls not in classes:
                classes.append(cls)
            cls_id = classes.index(cls)
            xmlbox = obj.find('bndbox')
            b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
            bb = convert((w, h), b)
            res.append(str(cls_id) + " " + " ".join([str(a) for a in bb]))
        if len(res) != 0:
            with open(txtfile, 'w+') as f:
                f.write('\n'.join(res))

if __name__ == "__main__":
    postfix = 'png'
    imgpath = r'E:\A-毕业设计代做数据\helmet\test\images'
    xmlpath = r'E:\A-毕业设计代做数据\helmet\test\annotations'
    txtpath = r'E:\A-毕业设计代做数据\helmet\test\labels'
    if not os.path.exists(txtpath):
        os.makedirs(txtpath, exist_ok=True)
    listdir = os.listdir(xmlpath)
    error_file_list = []
    for i in range(0, len(listdir)):
        try:
            path = os.path.join(xmlpath, listdir[i])
            if ('.xml' in path) or ('.XML' in path):
                convert_annotation(path, listdir[i])
                print(f'file {listdir[i]} convert success.')
            else:
                print(f'file {listdir[i]} is not xml format.')
        except Exception as e:
            print(f'file {listdir[i]} convert error.')
            print(f'error message:\n{e}')
            error_file_list.append(listdir[i])
    print(f'this file convert failure\n{error_file_list}')
    print(f'Dataset Classes:{classes}')

修改要点:调整 postfix、imgpath、xmlpath 和 txtpath 为你的实际路径。

3. 数据集划分

将图片与标签按 8:2 比例划分为训练集和验证集:

# -*- coding: utf-8 -*-
import os, shutil
from sklearn.model_selection import train_test_split

val_size = 0.2
postfix = 'jpg'
imgpath = r'E:\A-毕业设计代做数据\datasets\images'
txtpath = r'E:\A-毕业设计代做数据\datasets\labels'
output_train_img_folder = r'E:\A-毕业设计代做数据\datasets\dataset_kengwa\images\train'
output_val_img_folder = r'E:\A-毕业设计代做数据\datasets\dataset_kengwa\images\val'
output_train_txt_folder = r'E:\A-毕业设计代做数据\datasets\dataset_kengwa\labels\train'
output_val_txt_folder = r'E:\A-毕业设计代做数据\datasets\dataset_kengwa\labels\val'

os.makedirs(output_train_img_folder, exist_ok=True)
os.makedirs(output_val_img_folder, exist_ok=True)
os.makedirs(output_train_txt_folder, exist_ok=True)
os.makedirs(output_val_txt_folder, exist_ok=True)

listdir = [i for i in os.listdir(txtpath) if 'txt' in i]
train, val = train_test_split(listdir, test_size=val_size, shuffle=True, random_state=0)

for i in train:
    img_source_path = os.path.join(imgpath, '{}.{}'.format(i[:-4], postfix))
    txt_source_path = os.path.join(txtpath, i)
    img_destination_path = os.path.join(output_train_img_folder, '{}.{}'.format(i[:-4], postfix))
    txt_destination_path = os.path.join(output_train_txt_folder, i)
    shutil.copy(img_source_path, img_destination_path)
    shutil.copy(txt_source_path, txt_destination_path)

for i in val:
    img_source_path = os.path.join(imgpath, '{}.{}'.format(i[:-4], postfix))
    txt_source_path = os.path.join(txtpath, i)
    img_destination_path = os.path.join(output_val_img_folder, '{}.{}'.format(i[:-4], postfix))
    txt_destination_path = os.path.join(output_val_txt_folder, i)
    shutil.copy(img_source_path, img_destination_path)
    shutil.copy(txt_source_path, txt_destination_path)

确保图片与标签路径对应一致,文件夹结构符合 YOLO 要求。

4. 修改训练配置文件

在项目根目录创建 data.yaml,定义训练集、验证集路径及类别名称:

train: E:\Desktop\new-yolov9\yolotest\images\train # train images
val: E:\Desktop\new-yolov9\yolotest\images\val # val images
nc: 2 # class names
names: ['dog', 'cat']

注意路径需与实际存放位置一致。

四、YOLOv12 推理

1. 下载预训练模型

从 GitHub 仓库下载对应场景的权重文件(如 yolov12n.pt)。

2. 编写推理脚本

新建 detect.py,填入模型路径和图片路径:

# -*- coding: utf-8 -*-
from ultralytics import YOLO

if __name__ == '__main__':
    model = YOLO(model=r'D:\2-Python\1-YOLO\YOLOv11\ultralytics-8.3.2\yolo11n-seg.pt')
    model.predict(source=r'D:\2-Python\1-YOLO\YOLOv11\ultralytics-8.3.2\ultralytics\assets\bus.jpg', save=True, show=False,)

参数说明:

  • model: 模型权重路径
  • source: 图片或视频路径,摄像头填 0
  • save: True 保存结果
  • show: True 弹窗显示结果

五、YOLOv12 训练

新建 train.py 文件:

# -*- coding: utf-8 -*-
import warnings
warnings.filterwarnings('ignore')
from ultralytics import YOLO

if __name__ == '__main__':
    model = YOLO(model=r'D:\2-Python\1-YOLO\YOLOv12\yolov12-main\ultralytics\cfg\models\v12\yolov12.yaml')
    # model.load('yolo11n.pt') # 如需加载预训练权重可开启
    model.train(data=r'data.yaml', imgsz=640, epochs=50, batch=4, workers=0, device='', optimizer='SGD', close_mosaic=10, resume=False, project='runs/train', name='exp', single_cls=False, cache=False,)

参数解析:

  • model: 模型配置文件路径
  • data: 数据集配置文件路径
  • imgsz: 输入图像尺寸,默认 640
  • epochs: 训练轮数
  • batch: 批处理大小,显存大可调大
  • workers: 数据加载线程数,爆显存可设为 0
  • device: 设备选择,留空自动
  • resume: 是否断点续训
  • project / name: 结果保存路径

科研改进时,有时不加载预训练权重效果更佳。

六、断点续训

训练中断后可继续上次进度,方法如下:

  1. 找到上次训练的 last.pt 模型文件。
  2. 修改 train.py 中的参数:
    • model: 指向 last.pt
    • resume: 设置为 True

这样即可加载之前的权重和优化器状态继续训练。

总结

本文涵盖了 YOLOv12 从环境搭建、数据处理到训练推理的全流程。重点解决了依赖冲突、CUDA 版本匹配及数据集格式转换等常见问题。掌握这些步骤后,即可基于 YOLOv12 开展目标检测任务开发与优化。

目录

  1. YOLOv12 环境配置、训练与推理实战详解
  2. 一、代码获取与模型结构
  3. 1. 模型结构图
  4. 二、环境配置教程
  5. 1. 创建虚拟环境
  6. 2. 激活虚拟环境
  7. 3. 查询 CUDA 支持版本
  8. 4. PyTorch 安装
  9. 5. 验证 GPU 可用性
  10. 6. 安装其他依赖
  11. 7. Flash Attention 补充
  12. 三、数据集准备
  13. 1. 标注工具
  14. 2. VOC 格式转换
  15. -- coding: utf-8 --
  16. 3. 数据集划分
  17. -- coding: utf-8 --
  18. 4. 修改训练配置文件
  19. 四、YOLOv12 推理
  20. 1. 下载预训练模型
  21. 2. 编写推理脚本
  22. -- coding: utf-8 --
  23. 五、YOLOv12 训练
  24. -- coding: utf-8 --
  25. 六、断点续训
  26. 总结
  • 💰 8折买阿里云服务器限时8折了解详情
  • Magick API 一键接入全球大模型注册送1000万token查看
  • 🤖 一键搭建Deepseek满血版了解详情
  • 一键打造专属AI 智能体了解详情
极客日志微信公众号二维码

微信扫一扫,关注极客日志

微信公众号「极客日志V2」,在微信中扫描左侧二维码关注。展示文案:极客日志V2 zeeklog

更多推荐文章

查看全部
  • Windows 11 本地部署 Ollama AI 大模型服务完整指南
  • Python 30 行实现公开接口数据本地化存储
  • Python 月相可视化系统:从天文计算到 Web 界面生成
  • Python + MySQL + Web 构建私有 Apple 设备监控系统
  • Python 基于人脸识别的自习室座位预约系统
  • Kubernetes 测试环境部署方案(Round 2)
  • C++ STL 有序关联容器 set、multiset、map、multimap 使用指南
  • 大模型领域热门岗位解析与求职指南
  • 从 vw/vh 到 clamp(),前端响应式设计的痛点与进化
  • LigerUI入门帮助(API里没写的入门帮助)【不断更新】
  • 无人机航测内业处理:iTwin Capture Modeler 建模与土方算量
  • Python内存管理深潜:从引用计数到GC机制的全面优化实战
  • Vivado AXI4-Stream Data FIFO 核参数配置与测试解析
  • Web 自动化测试入门:Selenium 原理与实战
  • Windows 11 环境下通过命令行升级 Python 版本的方法
  • 免费在线 SQL 转 ER 图工具推荐
  • Qwen3-4B-Instruct 模型快速部署与 AI 写作应用指南
  • 字符串模拟算法题精选:思维与实现解析
  • MySQL 数据库基础核心概念与实战入门
  • 无线联邦学习:隐私保护下的分布式 AI 协同机制

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • RSA密钥对生成器

    生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online

  • Mermaid 预览与可视化编辑

    基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online

  • 随机西班牙地址生成器

    随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • curl 转代码

    解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online