1. 程式人生 > >ubuntu16.04下Detectron+caffe2(Pytorch)安裝配置過程

ubuntu16.04下Detectron+caffe2(Pytorch)安裝配置過程

#-------------------------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())'

    正常結果如下:

[email protected]:~$ python2 -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"
Success
[email protected]:~$ 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:

[email protected]:~/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格式

相關推薦

no