Caffe Matcaffe - eiichiromomma/CVMLAB GitHub Wiki

(Caffe) Matcaffe

MATLABでCaffeを使う

Ubuntu 14.04+MATLAB2016a

GitからcloneしてMakefile.configは適切に設定しておく。使ったのは下記設定(diff)

--- Makefile.config.example	2017-06-13 07:44:57.224914337 +0900
+++ Makefile.config	2017-06-15 17:05:59.038289985 +0900
@@ -2,15 +2,15 @@
 # Contributions simplifying and improving our build system are welcome!
 
 # cuDNN acceleration switch (uncomment to build with cuDNN).
-# USE_CUDNN := 1
+USE_CUDNN := 1
 
 # CPU-only switch (uncomment to build without GPU support).
 # CPU_ONLY := 1
 
 # uncomment to disable IO dependencies and corresponding data layers
-# USE_OPENCV := 0
-# USE_LEVELDB := 0
-# USE_LMDB := 0
+USE_OPENCV := 1
+# USE_LEVELDB := 1
+# USE_LMDB := 1
 
 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
 #	You should not set this flag if you will be reading LMDBs with any
@@ -18,11 +18,11 @@
 # ALLOW_LMDB_NOLOCK := 1
 
 # Uncomment if you're using OpenCV 3
-# OPENCV_VERSION := 3
+#OPENCV_VERSION := 3
 
 # To customize your choice of compiler, uncomment and set the following.
 # N.B. the default for Linux is g++ and the default for OSX is clang++
-# CUSTOM_CXX := g++
+#CUSTOM_CXX := g++-4.7
 
 # CUDA directory contains bin/ and lib/ directories that we need.
 CUDA_DIR := /usr/local/cuda
@@ -33,11 +33,7 @@
 # CUDA architecture setting: going with all of them.
 # For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
 # For CUDA < 8.0, comment the *_60 and *_61 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 \
+CUDA_ARCH :=  -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 \
@@ -60,8 +56,8 @@
 
 # This is required only if you will compile the matlab interface.
 # MATLAB directory should contain the mex binary in /bin.
-# MATLAB_DIR := /usr/local
-# MATLAB_DIR := /Applications/MATLAB_R2012b.app
+MATLAB_DIR := /usr/local/MATLAB/R2016a
+#MATLAB_DIR := /Applications/MATLAB_R2016a.app
 
 # NOTE: this is required only if you will compile the python interface.
 # We need to be able to find Python.h and numpy/arrayobject.h.
@@ -91,8 +87,8 @@
 # WITH_PYTHON_LAYER := 1
 
 # Whatever else you find you need goes here.
-INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
-LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
+INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/include/hdf5/serial /usr/local/include
+LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib/x86_64-linux-gnu/hdf5/serial /usr/lib
 
 # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
 # INCLUDE_DIRS += $(shell brew --prefix)/include
@@ -100,11 +96,11 @@
 
 # NCCL acceleration switch (uncomment to build with NCCL)
 # https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
-# USE_NCCL := 1
+#USE_NCCL := 1
 
 # Uncomment to use `pkg-config` to specify OpenCV library paths.
 # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
-# USE_PKG_CONFIG := 1
+USE_PKG_CONFIG := 1
 
 # N.B. both build and distribute dirs are cleared on `make clean`
 BUILD_DIR := build

あとはビルド

make -j16 all matcaffe
make mattest

の後でlibstdc++.so.6にGLIBCXX_3.何々が無いとエラーが出た場合、/usr/local/MATLAB/R2016a/sys/os/glnxa64のライブラリ群とGCCのバージョン不整合が原因。 で、上記で指摘されるlibstdc++.so.6はただのシンボリックリンクなので、問題があったらリンクを貼り直すだけで戻せるのでバックアップは特に取らない。

cd /usr/local/MATLAB/R2016a/sys/os/glnxa64/
sudo rm libstdc++.so.6
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 libstdc++.so.6

のようにシステム上のlibstdc++.so.6.x.xにシンボリックリンクを貼ってしまう。これで再度

make mattest

とすると、

undefined symbol:
_ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE

のようなエラーが出る。同じような理由で、今度はOpenCVのライブラリでの不整合なので、aptでlibopencv-devを入れているなら/usr/lib/x86_64-linux-gnuに入れ替えれば良い。また例によってシンボリックリンクの問題なのでバックアップは特に取らずに下記のようにすれば良い。直すのはimgproc, core, highguiで良いらしい。エラーがなお出る場合はそれも同じ処理をする。

cd /usr/local/MATLAB/R2016a/sys/os/glnxa64/
sudo rm libopencv_imgproc.so.2.4
sudo rm libopencv_core.so.2.4
sudo rm libopencv_highgui.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.9 libopencv_imgproc.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9 libopencv_core.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9 libopencv_highgui.so.2.4

で再度make mattest。

demoの実行

modelを拾ってくる。

cd models/bvlc_reference_caffenet/
wget http://dl.caffe.berkeleyvision.org/bvlc_reference_caffenet.caffemodel

でMATLABを起動してmatlab/demoに移動して

im = imread('../../examples/images/cat.jpg');
scores = classification_demo(im, 1);
[score, class] = max(scores);

でscore, classが出れば成功。