ytbilly3636’s 研究備忘録

機械学習,Python,ガンダムなど

Caffe2を使ってみたその1「インストール」

こんにちは.

Caffe2というDeep Learning用のフレームワークが出たようです. まだドキュメントをちゃんと読んでいないのでその良し悪しはわかりませんがとりあえず試してみることとします.

caffe2.ai

環境

項目 詳細
OS Ubuntu 14.04
GPU GTX970

ちなみに2じゃないほうのCaffeをインストール済みです.

手順

手順といっても,Caffe2の公式ドキュメントの方法に従うだけです. ソースからのビルドを試してみます.

Install | Caffe2

依存パッケージのインストー

# 私の場合Caffeを入れたときに以下のパッケージは導入済みなので飛ばしました
$ sudo apt-get update
$ sudo apt-get install -y --no-install-recommends build-essential cmake git libgoogle-glog-dev libprotobuf-dev protobuf-compiler python-dev python-pip
$ sudo pip install numpy protobuf

CUDAとcuDNNのインストー

# 公式ドキュメントにはグラフィックドライバーを更新してねと書いていました.
# そのあたりは各自で適宜行って下さい.

# 当初CUDA7.5を導入済みだったのでここを飛ばしましたが,後述のエラーが出ました.8.0が良いのでしょうか.
$ sudo apt-get update && sudo apt-get install wget -y --no-install-recommends
$ wget "http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_8.0.61-1_amd64.deb"
$ sudo dpkg -i cuda-repo-ubuntu1404_8.0.61-1_amd64.deb
$ sudo apt-get update
$ sudo apt-get install cuda

$ CUDNN_URL="http://developer.download.nvidia.com/compute/redist/cudnn/v5.1/cudnn-8.0-linux-x64-v5.1.tgz"
$ wget ${CUDNN_URL}
$ sudo tar -xzf cudnn-8.0-linux-x64-v5.1.tgz -C /usr/local
$ rm cudnn-8.0-linux-x64-v5.1.tgz && sudo ldconfig

残りの依存パッケージのインストー

$ sudo apt-get install -y --no-install-recommends libgflags2
$ sudo apt-get install -y --no-install-recommends libgtest-dev libiomp-dev libleveldb-dev liblmdb-dev libopencv-dev libopenmpi-dev libsnappy-dev openmpi-bin openmpi-doc python-pydot
$ sudo pip install flask graphviz hypothesis jupyter matplotlib pydot python-nvd3 pyyaml requests scikit-image scipy setuptools tornado

ソースのクローンとビルド

# ホームディレクトリにクローンしました.場所については適宜読み替えて行って下さい.
# makeでエラーが出たらmake cleanしてやり直し
$ git clone --recursive https://github.com/caffe2/caffe2.git && cd caffe2
$ make && cd build && sudo make install
$ python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"

環境変数の設定

「.bashrc」の末尾にパスを追記します.

$ gedit ~/.bashrc &
# .bashrcの末尾に追記
# 2行目のパスは各自読み替えて下さい.
export PYTHONPATH=/usr/local:$PYTHONPATH
export PYTHONPATH=$PYTHONPATH:/home/username/caffe2/build
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH

動作確認

動くかな? ワクワク……

$ python -m caffe2.python.operator_test.relu_op_test
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([[ 1.        ,  0.58414382, -0.07458938],
       [-0.92077535,  1.        , -0.31444678]], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'CUDNN')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([-0.        , -0.77440119], dtype=float32), gc=, dc=[, device_type: 1], engine=u'')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([ 0.,  0.], dtype=float32), gc=, dc=[, device_type: 1], engine=u'')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([[[ 0.12101772,  1.        ,  0.98191798, -0.75570065],
        [ 0.        ,  1.        , -0.66422468, -0.87821025],
        [-0.76647252,  0.19047344, -0.32112685, -0.        ],
        [ 0.6859479 , -0.47230539, -0.79940522, -0.24955569]],

       [[ 1.        , -0.36730164, -0.78970796,  0.46469435],
        [-0.95587206, -0.44132483,  0.        , -0.28097823],
        [-0.08904378,  0.38005501, -0.61275059,  0.79579282],
        [-0.02774031,  0.50639385, -0.91766995, -0.33312017]]], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'CUDNN')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([[-0.56960249, -0.84849566, -0.        ]], dtype=float32), gc=, dc=[, device_type: 1], engine=u'CUDNN')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([ -0.00000000e+00,  -6.69241369e-01,   3.31036976e-28], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'CUDNN')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([ 0.97428375, -0.07135547], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([-0., -0.], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([-0.        , -0.94964397], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'CUDNN')
Trying example: test_relu(self=<__main__.TestRelu testMethod=test_relu>, X=array([ 0.41938838,  0.64380383, -0.30215767], dtype=float32), gc=device_type: 1, dc=[, device_type: 1], engine=u'')
.
----------------------------------------------------------------------
Ran 1 test in 0.946s

OK

OKらしいです.

エラーの対処

make中に以下のエラーに遭遇しました

[ 59%] Built target Caffe2_CPU
make[3]: ディレクトリ `/home/username/caffe2/build' に入ります
[ 59%] Building NVCC (Device) object caffe2/CMakeFiles/Caffe2_GPU.dir/utils/./Caffe2_GPU_generated_math_gpu.cu.o
/home/username/caffe2/caffe2/utils/conversions.h(137): warning: variable "r" was declared but never referenced

/home/username/caffe2/caffe2/utils/math_gpu.cu(136): error: identifier "CUDA_R_16F" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(312): error: identifier "CUDA_R_16F" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(496): error: identifier "CUDA_R_16F" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(496): error: identifier "CUDA_R_32F" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(496): error: identifier "cublasDotEx" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(740): error: identifier "CUDA_R_16F" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(740): error: identifier "CUDA_R_32F" is undefined

/home/username/caffe2/caffe2/utils/math_gpu.cu(740): error: identifier "cublasAxpyEx" is undefined

8 errors detected in the compilation of "/tmp/tmpxft_000079b6_00000000-7_math_gpu.cpp1.ii".
CMake Error at Caffe2_GPU_generated_math_gpu.cu.o.cmake:260 (message):
  Error generating file
  /home/username/caffe2/build/caffe2/CMakeFiles/Caffe2_GPU.dir/utils/./Caffe2_GPU_generated_math_gpu.cu.o


make[3]: *** [caffe2/CMakeFiles/Caffe2_GPU.dir/utils/./Caffe2_GPU_generated_math_gpu.cu.o] エラー 1
make[3]: ディレクトリ `/home/username/caffe2/build' から出ます
make[2]: *** [caffe2/CMakeFiles/Caffe2_GPU.dir/all] エラー 2
make[2]: ディレクトリ `/home/username/caffe2/build' から出ます
make[1]: *** [all] エラー 2
make[1]: ディレクトリ `/home/username/caffe2/build' から出ます
make: *** [all] エラー 2

CUDA周りのエラーですかね? CUDAを入れ直してから「.bashrc」の末尾にパスを追加してやりました.

$ gedit ~/.bashrc &
# .bashrcの末尾に追記
export PATH=/usr/local/cuda-8.0/bin:${PATH}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:${LD_LIBRARY_PATH}

再起動してから再びmakeをするとうまく行きました.