目录
本文主要介绍windows系统下安装detectron2的全流程,包括环境搭建、torch安装、detectron2安装以及本人安装过程中遇到的bug及相应解决办法。
第1章 创建detectron2环境
1.1 创建虚拟环境
在终端下输入
conda create --name detectron2 python=3.9
- --name:【detectron2】为新创建的虚拟环境的名称,创建完的环境可以在Anaconda/envs目录下找到
- python=3.9:指创建的python版本号。
1.2 激活虚拟环境
终端下输入
conda activate detectron2
detectron2为激活的环境名称。
第2章 torch安装
2.1 查看cuda版本
终端输入
nvcc -V
得到输出结果,cuda版本为11.6
2.2 安装相应torch版本
推荐下载本地安装,torch、torchvision和python的对应版本查询链接如下:
https://pytorch.org/get-started/previous-versions/
根据相应的cuda版本,得到适配的torch版本为1.12.1,torchvision为0.13.1,torchaudio为0.12.1。在以下链接下载相对应的torch、torchvision等文件:
https://download.pytorch.org/whl/torch_stable.html
cu116表明cuda版本为11.6,torch-1.12.1表明torch版本为1.12.1,cp39表明python版本为3.9。请根据实际情况下载对应torch、torchvision等文件。
终端进入下载好的文件所在的路径,激活detectron2环境(window终端下dir可以显示指定目录中的文件和子目录列表):
pip安装torch、torchvision等文件(使用tab键可自动补齐唯一文件名):
pip install "torchvision-0.13.1+cu116-cp39-cp39-win_amd64.whl"
pip install "torch-1.12.1+cu116-cp39-cp39-win_amd64.whl"
2.3 验证torch是否安装成功
在detectron2环境下,终端输入python运行python,导入torch包:
import torch
print(torch.__version__) # 显示安装的torch版本
print(torch.cuda.is_available()) # 显示cuda是否可用
结果显示为:
成功安装则会显示torch版本号与cuda版本号。输入exit()即可退出python。
第3章 安装detectron2
3.1 下载detectron2
detectron2的github下载链接如下,下载后解压到本地:
https://github.com/facebookresearch/detectron2
3.2 下载依赖包
安装pycocotools、hydra-core、fvcore、omegaconf等包。
3.3 安装detectron2
进行detectron2文件内,可以看到有名为setup.py的文件:
在此路径下,激活detectron2环境,输入:
python setup.py build develop
成功安装detectron2。
第4章 报错与解决
在安装detecteron中,遇到报错:
error: command 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.6\\bin\\nvcc.exe' failed with exit code 1
解决办法:
找到如下文件:
detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cudetectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu
原文内容为:
// Copyright (c) Facebook, Inc. and its affiliates.
#include <ATen/ATen.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/CUDAApplyUtils.cuh>
#ifdef WITH_CUDA
#include "../box_iou_rotated/box_iou_rotated_utils.h"
#endif
// TODO avoid this when pytorch supports "same directory" hipification
#ifdef WITH_HIP
#include "box_iou_rotated/box_iou_rotated_utils.h"
#endif
修改为:
// Copyright (c) Facebook, Inc. and its affiliates.
#include <ATen/ATen.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/CUDAApplyUtils.cuh>
/*#ifdef WITH_CUDA
#include "../box_iou_rotated/box_iou_rotated_utils.h"
#endif
// TODO avoid this when pytorch supports "same directory" hipification
#ifdef WITH_HIP
#include "box_iou_rotated/box_iou_rotated_utils.h"
#endif*/
#include "box_iou_rotated/box_iou_rotated_utils.h"
修改完毕之后,再次运行 python setup.py build develop。