Ubuntu14.04 Anaconda(Python2)+Caffe(機械学習ライブラリ)をインストール

2015年9月2日

まっさらなUbuntu14.04にanacondaをインストールし、その後にcaffeをインストールしてanacondaに関連付ける。
Python3にインストールしたかったが、import時にエラーが出て使えなかったため、python2.7を使う。
(エラーに関しては後日まとめる)

[amazonjs asin="B00V2VV0V4″ locale="JP" title="自然言語処理と機械学習入門"]

事前にアップデート

事前にアップデートしておく。

[shell]
sudo apt-get update
sudo apt-get dist-upgrade -y
sudo apt-get install -y vim
[/shell]

 

CUDAのインストール

caffeにはCUDAが必要のため、インストールする。

[shell]
$ cd /usr/local/src
$ sudo wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_7.0-28_amd64.deb
$ sudo dpkg -i cuda-repo-ubuntu1404_7.0-28_amd64.deb
$ sudo apt-get update
$ sudo apt-get install -y cuda
[/shell]

 

依存パッケージをインストール

[shell]
$ sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler gfortran git gcc make openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev libatlas-base-dev
[/shell]

必要ないと思われるが、一緒にインストールしたものも一応記載しておく。

[shell]
$ sudo apt-get install -y python-pip python-numpy python-scipy python-skimage python3.4 python3-dev python3-pip python3-numpy python3-scipy python3-skimage
[/shell]

 

Anacondaのインストール

Anacondaをホームディレクトリ(今回の場合は/home/ubuntu)配下にインストールした。

[shell]
$ cd ~
$ wget https://3230d63b5fc54e62148e-c95ac804525aac4b6dba79b00b39d1d3.ssl.cf1.rackcdn.com/Anaconda-2.3.0-Linux-x86_64.sh
$ chmod +x Anaconda3-2.3.0-Linux-x86_64.sh
$ ./Anaconda3-2.3.0-Linux-x86_64.sh

指示に従って、/home/ubuntu/anacondaにインストール
(全てyes or yにした。)

$ source ~/.bashrc
[/shell]

 

Caffeをインストール

Caffe本体をホームディレクトリ配下にインストールする。

[shell]
$ cd ~
$ git clone https://github.com/BVLC/caffe
$ cd caffe
$ cp Makefile.config.example Makefile.config
$ vi Makefile.config
[/shell]

[shell title="Makefile.configの内容"]
# コメント以外部分を記載。
# 以下項目以外コメントアウトしている。

CPU_ONLY := 1

CUDA_DIR := /usr/local/cuda-7.0

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50

BLAS := atlas

ANACONDA_HOME := $(HOME)/anaconda
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

PYTHON_LIB := $(ANACONDA_HOME)/lib
[/shell]

[shell]
$ make all
$ make test
$ make runtest
[/shell]

runtestしたところ、

.build_release/tools/caffe: error while loading shared libraries: libhdf5_hl.so.10: cannot open shared object file: No such file or directory

といったエラーが出たため、anaconda/lib/libhdf5_hl.so.10のパスを指定した。
[shell]
sudo vi /etc/ld.so.conf.d/caffeLibs.conf
[/shell]

以下を書き込む。(ユーザー名がubuntuの場合)

[shell]
/home/ubuntu/anaconda/lib
[/shell]

再読込する。
[shell]
sudo ldconfig
[/shell]

上記でruntestが通った。
最後にpycaffeをmakeする。

[python][/python]
$ make pycaffe
[/shell]

 

確認

ipythonを呼び出して、caffeがimportできるか確認する。

[shell]
ubuntu@ip-10-1-10-59:~/caffe$ ipython
Python 2.7.10 |Anaconda 2.3.0 (64-bit)| (default, May 28 2015, 17:02:03)
Type "copyright", "credits" or "license" for more information.

IPython 3.2.0 — An enhanced Interactive Python.
Anaconda is brought to you by Continuum Analytics.
Please check out: https://continuum.io/thanks and https://anaconda.org
? -> Introduction and overview of IPython’s features.
%quickref -> Quick reference.
help -> Python’s own help system.
object? -> Details about 'object’, use 'object??’ for extra details.

In [1]: import caffe
libdc1394 error: Failed to initialize libdc1394
In [2]:
[/shell]