欢迎光临
我们一直在努力

Windows 下 CUDA / cuDNN / PyTorch GPU 训练环境搭建完整指南:虚拟环境与系统级环境两种方案

Windows 下 CUDA / cuDNN / PyTorch GPU 训练环境搭建完整指南:虚拟环境与系统级环境两种方案

面向读者:刚接触深度学习环境配置的新手、正在从云端迁移到本地训练的开发者、需要维护 Windows 训练机的中级开发者。 实践场景:Windows 10/11 + NVIDIA 显卡 + Python + PyTorch / YOLO 系列目标检测训练。 本文示例版本:Python 3.11、PyTorch 2.8.0 + CUDA 12.8 wheel、NVIDIA RTX 50 系显卡。实际项目中请优先参考 PyTorch 和 NVIDIA 官方页面的最新版本。

在这里插入图片描述

1. 为什么 Windows GPU 环境容易让人困惑

很多同学第一次在 Windows 上配置深度学习 GPU 环境时,最常遇到几个问题:

  • 明明装了显卡驱动,torch.cuda.is_available() 却是 False
  • nvidia-smi 显示 CUDA 13.x,但 PyTorch 安装命令却写 CUDA 12.8
  • 网上教程要求手动安装 CUDA Toolkit、cuDNN、复制 DLL,但 PyTorch 官网只让执行一条 pip install
  • 系统里有多个 Python,虚拟环境明明激活了,结果运行的还是另一个 Python
  • Windows 下训练启动时报 WinError 2、DataLoader worker、fbgemm.dll 等错误

根本原因是:NVIDIA 驱动、CUDA Toolkit、cuDNN、PyTorch wheel、Python 环境是不同层次的东西。它们有关联,但不是同一个东西。


2. 先理解四个核心概念

2.1 NVIDIA Driver:显卡驱动

显卡驱动是 GPU 能被系统和程序调用的基础。没有驱动,后面的 CUDA、PyTorch 都谈不上。

检查命令:

nvidia-smi

如果能看到显卡型号、驱动版本、显存占用,说明驱动大体正常。

注意:nvidia-smi 里显示的 CUDA Version 表示当前驱动支持的 CUDA Driver API 上限,不等于你已经安装了对应版本的 CUDA Toolkit。

例如:

Driver Version: 591.55
CUDA Version: 13.1

这只能说明驱动最高支持 CUDA 13.1 一类的运行能力,不代表你的电脑已经安装了 nvcc,也不代表 PyTorch 必须安装 CUDA 13.1 版本。

2.2 CUDA Toolkit:CUDA 开发工具包

CUDA Toolkit 是 NVIDIA 提供的开发工具包,里面包含:

  • nvcc 编译器
  • CUDA 头文件
  • 静态/动态库
  • 示例程序
  • Nsight 等开发工具

检查命令:

nvcc version

如果提示找不到 nvcc,说明没有安装 CUDA Toolkit,或者没有加入 PATH。

对于大多数 PyTorch 训练任务来说,不一定需要系统级 CUDA Toolkit。只有以下场景通常才需要:

  • 编译自定义 CUDA/C++ 扩展
  • 使用 nvcc
  • 编译依赖 CUDA Toolkit 的第三方库
  • 做 CUDA C/C++ 开发

2.3 cuDNN:深度神经网络加速库

cuDNN 是 NVIDIA 提供的深度学习加速库,常用于卷积、归一化、RNN 等深度学习算子。

过去很多教程会要求手动下载 cuDNN,然后把 DLL 复制到 CUDA Toolkit 目录。但对现代 PyTorch pip wheel 来说,通常不需要手工复制 cuDNN,因为 PyTorch 的 CUDA wheel 会携带匹配的运行时库。

在 PyTorch 中检查:

python c "import torch; print(torch.backends.cudnn.version())"

如果输出一个数字,例如:

91002

说明 PyTorch 能访问到 cuDNN。

2.4 PyTorch CUDA wheel:最常用、最省心的训练运行环境

PyTorch 官方提供带 CUDA 运行库的 pip wheel。以 CUDA 12.8 wheel 为例:

pip install torch torchvision index-url https://download.pytorch.org/whl/cu128

这类 wheel 通常已经包含训练所需的 CUDA Runtime、cuDNN、cuBLAS 等运行库。只要显卡驱动足够新,PyTorch 就可以调用 GPU。

所以很多时候,正确思路不是:

先装 CUDA Toolkit -> 再装 cuDNN -> 再装 PyTorch

而是:

先装 NVIDIA 驱动 -> 再安装匹配的 PyTorch CUDA wheel -> 验证 torch.cuda


3. 总体架构图

#mermaid-svg-r58LdvVuCKqjwgVq{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-r58LdvVuCKqjwgVq .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-r58LdvVuCKqjwgVq .error-icon{fill:#552222;}#mermaid-svg-r58LdvVuCKqjwgVq .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-r58LdvVuCKqjwgVq .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-r58LdvVuCKqjwgVq .marker{fill:#333333;stroke:#333333;}#mermaid-svg-r58LdvVuCKqjwgVq .marker.cross{stroke:#333333;}#mermaid-svg-r58LdvVuCKqjwgVq svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-r58LdvVuCKqjwgVq p{margin:0;}#mermaid-svg-r58LdvVuCKqjwgVq .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-r58LdvVuCKqjwgVq .cluster-label text{fill:#333;}#mermaid-svg-r58LdvVuCKqjwgVq .cluster-label span{color:#333;}#mermaid-svg-r58LdvVuCKqjwgVq .cluster-label span p{background-color:transparent;}#mermaid-svg-r58LdvVuCKqjwgVq .label text,#mermaid-svg-r58LdvVuCKqjwgVq span{fill:#333;color:#333;}#mermaid-svg-r58LdvVuCKqjwgVq .node rect,#mermaid-svg-r58LdvVuCKqjwgVq .node circle,#mermaid-svg-r58LdvVuCKqjwgVq .node ellipse,#mermaid-svg-r58LdvVuCKqjwgVq .node polygon,#mermaid-svg-r58LdvVuCKqjwgVq .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-r58LdvVuCKqjwgVq .rough-node .label text,#mermaid-svg-r58LdvVuCKqjwgVq .node .label text,#mermaid-svg-r58LdvVuCKqjwgVq .image-shape .label,#mermaid-svg-r58LdvVuCKqjwgVq .icon-shape .label{text-anchor:middle;}#mermaid-svg-r58LdvVuCKqjwgVq .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-r58LdvVuCKqjwgVq .rough-node .label,#mermaid-svg-r58LdvVuCKqjwgVq .node .label,#mermaid-svg-r58LdvVuCKqjwgVq .image-shape .label,#mermaid-svg-r58LdvVuCKqjwgVq .icon-shape .label{text-align:center;}#mermaid-svg-r58LdvVuCKqjwgVq .node.clickable{cursor:pointer;}#mermaid-svg-r58LdvVuCKqjwgVq .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-r58LdvVuCKqjwgVq .arrowheadPath{fill:#333333;}#mermaid-svg-r58LdvVuCKqjwgVq .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-r58LdvVuCKqjwgVq .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-r58LdvVuCKqjwgVq .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-r58LdvVuCKqjwgVq .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-r58LdvVuCKqjwgVq .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-r58LdvVuCKqjwgVq .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-r58LdvVuCKqjwgVq .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-r58LdvVuCKqjwgVq .cluster text{fill:#333;}#mermaid-svg-r58LdvVuCKqjwgVq .cluster span{color:#333;}#mermaid-svg-r58LdvVuCKqjwgVq div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-r58LdvVuCKqjwgVq .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-r58LdvVuCKqjwgVq rect.text{fill:none;stroke-width:0;}#mermaid-svg-r58LdvVuCKqjwgVq .icon-shape,#mermaid-svg-r58LdvVuCKqjwgVq .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-r58LdvVuCKqjwgVq .icon-shape p,#mermaid-svg-r58LdvVuCKqjwgVq .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-r58LdvVuCKqjwgVq .icon-shape .label rect,#mermaid-svg-r58LdvVuCKqjwgVq .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-r58LdvVuCKqjwgVq .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-r58LdvVuCKqjwgVq .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-r58LdvVuCKqjwgVq :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

可选:需要 nvcc 时

开发/编译 CUDA 扩展

Windows 系统

NVIDIA Driver 显卡驱动

nvidia-smi 可识别 GPU

Python 环境

虚拟环境 .venv

系统级 Python

PyTorch CUDA wheel

内置 CUDA Runtime / cuDNN / cuBLAS

torch.cuda.is_available() == True

YOLO / PyTorch GPU 训练

CUDA Toolkit


4. 两种安装方案怎么选

Windows 下建议优先考虑两种方案:

方案安装位置适合人群优点风险
虚拟环境方案 项目目录 .venv 大多数开发者、训练项目、多人协作 隔离、安全、容易删除重来 每个项目可能要单独建环境
系统级方案 全局 Python 3.11 单机固定训练环境、只维护一个项目 命令短,任何位置都能调用 会污染全局包,回滚麻烦

本文建议:

  • 新手和项目开发:优先虚拟环境
  • 固定训练机、明确知道风险:可以系统级安装
  • 需要 nvcc 或编译自定义 CUDA 算子:再安装 CUDA Toolkit

5. 方案选择流程图

#mermaid-svg-wZ4x3MQZRiYMI7UP{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-wZ4x3MQZRiYMI7UP .error-icon{fill:#552222;}#mermaid-svg-wZ4x3MQZRiYMI7UP .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-wZ4x3MQZRiYMI7UP .marker{fill:#333333;stroke:#333333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .marker.cross{stroke:#333333;}#mermaid-svg-wZ4x3MQZRiYMI7UP svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-wZ4x3MQZRiYMI7UP p{margin:0;}#mermaid-svg-wZ4x3MQZRiYMI7UP .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .cluster-label text{fill:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .cluster-label span{color:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .cluster-label span p{background-color:transparent;}#mermaid-svg-wZ4x3MQZRiYMI7UP .label text,#mermaid-svg-wZ4x3MQZRiYMI7UP span{fill:#333;color:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .node rect,#mermaid-svg-wZ4x3MQZRiYMI7UP .node circle,#mermaid-svg-wZ4x3MQZRiYMI7UP .node ellipse,#mermaid-svg-wZ4x3MQZRiYMI7UP .node polygon,#mermaid-svg-wZ4x3MQZRiYMI7UP .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .rough-node .label text,#mermaid-svg-wZ4x3MQZRiYMI7UP .node .label text,#mermaid-svg-wZ4x3MQZRiYMI7UP .image-shape .label,#mermaid-svg-wZ4x3MQZRiYMI7UP .icon-shape .label{text-anchor:middle;}#mermaid-svg-wZ4x3MQZRiYMI7UP .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .rough-node .label,#mermaid-svg-wZ4x3MQZRiYMI7UP .node .label,#mermaid-svg-wZ4x3MQZRiYMI7UP .image-shape .label,#mermaid-svg-wZ4x3MQZRiYMI7UP .icon-shape .label{text-align:center;}#mermaid-svg-wZ4x3MQZRiYMI7UP .node.clickable{cursor:pointer;}#mermaid-svg-wZ4x3MQZRiYMI7UP .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .arrowheadPath{fill:#333333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wZ4x3MQZRiYMI7UP .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-wZ4x3MQZRiYMI7UP .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wZ4x3MQZRiYMI7UP .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-wZ4x3MQZRiYMI7UP .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .cluster text{fill:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP .cluster span{color:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-wZ4x3MQZRiYMI7UP .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-wZ4x3MQZRiYMI7UP rect.text{fill:none;stroke-width:0;}#mermaid-svg-wZ4x3MQZRiYMI7UP .icon-shape,#mermaid-svg-wZ4x3MQZRiYMI7UP .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-wZ4x3MQZRiYMI7UP .icon-shape p,#mermaid-svg-wZ4x3MQZRiYMI7UP .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-wZ4x3MQZRiYMI7UP .icon-shape .label rect,#mermaid-svg-wZ4x3MQZRiYMI7UP .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-wZ4x3MQZRiYMI7UP .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-wZ4x3MQZRiYMI7UP .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-wZ4x3MQZRiYMI7UP :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

否,大多数 PyTorch/YOLO 训练

是,推荐

否,只维护一套全局环境

准备在 Windows 上训练深度学习模型

是否已经安装 NVIDIA 驱动?

先安装/更新 NVIDIA Driver

运行 nvidia-smi 验证

是否需要 nvcc / 编译 CUDA 扩展?

是否希望隔离项目依赖?

安装 CUDA Toolkit,可选安装系统级 cuDNN

使用虚拟环境方案

使用系统级 Python 方案

安装 PyTorch cu128 wheel

验证 torch.cuda / cuDNN / YOLO 导入

开始训练


6. 安装前检查清单

6.1 检查 Windows 和显卡

Get-ComputerInfo | Select-Object WindowsProductName, WindowsVersion, OsBuildNumber, OsArchitecture
Get-CimInstance Win32_VideoController | Select-Object Name, DriverVersion, AdapterRAM
nvidia-smi

重点看:

  • 是否能看到 NVIDIA 显卡
  • nvidia-smi 是否能正常运行
  • 显卡驱动是否较新
  • 显存大小是否符合训练需求

6.2 检查 Python

建议使用 Python 3.11:

where python
where python311
python version
python311 version

如果系统里有 Python 3.8、3.10、3.11 多个版本,一定要确认当前命令到底指向哪个解释器。

6.3 检查是否已经有 CUDA Toolkit

nvcc version

找不到 nvcc 并不代表不能训练 PyTorch 模型。它只说明系统级 CUDA Toolkit 没有安装或没有加入 PATH。


7. 虚拟环境方案:推荐方式

虚拟环境方案的核心目标是:不动系统 Python,不污染全局环境,所有依赖都装在项目目录里。

7.1 适合场景

  • 你有多个项目,依赖版本不同
  • 你担心全局 Python 被装乱
  • 你希望出问题后能直接删除环境重来
  • 你要把项目复制到另一台电脑复现

7.2 流程图

#mermaid-svg-rIoKXmzUAX3TB4C7{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-rIoKXmzUAX3TB4C7 .error-icon{fill:#552222;}#mermaid-svg-rIoKXmzUAX3TB4C7 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-rIoKXmzUAX3TB4C7 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .marker.cross{stroke:#333333;}#mermaid-svg-rIoKXmzUAX3TB4C7 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-rIoKXmzUAX3TB4C7 p{margin:0;}#mermaid-svg-rIoKXmzUAX3TB4C7 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .cluster-label text{fill:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .cluster-label span{color:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .cluster-label span p{background-color:transparent;}#mermaid-svg-rIoKXmzUAX3TB4C7 .label text,#mermaid-svg-rIoKXmzUAX3TB4C7 span{fill:#333;color:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .node rect,#mermaid-svg-rIoKXmzUAX3TB4C7 .node circle,#mermaid-svg-rIoKXmzUAX3TB4C7 .node ellipse,#mermaid-svg-rIoKXmzUAX3TB4C7 .node polygon,#mermaid-svg-rIoKXmzUAX3TB4C7 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .rough-node .label text,#mermaid-svg-rIoKXmzUAX3TB4C7 .node .label text,#mermaid-svg-rIoKXmzUAX3TB4C7 .image-shape .label,#mermaid-svg-rIoKXmzUAX3TB4C7 .icon-shape .label{text-anchor:middle;}#mermaid-svg-rIoKXmzUAX3TB4C7 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .rough-node .label,#mermaid-svg-rIoKXmzUAX3TB4C7 .node .label,#mermaid-svg-rIoKXmzUAX3TB4C7 .image-shape .label,#mermaid-svg-rIoKXmzUAX3TB4C7 .icon-shape .label{text-align:center;}#mermaid-svg-rIoKXmzUAX3TB4C7 .node.clickable{cursor:pointer;}#mermaid-svg-rIoKXmzUAX3TB4C7 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .arrowheadPath{fill:#333333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rIoKXmzUAX3TB4C7 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-rIoKXmzUAX3TB4C7 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rIoKXmzUAX3TB4C7 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-rIoKXmzUAX3TB4C7 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .cluster text{fill:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 .cluster span{color:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-rIoKXmzUAX3TB4C7 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-rIoKXmzUAX3TB4C7 rect.text{fill:none;stroke-width:0;}#mermaid-svg-rIoKXmzUAX3TB4C7 .icon-shape,#mermaid-svg-rIoKXmzUAX3TB4C7 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-rIoKXmzUAX3TB4C7 .icon-shape p,#mermaid-svg-rIoKXmzUAX3TB4C7 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-rIoKXmzUAX3TB4C7 .icon-shape .label rect,#mermaid-svg-rIoKXmzUAX3TB4C7 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-rIoKXmzUAX3TB4C7 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-rIoKXmzUAX3TB4C7 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-rIoKXmzUAX3TB4C7 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

进入项目目录

创建 .venv-yolo26-cu128

升级 pip / setuptools / wheel

安装 torch 2.8.0+cu128 / torchvision 0.23.0+cu128

安装项目依赖 requirements-yolo26-cu128.txt

验证 torch.cuda

验证 cuDNN

验证 YOLO 导入/推理

训练 train.py

7.3 手动安装步骤

这里我们以本地化开发训练YOLOv26目标检测模型为切入点进行环境的构建,进入项目目录:

cd D:\\self\\v26

创建虚拟环境:

python311 m venv .venv-yolo26-cu128

激活虚拟环境:

.\\.venv-yolo26-cu128\\Scripts\\Activate.ps1

如果 PowerShell 不允许执行脚本,可以临时使用:

powershell.exe ExecutionPolicy Bypass

或者不激活,直接使用完整路径:

.\\.venv-yolo26-cu128\\Scripts\\python.exe version

升级基础工具:

python m pip install upgrade pip setuptools wheel

安装 CUDA 版 PyTorch:

python m pip install torch==2.8.0 torchvision==0.23.0 index-url https://download.pytorch.org/whl/cu128

安装项目依赖:

python m pip install r requirements-yolo26-cu128.txt i https://pypi.tuna.tsinghua.edu.cn/simple trusted-host pypi.tuna.tsinghua.edu.cn

如果你使用官方 PyPI:

python m pip install r requirements-yolo26-cu128.txt

7.4 一键安装脚本

项目中已经提供虚拟环境一键安装脚本:

cd D:\\self\\v26
.\\install_yolo26_windows.bat

强制重建虚拟环境:

.\\install_yolo26_windows.bat ForceRecreate

指定 Python:

.\\install_yolo26_windows.bat PythonCommand D:\\Python311\\python311.exe

7.5 验证

where python
python c "import sys, torch; print(sys.executable); print(torch.__version__, torch.version.cuda, torch.cuda.is_available(), torch.cuda.get_device_name(0))"
python c "import torch; print(torch.backends.cudnn.version())"

期望看到类似:

D:\\self\\v26\\.venv-yolo26-cu128\\Scripts\\python.exe
2.8.0+cu128 12.8 True NVIDIA GeForce RTX 5060
91002

7.6 启动训练

python train.py

Windows 下建议在训练参数中显式指定:

workers=0

原因是 Windows 的多进程启动机制和 Linux 不一样,PyTorch DataLoader 在多 worker 时可能出现 WinError 2、子进程启动失败等问题。


8. 系统级方案:直接安装到全局 Python

系统级方案的核心目标是:让全局 Python 3.11 直接具备 GPU 训练能力。

8.1 适合场景

  • 这台电脑就是固定训练机
  • 你希望任何目录都能直接执行 python311
  • 你能接受全局包版本被改变

不适合:

  • 多项目、多版本依赖
  • 想要轻松回滚
  • 不确定全局 Python 里已有项目是否依赖旧版本包

8.2 系统级方案流程图

#mermaid-svg-YnjuamNZtLqptvqb{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-YnjuamNZtLqptvqb .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-YnjuamNZtLqptvqb .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-YnjuamNZtLqptvqb .error-icon{fill:#552222;}#mermaid-svg-YnjuamNZtLqptvqb .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-YnjuamNZtLqptvqb .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-YnjuamNZtLqptvqb .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-YnjuamNZtLqptvqb .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-YnjuamNZtLqptvqb .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-YnjuamNZtLqptvqb .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-YnjuamNZtLqptvqb .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-YnjuamNZtLqptvqb .marker{fill:#333333;stroke:#333333;}#mermaid-svg-YnjuamNZtLqptvqb .marker.cross{stroke:#333333;}#mermaid-svg-YnjuamNZtLqptvqb svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-YnjuamNZtLqptvqb p{margin:0;}#mermaid-svg-YnjuamNZtLqptvqb .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-YnjuamNZtLqptvqb .cluster-label text{fill:#333;}#mermaid-svg-YnjuamNZtLqptvqb .cluster-label span{color:#333;}#mermaid-svg-YnjuamNZtLqptvqb .cluster-label span p{background-color:transparent;}#mermaid-svg-YnjuamNZtLqptvqb .label text,#mermaid-svg-YnjuamNZtLqptvqb span{fill:#333;color:#333;}#mermaid-svg-YnjuamNZtLqptvqb .node rect,#mermaid-svg-YnjuamNZtLqptvqb .node circle,#mermaid-svg-YnjuamNZtLqptvqb .node ellipse,#mermaid-svg-YnjuamNZtLqptvqb .node polygon,#mermaid-svg-YnjuamNZtLqptvqb .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-YnjuamNZtLqptvqb .rough-node .label text,#mermaid-svg-YnjuamNZtLqptvqb .node .label text,#mermaid-svg-YnjuamNZtLqptvqb .image-shape .label,#mermaid-svg-YnjuamNZtLqptvqb .icon-shape .label{text-anchor:middle;}#mermaid-svg-YnjuamNZtLqptvqb .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-YnjuamNZtLqptvqb .rough-node .label,#mermaid-svg-YnjuamNZtLqptvqb .node .label,#mermaid-svg-YnjuamNZtLqptvqb .image-shape .label,#mermaid-svg-YnjuamNZtLqptvqb .icon-shape .label{text-align:center;}#mermaid-svg-YnjuamNZtLqptvqb .node.clickable{cursor:pointer;}#mermaid-svg-YnjuamNZtLqptvqb .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-YnjuamNZtLqptvqb .arrowheadPath{fill:#333333;}#mermaid-svg-YnjuamNZtLqptvqb .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-YnjuamNZtLqptvqb .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-YnjuamNZtLqptvqb .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YnjuamNZtLqptvqb .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-YnjuamNZtLqptvqb .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YnjuamNZtLqptvqb .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-YnjuamNZtLqptvqb .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-YnjuamNZtLqptvqb .cluster text{fill:#333;}#mermaid-svg-YnjuamNZtLqptvqb .cluster span{color:#333;}#mermaid-svg-YnjuamNZtLqptvqb div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-YnjuamNZtLqptvqb .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-YnjuamNZtLqptvqb rect.text{fill:none;stroke-width:0;}#mermaid-svg-YnjuamNZtLqptvqb .icon-shape,#mermaid-svg-YnjuamNZtLqptvqb .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-YnjuamNZtLqptvqb .icon-shape p,#mermaid-svg-YnjuamNZtLqptvqb .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-YnjuamNZtLqptvqb .icon-shape .label rect,#mermaid-svg-YnjuamNZtLqptvqb .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-YnjuamNZtLqptvqb .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-YnjuamNZtLqptvqb .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-YnjuamNZtLqptvqb :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

不需要,普通 PyTorch 训练

需要 nvcc/编译扩展

确认 nvidia-smi 正常

确认全局 Python 3.11

备份 pip freeze

是否需要 CUDA Toolkit?

跳过 Toolkit

winget 安装 Nvidia.CUDA

全局安装 PyTorch cu128 wheel

全局安装项目依赖

pip check

验证 torch.cuda / cuDNN

python311 train.py

8.3 手动安装步骤

确认 Python:

python311 version
python311 m pip version

备份当前全局依赖:

python311 m pip freeze > global_python311_freeze_before.txt

升级基础工具:

python311 m pip install upgrade pip setuptools wheel

安装 CUDA 版 PyTorch:

python311 m pip install torch==2.8.0 torchvision==0.23.0 index-url https://download.pytorch.org/whl/cu128

安装项目依赖:

python311 m pip install r requirements-yolo26-cu128.txt i https://pypi.tuna.tsinghua.edu.cn/simple trusted-host pypi.tuna.tsinghua.edu.cn

验证:

python311 c "import torch; print(torch.__version__, torch.version.cuda, torch.cuda.is_available(), torch.cuda.get_device_name(0))"
python311 c "from ultralytics import YOLO; print('ok')"

训练:

python311 train.py

8.4 系统级一键脚本

项目中已经提供系统级一键安装脚本:

cd D:\\self\\v26
.\\install_yolo26_windows_system.bat

先演练,不实际安装:

.\\install_yolo26_windows_system.bat DryRun

指定 Python:

.\\install_yolo26_windows_system.bat PythonCommand D:\\Python311\\python311.exe

同时安装 CUDA Toolkit:

.\\install_yolo26_windows_system.bat InstallCudaToolkit

脚本会保存安装前后的 pip freeze 快照,方便排查。


9. 是否要安装系统级 CUDA Toolkit 和 cuDNN

9.1 普通 PyTorch / YOLO 训练

通常不需要。

推荐组合:

NVIDIA Driver + PyTorch CUDA wheel

优点:

  • 安装简单
  • 版本匹配由 PyTorch 官方 wheel 处理
  • 不需要手动复制 cuDNN DLL
  • 不容易把系统 PATH 搞乱

9.2 需要编译 CUDA 扩展

如果你需要 nvcc,就需要 CUDA Toolkit:

winget install id Nvidia.CUDA exact source winget accept-source-agreements accept-package-agreements

安装后重新打开终端,验证:

nvcc version

9.3 手动安装 cuDNN

如果你不是使用 PyTorch wheel 自带的 cuDNN,而是做 TensorFlow、C++ 推理、CUDA 原生开发等,可能需要单独安装 cuDNN。此时应严格参考 NVIDIA 官方 cuDNN 文档。

常见方式包括:

  • zip/tarball 包安装
  • wheel 安装
  • conda 安装

不建议随意从旧教程下载 DLL 后到处复制。版本不匹配很容易导致运行时 DLL 加载失败。


10. 版本关系:不要被数字吓到

10.1 nvidia-smi 的 CUDA 版本

nvidia-smi 中的 CUDA 版本是驱动支持能力,不是 PyTorch 实际使用的 CUDA Runtime 版本。

10.2 PyTorch 的 CUDA 版本

PyTorch 中查看:

python c "import torch; print(torch.version.cuda)"

如果输出:

12.8

表示当前 PyTorch wheel 是基于 CUDA 12.8 构建的。

10.3 CUDA Toolkit 的版本

CUDA Toolkit 中查看:

nvcc version

这个版本只在你需要编译 CUDA 代码时重要。普通 PyTorch 训练不要求它和 torch.version.cuda 完全一致。


11. 常见错误与排查

11.1 torch.cuda.is_available(): False

可能原因:

  • 装的是 CPU 版 PyTorch
  • 当前 Python 不是你以为的那个环境
  • NVIDIA 驱动未安装或太旧
  • 显卡不支持当前 PyTorch wheel

排查:

where python
python c "import sys, torch; print(sys.executable); print(torch.__version__, torch.version.cuda, torch.cuda.is_available())"
nvidia-smi

如果看到:

torch 1.x.x+cpu

说明装的是 CPU 版,需要重新安装 CUDA wheel。

11.2 虚拟环境激活后仍然运行全局 Python

检查:

where python

第一条应该是:

项目目录\\.venv-yolo26-cu128\\Scripts\\python.exe

如果不是,说明虚拟环境没有正确激活,或者虚拟环境里缺少 python.exe 入口。

可以补齐:

Copy-Item .\\.venv-yolo26-cu128\\Scripts\\python311.exe .\\.venv-yolo26-cu128\\Scripts\\python.exe Force

11.3 fbgemm.dll 或 DLL 加载失败

可能原因:

  • PyTorch 安装不完整
  • wheel 与 Python 版本不匹配
  • 缺少运行库
  • 环境中混入了旧版本包

建议:

python m pip uninstall torch torchvision torchaudio y
python m pip install torch==2.8.0 torchvision==0.23.0 index-url https://download.pytorch.org/whl/cu128

虚拟环境中出问题时,最省事的方式通常是删除 .venv 后重建。

11.4 Windows DataLoader 报 WinError 2

错误类似:

FileNotFoundError: [WinError 2] 系统找不到指定的文件

通常是 Windows 下 PyTorch DataLoader 多进程 worker 启动失败。

解决:

model.train(..., workers=0)

训练速度可能略有影响,但稳定性更好。

11.5 CUDA out of memory

显存不足。

解决顺序:

  • 减小 batch
  • 减小 imgsz
  • 换更小模型,例如从 x/l 换到 m/s/n
  • 关闭其他占用显存的程序
  • 示例:

    model.train(data="self.yaml", batch=4, imgsz=640, device=0, workers=0)

    11.6 标签类别越界

    错误类似:

    Label class 10 exceeds dataset class count 10

    含义:数据集中出现了类别编号 10,但配置 nc: 10 只允许 0-9。

    解决:

    • 如果确实有 11 类,改成 nc: 11,并提供 11 个类别名
    • 如果原始标注是 1-10,需要整体平移成 0-9
    • 不要在没确认类别语义前盲目批量替换

    12. 训练脚本建议写法

    Windows 下建议把通用训练参数集中起来:

    from ultralytics import YOLO

    if __name__ == "__main__":
    save_dir = "runs/detect/"
    device = [0]
    epochs = 100

    train_args = dict(
    data="self.yaml",
    save_dir=save_dir,
    epochs=epochs,
    device=device,
    workers=0,
    )

    model = YOLO("weights/yolo26n.pt")
    model.train(batch=8, name="yolo26n", **train_args)

    这样以后切换模型时,不容易漏掉 workers=0、device=[0] 等关键参数。


    13. 验证命令合集

    13.1 驱动

    nvidia-smi

    13.2 Python 和 PyTorch

    where python
    python c "import sys; print(sys.executable)"
    python c "import torch; print(torch.__version__, torch.version.cuda, torch.cuda.is_available())"
    python c "import torch; print(torch.cuda.get_device_name(0))"
    python c "import torch; print(torch.backends.cudnn.version())"

    13.3 实际 GPU 计算

    python c "import torch; x=torch.randn(2048,2048,device='cuda'); y=x@x; torch.cuda.synchronize(); print(y.shape, y.device)"

    13.4 YOLO 导入

    python c "from ultralytics import YOLO; print('ultralytics ok')"

    13.5 YOLO 推理

    python c "from ultralytics import YOLO; model=YOLO('weights/yolo26n.pt'); r=model.predict(source='ultralytics/assets/bus.jpg', device=0, imgsz=640, save=False); print(len(r))"


    14. 回滚方案

    14.1 虚拟环境回滚

    虚拟环境最简单:

    Remove-Item .\\.venv-yolo26-cu128 Recurse Force

    然后重新运行一键脚本即可。

    14.2 系统级 Python 回滚

    系统级环境没有那么干净。建议安装前保存:

    python311 m pip freeze > global_python311_freeze_before.txt

    如果后续要排查,可以对比安装前后依赖差异。

    严重混乱时,最彻底的方式是:

    • 卸载/重装 Python 3.11
    • 或者新建虚拟环境,不再依赖全局 Python

    14.3 CUDA Toolkit 回滚

    如果通过 winget 安装:

    winget uninstall id Nvidia.CUDA

    卸载后重新打开终端,再检查:

    nvcc version


    15. 推荐实践总结

    对大多数 Windows 深度学习训练项目,推荐路径是:

    安装 NVIDIA Driver
    -> nvidia-smi 验证
    -> 创建项目虚拟环境
    -> 安装 PyTorch CUDA wheel
    -> 安装项目依赖
    -> 验证 torch.cuda / cuDNN
    -> 训练

    只有在确实需要 CUDA 编译能力时,才额外安装 CUDA Toolkit。

    一句话总结:

    PyTorch/YOLO 训练需要的是“能运行 CUDA 的 PyTorch 环境”,不一定需要你手工安装系统级 CUDA Toolkit 和 cuDNN。虚拟环境优先,系统级安装谨慎使用。


    16. 官方参考资料

    • PyTorch 官方安装页面:https://pytorch.org/get-started/locally/
    • PyTorch 历史版本安装命令:https://pytorch.org/get-started/previous-versions/
    • NVIDIA CUDA Windows 安装指南:https://docs.nvidia.com/cuda/cuda-installation-guide-microsoft-windows/
    • NVIDIA cuDNN 安装指南:https://docs.nvidia.com/deeplearning/cudnn/installation/latest/
    • NVIDIA cuDNN Developer 页面:https://developer.nvidia.com/cudnn
    赞(0)
    未经允许不得转载:171主机测评 » Windows 下 CUDA / cuDNN / PyTorch GPU 训练环境搭建完整指南:虚拟环境与系统级环境两种方案
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址