mac安装caffe   2016-10-25


1.安装nvidia驱动

在nvidia官网下载最新驱动,我选择的是网络dmg安装。安装的时候全选所有选项。

如果没有安装homebrew,安装homebrew

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. 安装依赖:

1
2
3
brew install -vd snappy leveldb gflags glog szip lmdb
brew tap homebrew/science
brew install hdf5 opencv

如果你需要使用pycaffe的话,安装依赖:

1
2
brew install --build-from-source --with-python -vd protobuf
brew install --build-from-source -vd boost boost-python

如果你不需要使用pycaffe的话,则安装依赖:

1
brew install protobuf boost

由于更新了OS X10.9+和CUDA 7.0的缘故,现在它们都支持libc++的编译方式了,所以以前资料中为此做的各种工作(修改brew edit的内容)都没有必要,直接装就好了。

3. 下载 Caffe 并修改配置

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

在Makefile.config中:

1)如果使用默认的atlas,则将BLAS_INCLUDE和BLAS_LIB指定为如下路径:

1
2
3
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指定为如下路径:

1
brew install openblas

1
2
3
BLAS := open
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib

3)其他设置

1
2
3
4
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. 安装

1
2
3
4
make clean
make -j8 all
make test
make runtest
1
2
3
4
5
6
7
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

1
2
3
4
5
6
make runtest                                                                                                                            
.build_release/tools/caffe
dyld: Library not loaded: libcaffe.so.1.0.0-rc3
Referenced from: /Users/ruifengshan/Downloads/caffe-rc3/.build_release/tools/caffe
Reason: image not found
make: *** [runtest] Abort trap: 6

在执行完make test命令后,切换到caffe的根目录中,执行如下命令:

1
cp -a .build_release/lib/. /usr/local/lib/

2) No module named _caffe

1
2
3
4
5
6
7
8
>>> import caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/ruifengshan/githubRepos/caffe/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver
File "/Users/ruifengshan/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,请自行替换),添加:

1
export PYTHONPATH=/Users/ruifengshan/githubRepos/caffe/python:$PYTHONPATH

然后执行:

1
source ~/.zshrc

1
2
3
4
python
>>> import sys
>>> sys.path.insert(0, '/Users/ruifengshan/githubRepos/caffe/python')
>>> import caffe

如果出现错误:

1
AttributeError: 'module' object has no attribute 'bool_'

是因为直接将PYTHONPATH路径指定了,并且和其他python库产生了冲突。需要

1
unset PYTHONPATH

然后用

1
2
3
import sys
sys.path.insert(0, '/Users/ruifengshan/githubRepos/caffe/python')
import caffe

这种方式引入caffe。

3) 其他错误

如果出现“Undefined symbols for architecture x86_64”错误

1
2
$ LDFLAGS="`pkg-config --libs protobuf` `pkg-config --libs opencv`"
$ make all

6. 参考资料


分享到:


  如果您觉得这篇文章对您的学习很有帮助, 请您也分享它, 让它能再次帮助到更多的需要学习的人. 您的支持将鼓励我继续创作 !
本文基于署名4.0国际许可协议发布,转载请保留本文署名和文章链接。 如您有任何授权方面的协商,请邮件联系我。

目录

  1. 1. 1.安装nvidia驱动
  2. 2. 2. 安装依赖:
  3. 3. 3. 下载 Caffe 并修改配置
  4. 4. 4. 安装
  5. 5. 5. 常见问题
    1. 5.1. 1) image not found
    2. 5.2. 2) No module named _caffe
    3. 5.3. 3) 其他错误
  6. 6. 6. 参考资料