Detectron2 是 Facebook AI Research 的下一代目标检测库,可提供最先进的检测和分割算法。 它是 Detectron 和 maskrcnn-benchmark 的继任者。它支持 Facebook 中的许多计算机视觉研究项目和生产应用程序。现在也有越来越多的模型基于detectron2构建或部署。下面是框架模型能实现的基本效果。
由于detectron2 官方不支持Windows系统,所以在windows系统上安装有些坑需要注意,本文介绍在windows11系统上安装Detectron2的步骤和注意事项。
图像来源:https://github.com/facebookresearch/detectron2
测试机器的软硬件基本配置如下:
conda create -n yolov7 --clone pytorch1.11
conda activate yolov7
对于还没有安装pytorch1.11的同学,可以在刚创建的conda环境中执行如下命令快速安装指定版本的torch和cudatoolkit
conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio cudatoolkit=11.3
(yolov7) D:\python\cocoapi-master>
(yolov7) D:\python\cocoapi-master>cd PythonAPI
(yolov7) D:\python\cocoapi-master\PythonAPI>
(yolov7) D:\python\cocoapi-master\PythonAPI>python setup.py build_ext --inplace
(yolov7) D:\python\cocoapi-master\PythonAPI>python setup.py build_ext install
# 执行提示:
Installed c:\users\irace\.conda\envs\yolov7\lib\site-packages\pycocotools-2.0-py3.8-win-amd64.egg
Processing dependencies for pycocotools==2.0
Finished processing dependencies for pycocotools==2.0
(yolov7) D:\python\fvcore-main>python setup.py build --force develop
# 安装提示:
Finished processing dependencies for fvcore==0.1.5
conda install ninja
Detectron2仓库地址
下载detectron2-main.zip后解压到D:\python\detectron2-main
修改文件内容:注意以下所有要修改的文件都是在yolov7环境下找,别改错其他环境的文件了
可通过everything搜索找到yolov7环境下对应文件,举例修改argument_spec.h文件,找env\yolov7文件夹中的文件
修改cpp_extension.py
C:\Users\irace.conda\envs\yolov7\Lib\site-packages\torch\utils\cpp_extension.py
第318行,注释的是原语句,后一行是改后的
# 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())
C:\Users\irace.conda\envs\yolov7\Lib\site-packages\torch\include\torch\csrc\jit\runtime\argument_spec.h
第170行,注释的是原语句,后一行是改后的
// static constexpr size_t ARG_SPEC_DEPTH_LIMIT = 128;
static const size_t ARG_SPEC_DEPTH_LIMIT = 128;
D:\python\detectron2-main\detectron2\layers\csrc\ROIAlignRotated\ROIAlignRotated_cuda.cu
将所有的ceil改为ceilf
建议用vs code等工具打开代码查找并替换,注意不能用replace all,因为有文件中有的函数名中包含ceil字母,必须一个个查看替换:
D:\python\detectron2-main\detectron2\layers\csrc\deformable\deform_conv_cuda_kernel.cu
将所有的floor改为floorf
D:\python\detectron2-main\detectron2\layers\csrc\cocoeval\cocoeval.cpp
487行,注释的是原语句,后一行是改后的
// localtime_r(&rawtime, &local_time);
localtime_s(&local_time,&rawtime);
(yolov7) D:\python\detectron2-main>python setup.py build develop
# 错误提示:
raise RuntimeError(CUDA_MISMATCH_MESSAGE.format(cuda_str_version, torch.version.cuda))
RuntimeError:
The detected CUDA version (10.0) mismatches the version that was used to compile
PyTorch (11.3). Please make sure to use the same CUDA versions.
pytorch 1.11.0 py3.8_cuda11.3_cudnn8_0
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.3
重启conda prompt (修改环境变量后必须重启命令行工具生效)
再次激活yolov7环境,并进入detectron2-main目录,再次执行命令
(yolov7) D:\python\detectron2-main>python setup.py build develop
Installed c:\users\irace\.conda\envs\yolov7\lib\site-packages\mypy_extensions-0.4.3-py3.8.egg
Searching for pathspec>=0.9.0
Reading https://pypi.org/simple/pathspec/
Downloading https://files.pythonhosted.org/packages/42/ba/a9d64c7bcbc7e3e8e5f93a52721b377e994c22d16196e2b0f1236774353a/pathspec-0.9.0-py2.py3-none-any.whl#sha256=7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a
error: Download error for https://files.pythonhosted.org/packages/42/ba/a9d64c7bcbc7e3e8e5f93a52721b377e994c22d16196e2b0f1236774353a/pathspec-0.9.0-py2.py3-none-any.whl#sha256=7d15c4ddb0b5c802d161efc417ec1a2558ea2653c2e8ad9c19098201dc1c993a: timed out
pip install pathspec==0.9.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
# ...
Successfully installed pathspec-0.9.0
(yolov7) D:\python\detectron2-main>python setup.py build develop -i https://pypi.tuna.tsinghua.edu.cn/simple
Finished processing dependencies for detectron2==0.6
Name | lr sched | train time (s/iter) | inference time (s/im) | train mem (GB) | box AP | model id | download |
---|---|---|---|---|---|---|---|
R50-C4 | 1x | 0.551 | 0.102 | 4.8 | 35.7 | 137257644 | model | metrics |
R50-DC5 | 1x | 0.380 | 0.068 | 5.0 | 37.3 | 137847829 | model | metrics |
R50-FPN | 1x | 0.210 | 0.038 | 3.0 | 37.9 | 137257794 | model | metrics |
R50-C4 | 3x | 0.543 | 0.104 | 4.8 | 38.4 | 137849393 | model | metrics |
R50-DC5 | 3x | 0.378 | 0.070 | 5.0 | 39.0 | 137849425 | model | metrics |
R50-FPN | 3x | 0.209 | 0.038 | 3.0 | 40.2 | 137849458 | model | metrics |
R101-C4 | 3x | 0.619 | 0.139 | 5.9 | 41.1 | 138204752 | model | metrics |
R101-DC5 | 3x | 0.452 | 0.086 | 6.1 | 40.6 | 138204841 | model | metrics |
R101-FPN | 3x | 0.286 | 0.051 | 4.1 | 42.0 | 137851257 | model | metrics |
X101-FPN | 3x | 0.638 | 0.098 | 6.7 | 43.0 | 139173657 | model | metrics |
(yolov7) D:\python\detectron2-main>pip install opencv-python==4.5.5.62 -i https://pypi.tuna.tsinghua.edu.cn/simple
准备待测图像
demo运行方式1:命令行运行demo.py
python demo/demo.py --config-file ../configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml --input images/rally.jpg --opts MODEL.WEIGHTS ../models/faster_rcnn_R_50_FPN_3x/model_final_280758.pkl
demo运行方式2:在pycharm中运行demo.py,这种方式方便调试
在pycharm中打开Detectron2工程中的demo.py文件
设置命令行参数:Run → Edit Configuration → Configuration → Parameters中输入命令行参数如下:
–config-file
…/configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml
–input
images/rally.jpg
–output
output
–opts
MODEL.WEIGHTS
…/models/faster_rcnn_R_50_FPN_3x/model_final_280758.pkl
点击【ok】后,直接即可在pycharm中运行demo.py
运行后,检测结果rally.jpg将自动保存在D:\python\detectron2-main\demo\output文件夹下
至此,Windows 11下安装Detectro2圆满结束。
本文更新地址:https://blog.csdn.net/iracer/article/details/125755029?spm=1001.2014.3001.5501
转载请注明出处。