深度学习框架Caffe在Mac上的安装和测试

深度学习框架介绍

先概括一下深度学习的几大流行的框架:Pylearn2, Theano, Caffe, Torch, Cuda-covnet,Deeplarning4j等。

Caffe的安装

Caffe的网站上提供了安装说明。由于其依赖的库比较多,通常安装过程会出现许多问题,在不同的机器和操作系统上可能遇到不同的问题。安装时可以根据网站上提供的说明步骤进行,遇到有问题时用Google搜索一下基本都能找到。本文记录了笔者在Mac上安装遇到的问题和解决办法。系统版本:OS X 10.9.5。

    1,安装Caffe的依赖库

1.1 安装CUDA。推荐7.0以上版本,6.*版本也可以。我安装的是最新版CUDA 7.5。

1.2 安装BLAS。这里我使用了OpenBLAS。推荐使用brew安装:brew install openblas

1.3 安装Boost。

通过brew install boost默认安装版本为1.60。但建议使用1.59。因为1.60编译后可能会出现问题。 

$ brew search boost boost homebrew/versions/boost-python159 ✔ boost-bcp homebrew/versions/boost149 boost-build homebrew/versions/boost150 boost-python homebrew/versions/boost155 homebrew/science/boost-compute homebrew/versions/boost159 ✔ Caskroom/cask/iboostup Caskroom/cask/turbo-boost-switcher Caskroom/cask/pivotalbooster $ brew install –build-from-source homebrew/versions/boost159

安装好后可以后在/usr/local/opt/boost159下看到该库。Caffe中把某些依赖库所在的文件夹名字限定为boost,可以将/usr/local/opt/boost159复制粘贴产生备份,将备份改名为/usr/local/opt/boost。

1.4 安装CuDNN。下载cuDNN v5.0版本。解压后将include和bin文件夹中的内容分别复制到/usr/lcoal 下面的/include和/bin中。

1.5 使用brew install 分别安装 protobuf, glog, gflags, hdf5, snappy, leveldb, szip, lmdb等。

如果使用python, protobuf安装命令为

$ brew install --build-from-source --with-python -vd protobuf<code> </code><code></code>

1.6  (可选)OpenCV, 我使用2.4.6版本。

1.7  (可选)Python 版本:2.7。

需要安装numpy。推荐使用Anaconda,里面包含了一个python版本2.7.11并且包含了大多数所需要的库,包括hdf5、numpy等。Anaconda默认安装在$(HOME)/anaconda目录下。

还需要安装python-boost。与boost类似的方法,推荐1.59版本。

1.8  (可选)Matlab 版本 2015a

    2,安装Caffe

2.1 下载Caffe后在caffe-master文件夹下,以Makefile.config.example为模板根据第一步中的安装情况,建立配置文件Makefile.config,内容如下:

## Refer to # Contributions simplifying and improving our build system are welcome! # cuDNN acceleration switch (uncomment to build with cuDNN). USE_CUDNN := 1 # CPU-only switch (uncomment to build without GPU support). # CPU_ONLY := 1 # uncomment to disable IO dependencies and corresponding data layers # USE_OPENCV := 0 # USE_LEVELDB := 0 # USE_LMDB := 0 # uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary) # You should not set this flag if you will be reading LMDBs with any # possibility of simultaneous read and write ALLOW_LMDB_NOLOCK := 1 # Uncomment if you're using OpenCV 3 OPENCV_VERSION := 2.4 # To customize your choice of compiler, uncomment and set the following. # N.B. the default for Linux is g++ and the default for OSX is clang++ CUSTOM_CXX := clang++ # CUDA directory contains bin/ and lib/ directories that we need. CUDA_DIR := /usr/local/cuda # On Ubuntu 14.04, if cuda tools are installed via # "sudo apt-get install nvidia-cuda-toolkit" then use this instead: # CUDA_DIR := /usr # CUDA architecture setting: going with all of them. # For CUDA < 6.0, comment the *_50 lines for compatibility. CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ -gencode arch=compute_20,code=sm_21 \ -gencode arch=compute_30,code=sm_30 \ -gencode arch=compute_35,code=sm_35 \ -gencode arch=compute_50,code=sm_50 \ -gencode arch=compute_50,code=compute_50 # BLAS choice: # atlas for ATLAS (default) # mkl for MKL # open for OpenBlas BLAS := open # Custom (MKL/ATLAS/OpenBLAS) include and lib directories. # Leave commented to accept the defaults for your choice of BLAS # (which should work)! # BLAS_INCLUDE := /usr/local/opt/openblas/include # BLAS_LIB := /usr/local/opt/openblas/lib # Homebrew puts openblas in a directory that is not on the standard search path BLAS_INCLUDE := $(shell brew --prefix openblas)/include BLAS_LIB := $(shell brew --prefix openblas)/lib # This is required only if you will compile the matlab interface. # MATLAB directory should contain the mex binary in /bin. # MATLAB_DIR := /usr/local MATLAB_DIR := /Applications/MATLAB_R2015a.app # 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 \ /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-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 \ # 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 #PYTHON_LIB +=/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/lib PYTHON_LIB +=$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/lib # Uncomment to support layers written in Python (will link against Python libs) WITH_PYTHON_LAYER := 1 # Whatever else you find you need goes here. INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/Cellar/boost159/1.59.0/include LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/cuda/lib /usr/local/Cellar/boost159/1.59.0/lib /usr/local/opt/boost-python159/lib # If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies INCLUDE_DIRS += $(shell brew --prefix)/include LIBRARY_DIRS += $(shell brew --prefix)/lib # Uncomment to use `pkg-config` to specify OpenCV library paths. # (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) # USE_PKG_CONFIG := 1 BUILD_DIR := build DISTRIBUTE_DIR := distribute # Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 # DEBUG := 1 # The ID of the GPU that 'make runtest' will use to run unit tests. TEST_GPUID := 0 # enable pretty build (comment to see full commands) Q ?= @


    2.2,命令行进入caffe-master文件夹下,运行:

$ make all

出现问题:

PROTOC src/caffe/proto/caffe.proto make: protoc: No such file or directory

解决办法:  需要用brew建立protobuf的链接。为此,运行

$ brew link protobuf

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/15221.html