Windows下Mask-RCNN Benchmark安装

Facebook官方提供的基于PyTorch的Mask R-CNN实现——MaskRCNN-Benchmark是一个较为优秀的实现范例,不过其在Windows下的安装仍需要不少调整。以下列出本人的安装过程,为未来重新安装,环境调整提供例子。

本人采用的环境是win10,cuda10.1, cudnn 7.5的组合,conda采用了miniconda的python3.7版本。

首先,如下是maskrcnn-benchmark提供的官方安装教程。

# first, make sure that your conda is setup properly with the right environment
# for that, check that `which conda`, `which pip` and `which python` points to the
# right path. From a clean conda env, this is what you need to do
conda create --name maskrcnn_benchmark
conda activate maskrcnn_benchmark
# this installs the right pip and dependencies for the fresh python
conda install ipython
# maskrcnn_benchmark and coco api dependencies
pip install ninja yacs cython matplotlib tqdm opencv-python
# follow PyTorch installation in https://pytorch.org/get-started/locally/
# we give the instructions for CUDA 9.0
conda install -c pytorch pytorch-nightly torchvision cudatoolkit=9.0
export INSTALL_DIR=$PWD
# install pycocotools
cd $INSTALL_DIR
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
python setup.py build_ext install
# install apex
cd $INSTALL_DIR
git clone https://github.com/NVIDIA/apex.git
cd apex
python setup.py install --cuda_ext --cpp_ext
# install PyTorch Detection
cd $INSTALL_DIR
git clone https://github.com/facebookresearch/maskrcnn-benchmark.git
cd maskrcnn-benchmark
# the following will install the lib with symbolic links, so that you can modify
# the files if you want and won't need to re-build it
python setup.py build develop
unset INSTALL_DIR

这里我们可以完全安全的执行如下步骤

conda create --name maskrcnn_benchmark
conda activate maskrcnn_benchmark
conda install ipython
pip install ninja yacs cython matplotlib tqdm opencv-python

但是,在安装pytorch时,本人选择了最新(本文写于2019-05-06)的PyTorch1.1版本。由于其发布于maskrcnn-benchmark教程写作后,因此有理由相信其版本内已添加了此前nightly更新的内容。因此采用了如下命令:

conda install pytorch torchvision cudatoolkit=10.0 -c pytorch

这里开始,给出的export unset相关命令在windows命令行本身似乎是不支持的,但是本质上就是在进行目录的切入切出,不用完全模仿。
CocoAPI官方没有支持windows的计划,这里我们采用第三方修改过的coco api实现。

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

Apex的安装直接依据官方教程就可以,要准备好自己的编译器就行,我这里用的是Visual Studio 2017 Win64的编译器MSVC 14.15.26726。
最后编译maskrcnn-benchmark的C语言库,使用

python setup.py build develop

将代码中的c++/cuda代码进行编译,并安装一个虚拟链接、可直接原地修改的库。
如确定不需要对代码进行修改,也可以考虑采用如下命令:

python setup.py install

或纳入包管理

pip install -e .

运行以上代码,在编译ROIAlign_cuda.cu和ROIPool_cuda.cu时,会报如下的错误:

/maskrcnn-benchmark/maskrcnn_benchmark/csrc/cuda/ROIAlign_cuda.cu(275): error: no instance of function template "THCCeilDiv" matches the argument list argument types are: (long long, long)

以ROIAlign_cuda.cu中出现的问题为例,这里网上很多人的修改方法如下:

/*  added function */
int  ceil_div(int a, int b){ 
    return  (a + b - 1) / b; 
}
...

/* replace the lines with THCCeilDiv, there are 2 palces in each file */
dim3  grid(std::min(ceil_div((int)output_size, 512), 4096));
// dim3  grid(std::min(THCCeilDiv(output_size, 512L), 4096L));

我个人思考了一下的修改如下,我认为相比之下更简洁且出现精度损失的可能性更小,不知其中是否还有其他我没有考虑到的点。

dim3  grid(std::min(THCCeilDiv((long)output_size, 512L), 4096L));
// dim3  grid(std::min(THCCeilDiv(output_size, 512L), 4096L));

你可能感兴趣的:(Windows下Mask-RCNN Benchmark安装)