Google Colab导入Detectron2的build_model时提示libtorch_cuda_cu.so缺失
问题根因
报错由PyTorch、CUDA、Detectron2三者版本不匹配导致:
- 当前环境CUDA驱动最高支持CUDA 11.2版本,但已安装的PyTorch为基于CUDA 10.1编译的
1.8.1+cu101版本 - 安装Detectron2时未对齐上述依赖版本,导致动态库调用失败
修复方案(Google Colab环境可直接执行)
- 卸载现有冲突的PyTorch相关组件
!pip uninstall -y torch torchvision torchaudio torchtext
- 安装适配CUDA 11.2驱动的PyTorch版本
!pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 -f https://download.pytorch.org/whl/torch_stable.html
- 安装匹配当前PyTorch版本的Detectron2预编译包
!pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.9/index.html
- 验证导入
from detectron2.modeling import build_model
执行后无报错即为修复成功。
注意事项
- Colab实例重启后会重置环境,每次重新进入都需要重新执行上述安装步骤
- 安装Detectron2必须严格匹配其与PyTorch、CUDA的版本对应关系,任意组件版本不匹配都会触发动态库找不到的报错
内容的提问来源于stack exchange,提问作者KamKam
相关产品推荐
相关产品推荐

