Jordi Pont-Tuset bio photo

Jordi Pont-Tuset

I'm from a small town near Girona, Catalunya. Currently in Zürich, Switzerland. Passionate about computer vision, technology, running, and mountains.

Twitter   G. Scholar LinkedIn Github e-Mail Google+

So if you’re into computer vision, you are likely dealing with some deep-learning flavoured techniques. This means that you are possibly struggling to compile and run some code written in Caffe. This post lists some of the problems I found in compiling MatCaffe on Mac. I particular, I am using Matlab R2012b on a Macbook Pro running Mac OS X El Capitan.

First, I followed the official installation guide strictly: I installed CUDA 7.5, and installed all dependencies (no Anaconda Python) using HomeBrew:

brew install -vd snappy leveldb gflags glog szip lmdb
# need the homebrew science source for OpenCV and hdf5
brew tap homebrew/science
brew install hdf5 opencv

No problem here, so I moved to compilation. Let’s start by make all -j8. Here I found the following problems:

1 - Missing Protobuf

$ make all
protoc --proto_path=src --cpp_out=build/src src/caffe/proto/caffe.proto
make: protoc: No such file or directory
make: *** [build/src/caffe/proto/caffe.pb.cc] Error 1

Solution, install it.

brew install protobuf

2 - Missing BLAS library

fatal error: 'cblas.h' file not found
#include <cblas.h>
1 error generated.
make: *** [build/src/caffe/blob.o] Error 1

Solution, install it:

brew install homebrew/science/openblas

And do the following changes in Makefile.config:

BLAS := open
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib
INCLUDE_DIRS += $(shell brew --prefix)/include
LIBRARY_DIRS += $(shell brew --prefix)/lib

With this I could build make all and make test, but I found the following problem when running make runtest:

3 - CUDA not found

dyld: Library not loaded: @rpath/libcudart.7.5.dylib
Reason: image not found
make: *** [runtest] Trace/BPT trap: 5

Solution, set the environment variable:

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib/

So, next was to build MatCaffe make matcaffe, for which I had to change:

MATLAB_DIR := /Applications/MATLAB_R2012b.app/

But when running a test from Matlab I encountered the following problem (even if I had set the correct DYLD_FALLBACK_LIBRARY_PATH).

4 - CUDA not found from Matlab

Invalid MEX-file 'matlab/+caffe/private/caffe_.mexmaci64':
dlopen(matlab/+caffe/private/caffe_.mexmaci64, 1): Library not loaded: @rpath/libcudart.7.5.dylib
Referenced from: matlab/+caffe/private/caffe_.mexmaci64
Reason: image not found

And the solution to this was in two fronts. First, Matlab was taking his own CUDA libraries, so I simply renamed them (not sure this affects Matlab in some way…):

mv /Applications/MATLAB_R2012b.app/bin/maci64/libcudart.dylib /Applications/MATLAB_R2012b.app/bin/maci64/libcudart.bkp
mv /Applications/MATLAB_R2012b.app/bin/maci64/libcublas.dylib /Applications/MATLAB_R2012b.app/bin/maci64/libcublas.bkp

And second, it seems that Mac OS X El Capitan implements some type of protection (System Integrity Protection) that clears DYLD_FALLBACK_LIBRARY_PATH when executing Matlab. To prevent this, I followed the comment in this thread:

  1. Boot to Recovery OS by restarting your machine and holding down the Command + R keys at startup.
  2. Launch Terminal from the Utilities menu.
  3. Enter the following command: csrutil disable
  4. Restart

Please note that you need to run Matlab from a command line with the correct value for DYLD_FALLBACK_LIBRARY_PATH by doing:

/Applications/MATLAB_R2012b.app/bin/matlab &

And with this it worked! Enjoy and comment if this helped you. :)