1.安装nvidia驱动
在nvidia官网下载最新驱动,我选择的是网络dmg安装。安装的时候全选所有选项。
如果没有安装homebrew,安装homebrew
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
2. 安装依赖:
brew install -vd snappy leveldb gflags glog szip lmdb
brew tap homebrew/science
brew install hdf5 opencv
如果你需要使用pycaffe的话,安装依赖:
brew install --build-from-source --with-python -vd protobuf
brew install --build-from-source -vd boost boost-python
如果你不需要使用pycaffe的话,则安装依赖:
brew install protobuf boost
由于更新了OS X10.9+和CUDA 7.0的缘故,现在它们都支持libc++的编译方式了,所以以前资料中为此做的各种工作(修改brew edit的内容)都没有必要,直接装就好了。
3. 下载 Caffe 并修改配置
git clone https://github.com/BVLC/caffe.git
cd caffe
cp Makefile.config.example Makefile.config
在Makefile.config中:
1)如果使用默认的atlas,则将BLAS_INCLUDE和BLAS_LIB指定为如下路径:
BLAS := atlas
BLAS_INCLUDE := /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/Headers
BLAS_LIB := /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A
2)如果使用openblas,则使用brew安装,并将BLAS_INCLUDE和BLAS_LIB指定为如下路径:
brew install openblas
BLAS := open
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib
3)其他设置
CPU_ONLY := 1
PYTHON_INCLUDE := /usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/include/python2.7 \
/usr/local/Cellar/numpy/1.11.2/lib/python2.7/site-packages/numpy/core/include
WITH_PYTHON_LAYER := 1
如果你需要使用pycaffe,则需要指定WITH_PYTHON_LAYER。另外此处python的include路径使用的是brew安装的python,如果你使用默认python,将来会因为osx的系统保护机制而报错。
4. 安装
make clean
make -j8 all
make test
make runtest
cd caffe
pip install --user -r python/requirements.txt #pip install -i https://pypi.douban.com/simple/ --user -r python/requirements.txt
make -j8 pycaffe
make distribute
cp -r distribute/python/caffe ~/Library/Python/2.7/lib/python/site-packages/
cp distribute/lib/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/libcaffe.so.1.0.0-rc3
install_name_tool -change @rpath/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/libcaffe.so.1.0.0-rc3 ~/Library/Python/2.7/lib/python/site-packages/caffe/_caffe.so
5. 常见问题
1) image not found
make runtest
.build_release/tools/caffe
dyld: Library not loaded: libcaffe.so.1.0.0-rc3
Referenced from: /Users/aaron/Downloads/caffe-rc3/.build_release/tools/caffe
Reason: image not found
make: *** [runtest] Abort trap: 6
在执行完make test命令后,切换到caffe的根目录中,执行如下命令:
cp -a .build_release/lib/. /usr/local/lib/
2) No module named _caffe
>>> import caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/aaron/githubRepos/caffe/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
File "/Users/aaron/githubRepos/caffe/python/caffe/pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: No module named _caffe
首先确定自己是否执行了make -j8 pycaffe命令,如果没执行,先执行该命令。然后在根目录下的.zshrc文件中(如果用的是其他shell,请自行替换),添加:
export PYTHONPATH=/Users/aaron/githubRepos/caffe/python:$PYTHONPATH
然后执行:
source ~/.zshrc
python
>>> import sys
>>> sys.path.insert(0, '/Users/aaron/githubRepos/caffe/python')
>>> import caffe
如果出现错误:
AttributeError: 'module' object has no attribute 'bool_'
是因为直接将PYTHONPATH路径指定了,并且和其他python库产生了冲突。需要
unset PYTHONPATH
然后用
import sys
sys.path.insert(0, '/Users/aaron/githubRepos/caffe/python')
import caffe
这种方式引入caffe。
3) 其他错误
如果出现“Undefined symbols for architecture x86_64”错误
$ LDFLAGS="`pkg-config --libs protobuf` `pkg-config --libs opencv`"
$ make all
6. 参考资料

评论