#-------------------------ZR----------------------------
#
# detectron配置
#
###########!未经同意,不得转载!##############
一、caffe2(Pytorch)
(如果有anaconda或者此处源码编译不成功时,可以参考这篇)
detectron是基于caffe2的,所以要先配置编译caffe2.但是目前caffe2已经并入了Pytorch,所以先下载Pytorch.
1、安装依赖项:
$ sudo apt-get update
$ sudo apt-get install -y --no-install-recommends \
build-essential \
cmake \
git \
libgoogle-glog-dev \
libgtest-dev \
libiomp-dev \
libleveldb-dev \
liblmdb-dev \
libopencv-dev \
libopenmpi-dev \
libsnappy-dev \
libprotobuf-dev \
openmpi-bin \
openmpi-doc \
protobuf-compiler \
python-dev \
python-pip
$ sudo pip install \
future \
numpy \
protobuf
# for Ubuntu 16.04
$ sudo apt-get install -y --no-install-recommends libgflags-dev
注:后续如果有出现依赖性的问题或者某些版本不匹配的问题,安装或更改即可。
2、安装CUDA+CUDNN
由于我之前已经安装过CUDA8.0+cuDNN5.1,所以跳过了这一步。caffe2官网上说也支持 cudnn6。
3、Pytorch下载
# Clone Caffe2's source code from our Github repository
$ git clone --recursive https://github.com/pytorch/pytorch.git && cd pytorch
$ git submodule update --init
4、编译安装
# Create a directory to put Caffe2's build files in
$ mkdir build && cd build
# Configure Caffe2's build
# This looks for packages on your machine and figures out which functionality
# to include in the Caffe2 installation. The output of this command is very
# useful in debugging.
$ cmake ..
# Compile, link, and install Caffe2
$ sudo make install
5、验证安装
# 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())'
正常结果如下:
zr@zr:~$ python2 -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"
Success
zr@zr:~$ python2 -c 'from caffe2.python import workspace; print(workspace.NumCudaDevices())'
1
注意:如果此处在build目录里验证安装成功,而在根目录下验证失败,说明只是环境变量问题,添加环境变量路径。
$ gedit ~/.bashrc
在最后添加:
# pytorch caffe2
export PYTHONPATH=/usr/local:$PYTHONPATH
export PYTHONPATH=~/home/zr/pytorch/build:$PYTHONPATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
然后使其生效
$ source ~/.bashrc
二、COCO API安装
$ git clone https://github.com/cocodataset/cocoapi.git
$ cd 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
三、Detectron安装
1、Detectron下载
$ git clone https://github.com/facebookresearch/detectron.git
若子模块未完全下载,输入以下命令直到全部下载成功:
$ git submodule update --init --recursive
2、设置Python模块
$ cd detectron
$ make
3、测试Detectron安装
$ python2 detectron/tests/test_spatial_narrow_as_op.py
正常情况,出现OK:
zr@zr:~/detectron$ python2 detectron/tests/test_spatial_narrow_as_op.py
E0719 15:51:21.806138 21225 init_intrinsics_check.cc:43] CPU feature avx is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
E0719 15:51:21.806161 21225 init_intrinsics_check.cc:43] CPU feature avx2 is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
E0719 15:51:21.806165 21225 init_intrinsics_check.cc:43] CPU feature fma is present on your machine, but the Caffe2 binary is not compiled with it. It means you may not get the full speed of your CPU.
Found Detectron ops lib: /usr/local/lib/libcaffe2_detectron_ops_gpu.so
...
----------------------------------------------------------------------
Ran 3 tests in 0.995s
OK
四、基于预训练模型简单测试
运行 infer_simple.py 即可进行测试
$ python2 tools/infer_simple.py \
--cfg configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml \
--output-dir tmp/detectron-visualizations \
--image-ext jpg \
--wts https://s3-us-west-2.amazonaws.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl \
demo
注:
根据--wts参数指定的 URL 自动下载模型,此处可以先将model_final.pkl下载下来,然后将其改为存放路径即可
根据--output-dir参数指定的路径,输出检测的可视化结果,PDF格式