Windows下MMDetection从安装到跑通第一个目标检测Demo(含权重文件下载与路径配置避坑)
Windows下MMDetection从安装到跑通第一个目标检测Demo(含权重文件下载与路径配置避坑)
在Windows系统上搭建深度学习环境总是充满挑战,尤其是当官方文档默认用户使用Linux时。作为国内最流行的开源目标检测框架之一,MMDetection的强大功能吸引着无数开发者,但Windows用户在安装过程中往往会遇到各种"特色问题"——从路径斜杠方向到CUDA版本冲突,从权重文件下载缓慢到配置文件路径错误。本文将手把手带你避开这些坑,用最直接的方式在原生Windows环境(非WSL)中完成MMDetection的完整部署,并运行第一个目标检测Demo。
1. 环境准备:打造专属Python沙盒
1.1 Conda环境配置
Windows下的Python环境管理推荐使用Miniconda3(比Anaconda更轻量),最新版本可从清华镜像站下载。安装时务必勾选"Add to PATH"选项,这是后续在普通CMD中使用conda命令的关键。
# 创建专用于MMDetection的Python环境(指定3.8版本更稳定) conda create -n mmdet python=3.8 -y conda activate mmdet注意:如果后续步骤出现DLL加载错误,很可能是环境变量问题。可尝试在Anaconda Prompt中执行
conda init然后重新打开终端。
1.2 PyTorch与CUDA的版本舞蹈
MMDetection对PyTorch和CUDA版本有严格限制,以下是经过验证的组合方案:
| 组件 | 推荐版本 | 必须匹配项 |
|---|---|---|
| PyTorch | 2.1.0 | CUDA运行时版本 |
| torchvision | 0.16.0 | PyTorch主版本 |
| CUDA | 11.8 | 显卡驱动版本≥522.06 |
安装命令使用国内镜像加速:
pip install torch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 --index-url https://mirrors.aliyun.com/pytorch-wheels/cu118/验证安装:
import torch print(torch.__version__) # 应输出2.1.0 print(torch.cuda.is_available()) # 应输出True2. MMDetection生态链安装指南
2.1 核心组件精准安装
OpenMMLab系列包的版本必须严格对齐,以下是经过测试的黄金组合:
# 先安装包管理工具MIM pip install -U openmim # 按顺序安装核心组件(注意版本号!) mim install mmengine==0.10.3 mim install mmcv==2.1.0 -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.1/index.html mim install mmdet==3.3.0常见安装问题排查:
- 报错"Could not build wheels for mmcv":检查VS Build Tools是否安装(需要C++14支持)
- 报错"DLL load failed":大概率是PyTorch与MMCV版本不匹配,使用
pip list核对版本
2.2 源码获取与路径处理
从GitHub克隆MMDetection仓库时,Windows用户需特别注意:
# 推荐使用SSH方式克隆(需先配置Git SSH Key) git clone git@github.com:open-mmlab/mmdetection.git cd mmdetection重要:Windows路径建议全部使用原始字符串表示法,例如:
config_file = r'D:\mmdetection\configs\faster_rcnn\faster-rcnn_r50_fpn_1x_coco.py'
3. 预训练模型获取与配置技巧
3.1 权重文件高速下载
官方模型库(Model Zoo)的权重文件通常存放在海外服务器,国内用户可通过以下方式加速:
使用开源镜像站替换URL:
原始URL:https://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth 替换为:https://openmmlab.oss-cn-hangzhou.aliyuncs.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth使用迅雷等下载工具接管浏览器下载任务
3.2 模型文件目录规范
建议建立如下目录结构,避免路径混乱:
mmdetection/ ├── configs/ ├── checkpoints/ # 存放所有.pth权重文件 ├── demo/ │ ├── demo.jpg # 测试图片 │ └── result.png # 输出结果 └── outputs/ # 可视化结果保存目录4. 第一个目标检测Demo实战
4.1 测试脚本深度适配
以下是针对Windows系统优化过的完整测试脚本(保存为demo.py):
import mmcv from mmdet.apis import init_detector, inference_detector from mmdet.registry import VISUALIZERS # 使用原始字符串避免转义问题 config = r'configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py' checkpoint = r'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth' img_path = r'demo/demo.jpg' # 初始化模型(自动切换到GPU) model = init_detector(config, checkpoint, device='cuda:0') # 执行推理 result = inference_detector(model, img_path) # 可视化设置 visualizer = VISUALIZERS.build(model.cfg.visualizer) visualizer.dataset_meta = model.dataset_meta # 保存检测结果 image = mmcv.imread(img_path) visualizer.add_datasample( 'result', image, data_sample=result, draw_gt=False, show=False, # Windows下建议关闭直接显示 out_file='outputs/result.jpg' )4.2 典型报错解决方案
问题1:RuntimeError: Couldn't load custom C++ ops
- 原因:MMCV编译版本与PyTorch不匹配
- 解决:重新安装匹配版本的MMCV
mim uninstall mmcv mim install mmcv==2.1.0 -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.1/index.html
问题2:FileNotFoundError配置文件路径错误
- 原因:Windows路径分隔符问题
- 解决:使用
os.path模块处理路径import os config = os.path.join('configs', 'faster_rcnn', 'faster-rcnn_r50_fpn_1x_coco.py')
问题3:CUDA out of memory
- 修改配置文件的
test_dataloader部分:test_dataloader = dict( batch_size=1, # 改为1 ... )
5. 效率优化与进阶技巧
5.1 开启DNN加速
在configs/_base_/datasets/coco_detection.py中添加:
model = dict( ... dcn=dict(type='DCNv2', deform_groups=1, fallback_on_stride=False), stage_with_dcn=(False, True, True, True))5.2 多GPU训练配置
修改tools/train.py启动参数:
set CUDA_VISIBLE_DEVICES=0,1 # 使用前两块GPU python -m torch.distributed.launch --nproc_per_node=2 tools/train.py configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py5.3 模型转换与部署
将PyTorch模型转换为ONNX格式:
from mmdet.apis import export_model config_file = 'configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py' checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth' export_model(config_file, checkpoint_file, 'faster_rcnn.onnx')6. 可视化增强与结果分析
6.1 检测结果二次加工
在输出图像上添加自定义信息:
from PIL import Image, ImageDraw result_img = Image.open('outputs/result.jpg') draw = ImageDraw.Draw(result_img) # 在左上角添加模型信息 draw.text((10, 10), f"Model: Faster R-CNN\nConfidence: {result[0][0][4]:.2f}", fill=(255, 0, 0)) result_img.save('outputs/result_with_info.jpg')6.2 性能评估脚本
创建eval.py进行定量评估:
from mmdet.apis import init_detector, inference_detector import time config = 'configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py' checkpoint = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth' model = init_detector(config, checkpoint, device='cuda:0') # 预热GPU for _ in range(10): _ = inference_detector(model, 'demo/demo.jpg') # 正式测试 start = time.time() for _ in range(100): result = inference_detector(model, 'demo/demo.jpg') elapsed = time.time() - start print(f"Average inference time: {elapsed/100:.4f}s")