Detectronをうごかしてみた

動かすまで悪戦苦闘したので記録。
参考になれば幸いです。


公式URL：https://github.com/facebookresearch/Detectron

![33823288584_1d21cf0a26_k_example_output.jpg](https://qiita-image-store.s3.amazonaws.com/0/245670/b5f59ee2-af28-697c-f895-5a103e763d88.jpeg)




【必須環境】
NVIDIA GPU
Linux
Python2
Caffe2
COCO API

【My環境】
ノートPC: ALIENWARE 15 R13
OS: Ubuntu 16.04 LTS(Windowsマシンでデュアルブート)
MEMORY: 16GB
GPU: GeForce GTX 1070
CUDA: 8.0
Python: 2.7(Anaconda)

# 0. AnacondaでPython2の環境を作成

公式ではPython2を使ってるので以下のリンクからAnaconda 2をダウンロードします
https://www.anaconda.com/download/#linux

シェルを実行

```
$ bash Anaconda2-5.1.0-Linux-x86_64.sh
```

Enterキーを押す

```
Welcome to Anaconda2 5.1.0

In order to continue the installation process, please review the license
agreement.
Please, press ENTER to continue
>>> 
```


ライセンスの説明が表示されます。Enterキーを押すと下にスクロールされます

```
===================================
Anaconda End User License Agreement
===================================

Copyright 2015, Anaconda, Inc.

All rights reserved under the 3-clause BSD License:

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the f
ollowing conditions are met:

  * Redistributions of source code must retain the above copyright notice, this list of conditions and the followi
ng disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the foll
owing disclaimer in the documentation and/or other materials provided with the distribution.
  * Neither the name of Anaconda, Inc. ("Anaconda, Inc.") nor the names of its contributors may be used to endorse
 or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 DISCLAIMED. IN NO EVENT SHALL ANACONDA, INC. BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, S
TRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, E
VEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Notice of Third Party Software Licenses
=======================================

Anaconda Distribution contains open source software packages from third parties. These are available on an "as is"
 basis and subject to their individual license agreements. These licenses are available in Anaconda Distribution o
r at http://docs.anaconda.com/anaconda/pkg-docs. Any binary packages of these third party tools you obtain via Ana
conda Distribution are subject to their individual licenses as well as the Anaconda license. Anaconda, Inc. reserv
es the right to change which third party tools are provided in Anaconda Distribution.

In particular, Anaconda Distribution contains re-distributable, run-time, shared-library files from the Intel(TM) 
Math Kernel Library ("MKL binaries"). You are specifically authorized to use the MKL binaries with your installati
on of Anaconda Distribution. You are also authorized to redistribute the MKL binaries with Anaconda Distribution o
r in the conda package that contains them. Use and redistribution of the MKL binaries are subject to the licensing
 terms located at https://software.intel.com/en-us/license/intel-simplified-software-license. If needed, instructi
ons for removing the MKL binaries after installation of Anaconda Distribution are available at http://www.anaconda
.com.
```

「yes」とタイプしEnterキーを押して同意しましょう。

```
Do you accept the license terms? [yes|no]
[no] >>> 
```

インストールする場所の確認です。問題なければEnterキーを押します

```
Anaconda2 will now be installed into this location:
/home/Users/anaconda2

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/home/Users/anaconda2] >>>
```

noをタイプしてEnterキーを押す

```
WARNING:
    You currently have a PYTHONPATH environment variable set. This may cause
    unexpected behavior when running the Python interpreter in Anaconda2.
    For best results, please verify that your PYTHONPATH only points to
    directories of packages that are compatible with the Python interpreter
    in Anaconda2: /home/Users/anaconda2
Do you wish the installer to prepend the Anaconda2 install location
to PATH in your /home/Users/.bashrc ? [yes|no]
[no] >>> no

```

```
You may wish to edit your .bashrc to prepend the Anaconda2 install location to PATH:

export PATH=/home/Users/anaconda2/bin:$PATH

Thank you for installing Anaconda2!
```

「yes」とタイプして、一応VSCodeをインストールしておきます。

```
===========================================================================

Anaconda is partnered with Microsoft! Microsoft VSCode is a streamlined
code editor with support for development operations like debugging, task
running and version control.

To install Visual Studio Code, you will need:
  - Administrator Privileges
  - Internet connectivity

Visual Studio Code License: https://code.visualstudio.com/license

Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>>
```

ホームディレクトリに移動

```
$ cd ~/
```

anaconda2が作成されているか確認

```
$ ls -la
```

さきほどPYTHONPATHのところで「no」と選択したので、手動でパスを追加します。

```
$ export PATH=~/anaconda2/bin:$PATH
```

「detectron」という名前でpython2の環境を構築。名前は自由に決めて大丈夫です。

```
cd anaconda2
```

```
$ conda create -n detectron python=2.7
```

「y」をタイプしてEnterキーを押す

```
Solving environment: done

## Package Plan ##

  environment location: /home/Users/anaconda2/envs/detectron

  added / updated specs: 
    - python=2.7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    ca-certificates-2018.03.07 |                0         124 KB
    libgcc-ng-7.2.0            |       hdf63c60_3         6.1 MB
    pip-9.0.3                  |           py27_0         2.4 MB
    wheel-0.31.0               |           py27_0          61 KB
    python-2.7.14              |      h1571d57_31        11.8 MB
    setuptools-39.0.1          |           py27_0         582 KB
    libstdcxx-ng-7.2.0         |       hdf63c60_3         2.5 MB
    ------------------------------------------------------------
                                           Total:        23.5 MB

The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0      
    certifi:         2018.1.18-py27_0  
    libedit:         3.1-heed3624_0    
    libffi:          3.2.1-hd88cf55_4  
    libgcc-ng:       7.2.0-hdf63c60_3  
    libstdcxx-ng:    7.2.0-hdf63c60_3  
    ncurses:         6.0-h9df7e31_2    
    openssl:         1.0.2o-h20670df_0 
    pip:             9.0.3-py27_0      
    python:          2.7.14-h1571d57_31
    readline:        7.0-ha6073c6_4    
    setuptools:      39.0.1-py27_0     
    sqlite:          3.22.0-h1bed415_0 
    tk:              8.6.7-hc745277_3  
    wheel:           0.31.0-py27_0     
    zlib:            1.2.11-ha838bed_2 

Proceed ([y]/n)?
```

成功しました！

```
ca-certificates 2018.03.07: ############################################################################## | 100% 
libgcc-ng 7.2.0: ######################################################################################### | 100% 
pip 9.0.3: ############################################################################################### | 100% 
wheel 0.31.0: ############################################################################################ | 100% 
python 2.7.14: ########################################################################################### | 100% 
setuptools 39.0.1: ####################################################################################### | 100% 
libstdcxx-ng 7.2.0: ###################################################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use:
# > source activate detectron
#
# To deactivate an active environment, use:
# > source deactivate
#

```

作成した環境をアクティベート

```
$ source activate detectron
```


# 1. Caffe2

私はCUDA 8、CuDNN 7を利用しているため、以下のパッケージをインストールします

Caffe2: https://caffe2.ai/docs/getting-started.html?platform=ubuntu&configuration=prebuilt


```
$ conda install -c caffe2 caffe2-cuda8.0-cudnn7
```

```
Solving environment: done

## Package Plan ##

  environment location: /home/Users/anaconda2/envs/detectron

  added / updated specs: 
    - caffe2-cuda8.0-cudnn7


The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    future-0.16.0              |           py27_1         641 KB
    ffmpeg-3.4                 |       h7264315_0         8.0 MB
    numpy-1.14.2               |   py27hdbf6ddf_1         4.1 MB
    libxcb-1.13                |       h1bed415_0         501 KB
    gflags-2.2.1               |       hf484d3e_0         180 KB
    lmdb-0.9.21                |       hf484d3e_1         677 KB
    leveldb-1.20               |       hf484d3e_1         353 KB
    libprotobuf-3.5.2          |       h6f1eeef_0         4.2 MB
    mkl_fft-1.0.1              |   py27h3010b51_0         137 KB
    snappy-1.1.7               |       hbae5bb6_3          38 KB
    mkl_random-1.0.1           |   py27h629b387_0         361 KB
    libgfortran-ng-7.2.0       |       hdf63c60_3         1.2 MB
    protobuf-3.5.2             |   py27hf484d3e_0         603 KB
    intel-openmp-2018.0.0      |                8         620 KB
    opencv-3.3.1               |   py27hdcf4849_0        38.9 MB
    libopus-1.2.1              |       hb9ed12e_0         382 KB
    fontconfig-2.12.6          |       h49f89f6_0         283 KB
    harfbuzz-1.7.6             |       hc5b324e_0         854 KB
    glog-0.3.5                 |       hf484d3e_1         158 KB
    libvpx-1.6.1               |       h888fd40_0         2.3 MB
    mkl-2018.0.2               |                1       205.2 MB
    jasper-1.900.1             |       hd497a04_4         279 KB
    caffe2-cuda8.0-cudnn7-0.8.dev|   py27h7cd0f3b_0        24.1 MB  caffe2
    libxml2-2.9.8              |       hf84eae3_0         2.0 MB
    ------------------------------------------------------------
                                           Total:       295.9 MB

The following NEW packages will be INSTALLED:

    bzip2:                 1.0.6-h9a117a8_4             
    caffe2-cuda8.0-cudnn7: 0.8.dev-py27h7cd0f3b_0 caffe2
    cairo:                 1.14.12-h77bcde2_0           
    ffmpeg:                3.4-h7264315_0               
    fontconfig:            2.12.6-h49f89f6_0            
    freetype:              2.8-hab7d2ae_1               
    future:                0.16.0-py27_1                
    gflags:                2.2.1-hf484d3e_0             
    glib:                  2.53.6-h5d9569c_2            
    glog:                  0.3.5-hf484d3e_1             
    graphite2:             1.3.10-hf63cedd_1            
    harfbuzz:              1.7.6-hc5b324e_0             
    hdf5:                  1.10.1-h9caa474_1            
    icu:                   58.2-h9c2bf20_1              
    intel-openmp:          2018.0.0-8                   
    jasper:                1.900.1-hd497a04_4           
    jpeg:                  9b-h024ee3a_2                
    leveldb:               1.20-hf484d3e_1              
    libgfortran-ng:        7.2.0-hdf63c60_3             
    libopus:               1.2.1-hb9ed12e_0             
    libpng:                1.6.34-hb9fc6fc_0            
    libprotobuf:           3.5.2-h6f1eeef_0             
    libtiff:               4.0.9-h28f6b97_0             
    libvpx:                1.6.1-h888fd40_0             
    libxcb:                1.13-h1bed415_0              
    libxml2:               2.9.8-hf84eae3_0             
    lmdb:                  0.9.21-hf484d3e_1            
    mkl:                   2018.0.2-1                   
    mkl_fft:               1.0.1-py27h3010b51_0         
    mkl_random:            1.0.1-py27h629b387_0         
    numpy:                 1.14.2-py27hdbf6ddf_1        
    opencv:                3.3.1-py27hdcf4849_0         
    pcre:                  8.41-hc27e229_1              
    pixman:                0.34.0-hceecf20_3            
    protobuf:              3.5.2-py27hf484d3e_0         
    six:                   1.11.0-py27h5f960f1_1        
    snappy:                1.1.7-hbae5bb6_3             
    xz:                    5.2.3-h55aa19d_2             

Proceed ([y]/n)?
```

「y」をタイプしてEnterキーを押す

```
Downloading and Extracting Packages
future 0.16.0: ########################################################################################### | 100% 
ffmpeg 3.4: ############################################################################################## | 100% 
numpy 1.14.2: ############################################################################################ | 100% 
libxcb 1.13: ############################################################################################# | 100% 
gflags 2.2.1: ############################################################################################ | 100% 
lmdb 0.9.21: ############################################################################################# | 100% 
leveldb 1.20: ############################################################################################ | 100% 
libprotobuf 3.5.2: ####################################################################################### | 100% 
mkl_fft 1.0.1: ########################################################################################### | 100% 
snappy 1.1.7: ############################################################################################ | 100% 
mkl_random 1.0.1: ######################################################################################## | 100% 
libgfortran-ng 7.2.0: #################################################################################### | 100% 
protobuf 3.5.2: ########################################################################################## | 100% 
intel-openmp 2018.0.0: ################################################################################### | 100% 
opencv 3.3.1: ############################################################################################ | 100% 
libopus 1.2.1: ########################################################################################### | 100% 
fontconfig 2.12.6: ####################################################################################### | 100% 
harfbuzz 1.7.6: ########################################################################################## | 100% 
glog 0.3.5: ############################################################################################## | 100% 
libvpx 1.6.1: ############################################################################################ | 100% 
mkl 2018.0.2: ############################################################################################ | 100% 
jasper 1.900.1: ########################################################################################## | 100% 
caffe2-cuda8.0-cudnn7 0.8.dev: ########################################################################### | 100% 
libxml2 2.9.8: ########################################################################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

```

Caffe2がちゃんとインストールされてるかチェックします

```
$ python2 -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"
```

Successと表示されればOK!

```
Success
```


利用できるGPUの数をチェックします

```
$ python2 -c 'from caffe2.python import workspace; print(workspace.NumCudaDevices())'
```

私の環境では1つのGPUであるGTX1070しかないため「1」と表示されます

```
1
```

# 2. 依存関係に必要なものをインストール

```
$ pip install numpy>=1.13 pyyaml>=3.12 matplotlib opencv-python>=3.2 setuptools Cython mock scipy
```


COCO APIをクローン

```
$ git clone https://github.com/cocodataset/cocoapi.git
```

移動

```
$ cd cocoapi/PythonAPI
```

インストール

```
$ make install
```

```
# install pycocotools to the Python site-packages
python setup.py build_ext install
Compiling pycocotools/_mask.pyx because it changed.
[1/1] Cythonizing pycocotools/_mask.pyx
running build_ext
building 'pycocotools._mask' extension
creating build
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/pycocotools
creating build/common
gcc -pthread -B /home/Users/anaconda2/envs/detectron/compiler_compat -Wl,--sysroot=/ -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/numpy/core/include -I../common -I/home/Users/anaconda2/envs/detectron/include/python2.7 -c pycocotools/_mask.c -o build/temp.linux-x86_64-2.7/pycocotools/_mask.o -Wno-cpp -Wno-unused-function -std=c99
gcc -pthread -B /home/Users/anaconda2/envs/detectron/compiler_compat -Wl,--sysroot=/ -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/numpy/core/include -I../common -I/home/Users/anaconda2/envs/detectron/include/python2.7 -c ../common/maskApi.c -o build/temp.linux-x86_64-2.7/../common/maskApi.o -Wno-cpp -Wno-unused-function -std=c99
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/pycocotools
gcc -pthread -shared -B /home/Users/anaconda2/envs/detectron/compiler_compat -L/home/Users/anaconda2/envs/detectron/lib -Wl,-rpath=/home/Users/anaconda2/envs/detectron/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-2.7/pycocotools/_mask.o build/temp.linux-x86_64-2.7/../common/maskApi.o -L/home/Users/anaconda2/envs/detectron/lib -lpython2.7 -o build/lib.linux-x86_64-2.7/pycocotools/_mask.so
running install
running build
running build_py
copying pycocotools/cocoeval.py -> build/lib.linux-x86_64-2.7/pycocotools
copying pycocotools/__init__.py -> build/lib.linux-x86_64-2.7/pycocotools
copying pycocotools/coco.py -> build/lib.linux-x86_64-2.7/pycocotools
copying pycocotools/mask.py -> build/lib.linux-x86_64-2.7/pycocotools
running install_lib
creating /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools
copying build/lib.linux-x86_64-2.7/pycocotools/cocoeval.py -> /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools
copying build/lib.linux-x86_64-2.7/pycocotools/__init__.py -> /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools
copying build/lib.linux-x86_64-2.7/pycocotools/_mask.so -> /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools
copying build/lib.linux-x86_64-2.7/pycocotools/coco.py -> /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools
copying build/lib.linux-x86_64-2.7/pycocotools/mask.py -> /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools
byte-compiling /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools/cocoeval.py to cocoeval.pyc
byte-compiling /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools/__init__.py to __init__.pyc
byte-compiling /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools/coco.py to coco.pyc
byte-compiling /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools/mask.py to mask.pyc
running install_egg_info
Writing /home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/pycocotools-2.0-py2.7.egg-info
rm -rf build
```

# 3. Detectron

クローン

```
$ git clone https://github.com/facebookresearch/detectron
```

```
Cloning into 'detectron'...
remote: Counting objects: 610, done.
remote: Compressing objects: 100% (24/24), done.
remote: Total 610 (delta 12), reused 14 (delta 7), pack-reused 579
Receiving objects: 100% (610/610), 3.77 MiB | 285.00 KiB/s, done.
Resolving deltas: 100% (346/346), done.
```

移動

```
$ cd detectron/lib
```

Make

```
$ make
```

```
python2 setup.py develop --user
Compiling utils/cython_bbox.pyx because it changed.
Compiling utils/cython_nms.pyx because it changed.
[1/2] Cythonizing utils/cython_bbox.pyx
[2/2] Cythonizing utils/cython_nms.pyx
running develop
running egg_info
creating Detectron.egg-info
writing Detectron.egg-info/PKG-INFO
writing top-level names to Detectron.egg-info/top_level.txt
writing dependency_links to Detectron.egg-info/dependency_links.txt
writing manifest file 'Detectron.egg-info/SOURCES.txt'
reading manifest file 'Detectron.egg-info/SOURCES.txt'
writing manifest file 'Detectron.egg-info/SOURCES.txt'
running build_ext
building 'utils.cython_bbox' extension
creating build
creating build/temp.linux-x86_64-2.7
creating build/temp.linux-x86_64-2.7/utils
gcc -pthread -B /home/Users/anaconda2/envs/detectron/compiler_compat -Wl,--sysroot=/ -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/numpy/core/include -I/home/Users/anaconda2/envs/detectron/include/python2.7 -c utils/cython_bbox.c -o build/temp.linux-x86_64-2.7/utils/cython_bbox.o -Wno-cpp
creating build/lib.linux-x86_64-2.7
creating build/lib.linux-x86_64-2.7/utils
gcc -pthread -shared -B /home/Users/anaconda2/envs/detectron/compiler_compat -L/home/Users/anaconda2/envs/detectron/lib -Wl,-rpath=/home/Users/anaconda2/envs/detectron/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-2.7/utils/cython_bbox.o -L/home/Users/anaconda2/envs/detectron/lib -lpython2.7 -o build/lib.linux-x86_64-2.7/utils/cython_bbox.so
building 'utils.cython_nms' extension
gcc -pthread -B /home/Users/anaconda2/envs/detectron/compiler_compat -Wl,--sysroot=/ -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/Users/anaconda2/envs/detectron/lib/python2.7/site-packages/numpy/core/include -I/home/Users/anaconda2/envs/detectron/include/python2.7 -c utils/cython_nms.c -o build/temp.linux-x86_64-2.7/utils/cython_nms.o -Wno-cpp
gcc -pthread -shared -B /home/Users/anaconda2/envs/detectron/compiler_compat -L/home/Users/anaconda2/envs/detectron/lib -Wl,-rpath=/home/Users/anaconda2/envs/detectron/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-2.7/utils/cython_nms.o -L/home/Users/anaconda2/envs/detectron/lib -lpython2.7 -o build/lib.linux-x86_64-2.7/utils/cython_nms.so
copying build/lib.linux-x86_64-2.7/utils/cython_bbox.so -> utils
copying build/lib.linux-x86_64-2.7/utils/cython_nms.so -> utils
Creating /home/Users/.local/lib/python2.7/site-packages/Detectron.egg-link (link to .)
Removing Detectron 0.0.0 from easy-install.pth file
Adding Detectron 0.0.0 to easy-install.pth file

Installed /home/Users/workspace/GitHub/detectron/lib
Processing dependencies for Detectron==0.0.0
Finished processing dependencies for Detectron==0.0.0
```

移動

```
$ cd ..
```

テスト

```
$ python tests/test_spatial_narrow_as_op.py
```

```
No handlers could be found for logger "caffe2.python.net_drawer"
net_drawer will not run correctly. Please install the correct dependencies.
E0408 07:22:00.859783  4508 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.
E0408 07:22:00.859844  4508 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.
E0408 07:22:00.859859  4508 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 2.761s

OK
```

いざ、Detectronを実行...

```
$ python 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
```



Error発生orz
以下のエラーが出た場合、PYTHONPATHが影響している可能性があります。

```
Traceback (most recent call last):
  File "tools/infer_simple.py", line 43, in <module>
    import core.test_engine as infer_engine
  File "/home/Users/workspace/GitHub/detectron/lib/core/test_engine.py", line 35, in <module>
    from core.rpn_generator import generate_rpn_on_dataset
  File "/home/Users/workspace/GitHub/detectron/lib/core/rpn_generator.py", line 42, in <module>
    from datasets import task_evaluation
ImportError: cannot import name task_evaluation
```

チェックしてみましょう。

```
$ echo $PYTHONPATH
```

TensorFlowのObject Detection APIで設定したパスがありました。(Object Detection APIについては後日記事にします)

```
/home/Users/workspace/GitHub/tensorflow:/home/Users/workspace/GitHub/tensorflow/models/research/slim
```

```
$ vim ~/.bashrc
```

exportの箇所をコメントアウトします

Before

```
# Object Detection API
export PYTHONPATH=$PYTHONPATH:/home/Users/workspace/GitHub/tensorflow
export PYTHONPATH=$PYTHONPATH:/home/Users/workspace/GitHub/tensorflow/models/research/slim
```

After
コメントアウトして保存します

```
# Object Detection API
# export PYTHONPATH=$PYTHONPATH:/home/Users/workspace/GitHub/tensorflow
# export PYTHONPATH=$PYTHONPATH:/home/Users/workspace/GitHub/tensorflow/models/research/slim
```

```
$ source ~/.bashrc
```

もう一度作成したPythonの環境に入ります

```
$ source activate detectron
```

改めて実行

```
$ python 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
```

おお！

```
Found Detectron ops lib: /usr/local/lib/libcaffe2_detectron_ops_gpu.so
E0408 07:59:06.081162  6595 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.
E0408 07:59:06.081207  6595 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.
E0408 07:59:06.081216  6595 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.
WARNING cnn.py:  25: [====DEPRECATE WARNING====]: you are creating an object from CNNModelHelper class which will be deprecated soon. Please use ModelHelper object with brew module. For more information, please refer to caffe2.ai and python/brew.py, python/brew_test.py for more information.
INFO net.py:  57: Loading weights from: /tmp/detectron-download-cache/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
I0408 07:59:18.816251  6595 net_dag_utils.cc:102] Operator graph pruning prior to chain compute took: 0.000639494 secs
I0408 07:59:18.817175  6595 net_dag.cc:46] Number of parallel execution chains 63 Number of operators = 402
I0408 07:59:18.865633  6595 net_dag_utils.cc:102] Operator graph pruning prior to chain compute took: 0.000171562 secs
I0408 07:59:18.865896  6595 net_dag.cc:46] Number of parallel execution chains 30 Number of operators = 358
I0408 07:59:18.867718  6595 net_dag_utils.cc:102] Operator graph pruning prior to chain compute took: 1.2368e-05 secs
I0408 07:59:18.867754  6595 net_dag.cc:46] Number of parallel execution chains 5 Number of operators = 18
INFO infer_simple.py: 112: Processing demo/18124840932_e42b3e377c_k.jpg -> /tmp/detectron-visualizations/18124840932_e42b3e377c_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 5.019s
INFO infer_simple.py: 122:  | im_detect_bbox: 4.956s
INFO infer_simple.py: 122:  | misc_mask: 0.028s
INFO infer_simple.py: 122:  | im_detect_mask: 0.031s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 125:  \ Note: inference on the first image will be slower than the rest (caches and auto-tuning need to warm up)
INFO infer_simple.py: 112: Processing demo/17790319373_bd19b24cfc_k.jpg -> /tmp/detectron-visualizations/17790319373_bd19b24cfc_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.411s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.319s
INFO infer_simple.py: 122:  | misc_mask: 0.056s
INFO infer_simple.py: 122:  | im_detect_mask: 0.032s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/15673749081_767a7fa63a_k.jpg -> /tmp/detectron-visualizations/15673749081_767a7fa63a_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.373s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.172s
INFO infer_simple.py: 122:  | misc_mask: 0.062s
INFO infer_simple.py: 122:  | im_detect_mask: 0.136s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/24274813513_0cfd2ce6d0_k.jpg -> /tmp/detectron-visualizations/24274813513_0cfd2ce6d0_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.201s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.166s
INFO infer_simple.py: 122:  | misc_mask: 0.021s
INFO infer_simple.py: 122:  | im_detect_mask: 0.012s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/19064748793_bb942deea1_k.jpg -> /tmp/detectron-visualizations/19064748793_bb942deea1_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.278s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.168s
INFO infer_simple.py: 122:  | misc_mask: 0.063s
INFO infer_simple.py: 122:  | im_detect_mask: 0.044s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/16004479832_a748d55f21_k.jpg -> /tmp/detectron-visualizations/16004479832_a748d55f21_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.193s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.166s
INFO infer_simple.py: 122:  | misc_mask: 0.016s
INFO infer_simple.py: 122:  | im_detect_mask: 0.007s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/33887522274_eebd074106_k.jpg -> /tmp/detectron-visualizations/33887522274_eebd074106_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.191s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.169s
INFO infer_simple.py: 122:  | misc_mask: 0.012s
INFO infer_simple.py: 122:  | im_detect_mask: 0.007s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/33823288584_1d21cf0a26_k.jpg -> /tmp/detectron-visualizations/33823288584_1d21cf0a26_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.374s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.267s
INFO infer_simple.py: 122:  | misc_mask: 0.073s
INFO infer_simple.py: 122:  | im_detect_mask: 0.032s
INFO infer_simple.py: 122:  | misc_bbox: 0.003s
INFO infer_simple.py: 112: Processing demo/34501842524_3c858b3080_k.jpg -> /tmp/detectron-visualizations/34501842524_3c858b3080_k.jpg.pdf
INFO infer_simple.py: 120: Inference time: 0.186s
INFO infer_simple.py: 122:  | im_detect_bbox: 0.160s
INFO infer_simple.py: 122:  | misc_mask: 0.015s
INFO infer_simple.py: 122:  | im_detect_mask: 0.007s
INFO infer_simple.py: 122:  | misc_bbox: 0.002s
(detectron) Users@Alienware-15-R3:~/workspace/GitHub/detectron$ cd /tmp/detectron-visualizations/
(detectron) Users@Alienware-15-R3:/tmp/detectron-visualizations$ ll
total 18676
drwxrwxr-x  2 Users Users     4096  4月  8 07:59 ./
drwxrwxrwt 18 root   root      20480  4月  8 08:00 ../
-rw-rw-r--  1 Users Users  1124574  4月  8 07:59 15673749081_767a7fa63a_k.jpg.pdf
-rw-rw-r--  1 Users Users   846650  4月  8 07:59 16004479832_a748d55f21_k.jpg.pdf
-rw-rw-r--  1 Users Users  1075018  4月  8 07:59 17790319373_bd19b24cfc_k.jpg.pdf
-rw-rw-r--  1 Users Users   952702  4月  8 07:59 18124840932_e42b3e377c_k.jpg.pdf
-rw-rw-r--  1 Users Users  1008141  4月  8 07:59 19064748793_bb942deea1_k.jpg.pdf
-rw-rw-r--  1 Users Users   717422  4月  8 07:59 24274813513_0cfd2ce6d0_k.jpg.pdf
-rw-rw-r--  1 Users Users  1287714  4月  8 07:59 33823288584_1d21cf0a26_k.jpg.pdf
-rw-rw-r--  1 Users Users   758560  4月  8 07:59 33887522274_eebd074106_k.jpg.pdf
-rw-rw-r--  1 Users Users  1174700  4月  8 07:59 34501842524_3c858b3080_k.jpg.pdf
-rw-rw-r--  1 Users Users 10133503  4月  7 10:38 IMG_0560.jpg.pdf
(detectron) Users@Alienware-15-R3:/tmp/detectron-visualizations$ xdg-open .
(detectron) Users@Alienware-15-R3:/tmp/detectron-visualizations$ 
(nautilus:6720): GLib-GIO-CRITICAL **: g_dbus_interface_skeleton_unexport: assertion 'interface_->priv->connections != NULL' failed

(nautilus:6720): GLib-GIO-CRITICAL **: g_dbus_interface_skeleton_unexport: assertion 'interface_->priv->connections != NULL' failed

(nautilus:6720): Gtk-CRITICAL **: gtk_icon_theme_get_for_screen: assertion 'GDK_IS_SCREEN (screen)' failed

(nautilus:6720): GLib-GObject-WARNING **: invalid (NULL) pointer instance

(nautilus:6720): GLib-GObject-CRITICAL **: g_signal_connect_object: assertion 'G_TYPE_CHECK_INSTANCE (instance)' failed
```

マスクした結果が/tmp/detectron-visualizationsに保存されます。移動しましょう。

```
$ cd /tmp/detectron-visualizations
```

Macでいう`open .`でフォルダを開きます

```
$ xdg-open .
```

![Screenshot from 2018-04-08 08-21-15.png](https://qiita-image-store.s3.amazonaws.com/0/245670/19c0fb4c-f36c-37b6-cb1b-6a881d3596cf.png)

![Screenshot from 2018-04-08 08-21-57.png](https://qiita-image-store.s3.amazonaws.com/0/245670/64b2df78-1223-8189-a31e-45995cc49374.png)

![Screenshot from 2018-04-08 08-23-10.png](https://qiita-image-store.s3.amazonaws.com/0/245670/155f46f3-3cda-4e7d-15c4-47888fbefeec.png)

![Screenshot from 2018-04-08 08-23-31.png](https://qiita-image-store.s3.amazonaws.com/0/245670/29a0f060-bcd3-b67a-9a20-12762d21e9df.png)

![Screenshot from 2018-04-08 08-23-50.png](https://qiita-image-store.s3.amazonaws.com/0/245670/3752a4a7-5767-ca0b-dcdf-d36e92bf2d9c.png)

![Screenshot from 2018-04-08 08-24-09.png](https://qiita-image-store.s3.amazonaws.com/0/245670/119de2b4-8e05-9766-16f6-ee143378beb2.png)

![Screenshot from 2018-04-08 08-24-34.png](https://qiita-image-store.s3.amazonaws.com/0/245670/ce0e1da9-6fb7-8867-388d-796ebdccb1c2.png)

![Screenshot from 2018-04-08 08-24-50.png](https://qiita-image-store.s3.amazonaws.com/0/245670/de79552c-8852-c2f3-be9c-f5005a9b753e.png)

![Screenshot from 2018-04-08 08-25-07.png](https://qiita-image-store.s3.amazonaws.com/0/245670/3c574e2b-a96a-cba3-86b6-df791a596227.png)


ヤバイっすね！