Install Caffe on Mac OS X 10.10

Take note, reference at: http://smrmkt.hatenablog.jp/entry/2015/02/11/195213


Machine: Macbook Air 2013 OS X Yosemite Version 10.10.2 (core i7 1.7 GHz quad core)

CUDA

GPU: Intel HD Graphics 5000 1536 MB (cannot use CUDA)

But you can install from NVIDIA CUDA

BLAS

Installed on Mac

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

Update packages in anaconda

conda update conda
conda update anaconda

Check anaconda path

conda env list
/Users/zoro/.pyenv/versions/anaconda-2.0.1

OpenCV

brew update
brew tap homebrew/science
brew install opencv

Boost

Modify Formula for fix boost 1.55

cd /usr/local
git checkout a252214 /usr/local/Library/Formula/boost.rb
Set C++ library as libstdc++
Open formula for corresponded library
for x in snappy leveldb protobuf gflags glog szip boost boost-python lmdb homebrew/science/opencv; do brew edit $x; done

Put and modify these lines (below def install)

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

Modify Formula for fix boost 1.55.

brew edit boost-python

Modify as

   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"

Boost install

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

Finish preparation for dependencies

Caffe

git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config

Makefile.config

Modify Makefile.config for my machine using CPU_ONLY option

# CPU-only switch (uncomment to build without GPU support).
- # CPU_ONLY := 1
+ CPU_ONLY := 1
# 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.
# PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := /path/to/anaconda-2.0.1
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
 $(ANACONDA_HOME)/include/python2.7 \
 $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

Makefile

ifeq ($(shell echo $(CUDA_VERSION) \< 7.0 | bc), 1)
 CXXFLAGS += -stdlib=libstdc++
 LINKFLAGS += -stdlib=libstdc++
endif
 
+ ifneq ($(findstring 10.10, $(shell sw_vers -productVersion)),)
+ CXXFLAGS += -stdlib=libstdc++
+ LINKFLAGS += -stdlib=libstdc++
+ endif

 PATH setting

vim ~/.zshrc
# 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
source ~/.zshrc

 Compile and Test

make all
make runtest
make pycaffe
make distribute
make

If there’s no problem, you can see the below message

[----------] Global test environment tear-down
[==========] 581 tests from 111 test cases ran. (14856 ms total)
[ PASSED ] 581 tests.

Update

  • hdf5.h not found error → brew install hdf5
  • dyld: Library not loaded: /usr/local/lib/libpng16.16.dylib error when run “make runtest”
    • brew install libpng
    • cp /usr/local/Cellar/libpng/1.6.18/lib/*.dylib /usr/local/lib/

Import caffe from Python

If the below error has occured

caffe Fatal Python error: PyThreadState_Get: no current thread

Try to re-install boost-python

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s