..

如何在Macosx上安装caffe

在macosx上面安装深度学习框架caffe是个相对比较麻烦的过程。如果根据caffe项目的install_osx文件指引去安装,会遇到一些问题。所以我决定把自己的安装过程记录下来。

硬软件环境

电脑:iMac
系统:OX EI Caption 版本10.11.4

Homebrew

官方推荐使用Homebrew来管理依赖项的下载,安装。

##Anaconda Python Anaconda是python的一个常用的科学计算发行版,自带包管理器conda。从 https://store.continuum.io/cshop/anaconda/ 下载并安装Anaconda Python。安装成功之后,记得在PATH添加ananconda执行文件的路径:

export PATH=~/anaconda/bin:$PATH

CUDA

https://developer.nvidia.com/cuda-downloads 下载并安装CUDA 8.0 (for OSX)
http://www.nvidia.com/object/mac-driver-archive.html 下载并安装最新的CUDA独立驱动。 同样我们把驱动的可执行文件路径添加到PATH环境变量中(注意下载安装的CUDA的版本号):

export PATH=/Developer/NVIDIA/CUDA-8.0/bin/$PATH
export DYLD_LIBRARY_PATH=/Developer/NVIDIA/CUDA-8.0/lib:$DYLD_LIBRARY_PATH

BLAS

网上搜索到的教程中都是使用Intel MKL,查看网站会发现intel-mkl收费很贵,所以我安装的时候选择了OpenBLAS

brew install openblas

关于 intel-mkl 和 openblas 的优劣,没有做过比较。在校大学生可通过学校邮箱在页面 https://software.intel.com/en-us/qualify-for-free-software/student 申请安装包。

##cuDNN

https://developer.nvidia.com/cudnn 页面下载并安装cuDNN库,安装过程如下:

tar -xzvf cudnn-8.0-osx-x64-v5.1.tgz
cd cuda
sudo cp lib/* /usr/local/cuda/lib
sudo cp include/cudnn.h /usr/local/cuda/include/

##安装依赖项 via Homebrew

首先我们需要修改下homebrew安装 opencv 时需要用到的opencv.rb文件

brew edit opencv

将下面两行:

args << "-DPYTHON_LIBRARY=#{py_lib}/libpython2.7.#{dylib}"
args << "-DPYTHON_INCLUDE_DIR=#{py_prefix}/include/python2.7"

替换为:

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

然后开始安装依赖项:

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
brew install hdf5

安装过程有点漫长,可能会出现一些错误,需要耐心的修复掉。
如果你把电脑升级到最新的 macOS Sierra 10.12,会由于新系统不内置QTKit导致opencv编译失败。

##下载Caffe代码

git clone https://github.com/BVLC/caff.git
cd caffe
cp Makefile.config.example Makefile.config

##Makefile.config

修改Makefile.config

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

取消CUDDNN的注释

USE_CUDNN := 1

检查设置Python路径

# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
# 		/usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		# $(ANACONDA_HOME)/include/python2.7 \
		# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

ANACONDA_HOME := $(HOME)/anaconda
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \


# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

# Homebrew installs numpy in a non standard path (keg only)
PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
PYTHON_LIB += $(shell brew --prefix numpy)/lib

##环境变量

export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:/Users/cp/anaconda/lib:/usr/local/lib:/usr/local/Cellar/:/usr/local/cuda/lib:~/anaconda/lib:/usr/local/lib:/usr/lib:$DYLD_FALLBACK_LIBRARY_PATH

##编译caffe make clean make all -j8 make runtest

runtest 相对会花些时间

make pycaffe
make distribute

就大功告成了

##References: