Mac上安装开启GPU的Caffe(Mac OSX 10.12 + CUDA 7.5 + Anaconda 2)

安装流程参考

How to install Caffe on Mac OS X 10.10

caffe安装过程分享(for osx10.11.4)

.zshrc设置

export PATH=~/anaconda2/bin:/usr/local/cuda:/usr/local/cuda/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:/usr/local/cuda/lib:usr/local/lib:/usr/local/cuda/extras/CUPTI/lib64:$LD_LIBRARY_PATH
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/anaconda2/lib:/usr/local/lib:/usr/lib:/opt/intel/mkl/lib:/opt/intel/lib

Makefile.config设置

USE_CUDNN := 1
CUDA_DIR := /usr/local/cuda
BLAS := mkl
MATLAB_DIR := /Applications/MATLAB_R2014b.app
ANACONDA_HOME := $(HOME)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
PYTHON_LIB := $(ANACONDA_HOME)/lib
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

make all failed

LD -o .build_release/lib/libcaffe.so
clang: warning: argument unused during compilation: '-pthread'
Undefined symbols for architecture x86_64:
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
caffe::WindowDataLayer<float>::InternalThreadEntry() in window_data_layer.o
caffe::WindowDataLayer<double>::InternalThreadEntry() in window_data_layer.o
caffe::ReadImageToCVMat(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int, int, bool) in io.o

解决方案

修改 Caffe根目录下的 Makefile

LIBRARIES += glog gflags protobuf leveldb snappy
lmdb boost\_system hdf5\_hl hdf5 m
opencv\_core opencv\_highgui opencv\_imgproc

add :opencv_imgcodecs solve my problem

make runtest failed

[ FAILED ] IOTest.TestDecodeDatumToCVMatNative
[ FAILED ] IOTest.TestDecodeDatumNative
[ FAILED ] IOTest.TestDecodeDatumToCVMatNativeGray
[ FAILED ] IOTest.TestDecodeDatum
[ FAILED ] IOTest.TestDecodeDatumToCVMatContent
[ FAILED ] IOTest.TestDecodeDatumToCVMatContentNative
[ FAILED ] IOTest.TestDecodeDatumNativeGray
[ FAILED ] IOTest.TestDecodeDatumToCVMat

解决方案:

git add.
git commit -m 'yuliang'
git checkout rc
git push origin master
make all -j8
make test -j8
make runtest -j8
make pycaffe
make matcaffe
make distribute

brew install

brew edit opencv

brew edit opencv
args << "-DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib"
args << "-DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7"

brew install

brew install --fresh -vd snappy leveldb gflags glog szip lmdb homebrew/science/opencv
brew install --build-from-source --with-python --fresh -vd protobuf
brew install --build-from-source --fresh -vd boost boost-python

更改clang version

brew需要clang 8.0

make编译caffe的时候需要clang 7.2

  1. Apple Developer 下载合适版本的Command line Tools
  2. 安装CLT,然后 sudo xcode-select --switch /Library/Developer/CommandLineTools
  3. 如果再更换clang,则需要重新安装,重新xcode-select
  4. clang -v 查看当前clang版本

python调用caffe

export PYTHONPATH=~/caffe/distribute/python:$PYTHONPATH

如果出现错误

import caffe
python(74368,0x7fff95f3d3c0) malloc: *** malloc_zone_unregister() failed for 0x7fff95f33000

解决方案

brew reinstall gperftools --HEAD