I guess that many engineers have some troubles when they try to build matcaffe on OSX.
1. Link error – google::protobuf
2. Link error – mex functions – not found _mxArrayToString
3. Link error – dyld: Library not loaded: @rpath/libmkl_intel_lp64.dylib
I found a way to avoid these troubles, so I will write down about it.
At first, my system is
– Mac Pro (Late 2013)
– OSX 10.11.5
– MATLAB 2016a
– Intel Math Kernel Library(or OpenBLAS via homebrew)
– without Python interface
– without CUDA
1. Install some packages according to “OSX Installation”.
2. Install Intel Math Kernel Library.
Or, you can use OpenBLAS or Accelerate.framework.
3. Edit Makefile.config.
You have to edit
Makefile.config very carefully.
Make it use only CPU.
# CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1
# 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 := /usr/bin/clang++
Set BLAS path according to your library.
This sample is for Intel Math Kernel Library.
# BLAS choice: # atlas for ATLAS (default) # mkl for MKL # open for OpenBlas BLAS := mkl # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. # Leave commented to accept the defaults for your choice of BLAS # (which should work)! BLAS_INCLUDE := /opt/intel/compilers_and_libraries_2016.3.170/mac/mkl/include BLAS_LIB := /opt/intel/compilers_and_libraries_2016.3.170/mac/mkl/lib
Set MATLAB path to build matcaffe.
# 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_R2016a.app
This is the most important point.
Remove paths for python and keep the under order of LIBRARY_DIRS’s elements in order to avoid link error.
# Whatever else you find you need goes here. INCLUDE_DIRS := /usr/local/include LIBRARY_DIRS := /usr/local/lib $(MATLAB_DIR)/bin/maci64 /usr/lib
Next, build caffe.
4. Build caffe and matcaffe
If you want to use Intel Math Kernel Library, you have to execute this command before it.
I recommend you should copy this command into
# build caffe source /opt/intel/compilers_and_libraries/mac/mkl/bin/mklvars.sh intel64 ilp64 cd caffe make all make all test make runtest make all matcaffe
If failed to build caffe, check Makefile.config again.
5. How to use matcaffe.
You have to run MATLAB from terminal.app because MATLAB does not load environment variables when it runs via Finder.app……
cd caffe # You have set this environment variable # in order to avoid caffe load MATLAB's tiff library.... export DYLD_INSERT_LIBRARIES=/usr/local/opt/libtiff/lib/libtiff.5.dylib /Applications/MATLAB_R2016a.app/bin/matlab &
You can use matcaffe in MATLAB console.
cd caffe addpath('./matlab'); caffe.set_mode_cpu();
That’s all! Have fun!