目标检测框架——Dtectron2

    技术2022-07-11  121

    文章目录

    前言1. Windows10 Dtectron2 配置1.1 虚拟环境1.2 安装pytorch1.3 安装一些依赖包1.4 修改文件内部的一些参数(非常重要)1.5 安装pycocotools1.7 安装Dtectron21.8 测试 2. Linux Dtectron2 配置

    前言

    目标检测的模型还是很多的,要挨个挨个学还是有点吃力(精力旺盛可忽略),所以这里总结一下当前流行的目标检测的框架:darknet yolov4,mmdetection2.0,detectron2.0。至于这三个框架怎么去选择,我的答案是只有你真正懂了,上手试了这几个框架之后你才能比较它们之间的优劣。

    1. Windows10 Dtectron2 配置

    我的环境:WIN10,CUDA10.1,cuDNN7.6.4,Anaconda,VS2019(这个必须有),OpenCV3.4.10,GTX1660(6G内存)

    1.1 虚拟环境

    至于为什么要创建一个虚拟环境,起作用主要有以下几个:

    虚拟环境可以搭建独立的python运行环境, 使得单个项目的运行环境与其它项目互不影响。可以避免一个环境造成得安装包过的多,导致环境加载缓慢。

    创建一个没有存在过的虚拟环境:

    conda create -n detectron2 python=3.7

    激活虚拟环境:

    conda activate detectron2

    1.2 安装pytorch

    conda install pytorch=1.4 torchvision cudatoolkit=10.1

    这里pytorch只能是1.4版本的,太低会运行不成功,这也是为什么要创建虚拟环境的原因。

    1.3 安装一些依赖包

    pip install cython opencv-python pillow matplotlib termcolor cloudpickle tabulate tensorboard termcolor tqdm yacs mock fvcore pydot wheel future

    1.4 修改文件内部的一些参数(非常重要)

    修改C:\Users\lee\Anaconda3\envs\mmd2\Lib\site-packages\torch\utils\cpp_extension.py185行:match = re.search(r'(\d+)\.(\d+)\.(\d+)', compiler_info.decode("utf8","ignore").strip())

    修改C:\Users\lee\Anaconda3\envs\detectron\Lib\site-packages\torch\include\torch\csrc\jit\argument_spec.h文件中的static constexpr size_t DEPTH_LIMIT = 128; 改成 static const size_t DEPTH_LIMIT = 128;

    修改C:\Users\lee\Anaconda3\envs\detectron\Lib\site-packages\torch\include\pybind11\cast.h中的explicit operator type&() { return *(this->value); } (大约在1449行)改成 explicit operator type&() { return ((type)this->value); }

    1.5 安装pycocotools

    这个库就不用我多说了,做目标检测没人不知道他吧 两种办法:(我用的第二种)

    pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI

    或者https://github.com/philferriere/cocoapi下载源码,并进行解压。以管理员身份打开 CMD 终端,并切换到 cocoapi\PythonAPI目录。运行以下指令:

    python setup.py build_ext install

    1.7 安装Dtectron2

    去GitHub下载Dtectron2进入detectron2-0.1.3,执行:执行python setup.py build develop

    1.8 测试

    测试一张图(记得自己改图片地址)

    python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input 1.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

    测试摄像头

    python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --webcam --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

    根据GitHub给的说明,可以进行以下替换:

    要在你的网络摄像头上运行,请把–input files 替换为–webcam

    要播放视频,请把–input files替换为–video-input video.mp4

    要在cpu上运行,请在 -opts之后添加MODEL.DEVICE cpu-。

    要将输出保存到目录(用于图像)或文件(用于网络摄像头或视频),请使用–output。

    2. Linux Dtectron2 配置

    我的环境:Ubuntu18.04,CUDA10.2,cuDNN8.0.2,Anaconda,GP106(3Gx2内存)

    创建一个没有存在过的虚拟环境:

    注意此处一定要是python=3.7,3.8版本会报错(很多坑)

    conda create -n detectron2 python=3.7

    激活虚拟环境:

    conda activate detectron2

    安装pytorch:

    conda install pytorch=1.5 torchvision cudatoolkit=10.2

    安装一些依赖包

    pip install cython opencv-python==4.2.0.34 pillow matplotlib termcolor cloudpickle tabulate tensorboard termcolor tqdm yacs mock fvcore pydot wheel future

    注意:不需要安装pycocotools

    安装Dtectron2

    去GitHub下载Dtectron2进入detectron2,执行:执行python python -m pip install -e .

    测试 测试一张图(记得自己改图片地址)

    python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input 1.jpg --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

    测试摄像头

    python demo/demo.py --config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --webcam --opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl

    根据GitHub给的说明,可以进行以下替换:

    要在你的网络摄像头上运行,请把–input files 替换为–webcam

    要播放视频,请把–input files替换为–video-input video.mp4

    要在cpu上运行,请在 -opts之后添加MODEL.DEVICE cpu-。

    要将输出保存到目录(用于图像)或文件(用于网络摄像头或视频),请使用–output。

    Processed: 0.010, SQL: 9