windows10+detectron2完美安装教程

前言

需要下载detectron2的github项目,安装vs2019 (强烈建议这个版本,其他的版本需要做更多地操作才能成功安装),默认其他环境没问题。

下载detectron2

链接:https://github.com/facebookresearch/detectron2
也可以克隆到本地

git clone https://github.com/facebookresearch/detectron2.git

将prompt的当前路径改为detectron2

cd detectron2

安装Visual Studio 2019

官网:VS2019
下载社区版安装即可,第一次进入这个界面可能需要登录微软账号。
在这里插入图片描述
双击运行下载得到的可执行文件,安装时选择使用C++的桌面开发,然后全部默认安装即可。
在这里插入图片描述
这里耗时比较久,需要十几分钟的样子,等待安装完成后不用启动。接下来需要配置一个环境变量。右击此电脑选择属性–>高级系统设置–>环境变量–>系统变量–>path–>编辑–>新建,输入

C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\bin\Hostx64\x64

注意,这个路径要跟自己电脑上的实际情况对应,其中的14.29这个编号下一步需要。
接下来在点击win+R打开命令行窗口,输入cl,查看是否有输出。
在这里插入图片描述
如上图说明环境变量没有问题,此时需要再设置一下编译器。

SET MSSdk=1
SET DISTUTILS_USE_SDK=1
call “C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Auxiliary\Build\vcvarsall.bat” amd64 -vcvars_ver=14.29

运行后结果如图。
在这里插入图片描述

修改代码

先安装fvcore包,可以直接使用pip安装

pip install fvcore -i https://pypi.mirrors.ustc.edu.cn/simple/

也可以先下载或者git克隆fvcore的github包,然后安装。

git clone https://github.com/facebookresearch/fvcore.git
cd fvcore
python setup.py build --force develop

接下来需要修改一些文件里的内容。

  1. 在Anaconda的安装路径里,D:\Anaconda3\envs\pt2.0\Lib\site-packages\torch\utils\cpp_extension.py,修改在文件中的SUBPROCESS_DECODE_ARGS参数为gbk,上边是注释掉的,可以使用查找来找这个变量。注意路径中的虚拟环境名与自己实际情况对应。
# SUBPROCESS_DECODE_ARGS = ('',) if IS_WINDOWS else ()
SUBPROCESS_DECODE_ARGS = ('gbk',) if IS_WINDOWS else ()

如果不能找到这个变量,那可能是pytorch版本不一样导致的,此时还可以有另一种解决办法,修改如下

# match = re.search(r'(\d+)\.(\d+)\.(\d+)', compiler_info.decode(*SUBPROCESS_DECODE_ARGS).strip())
match = re.search(r'(\d+)\.(\d+)\.(\d+)', compiler_info.decode(‘gbk’).strip())

不同的pytorch版本可能存在一些小差异,但基本这条语句类似。

  1. 继续修改当前环境里的,D:\Anaconda3\envs\pt2.0\Lib\site-packages\torch\include\torch\csrc\jit\runtime\argument_spec.h,修改如下
// static constexpr size_t ARG_SPEC_DEPTH_LIMIT = 128;
static const size_t ARG_SPEC_DEPTH_LIMIT = 128;

注意这里如果是加注释的话要注意注释格式中python与c++写法的不同。

  1. 继续修改当前环境里的,D:\Anaconda3\envs\pt2.0\Lib\site-packages\torch\include\torch\csrc\jit\ir\ir.h,注释掉下边这句,一些版本的pytorch里边可能直接没有这条语句。
// static constexpr Symbol Kind = ::c10::prim::profile_optional
  1. 修改detectron2里的,I:\detectron2\detectron2\layers\csrc\ROIAlignRotated\ROIAlignRotated_cuda.cu,将所有的ceil替换为ceilf,这里要注意区分大小写,有两处大写的Ceil不要替换,在使用一键替换时一定要严格限制大小写。
  2. 继续修改detectron2里的,I:\detectron2\detectron2\layers\csrc\deformable\deform_conv_cuda_kernel.cu,将所有的floor改为floorf
  3. 继续修改detectron2里的,I:\detectron2\detectron2\layers\csrc\cocoeval\cocoeval.cpp
// localtime_r(&rawtime, &local_time); 
localtime_s(&local_time,&rawtime);
  1. 继续修改detectron2里的,I:\detectron2\detectron2\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*/
#include "box_iou_rotated/box_iou_rotated_utils.h"

此时prompt在detectron2路径下,运行

python setup.py build develop

如下则表示已经安装成功了。
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/Wenyuanbo/article/details/130577744