系统环境
Windows 10
CUDA 11.3
VS2022
Anaconda Python3.9.16(env)
Pytorch 1.12
安装步骤
pycocotools的安装
参考https://blog.youkuaiyun.com/qq_55621259/article/details/125898710,使用以下命令:
conda install -c conda-forge pycocotools
其实际使用的是https://github.com/ppwwyyxx/cocoapi,而不是文章中所说的https://github.com/philferriere/cocoapi.git,而且其版本为2.0.6,不需要修改detectron2的文件。
环境变量配置
使用`x64 Native Tools Command Prompt for VS 2022`而不是`Developer Command Prompt for VS 2022`打开cmd(否则会自动编译x86,导致链接错误),然后进入conda环境,再使用以下命令设置环境变量(推荐设置到系统,参考https://blog.youkuaiyun.com/qq_21532607/article/details/128075997):
set MAX_JOBS=4
set DISTUTILS_USE_SDK=1
set OMP_NUM_THREADS=1
修改文件
detectron2文件修改
首先`git clone https://github.com/facebookresearch/detectron2.git`,然后修改`setup.py`(参考https://huaweicloud.youkuaiyun.com/63802ffedacf622b8df8662a.html):
#cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension},
cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension.with_options(use_ninja=False)},
cuda文件修改
因为VS2022版本太高,所以需要修改`C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3\include\crt\host_config.h`(参考https://blog.youkuaiyun.com/lishiyu93/article/details/114599859):修改`_MSC_VER >=`后面的数值到`1934`(vs2022最大是1933)。
detectron2文件修改(未测试)
还是依据https://blog.youkuaiyun.com/qq_21532607/article/details/128075997,对`detectron2\detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu`进行如下修改(我是直接修改了,没有测试在不修改情况下会有什么错误):
/*#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"
开始安装
在cmd中,运行以下命令:
python -m pip install -e detectron2
运行Demo
因为anaconda和anaconda envs中各存在1个libiomp5md.dll文件,所以会报链接错误。所以依据https://blog.youkuaiyun.com/qq_37164776/article/details/126832303,对demo.py添加2行代码:
import os
os.environ["KMP_DUPLICATE_LIB_OK"]="TRUE"