Win11 pytorch 1.13 安装detectron2的艰难

大概是全CSDN首个在高版本pytorch下安装detectron2的教学

Windows 11下detectron2 编译安装 【高版本cudatoolkit(v11.7) pytorch=1.13】无痛安装
Win11 查看硬件环境可以在cmd中输入DXDIAG

硬件环境

在这里插入图片描述
在这里插入图片描述

软件环境

Visual Studio 2022

安装detectron2之前确保安装好了Visual Studio 2022, Anaconda
并且看看系统路径中有没有VS 2022的二进制可执行文件目录
在这里插入图片描述
笔者这里的CUDA版本比较难搞,建议各位观众谨慎食用
本机 [CUDA 11.7, cudnn]
在这里插入图片描述

创建detectron2的虚拟环境 并进入

conda create -n detectron2 python=3.10
# 安装后启动并进入环境
conda activate detectron2

之后正常按照pytorch官网对应安装pytorch 1.13即可,由于接下来要涉及detectron2/setup.py部分源码的修改
所以就先跳过pytorch == 1.13 的安装【具体安装可以参考Pytorch官方文档,这里不做赘述】

在这里插入图片描述

安装其他依赖库
# 这里opencv-python只是为了方便可视化,可以不安装
# 强烈建议安装ninja
pip install pycocotools opencv-python ninja

基于detectron2源码安装

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

在进行源码编译前,修改下列文件

修改detectron2里的源码 【红色下划线的源码】

在这里插入图片描述

  1. 将所有 \detectron2\detectron2\layers\csrc\ROIAlignRotated\ROIAlignRotated_cuda.cu文件中
    ceil替换为ceilf,这里要注意区分大小写,有两处大写的Ceil不要替换,一键替换时一定要严格限制大小写。

  2. 继续修改detectron2中\detectron2\detectron2\layers\csrc\deformable\deform_conv_cuda_kernel.cu,将所有的floor改为floorf

  3. 继续修改detectron2里的,\detectron2\detectron2\layers\csrc\cocoeval\cocoeval.cpp第487行。
    localtime_r(&rawtime, &local_time);替换为 localtime_s(&local_time,&rawtime);

  4. 继续修改detectron2里的,\detectron2\detectron2\layers\csrc\nms_rotated\nms_rotated_cuda.cu
    对头文件定义中注释掉一些,添加一行代码#include "box_iou_rotated/box_iou_rotated_utils.h,如下:

// 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"

修改setup.py

由于VS默认系统路径的cl 编译器忽略了 -allow-unsupported-compiler 选项,而这其实是一个 nvcc 特定的选项,而不是 cl 的选项。
为了确保这个选项仅传递给 nvcc 而不是 cl,你需要在 setup.py 中更明确地设置 nvcc 编译器选项。
在当前目录,即\detectron2\下,针对setup.py文件第79行,添加如下代码
在这里插入图片描述

编译安装

在当前\detectron2\目录下运行

# build and develop
python setup.py build develop

develop命令本身不会安装软件包,但它会创建一个.egg-link部署目录回项目源代码目录。所以这就像安装,但不是复制到site-packages。它添加一个符号链接(.egg-link充当多平台符号链接)

python setup.py develop不会真正安装包,而是在系统环境中创建一个软连接指向包实际所在的目录,这样修改了相关文件之后不用再安装便能生效,便于开发调试等。

这样,你可以编辑源代码并直接查看更改,而无需在每次进行一些更改时重新安装,但是重装pytorch之后detectron2需要重新编译

验证

在这里插入图片描述
可在\detectron2\tests\下创建test_detectron2.py,借鉴

from detectron2.engine import DefaultPredictor
from detectron2.data import MetadataCatalog
from detectron2.config import get_cfg
from detectron2.utils.visualizer import ColorMode, Visualizer
from detectron2 import model_zoo

import cv2
import numpy as np
import requests

# Load an image
res = requests.get("https://live.staticflickr.com/700/33224654191_fdaee2e3f1_c_d.jpg")
image = np.asarray(bytearray(res.content), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)

config_file = 'COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml'
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file(config_file))
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.75 # Threshold
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(config_file)
cfg.MODEL.DEVICE = "cuda" # cpu or cuda

# Create predictor
predictor = DefaultPredictor(cfg)

# Make prediction
output = predictor(image)
print(output)
v = Visualizer(image[:, :, ::-1],
               scale=0.8,
               metadata=MetadataCatalog.get(cfg.DATASETS.TRAIN[0]),
               instance_mode=ColorMode.IMAGE
               )
v = v.draw_instance_predictions(output["instances"].to("cpu"))
cv2.imshow('images', v.get_image()[:, :, ::-1])
cv2.waitKey(0)

在这里插入图片描述

使用说明

编译成功后会在·detectron2·(子目录)目录下生成一个pyd文件。
使用detectron2平台时将此detectron2\detectron2\_C.cp310-win_amd64.pyd文件复制到对应的detectron2目录下,或将detectron2目录复制到你所需的python工程目录下即可

参考 并 感谢

1.测试detectron2原文链接:https://blog.csdn.net/lishiyu93/article/details/116459114

  • 12
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值