在写这篇心得之前, 容许我吐槽一下 — caffe真TMD难安装.
硬件环境:
macOS Seirra 10.12.6
Intel Iris 1536 MB
乞丐版本rmbp
Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew安装依赖包
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
安装Anaconda Python
注意: 下文中路径中username是Mac的用户文件夹(/User/username/)
Anaconda 下载并安装Anaconda Python。安装成功之后,添加ananconda执行文件的路径()
注意: caffe所有的python环境都是使用Anaconda内的pythonexport PATH="/Users/username/anaconda2/bin:$PATH" export DYLD_FALLBACK_LIBRARY_PATH=/Users/usernam/anaconda2/lib:$DYLD_FALLBACK_LIBRARY_PATH
安装BLAS
大部分教程使用Intel MKL, 这里我使用openblas.
brew install openblas
OpenVC环境变量
brew edit opencv
将下面两行
-DPYTHON_LIBRARY=#{py_lib}/libpython2.7.#{dylib}
-DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7
替换为:
-DPYTHON_LIBRARY=#{py_prefix}/lib/libpython2.7.dylib
-DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7
python依赖项目:
brew install --build-from-source --with-python --fresh -vd protobuf
brew install --build-from-source --fresh -vd boost boost-python
配置Caffe
cd ~/Documents
git clone https://github.com/BVLC/caff.git
cd caffe
cp Makefile.config.example Makefile.config
配置Makefile.config
CUDA, cuDNN (因为乞丐版没有集成显卡, 所以不安装)
下面是我配置的Makefile.config:
CPU_ONLY := 1
OPENCV_VERSION := 3
BLAS := open
BLAS_INCLUDE := $(shell brew --prefix openblas)/include
BLAS_LIB := $(shell brew --prefix openblas)/lib
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
PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
PYTHON_LIB += $(shell brew --prefix numpy)/lib
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
Q ?= @
编译caffe:
make clean
make all -j8
make runtest
打包caffe:
make pycaffe
make distribute
编译完成:
cd python/
python
>> import caffe
>>
如果import caffe之后没有报错, 那么你已经成功安装好caffe环境. 如果报错就继续阅读.
编译报错解决:
报错: import caffe Segmentation fault: 11 报错
这是系统python和Anaconda python发生冲突, 将默认python设置成Anaconda python, 然后重启一次Terminal.
报错: dyld: Library not loaded: @rpath/libhdf5_hl.10.dylib
dyld: Library not loaded: @rpath/libhdf5_hl.10.dylib
Referenced from: /Users/work/gitclone/caffe/.build_release/test/test_all.testbin
Reason: image not found
使用install_name_tool
添加anaconda2库路径:
install_name_tool -add_rpath ~/anaconda2/lib /Users/username/Documents/caffe/.build_release/tools/caffe
dyld: Library not loaded: @rpath/libhdf5_hl.10.dylib
报错编译都完成后,import caffe
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "caffe/pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: dlopen(caffe/_caffe.so, 2): Library not loaded: @rpath/libcaffe.so.1.0.0
Referenced from: /Users/zuoxiansheng/Documents/caffe/distribute/python/caffe/_caffe.so
Reason: image not found
这个报错差点把我折腾到放弃, 直到第三天, 在这里我绝望地寻找解决方法, 心灰意冷地准备尝试这个命令.
1. step: 添加python lib 环境变量路径
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:$HOME/anaconda2/lib:/usr/local/lib:/usr/local/Cellar/:/usr/local/cu da/lib:~/anaconda2/lib:/usr/local/lib:/usr/lib:$DYLD_FALLBACK_LIBRARY_PATH
2.step:仍然报错报错就把lib拷贝到本地lib
After make test in $CAFFE_ROOT:
cp -a .build_release/lib/. /usr/local/lib/
然后, import caffe
终于成功了… (确保命令行执行的是Anaconda python, 因为caffe的Make.config的python路径是Anaconda). 我使用pyenv管理不同版本的python (System,brew,Anaconda)
如果显示的系统python, 那么查看~/.bash_profile
的Anaconda的python环境变量是否已设置 export PATH="/Users/username/anaconda2/bin:$PATH"
Python 2.7.13 |Anaconda 4.4.0 (x86_64)| (default, Dec 20 2016, 23:05:08)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
Anaconda is brought to you by Continuum Analytics.
Please check out: http://continuum.io/thanks and https://anaconda.org
f**k the caffe!!
总结
这次无比坑爹的安装过程, 验证了坚持不懈就是✌️
相关文章:
如何在Macosx上安装caffe
OSX 10.11 linking issue: dyld: Library not loaded: libcaffe.so.1.0.0-rc3
Caffe Mac安装环境配置:macOS 10.12+集成显卡
在 macOS Sierra 10.12.2 上編譯 Caffe 並使用 DIGITS