Detectron2 安装问题解决方案
问题描述
安装 Detectron2 时遇到以下错误:
ImportError: libtorch_cuda_cu.so: cannot open shared object file: No such file or directory
这个错误通常由 CUDA 版本与 PyTorch 版本不匹配导致。本指南将帮助你正确配置环境。
环境检查
1. 检查 CUDA 版本
首先确认系统安装的 CUDA 版本:
# 方法1:检查 NVCC 版本
nvcc --version
# 方法2:检查 CUDA 版本
nvidia-smi
2. 检查 PyTorch 版本
检查当前环境中的 PyTorch 版本:
pip show torch
安装步骤
1. 创建虚拟环境(推荐)
# 创建新的虚拟环境
conda create -n detectron2 python=3.8
# 激活虚拟环境
conda activate detectron2
2. 安装 PyTorch
根据你的 CUDA 版本选择对应的 PyTorch 版本。以下以 CUDA 11.3 为例:
pip install torch==1.10.0+cu113 \
torchvision==0.11.0+cu113 \
torchaudio==0.10.0+cu113 \
-f https://download.pytorch.org/whl/cu113/torch_stable.html \
-i https://pypi.tuna.tsinghua.edu.cn/simple
3. 安装 Detectron2
确保 PyTorch 安装成功后,安装对应版本的 Detectron2:
python -m pip install detectron2 -f \
https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html
版本对应关系
CUDA 版本 | PyTorch 推荐版本 | Detectron2 对应版本 |
---|---|---|
CUDA 11.3 | 1.10 | 0.6 |
CUDA 11.1 | 1.10 | 0.6 |
CUDA 10.2 | 1.10 | 0.6 |
其他建议
如果使用 conda,可以考虑使用 conda 安装 CUDA 工具包