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

前言

Detectron2是Facebook AI Research的下一代库,提供最先进的检测和分割算法。它是Detectron和maskrcnn-benchmark基准的继承者。由于其文档丰富,代码有长久维护,所以建议使用该版本的Mask-RCNN。

Github链接:https://github.com/facebookresearch/detectron2

安装

官方指导链接:Installation

  • 在Ubuntu下选择预编译好的的detectron2,而不是从源码编译。
  • 我的CUDA版本是11.2,由于没有提供11.2的版本,所以我选择11.1版本。
# 创建虚拟环境
conda create -n detectron2 python=3.8

# 激活虚拟环境
conda activate detectron2

# 安装pytorch,注意对应的 cuda
pip install torch==1.9.1+cu111 torchvision==0.10.1+cu111 -f https://download.pytorch.org/whl/torch_stable.html
conda install --channel https://conda.anaconda.org/Zimmf cudatoolkit=11.1
conda install opencv-python


# To install it from a local clone:
git clone https://github.com/facebookresearch/detectron2.git
python -m pip install -e detectron2

数据集准备

使用Labelme来制作数据集标签。
在这里插入图片描述
数据集文件结构采用Coco数据集的格式,格式如下:
在这里插入图片描述
在这里插入图片描述

训练

python tools/train_net.py --config-file "configs/e2e_mask_rcnn_R_50_FPN_1x.yaml"

测试

python demo.py --config-file ../configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml --input test_data/20211109_212331_1.jpg --confidence-threshold 0.6 --output output/20211109_212331_1.jpg --opts MODEL.WEIGHTS ../tools/output/model_0074999.pth