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

深度学习框架介绍

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

Theano是一个Python库,也是一个强大的数学表达式编译器。Pylearn2是在Theano基础上建立的机器学习库。用户可以用数学表达式写Pylearn2的插件(新的model, algorithm等), Theano将这些表达式进行优化和稳定化,然后进行编译。

Caffe是由Berkely Vision and Learning Center的贾杨清博士(毕业后在谷歌工作)主导开发的基于ConvNets和C++的深度学习库。Caffe允许网络模型和优化方法都定义在配置文件中而不需要写代码,可以很方便地在CPU和GPU之间切换。

Torch更偏向企业级应用,是用Lua写的,Facebook AI实验室和Google DeepMind团队等都使用Torch。可以为机器学习算法提供类似于Matlab的环境。Lua可以轻易地与C结合,任何C或者C++库都可以成为Lua库。OverFeat是用Torch7在ImageNet上训练得到的特征提取工具。

Cuda-convnet或者CuDNN是NVIDIA提供的基于GPU加速的深度学习工具,对主流的软件包括Caffe,Torch和Theano都提供支持。

Deeplarning4j面向商业应用,是基于Java的机器学习框架。更多介绍可阅读各自的网站或者阅读这篇文章。

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

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 http://caffe.berkeleyvision.org/installation.html

# 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

如果运行上述命令又出问题,比如brew的权限问题:permission denied for /usr/local。需要设置一下权限,更新一下brew, 为此,运行

$ sudo chown -R $USER:admin /usr/local

$ cd /usr/local

$ git reset --hard origin/master

$ brew update

上述问题可以得到解决。

2.3 上一步通过后,运行

$ make test

这一步没问题。将build_release/lib下的所有文件复制到/usr/local/lib

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

再运行

$ make runtest

报错:

.build_release/tools/caffe

dyld: Library not loaded: @rpath/libcudart.7.5.dylib

Referenced from: /Developer/caffe/.build_release/tools/caffe

Reason: image not found

为此需要设置一下环境变量DYLD_FALLBACK_LIBRARY_PATH

$ export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:/usr/local/lib:$(HOME)/anaconda/lib

再运行make runtest,一切顺利。

2.4 如果使用python,再运行

$ make pycaffe

$ make pytest

3, 运行mnist的例子。

详细步骤见:http://caffe.berkeleyvision.org/gathered/examples/mnist.html

3.1,下载mnist数据。在caffe-master目录下运行

$ ./data/mnist/get_mnist.sh

3.2,建立训练数据和测试数据,运行

$ ./examples/mnist/create_mnist.sh

出现以下错误,说convert_mnist_data.bin找不到:

Creating lmdb...

./examples/mnist/create_mnist.sh: line 16: build/examples/mnist/convert_mnist_data.bin: No such file or directory

./examples/mnist/create_mnist.sh: line 18: build/examples/mnist/convert_mnist_data.bin: No such file or directory

Done.

解决办法:搜索convert_mnist_data.bin发现该文件位于./distribute/bin目录下,因此在在./examples/mnist/create_mnist.sh文件中将BUILD的值改为distribute/bin即可。

3.3,训练和测试,运行:

$ ./examples/mnist/create_mnist.sh

如果出现和上面类似的错误,说caffe找不到 (caffe.bin位于./distribute/bin目录下或者build/tools下),检查create_mnist.sh的内容,保证caffe.bin的路径正确

./distribute/bin/caffe.bin train--solver=examples/mnist/lenet_solver.prototxt

然后就能看到运行结果了。

4, 在python中使用caffe的例子

详见:http://nbviewer.jupyter.org/github/BVLC/caffe/blob/master/examples/00-classification.ipynb

该例子用Caffe中已经训练好的模型(基于Alexnet的结构)对图像进行分类。并且可以显示不同层中训练得到的特征。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值