在 windows 安装 detectron2 一直是 比较令人头疼的问题,
核心安装流程还是按照,官网指出的:
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 ...
进行操作,而且出现报错的时候不要×只看最后几行,比如
多往上翻翻才能看到真正错误的原因,一般报错分为 三大点:【cl.exe】【cuda-pytorch】和【编译相关】
往往上来就报错的就是【cuda-pytorch问题】 说
The detected CUDA version (12.4) has a minor version mismatch with the version that was used to compile PyTorch (12.1).
这种错误的,有三种解决思路
1 安装 匹配 pytorch 的 cuda ,网址:https://developer.nvidia.com/
2 安装匹配 的 cuda 的pytorch ,请自行前往 pytorch 官网
3 跳过检测(不推荐):
在 site-packages/torch/utils/cpp_extension.py", in BuildExtension
中注释掉
if cuda_ext and not IS_HIP_EXTENSION:
_check_cuda_version(compiler_name, compiler_version)
【cl.exe】问题
一是说 找不到 cl.exe 的没错别犹豫下载合适版本的 VS,不然会出现下面问题
fatal error C1189: #error: -- unsupported Microsoft Visual Studio version! Only the versions bet ween 2017 and 2019 (inclusive) are supported!
以上就是说明 咱的 vs 版本和 cuda 之间的不匹配,此时咱就安装何时版本的 cuda 或者 VS,具体版本怎么匹配的基本上 cuda 12.2及以上使用 vs2022.然后安装 【C++配套 开发】,最后将 cl.exe 添加到 系统环境变量如例:C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64,此处有困难直接百度。
这里说个特例 我的 环境是 windows server 2022 出现过,超老旧版本的 vs:
: fatal error C1083: Cannot open include file: 'string_view': No such file or directory error: command 'C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe' failed with exit code 2
但我的我的环境变量是C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.43.34808\bin\Hostx64\x64
完全不匹配,即使我反复确认并且 使用命令:cl;和 where cl 都没问题,此时问题是注册表中仍有 封建余孽 :
使用 Visual Studio Uninstaller 工具删除旧版 VS2015 的残留组件然后强制修改注册表
删除旧版 VS2015 的注册表项(谨慎操作):
- 按
Win + R
输入regedit
打开注册表编辑器。 - 导航到:
-
HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\VisualStudio\SxS\VS7
- 删除所有包含
14.0
的键值(如14.0
对应的路径指向 VS2015)。 - 重启系统。
【编译问题】:可能很多,但一般出现较为频繁的是如下问题
D:\PYTHON_PROJECY\InsSeg\detectron2\detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu(14): error: name must be a namespace name
using namespace detectron2;
^
D:\PYTHON_PROJECY\InsSeg\detectron2\detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu(68): error: identifier "single_box_iou_rotated" is undefined
if (single_box_iou_rotated<T>(cur_box, block_boxes + i * 5) >
^
D:\PYTHON_PROJECY\InsSeg\detectron2\detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu(68): error: type name is not allowed
if (single_box_iou_rotated<T>(cur_box, block_boxes + i * 5) >
^
3 errors detected in the compilation of "D:/PYTHON_PROJECY/InsSeg/detectron2/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu".
nms_rotated_cuda.cu
这里我们修改 对应 文件 D:/PYTHON_PROJECY/InsSeg/detectron2/detectron2/layers/csrc/nms_rotated/nms_rotated_cuda.cu
将开头的引用改为
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
#include <ATen/ATen.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <ATen/cuda/CUDAApplyUtils.cuh>
//NOTE: replace relative import
/*#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"
应该就可顺利编译。