安装步骤:
1、创建环境
conda create -n detectron2 python=3.8 -y
2、激活环境
conda activate detectron2
3、在Pytorch官网中找到对应的pytorch版本以及CUDA版本,要求pytorch版本大于1.8
pip install torch==1.9.0+cu111 torchvision==0.10.0+cu111 torchaudio==0.9.0 -f https://download.pytorch.org/whl/torch_stable.html
4、安装git
conda install git
5、安装必要库
pip install cython opencv-python pillow matplotlib termcolor cloudpickle tabulate tensorboard termcolor tqdm yacs mock fvcore pydot wheel futrue -i https://pypi.tuna.tsinghua.edu.cn/simple
6、下载源码地址
https://github.com/facebookresearch/detectron2
7、cd到detectron2目录下,开始构建
python setup.py build --force develop
报错:
#error: – unsupported Microsoft Visual Studio version! Only the versions between 2017 and 2019 (inclusive) are supported!
查到的原因是CUDA版本需要与微软的C/C++编译器版本匹配
解决方法找到对应的头文件:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1\include\crt\host
_config.h
找到对应的代码行更改对应的_MSC_VER,为了省事,直接给了一个比较大的值:
重新执行安装命令
python setup.py build --force develop
编译报错:
nms_rotated_cuda.cu
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(14): error: name must be a namespace name
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(68): error: identifier "single_box_iou_rotated" is undefined
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=double]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(68): error: type name is not allowed
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=double]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(68): warning: expression has no effect
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=double]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(58): warning: variable "cur_box" was set but never used
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=double]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(68): error: identifier "single_box_iou_rotated" is undefined
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=float]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(68): error: type name is not allowed
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=float]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(68): warning: expression has no effect
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=float]"
(105): here
D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu(58): warning: variable "cur_box" was set but never used
detected during instantiation of "void nms_rotated_cuda_kernel(int, double, const T *, unsigned long long *) [with T=float]"
(105): here
5 errors detected in the compilation of "D:/mega_work/python/detectron2-main/detectron2-main/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu".
error: command 'C:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v11.1\\bin\\nvcc.exe' failed with exit code 1
解决方法:定位到文件
更改头文件:如下所示屏蔽原始的头文件。添加#include "box_iou_rotated/box_iou_rotated_utils.h"头文件,如下所示:
更改后保存:继续 python setup.py build --force develop
安装好后就可以进行测试了
cd到网盘中下载的Detectron2文件夹下,执行预测即可。
python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input 1.jpg --output ./result.jpg --opts MODEL.WEIGHTS detectron2://COCO-
InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl