LoginSignup
2
6

More than 3 years have passed since last update.

Ubuntu 16.04にCaffeをインストールする

Last updated at Posted at 2019-06-25

はじめに

Ubuntu 16.04にCaffeをインストールするときに,そのままではうまく行かなかったので,色々と調べたことをまとめました.
また,今回の手順ではPythonではPython 2でのみ使用できます.
注意として,CUDA,cuDNN以外はほぼまっさらな状態で作業をしています.

環境

  • Ubuntu 16.04
  • CUDA 10.0.130
  • cuDNN 7.6.0.64

CUDAやcuDNNのパスの設定は一通りやってあるとします.

恐らくCUDAやcuDNNのバージョンの違いはそこまでないかと思います.

インストール手順

依存環境のインストール

試行錯誤しながらの作業だったので,必要のないものもあるかもしれません.

sudo apt update
sudo apt install -y --no-install-recommends libboost-all-dev build-essential cmake git pkg-config vim \
libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler \
libatlas-base-dev libjasper-dev libgflags-dev libgoogle-glog-dev liblmdb-dev \
python-pip python-dev python-numpy python-scipy libopencv-dev python-skimage python-protobuf

Caffeのダウンロード

Caffe本体をダウンロードします.
特定のバージョンをインストールしたい場合は各自そちらをダウンロードしてください.

git clone https://github.com/BVLC/caffe.git

Makefile.configの編集

Makefile.configの編集をしていきます.

cd caffe
cp Makefile.config.example Makefile.config

Makefile.configを以下の変数を編集します.

USE_CUDNN:= 1
CUDA_DIR := /usr/local/cuda 
PYTHON_INCLUDE := /usr/include/python2.7 /usr/lib/python2.7/dist-packages/numpy/core/include
PYTHON_LIB := /usr/lib/x86_64-linux-gnu
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
                -gencode arch=compute_35,code=sm_35 \
                -gencode arch=compute_50,code=sm_50 \
                -gencode arch=compute_52,code=sm_52 \
                -gencode arch=compute_60,code=sm_60 \
                -gencode arch=compute_61,code=sm_61 \
                -gencode arch=compute_61,code=compute_61

Makefileの編集

MakefileのNVCCFLAGSの行を以下のように編集します.


NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

hdf5の調整

Caffeがhdf5を読み込めるようにシンボリックリンクを作ります.

sudo ln -s /usr/lib/x86_64-linux-gnu/libhdf5_serial.so.10.1.0 /usr/lib/x86_64-linux-gnu/libhdf5.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libhdf5_serial_hl.so.10.0.2 /usr/lib/x86_64-linux-gnu/libhdf5_hl.so

Pythonパッケージのインストール

最新のバージョンではPython2系に対応していないパッケージがいくつかあるのでpython/requirements.txtの中身を以下のように編集します.

Cython>=0.19.2
numpy>=1.7.1
scipy==0.17.0
scikit-image==0.9.3
matplotlib==1.5.3
ipython==5.0.0
h5py>=2.2.0
leveldb>=0.191
networkx==2.2
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0

その後以下のコマンドでインストール.

pip install -r python/requirements.txt --no-dependencies

make

makeしていきます.

make all
make test
make runtest
make pycaffe
make distribute

すべてのmakeコマンドでエラーが出なければビルド成功です.
最後にbashrcに以下の行を追加すれば終了です.
{path-to-caffe}はCaffeをインストールしたディレクトリを絶対パスで指定してください.

export PYTHONPATH="/usr/lib/python2.7:{path-to-caffe}/caffe/python:$PYTHONPATH"
2
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
6