Caffe2 - Detectron 安装-MaskRCNN完整

Caffe2 - Detectron 安装

2018年01月23日 16:57:09 AIHGF 阅读数:14958更多

所属专栏: Caffe2

 版权声明:本文为博主原创文章,未经博主允许不得转载。欢迎访问 AIUAI.CN 交流学习. https://blog.csdn.net/oJiMoDeYe12345/article/details/79141879

Detectron 安装

Detectron 项目地址

基于 Caffe2.

Detectron 项目:

基于的 Backbone 网络结构:

这里写图片描述

Mask RCNN 输出.

From Detectron README.md.

1. Detectron 安装

Detectron 现还未给出 CPU 实现,需要基于 GPU 环境.

Requirements:

  • NVIDIA GPU, Linux, Python2
  • Caffe2, various standard Python packages, and the COCO API
  • CUDA 8.0, cuDNN 6.0.21.

1.1 Caffe2 安装

参考 Caffe2 - (一)Source 安装及问题解决 和 Caffe2 官方 Install Instructions.

由于 Detectron 需要 Caffe2 包含 Detectron module,查看是否有该模块,没有的话更新 Caffe2 版本.

将 Caffe2 的 Python package 路径添加到 PYTHONPATH 环境变量,并确保 Caffe2 安装成功:

# To check if Caffe2 build was successful
python2 -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

# To check if Caffe2 GPU build was successful
# This must print a number > 0 in order to use Detectron
python2 -c 'from caffe2.python import workspace; print(workspace.NumCudaDevices())'

1.2 依赖性安装

Python 依赖项:

pip install numpy pyyaml matplotlib opencv-python>=3.0 setuptools Cython mock
  •  

COCO API 安装:

# COCOAPI=/path/to/clone/cocoapi
git clone https://github.com/cocodataset/cocoapi.git $COCOAPI
cd $COCOAPI/PythonAPI
# Install into global site-packages
make install
# Alternatively, if you do not have permissions or prefer
# not to install the COCO API into global site-packages
python2 setup.py install --user

1.3 Detectron 安装

  • Clone Detectron reposity:

    
    # DETECTRON=/path/to/clone/detectron
    
    git clone https://github.com/facebookresearch/detectron $DETECTRON
  • 设置 Python 模块:

    cd $DETECTRON/lib && make
    • 1
  • 测试 Detectron 安装,如 SpatialNarrowAsOp test

    python2 $DETECTRON/tests/test_spatial_narrow_as_op.py
    • 1

安装完成后,即可用提供的模型进行推理测试.

2. COCO Dataset

Detectron 通过软链接将 COCO 数据集的 images 和 annotations 链接到路径 lib/datasets/data,如:

ln -s /path/to/coco $DETECTRON/lib/datasets/data/coco
  • 1

更多关于 COCO 数据集和其它数据集软链接创建方法参考:lib/datasets/data/README.md.

完成以后,即可进行模型训练.

3. 定制新的 Operators

首先阅读 FAQ.md 中关于定制 operators 的部分.

方便起见,Detectron 提供 CMake 支持以编译定制的 operators. 所有的定制 operators 可以被编译成单个库,以便于 从 Python 动态的加载.

将定制的 operator 实现放在路径 lib/ops/ .

参考示例 tests/test_zero_even_op.py,关于如何从 Python 中加载定制的 operators.

  • 编译定制的 operators 库:

    cd $DETECTRON/lib && make ops
    • 1
  • 测试定制的 operator 是否成功:

    python2 $DETECTRON/tests/test_zero_even_op.py
    • 1

4. Detectron Docker 镜像

Detectron Dockerfile

  • 首先,创建 Caffe2 镜像:

    cd /path/to/caffe2/docker/ubuntu-16.04-cuda8-cudnn6-all-options
    
    # Use the latest Caffe2 master
    
    sed -i -e 's/ --branch v0.8.1//g' Dockerfile
    docker build -t caffe2:cuda8-cudnn6-all-options .
  • 然后,创建 Detecron 镜像:

    cd $DETECTRON/docker
    docker build -t detectron:c2-cuda8-cudnn6 .
  • 运行 Detectron 镜像,如 BatchPermutationOp test

    nvidia-docker run --rm -it detectron:c2-cuda8-cudnn6 python2 tests/test_batch_permutation_op.py
    • 1

5. 问题及解决

5.1 Caffe2 Operator Profiling

profiling 为 Caffe2 提供了有用的 operators benchmarking 和 debugging 工具,示例可参考 BatchPermutationOp test.

Profiling 不是默认编译的,可以在运行 Caffe2 CMake 时设置 -DUSE_PROF=ON 以开启.

5.2 CMake Cannot Find CUDA and cuDNN

编译 Caffe2 时如果找不到 CUDA 和 cuDNN 路径,可以在编译时指定 CMake 的 CUDA 和 cuDNN 路径:

cmake .. \
  # insert your Caffe2 CMake flags here
  -DCUDA_TOOLKIT_ROOT_DIR=/path/to/cuda/toolkit/dir \
  -DCUDNN_ROOT_DIR=/path/to/cudnn/root/dir

类似地,编译定制的 Detectron operators 时可以运行:

cd $DETECTRON/lib
mkdir -p build && cd build
cmake .. \
  -DCUDA_TOOLKIT_ROOT_DIR=/path/to/cuda/toolkit/dir \
  -DCUDNN_ROOT_DIR=/path/to/cudnn/root/dir
make

可以采用相同的方式,处理机器上同时有多个 CUDA 和 cuDNN 版本时的情况.

5.3 Protobuf Errors

Caffe2 使用 protobuf 作为序列化格式,并需要其版本高于 3.2.0.

如果 protobuf 版本较低,可以从 Caffe2 的 protobuf 子模块编译 protobuf,来代替旧版本.

  • 编译 Caffe2 protobuf 子模块:

    
    # CAFFE2=/path/to/caffe2
    
    cd $CAFFE2/third_party/protobuf/cmake
    mkdir -p build && cd build
    cmake .. \
    -DCMAKE_INSTALL_PREFIX=$HOME/c2_tp_protobuf \
    -Dprotobuf_BUILD_TESTS=OFF \
    -DCMAKE_CXX_FLAGS="-fPIC"
    make install
  • 指定 Caffe2 CMake 到新版本的 protobuf:

    cmake .. \
    
    # insert your Caffe2 CMake flags here
    
    -DPROTOBUF_PROTOC_EXECUTABLE=$HOME/c2_tp_protobuf/bin/protoc \
    -DPROTOBUF_INCLUDE_DIR=$HOME/c2_tp_protobuf/include \
    -DPROTOBUF_LIBRARY=$HOME/c2_tp_protobuf/lib64/libprotobuf.a

5.4 Caffe2 Python Binaries

如果在编译 Caffe2 Python binaries时,不能找到需要的 Python 路径,如 virtualenv 环境,可以指定 Caffe2 CMake 到 python library 库路径和 include 路径:

cmake .. \
  # insert your Caffe2 CMake flags here
  -DPYTHON_LIBRARY=$(python2 -c "from distutils import sysconfig; print(sysconfig.get_python_lib())") \
  -DPYTHON_INCLUDE_DIR=$(python2 -c "from distutils import sysconfig; print(sysconfig.get_python_inc())")

5.5 Caffe2 with NNPACK Build

Detectron 不需要 Caffe2 的 NNPACK 支持.

如果编译时遇到 NNPACK 相关的问题,可以关闭 NNPACK,设置 CMake -DUSE_NNPACK=OFF .

5.6 Caffe2 with OpenCV Build

Detectron 也不需要 Caffe2 的 OpenCV 的支持,也可以关闭,设置 CMake -DUSE_OPENCV=OFF.

5.7 COCO API Undefined Symbol Error

如果遇到由于 undefined symbol 原因,出现的 COCO API import error,类似与 cocoapi - Import error,则需要确定 Python 版本是够混淆,如 both system and conda numpy installed 则会导致该问题.

5.8 CMake Cannot Find Caffe2

如果在编译定制的 operators 时,CMake 出现不能找到 Caffe2 package 的问题,则需要确定在 Caffe2 安装时运行了 make install.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值