Detectron2官方文档翻译(一)

开始学习使用Detectron2框架,对官方文档翻译


安装

要求

  1. Linux 或者macOS 中python ≥ 3.6
  2. Pytorch ≥ 1.7 ,torchvision与pytorch版本匹配安装,在https://pytorch.org/上下载安装(国内可以使用镜像地址,在pytorch上面下载whl文件,手动安装)
  3. OpenCV可选,但是演示demo和数据可视化需要使用(就是直接装上)

(Pytorch镜像地址https://download.pytorch.org/whl/torch_stable.html)

从源生成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(linux系统删除文件的操作)清除以前建立的旧的项目。需要在重新安装了Pytorch之后需要rebuild detectron2项目。

在安装detectron2前需要安装的(只有Linux系统需要)

       根据自己的系统环境选择以下的安装命令,这里是安装detectron2 V0.5(detectron2的release版本)的要求

# CUDA11.1
# torch1.9 
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.9/index.html  
# torch1.8
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu111/torch1.8/index.html

# CUDA11.0
# torch1.7
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu110/torch1.7/index.html

# CUDA10.2
# torch1.9
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.9/index.html
# torch1.8
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.8/index.html
# torch1.7
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu102/torch1.7/index.html

# CUDA10.1
# torch1.8
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.8/index.html
# torch1.7
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu101/torch1.7/index.html

# CUDA9.2
# torch1.7
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cu92/torch1.7/index.html

# CPU
# torch1.9
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.9/index.html
# torch1.8
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.8/index.html
# torch1.7
python -m pip install detectron2 -f https://dl.fbaipublicfiles.com/detectron2/wheels/cpu/torch1.7/index.html

另外

  1. 安装构建CUDA与Pytorch必须要相匹配,就是Pytorch必须是官方发行的(国内镜像网站下载的也没问题,都是一样的),否则需要从源代码构建detectron2
  2. 每个几个月就会新发布一个released版本,因此发布的包可能不包含主分支里面最新的功能,并且可能与使用detectron2主分支的研究项目不兼容。

(这部分其实主要是当你选择release版本的detectron2时,它所需要的配置环境,当clone detectron2的源,也就是主分支代码时,需要的环境更复杂,但是可以根据自己的环境对detectron2进行编译)

常见安装问题

点击每个问题查看解决办法:

1、Undefined symbols that contains TH,aten,torch,caffe2.

        经常发生在detectron2或者torchvision未使用你正在运行的Pytroch版本编译时。

    如果是在预构建torchvision时发生错误,卸载torch vision和Pytorch,然后在https://pytorch.org/,这个网站上下载匹配的版本重新安装。(同样可以使用我前面提到的国内的镜像网站)

        如果时发生在与构建detectron2时,检查release notes,卸载然后重新安装与Pytorch版本匹配的detectron2。

        如果时在你从源代码手动构建detectron2或者torchvision时,移除你构建的文件(build/,**/*.so这两个文件,文件夹和.so文件)并重新构建,它会选择适合你系统环境的版本。

        如果以上的指示没有解决这个问题,请提供你的环境信息,以便重置这个问题。

2、Missing torch dynamic libraries, OR segmentation fault immediately when using detectron2.

        经常发生在detectron2或者torchvision未使用你正在运行的Pytroch版本编译时。与上一问题的解决方法一致

3、Undefined C++ symbols (e.g. GLIBCXX) or C++ symbols not found.

        通常它是因为库在一个更新的C++编译器上编译,但是运行在一个旧的C++环境中

        在旧版本的anaconda中经常发生,运行conda update libgcc命令可能有助于更新它的环境

        基本的解决方法时避免使用错误的配置,或者使用编译的旧版本的编译器编译,或者在合适的C++运行环境中运行代码,如果想要在特定的C++运行环境中运行,可以使用环境变量LD_PRELOAD=/path/to/libstdc++.so

        (这里就是把你系统中libstdc++.so的位置加到环境变量中)

4、“nvcc not found”或者“Not compiled with GPU support”或者“Detectron2 CUDA Complier:not avaiable”

        当你构建detectron2时未找到CUDA,要先确认你的CUDA正确安装了,可以在python中导入,使用以下命令查看:

python -c 'import torch; from torch.utils.cpp_extension import CUDA_HOME; print(torch.cuda.is_available(), CUDA_HOME)'

        (这个命令是查看系统中的CUDA是否可以被python程序使用,并输出CUDA_HOME)当输出(True, a directory with cuda)时,就可以构建detectron2。

        大部分模型都可以在不支持GPU时进行推理过程,但不支持训练过程,如果要使用cpu,在config文件中设置MODEL.DEVICE=’cpu’

5、"invalid device function" 或者 "no kernel image is available for execution"

        两种可能:

        (1)你在一个版本的CUDA上构建detectron2,但是在另外的CUDA版本上运行。为了确认是不是这个原因,可以使用

python -m detectron2.utils.collect_env

命令来查看CUDA版本是否一致,通过这个命令的输出,你可以看到“Detectron2 CUDA Compiler”, “CUDA_HOME”, “PyTorch built with - CUDA”包含的CUDA库版本是一致的

         当他们不一致时,你需要要么安装一个不同的pytorch版本以匹配你本地安装的CUDA,要么安装一个不同版本的CUDA来匹配pytorch版本。

        (2)PyTorch/torchvision/Detectron2没用使用正确的GPU SM architecture进行构建(aka.显卡算力)

        PyTorch/detectron2/torchvision可以使用的架构,可以在“architecture flags”中查看,可以在https://developer.nvidia.com/zh-cn/cuda-gpus网址中查看你的gpu算力,查看模型是否支持。

        (如果上面的网站中,没有你的显卡型号,可以使用下面的命令进行查看,输出为(8,6),代表显卡算力为8.6)

        如果你使用预先构建的detectron2/torchvision版本,它们已经支持在主流GPU上运行。如果不支持,那需要你通过源代码进行编译构建。

        如果是从源代码构建detectron2,那么它只支持它检测到的GPU设备。这意味着编译的代码可能无法在不同的GPU设备上运行,需要重新在正确的环境中进行编译,移除所有安装、编译的文件,将环境变量TORCH_CUDA_ARCH_LIST设置为合适的值,然后重新构建项目。例如export TORCH_CUDA_ARCH_LIST="6.0;7.0",使它同时适用于P100s和V100s。

        (这里大概意思就是从源代码构建detectron2,那么构建的项目只能在你构建项目的环境下运行,如果复制到其它环境需要把编译文件删除重新构建。)

6、Undefined CUDA symbols; Cannot open libcudart.so

        (这里与上一个问题的第1个解决办法是一样的,可以看上面的解决方法,官方文档在这里与上面的完全一样)

7、C++ compilation errors from NVCC / NVRTC, or "Unsupported gpu architecture"

        一些可能的问题:

        (1)你安装的Pytorch版本必须与电脑安装的CUDA版本匹配。它们都可以使用python collent_env.py命令查看。当它们不一致时,你需要要么安装一个不同的pytorch版本以匹配你本地安装的CUDA,要么安装一个不同版本的CUDA来匹配pytorch版本.

        (2)本地安装的CUDA版本需要支持你的GPU算力,GPU的算力可以在nvidia中查到,通过cuda可以查看与CUDA匹配的显卡算力。如果你的nvcc版本太旧,可以通过设置环境变量TORCH_CUDA_ARCH_LIST,降低版本,来支持gpu算力。(最好就是下载合适的版本)

nvidia:https://developer.nvidia.com/zh-cn/cuda-gpus

cuda:https://gist.github.com/ax3l/9489132

        (3)你使用的nvcc与gcc组合不兼容,需要改变它们其中之一的版本,可以通过下面的网址https://gist.github.com/ax3l/9489132,查看有效的组合。另外当CUDA≤10.1.105时,不支持gcc>7.3。

        Pytorch使用的CUDA和gcc版本,可以通过print(torch.__config__.show())查看。

 8、"ImportError: cannot import name '_C'"

        请根据上面的指示安装detectron2。或者如果你在detectron2的根目录运行代码,cd到其他的路径,不然可能无法导入你安装的代码。(这里有可能时python环境不支持,或者编译出问题,我开始用python3.8编译就出现了这个问题,后来换了python3.6就可以了)

9、Any issue on windows

        Detectron2可以部署在有CircleCI的windows上,目前没有提供官方的支持。欢迎支持拉取请求(PRs)兼容windows系统的代码

10、ONNX conversion segfault after some "TraceWarning"

        ONNX包使用太旧的的编译器进行编译,从onnx的源代码进行编译安装,使用与Pytorch使用的版本接近的编译器(可以通过torch.__config__.show()命令查看,前面已经有展示了)

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

        在网址中,查看这个栈溢出的答案。

网址:https://stackoverflow.com/questions/56083725/macos-build-issues-lstdc-not-found-while-building-python-package

在特定的环境中安装

        1、Colab:查看我们的Colab Tutorial,有详细的安装指导。

        https://colab.research.google.com/drive/16jcaJoc6bCFAQ96jDe2HwtXj7BMD_-m5

        2、Docker:官方的Dockerfile安装detectron2只要几行简单的命令

        https://github.com/facebookresearch/detectron/tree/master/docker

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值