Ubuntu14.04 Caffe安装

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的显卡,当然这个不是必须的

依赖程序

基本包

安装开发需要的一些基本包

sudo apt-get update
sudo apt-get install build-essential
sudo 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’文件根据自己的需求进行修改,下面是我的配置以及一些注释。

## 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   #如果没有安装cudnn的注掉

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1   #如果没有安装cuda,不是要GPU的设置开启  

# 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 := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda   #cuda默认安装目录(其实是软链接),一般不需要修改,但如果没有安装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 := mkl   #这里使用的是Intel MKL,如果使用的ATLAS,请自行修改
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /opt/intel/mkl/include   #如果是atlas,这两个路径要注掉
BLAS_LIB := /opt/intel/mkl/lib/intel64

# 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/R2014a   #Matlab的路径,没安装Matlab或者不需要编译该接口可以注掉
# MATLAB_DIR := /Applications/MATLAB_R2012b.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库的路径,使用的是虚拟环境capy这里就是一直提到的ENV,有时候可能不起作用,还是会报错,不知道为什么,那就设置下面的export,一般就没问题了。
PYTHON_INCLUDE := /home/huayong/capy/include/python2.7 \
                /home/huayong/capy/lib/python2.7/site-packages/numpy/core/include

#If it doesn't work above, you can try this below.
export CPLUS_INCLUDE_PATH=/home/huayong/capy/include/python2.7:/home/huayong/capy/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/wenzheng/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
                # $(ANACONDA_HOME)/include/python2.7 \
                # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \

# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib/x86_64-linux-gnu
# PYTHON_LIB := $(ANACONDA_HOME)/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/include
# LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/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 ?= @

caffe编译

基本的编译,j代表是多线程编译,一般前两个命令不会出错,最后一个可能会出错,但有时候也不影响使用。

make all -j8
make alltest -j8
make runtest

编译matlab和python接口,这个当然不是必须的,看你自己的需求。

make pycaffe
make matcaffe

结束语

到这来我们就成功编译好了caffe,那么下面就要开始真正的caffe使用之旅了。下面有时间会把自己学习使用caffe的一些过程写下来,包括训练测试caffe提供的examples,caffe的可视化,运行一些caffe经典的例子(如R-CNN)。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值