开发环境

  • Ubuntu 20.04
  • RTX3070

开源代码


https://github.com/matterport/Mask_RCNN


https://github.com/facebookresearch/Detectron
Facebook曾发布过名叫Detecron的项目,也是一款图像分割与识别平台,其中也包含Mask R-CNN。不过它是基于Caffe 2深度学习框架编写的。


https://github.com/facebookresearch/maskrcnn-benchmark
Mask R-CNN Benchmark是一个完全由PyTorch 1.0写成,快速、模块化的Faster R-CNN和Mask R-CNN组件。该项目旨在让用户更容易地创建一种模块,实现对图片中物品的识别与分割。除了更改框架,Mask R-CNN Benchmark相比它的“前辈”Detectron,训练速度提高了一倍。


https://github.com/open-mmlab/mmdetection
商汤和香港中文大学的多媒体实验室也开源了一个类似项目:mmdetection。它支持Faster R-CNN、Mask R-CNN、RetinaNet等等,相比Facebook的Detecron有5%到20%的性能提升。这个模型还在2018年的COCO Detection竞赛中拿下了冠军。

环境配置

官方给出的配置方法是:https://github.com/facebookresearch/maskrcnn-benchmark/blob/main/INSTALL.md


conda create --name maskrcnn_benchmark -y
conda activate maskrcnn_benchmark

conda install ipython pip
pip install ninja yacs cython matplotlib tqdm opencv-python
# 下面这条选择与自己的cuda版本匹配的安装命令,可在https://pytorch.org/get-started/locally/上查看
# 如果cudatoolkit的版本和自己cuda的版本不一致,后面的其他包安装会报错,只能卸载重新安装对应版本的
# 在这里查看命令https://pytorch.org/get-started/previous-versions/,没有对应cuda11.2的pytorch-nightly
# 照葫芦画瓢,我使用的命令为conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=11.1 -c pytorch -c conda-forge
conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=11.1 -c pytorch -c conda-forge # 这是一个漫长的过程

# 进入想要安装的目录下
export INSTALL_DIR=$PWD # 设置当前路径为安装路径
cd $INSTALL_DIR # 进入安装路径
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
python setup.py build_ext install
cd $INSTALL_DIR
git clone https://github.com/mcordts/cityscapesScripts.git
cd cityscapesScripts/
python setup.py build_ext install
cd $INSTALL_DIR
git clone https://github.com/NVIDIA/apex.git
cd apex
gedit setup.py # 为了避免报错,编辑该文件
# 参考链接:https://github.com/NVIDIA/apex/pull/323#issuecomment-652382641
# 注释掉下面的代码
# if (bare_metal_major != torch_binary_major) or (bare_metal_minor != torch_binary_minor):
#     raise RuntimeError("Cuda extensions are being compiled with a version of Cuda that does " +
#                        "not match the version used to compile Pytorch binaries.  " +
#                        "Pytorch binaries were compiled with Cuda {}.\n".format(torch.version.cuda) +
#                        "In some cases, a minor-version mismatch will not cause later errors:  " +
#                        "https://github.com/NVIDIA/apex/pull/323#discussion_r287021798.  "
#                        "You can try commenting out this check (at your own risk).")
python setup.py install --cuda_ext --cpp_ext
cd $INSTALL_DIR
git clone https://github.com/facebookresearch/maskrcnn-benchmark.git
cd maskrcnn-benchmark
python setup.py build develop # 报错参考:https://blog.csdn.net/isunLt/article/details/109169409
unset INSTALL_DIR

用demo测试一下是否安装配置成功:

conda activate maskrcnn_benchmark
cd PATH/maskrcnn-benchmark/demo
python webcam.py # 调用摄像头测试程序,如果没有摄像头就读取图片来测试


测试过程中可能会发生报错,解决方法参考下面的链接:

https://blog.csdn.net/pangweijian/article/details/120371802