Caffe简介
Caffe全称Convolutional Architecture for Fast Feature Embedding,是一个计算CNN相关算法的框架。就目前来说是deeplearning最流行的开源的框架,所以使用的人也是最多的。
caffe网址:http://caffe.berkeleyvision.org/
github网址:https://github.com/BVLC/caffe/
安装环境
系统:Ubuntu14.04 Desktop x64(建议使用刚安装好的系统,同时不要更新,不然会影响后面的某些环节)
显卡:支持CUDA的显卡,当然这个不是必须的
依赖程序
基本包
安装开发需要的一些基本包
1sudo apt-get update 2sudo apt-get install build-essential 3sudo apt-get install gfortran ## python中安装scipy需要依赖gfortran
注:Ubuntu默认环境虽然已经有gcc,但并没有提供C/C++的编译环境,单独安装编译环境比较复杂,所以提供了build-essential命令一键来构建所需的编译环境。
CUDA(可选)
cuda在caffe编译和使用中不是必须的,如果实在没有支持cuda的显卡或者只想尝试一下caffe,不需要用它来进行复杂的训练,不安装也可以。
安装链接:http://blog.csdn.net/honyniu/article/details/46387429
CuDNN(可选)
这个库是基于cuda,当然也是可选的。
安装链接:http://blog.csdn.net/honyniu/article/details/46388241
BLAS(必选)
这个库提供了caffe需要的一些基本的矩阵和向量库,这个是必选的。
安装链接:http://blog.csdn.net/honyniu/article/details/46388915
OpenCV(必选)
OpenCV提供了图像处理和计算机视觉的库,在caffe使用中是必须的,同时最好自己编译安装。
安装链接:http://blog.csdn.net/honyniu/article/details/46390097
其他依赖项
caffe需要一些特定的数据库的操作, 因此需要安装leveldb, lmdb, hdf5等库。 此外, caffe采用了google的一些库, 比如protobuf以及glog, 这些也需要安装,命令如下。
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
Matlab和python
这两个不是必须的,但是caffe提供了bash、matlab和python的接口,同时caffe许多的可视化接口都是通过python实现的,推荐安装。
matlab在这里使用的是Matlab-R2014A,这里就不讲怎么安装得了,自己去百度教程,默认安装即可,安装在’/usr/local/MATLAB’目录下。
ubuntu默认安装的2.7的版本但是为了安装Python开发环境,方便今后编译其他扩展库,命令如下
sudo apt-get install python-dev
python是使用的虚拟环境,具体虚拟环境的构建见http://blog.csdn.net/honyniu/article/details/46382929
激活虚拟环境
source ENV/bin/activate
安装编译pycaffe的依赖包, 进入caffe安装目录中的python文件夹, 你会看到一个requirements.txt, 首先, 修改protobuf那一行为protobuf==2.5.0, 然后执行下面命令:
for req in $(cat requirements.txt); do pip install $req; done
这里我们没安装caffe推荐的anaconda包, 因为安装之后进不去桌面。而且anaconda包只是一个自带了requirements.txt中所有选项的python集合库, 将上面的库安装好之后, 不装anaconda没有任何影响。
配置编译
caffe程序
可以去上面的caffe的github网址把caffe最新代码下载下来,也可以使用git命令获取,如下
git clone https://github.com/BVLC/caffe.git
caffe配置
终于到了最后的一步了,如果安装好了上面的所有的caffe的依赖,那么下面就可以对caffe进行配置和编译了。
首先拷贝生成所需要的’Makefile.config’文件,caffe安装包下面给提供了example,注意直接拷贝下面的可能会报错,因为每一行命令的后面不能有空白的字符串,有可能会导致编译不通过。
cp Makefile.config.example Makefile.config
然后对该’Makefile.config’文件根据自己的需求进行修改,下面是我的配置以及一些注释。
1## Refer to http://caffe.berkeleyvision.org/installation.html 2# Contributions simplifying and improving our build system are welcome! 3 4# cuDNN acceleration switch (uncomment to build with cuDNN). 5USE_CUDNN := 1 #如果没有安装cudnn的注掉 6 7# CPU-only switch (uncomment to build without GPU support). 8# CPU_ONLY := 1 #如果没有安装cuda,不是要GPU的设置开启 9 10# To customize your choice of compiler, uncomment and set the following. 11# N.B. the default for Linux is g++ and the default for OSX is clang++ 12# CUSTOM_CXX := g++ 13 14# CUDA directory contains bin/ and lib/ directories that we need. 15CUDA_DIR := /usr/local/cuda #cuda默认安装目录(其实是软链接),一般不需要修改,但如果没有安装cuda需要注掉 16# On Ubuntu 14.04, if cuda tools are installed via 17# "sudo apt-get install nvidia-cuda-toolkit" then use this instead: 18# CUDA_DIR := /usr 19 20# CUDA architecture setting: going with all of them. 21# For CUDA < 6.0, comment the *_50 lines for compatibility. 22CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \ 23 -gencode arch=compute_20,code=sm_21 \ 24 -gencode arch=compute_30,code=sm_30 \ 25 -gencode arch=compute_35,code=sm_35 \ 26 -gencode arch=compute_50,code=sm_50 \ 27 -gencode arch=compute_50,code=compute_50 28 29# BLAS choice: 30# atlas for ATLAS (default) 31# mkl for MKL 32# open for OpenBlas 33BLAS := mkl #这里使用的是Intel MKL,如果使用的ATLAS,请自行修改 34# Custom (MKL/ATLAS/OpenBLAS) include and lib directories. 35# Leave commented to accept the defaults for your choice of BLAS 36# (which should work)! 37BLAS_INCLUDE := /opt/intel/mkl/include #如果是atlas,这两个路径要注掉 38BLAS_LIB := /opt/intel/mkl/lib/intel64 39 40# This is required only if you will compile the matlab interface. 41# MATLAB directory should contain the mex binary in /bin. 42MATLAB_DIR := /usr/local/MATLAB/R2014a #Matlab的路径,没安装Matlab或者不需要编译该接口可以注掉 43# MATLAB_DIR := /Applications/MATLAB_R2012b.app 44 45# NOTE: this is required only if you will compile the python interface. 46# We need to be able to find Python.h and numpy/arrayobject.h. 47#设置是python库的路径,使用的是虚拟环境capy这里就是一直提到的ENV,有时候可能不起作用,还是会报错,不知道为什么,那就设置下面的export,一般就没问题了。 48PYTHON_INCLUDE := /home/huayong/capy/include/python2.7 \ 49 /home/huayong/capy/lib/python2.7/site-packages/numpy/core/include 50 51#If it doesn't work above, you can try this below. 52export CPLUS_INCLUDE_PATH=/home/huayong/capy/include/python2.7:/home/huayong/capy/lib/python2.7/site-packages/numpy/core/include 53 54# Anaconda Python distribution is quite popular. Include path: 55# Verify anaconda location, sometimes it's in root. 56# ANACONDA_HOME := /home/wenzheng/anaconda 57# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \ 58 # $(ANACONDA_HOME)/include/python2.7 \ 59 # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \ 60 61# We need to be able to find libpythonX.X.so or .dylib. 62PYTHON_LIB := /usr/lib/x86_64-linux-gnu 63# PYTHON_LIB := $(ANACONDA_HOME)/lib 64 65# Uncomment to support layers written in Python (will link against Python libs) 66WITH_PYTHON_LAYER := 1 67 68# Whatever else you find you need goes here. 69# INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include 70# LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 71 72# Uncomment to use `pkg-config` to specify OpenCV library paths. 73# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.) 74USE_PKG_CONFIG := 1 75 76BUILD_DIR := build 77DISTRIBUTE_DIR := distribute 78 79# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171 80# DEBUG := 1 81 82# The ID of the GPU that 'make runtest' will use to run unit tests. 83TEST_GPUID := 0 84 85# enable pretty build (comment to see full commands) 86Q ?= @
caffe编译
基本的编译,j代表是多线程编译,一般前两个命令不会出错,最后一个可能会出错,但有时候也不影响使用。
1make all -j8 2make alltest -j8 3make runtest
编译matlab和python接口,这个当然不是必须的,看你自己的需求。
1make pycaffe 2make matcaffe
结束语
到这来我们就成功编译好了caffe,那么下面就要开始真正的caffe使用之旅了。下面有时间会把自己学习使用caffe的一些过程写下来,包括训练测试caffe提供的examples,caffe的可视化,运行一些caffe经典的例子(如R-CNN)。
本文同步分享在 博客“无比机智的永哥”(CSDN)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。