Skip to content

Instantly share code, notes, and snippets.

@yusuketomoto
Last active March 17, 2016 19:10
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusuketomoto/da4ab182c1693fc3995e to your computer and use it in GitHub Desktop.
Save yusuketomoto/da4ab182c1693fc3995e to your computer and use it in GitHub Desktop.

CaffeをOSX 10.10 Yosemiteでビルドする

2014.11.11時点でのインストール情報です。

pythonまわりがややこしいので、pyenvのanaconda-2.0.1にインストールする方針

CUDA

以下をインストール

BLAS

Caffeは3種類のライブラリに対応していて、どれを使っても良い。 MacOSのSDKにもともと含まれるATLASを使用するが、10.10からは保存場所が変わっていてcaffeのmakefile.configを修正する必要がある。

Anaconda

科学計算ライブラリのパッケージ。

brew install pyenv

pyenv install anaconda-2.0.1

pyenv rehash

sudo pyenv local anaconda-2.0.1

sudo pyenv global anaconda-2.0.1

The prerequisite home-brew formula

Homebrewのformulaを変更する必要あり。

boost

バージョンが1.55でないと調子が悪いので、1.55のformulaを取得。 一応1.56でも試してみたが、ビルドは通るがruntestでいくつか失敗が出るという感じ。 以下の要領でboostのformulaを1.55に

cd /usr/local

git checkout a252214 /usr/local/Library/Formula/boost.rb

C++の標準ライブラリをlibstdc++を使うよう指定

下記のコマンドで該当のライブラリのformulaが逐次開くので、それぞれdef install以下に修正を加える。

for x in snappy leveldb protobuf gflags glog szip boost boost-python lmdb homebrew/science/opencv; do brew edit $x; done

以下のように修正

def install
    # ADD THE FOLLOWING:
    ENV.append "CXXFLAGS", "-stdlib=libstdc++"
    ENV.append "CFLAGS", "-stdlib=libstdc++"
    ENV.append "LDFLAGS", "-stdlib=libstdc++ -lstdc++"
    # The following is necessary because libtool likes to strip LDFLAGS:
    ENV["CXX"] = "/usr/bin/clang++ -stdlib=libstdc++"

Boost.python

boost-pythonのformulaも1.55を使うように手動で修正

brew edit boost-python

   homepage "http://www.boost.org"
-  url "https://downloads.sourceforge.net/project/boost/boost/1.56.0/boost_1_56_0.tar.bz2"
-  sha1 "f94bb008900ed5ba1994a1072140590784b9b5df"
+  url 'https://downloads.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.tar.bz2'
+  sha1 'cef9a0cc7084b1d639e06cd3bc34e4251524c840'
+  revision 2
   head "https://github.com/boostorg/boost.git" 

OpenCVも

python関連のパスを2.7固定に

brew edit opencv

  -DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib
  -DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7

以上のようにformulaを書き換えたらインストール

for x in snappy leveldb gflags glog szip lmdb homebrew/science/opencv; do brew uninstall $x; brew install --build-from-source --fresh -vd $x; done

brew uninstall protobuf; brew install --build-from-source --with-python --fresh -vd protobuf

brew uninstall boost

brew uninstall boost-python

brew install --build-from-source --fresh -vd boost boost-python

Caffe

Caffeを落としましょう。

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

cd caffe

cp Makefile.config.example Makefile.config

Makefile.configを編集

cuda6以上を入れているので以下をアンコメント

-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_50,code=compute_50

pythonインターフェースのパスPYTHON_INCLUDE, PYTHON_LIB を変更する。

# Anaconda Python distribution is quite popular. Include path:
PYTHON_INCLUDE := $(HOME)/.pyenv/versions/anaconda-2.0.1/include \
                  $(HOME)/.pyenv/versions/anaconda-2.0.1/include/python2.7 \
                  $(HOME)/.pyenv/versions/anaconda-2.0.1/lib/python2.7/site-packages/numpy/core/include
PYTHON_LIB := $(HOME)/.pyenv/versions/anaconda-2.0.1/lib

Makefileを編集

OSのバージョンをfindstringしてる箇所があるので直し

- ifneq ($(findstring 10.9, $(shell sw_vers -productVersion)),)
+ ifneq ($(findstring 10.10, $(shell sw_vers -productVersion)),)
	  CXXFLAGS += -stdlib=libstdc++
	  LINKFLAGS += -stdlib=libstdc++
  endif

10.10からblasの場所が変わってOSXSDK内のaccelerate.Frameworkの中に移ったのでパスを直し

	else ifeq ($(OSX), 1)
	# OS X packages atlas as the vecLib framework
	BLAS_INCLUDE ?= /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/System/Library/Frameworks/Accelerate.framework/Versions/Current/Frameworks/vecLib.framework/Headers/
	LIBRARIES += cblas
	LDFLAGS += -framework Accelerate
endif

コンパイル&テスト

make all

make test

make runtest

[----------] Global test environment tear-down [==========] 838 tests from 169 test cases ran. (172365 ms total) [ PASSED ] 838 tests.

YOU HAVE 2 DISABLED TESTS

これでコマンドラインからCaffeを使えるようになったはず。

その他

シェルのパスに追加。必要があれば適宜。

# pyenv
export PYENV_ROOT="${HOME}/.pyenv"
if [ -d "${PYENV_ROOT}" ]; then
export PATH=${PYENV_ROOT}/bin:$PATH
    eval "$(pyenv init -)"
fi

# caffe
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/.pyenv/versions/anaconda-2.0.1/lib:/usr/local/lib:/usr/lib

pycaffe

pythonインターフェース使いたい人は以下も。 pycaffe をコンパイル

make pycaffe

protobufをpipで入れておく

pip install protobuf

reference

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment