安装

需求

  • Linux 或 macOS 系统,Python 版本 ≥ 3.7

  • PyTorch ≥ 1.8 以及与之匹配的 torchvision。 请前往 pytorch.org 同时安装它们以确保版本兼容

  • OpenCV是可选的,但demo和可视化需要它

从源码构建Detectron2

需要安装gcc和g++版本≥5.4。ninja是可选的,但建议安装以加快构建速度。 安装完成后,运行:

python -m pip install 'git+https://github.com/facebookresearch/detectron2.git'
# (add --user if you don't have permission)

# Or, to install it from a local clone:
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2

# On macOS, you may need to prepend the above commands with a few environment variables:
CC=clang CXX=clang++ ARCHFLAGS="-arch x86_64" python -m pip install ...

重新构建从本地克隆的detectron2,请先使用rm -rf build/ **/*.so清理旧构建。在重新安装PyTorch后,通常需要重新构建detectron2。

安装预构建的Detectron2 (仅限Linux系统)

从下表中选择安装 v0.6 (2021年10月)

CUDA torch 1.10torch 1.9torch 1.8
11.3
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu113/torch1.10/index.html
11.1
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.10/index.html
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.9/index.html
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.8/index.html
10.2
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.10/index.html
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.8/index.html
10.1
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.8/index.html
cpu
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.10/index.html
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.9/index.html
install
python -m pip install detectron2 -f \
  https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.8/index.html

请注意:

  1. 预构建的软件包必须与对应版本的CUDA及PyTorch官方包配合使用。否则,请从源代码构建detectron2。

  2. 新包每隔几个月发布一次。因此,这些包可能不包含主分支中的最新功能,也可能与使用detectron2的研究项目的主分支不兼容(例如projects中的项目)。

常见安装问题

点击每个问题查看其解决方案:

Undefined symbols that looks like "TH..","at::Tensor...","torch..."

这通常发生在detectron2或torchvision没有使用您当前运行的PyTorch版本进行编译时。

如果错误来自预构建的torchvision,请卸载torchvision和pytorch,然后按照pytorch.org重新安装。这样版本就能匹配。

如果错误来自预构建的detectron2,请查阅发布说明,卸载并重新安装与pytorch版本匹配的正确预构建detectron2。

如果错误来自您手动从源码构建的detectron2或torchvision, 请删除您构建的文件(build/, **/*.so)并重新构建,以便它能使用您当前环境中的pytorch版本。

如果上述说明未能解决此问题,请提供一个可重现该问题的环境(例如dockerfile)。

Missing torch dynamic libraries, OR segmentation fault immediately when using detectron2. This usually happens when detectron2 or torchvision is not compiled with the version of PyTorch you're running. See the previous common issue for the solution.
Undefined C++ symbols (e.g. "GLIBCXX..") or C++ symbols not found.
Usually it's because the library is compiled with a newer C++ compiler but run with an old C++ runtime.

这种情况常发生在旧版Anaconda中。 运行conda update libgcc升级运行时库可能会解决问题。

根本解决方案是避免版本不匹配,可以通过使用旧版C++编译器编译,或者使用正确的C++运行时运行代码。 要使用特定C++运行时运行代码,可以使用环境变量LD_PRELOAD=/path/to/libstdc++.so

"nvcc not found" or "Not compiled with GPU support" or "Detectron2 CUDA Compiler: not available".
CUDA is not found when building detectron2. You should make sure
python -c 'import torch; from torch.utils.cpp_extension import CUDA_HOME; print(torch.cuda.is_available(), CUDA_HOME)'

在构建detectron2时打印 (True, a directory with cuda)

大多数模型可以在没有GPU支持的情况下运行推理(但不支持训练)。要使用CPU,请在配置中设置MODEL.DEVICE='cpu'

"invalid device function" or "no kernel image is available for execution".
Two possibilities:
  • 您使用一个版本的CUDA构建detectron2,但运行时使用了不同版本。

    要检查是否是这种情况, 使用python -m detectron2.utils.collect_env来发现不一致的CUDA版本。 在这个命令的输出中,您应该期望"Detectron2 CUDA Compiler"、"CUDA_HOME"、"PyTorch built with - CUDA" 包含相同版本的cuda库。

    当它们不一致时, 您需要安装不同版本的PyTorch(或自行构建) 以匹配您本地的CUDA安装,或者安装不同版本的CUDA以匹配PyTorch。

  • PyTorch/torchvision/Detectron2 未针对正确的GPU SM架构(即计算能力)构建。

    PyTorch/detectron2/torchvision包含的架构可在python -m detectron2.utils.collect_env的"architecture flags"中查看。它必须包含您的GPU架构,该架构可在developer.nvidia.com/cuda-gpus找到。

    如果您使用的是预构建的PyTorch/detectron2/torchvision,它们已经包含了对大多数流行GPU的支持。如果不支持,您需要从源代码构建它们。

    当从源代码构建detectron2/torchvision时,它们会检测GPU设备并仅针对该设备构建。这意味着编译后的代码可能无法在其他GPU设备上运行。要为正确的架构重新编译它们,请删除所有已安装/编译的文件,并使用正确设置的TORCH_CUDA_ARCH_LIST环境变量重新构建。例如,export TORCH_CUDA_ARCH_LIST="6.0;7.0"会使其同时为P100和V100进行编译。

Undefined CUDA symbols; Cannot open libcudart.so
The version of NVCC you use to build detectron2 or torchvision does not match the version of CUDA you are running with. This often happens when using anaconda's CUDA runtime.

使用python -m detectron2.utils.collect_env来检查CUDA版本是否一致。 在该命令的输出中,您应该确认"Detectron2 CUDA Compiler"、"CUDA_HOME"、"PyTorch built with - CUDA" 这几个字段包含相同版本的CUDA库。

当它们不一致时,您需要安装不同版本的PyTorch(或自行构建)以匹配本地CUDA安装,或者安装不同版本的CUDA以匹配PyTorch。

C++ compilation errors from NVCC / NVRTC, or "Unsupported gpu architecture"
A few possibilities:
  1. 本地CUDA/NVCC版本必须与PyTorch的CUDA版本匹配。两者都可以在python collect_env.py中找到 (从这里下载)。 当版本不一致时,您需要安装不同版本的PyTorch(或自行构建)以匹配本地CUDA安装,或者安装不同版本的CUDA以匹配PyTorch。

  2. 本地CUDA/NVCC版本必须支持您GPU的SM架构(又称计算能力)。您GPU的计算能力可在developer.nvidia.com/cuda-gpus查询。NVCC支持的计算能力清单见here。如果您的NVCC版本过旧,可通过设置环境变量TORCH_CUDA_ARCH_LIST为较低且受支持的计算能力来解决此问题。

  3. 您使用的NVCC和GCC版本组合不兼容。需要更改其中一个的版本。 查看此处了解一些有效组合。 特别要注意,CUDA≤10.1.105不支持GCC>7.3。

    PyTorch使用的CUDA/GCC版本可通过print(torch.__config__.show())查看。

"ImportError: cannot import name '_C'".
Please build and install detectron2 following the instructions above.

或者,如果您正在从detectron2的根目录运行代码,请cd切换到其他目录。 否则可能无法导入已安装的代码。

Any issue on windows.

Detectron2在Windows上通过CircleCI持续构建,但我们不提供官方支持。欢迎提交能改进Windows代码兼容性的PR。

ONNX conversion segfault after some "TraceWarning".
The ONNX package is compiled with a too old compiler.

请使用与PyTorch所用版本更接近的编译器(可通过torch.__config__.show()查看)从源代码构建并安装ONNX。

"library not found for -lstdc++" on older version of MacOS

参见这个stackoverflow回答

在特定环境中的安装:

  • Colab: 查看我们的Colab教程,其中包含分步说明。

  • Docker: 官方提供的Dockerfile只需几条简单命令即可安装detectron2。