学习Caffe(一)安装Caffe

Caffe是一个深度学习框架,本文讲阐述如何在linux下安装GPU加速的caffe。
系统配置是:

  • OS: Ubuntu14.04
  • CPU: i5-4690
  • GPU: GTX960
  • RAM: 8G

安装方法参见caffe的官方文档:http://caffe.berkeleyvision.org/installation.html#compilation
依赖项:

  • CUDA:推荐7.0以上的cuda和最新的显卡驱动。
  • BLAS:ATLAS, MKL, or OpenBLAS。C++矩阵运算库。
  • Boost >= 1.55。用到一些数学函数等。
  • protobuf:是一种轻便、高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式。
  • glog&&gflags:谷歌的一个日志库;命令行参数解析库。方便调试使用。
  • hdf5:
  • lmdb,leveldb:数据库IO。准备数据时会用到。

可选依赖:

  • OpenCV >= 2.4 including 3.0
  • IO libraries: lmdb, leveldb (note: leveldb requires snappy)
  • cuDNN for GPU acceleration (v5)

Pycaffe:
Python 2.7 or Python 3.3+, numpy (>= 1.7), boost-provided boost.python

Matcaffe:
MATLAB with the mex compiler

安装CUDA7.5

CUDA维基百科:https://zh.wikipedia.org/wiki/CUDA
CUDA(Compute Unified Device Architecture,统一计算架构)是由NVIDIA所推出的一种集成技术,是该公司对于GPGPU的正式名称。通过这个技术,用户可利用NVIDIA的GeForce 8以后的GPU和较新的Quadro GPU进行计算。亦是首次可以利用GPU作为C-编译器的开发环境。

安装过程

1.下载Cuda

下载CUDA:https://developer.nvidia.com/cuda-downloads 选择下载deb包(或者runfile),下载完后用mu5sum检查一下文件是否完整。按照cuda官方文档安装cuda.

2.安装

先关闭桌面显示管理器lightdm,进入字符界面,在字符界面安装cuda。(这是因为cuda的安装包里包含了显卡驱动,安装驱动前要先关闭桌面显示管理器)
(也可分别安装显卡驱动与cuda库)

sudo service stop

切换到deb包目录,执行下面的命令

sudo dpkg -i cuda-repo-<distro>_<version>_<architecture>.deb  
sudo apt-get update  
sudo apt-get install cuda  

然后重启电脑:sudo reboot
注意,cuda的安装包中已经包含了较新版本的显卡驱动。

3.配置环境变量

将cuda安装目录下的bin路径导出到系统的搜索路径path


这里写图片描述

并使之生效


这里写图片描述

添加动态库查找路径:在 /etc/ld.so.conf.d/加入文件 cuda.conf, 内容如下

/usr/local/cuda/lib64

保存后,执行下列命令使之立刻生效:

sudo ldconfig

4.验证

查看Cuda的C编译器NVCC的版本:

nvcc -V

这里写图片描述

编译并运行例子,进入cuda目录下的samples目录,然后在该目录下make,等待十来分钟。编译完成后,可以在Samples里面找到bin/x86_64/linux/release/目录,并切换到该目录
运行deviceQuery程序,查看输出结果如下(重点关注最后一行,Pass表示通过测试)。


这里写图片描述

5.gcc编译器版本

该版本cuda不支持gcc5.0的编译器

参考文献:
[1]Ubuntu 16.04 安装 NVIDIA CUDA Toolkit 7.5 https://gist.github.com/dangbiao1991/2c895917ea888ce33af8c1c72444b7bf
[2]Ubuntu 14.04+cuda 7.5+caffe安装配置 http://blog.csdn.net/ubunfans/article/details/47724341

安装Cudnn

下载cudnn https://developer.nvidia.com/rdp/cudnn-download, 解压,把lib目录,include目录分别复制到cuda的安装目录下。

安装BLAS

install ATLAS by sudo apt-get install libatlas-base-dev or install OpenBLAS or MKL for better CPU performance.

下载Caffe

安装Caffe依赖库

通用依赖库:

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev

Ubuntu14.04 依赖库:

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

PyCaffe依赖库

进入caffe/python目录,安装依赖项:

for req in $(cat requirements.txt); do pip install $req; done

caffe官网推荐使用Anaconda http://continuum.io/downloads#all Anaconda是一个和Canopy类似的科学计算环境,但用起来更加方便。自带的包管理器conda也很强大。

MatCaffe

安装matlabR2014a

编译caffe

复制并修改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 := 3

# 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
# 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 := 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 := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas

# 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/R2014a
# 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_INCLUDE := /usr/include/python2.7 \
        /usr/local/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 \

# 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

# 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

# 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

# N.B. both build and distribute dirs are cleared on `make clean`
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目录,执行:

make all
make test
make runtest

无错误,编译完成。

编译pycaffe与matcaffe

进入caffe目录,执行

make pycaffe
make matcaffe

Caffe python接口

复制caffe/python/caffe 到/usr/local/lib/python2.7/dist-packages/目录下。
复制caffe/build/lib/下的库文件到/usr/local/lib

$ sudo ldconfig

打开python,import caffe,无错误。

Caffe C++接口

分别将include,lib目录复制。

测试

测试mnist http://caffe.berkeleyvision.org/gathered/examples/mnist.html

准备数据

cd $CAFFE_ROOT
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh

LeNet: the MNIST Classification Model

...


升级cuda8.0

安装cuda8.0,重启
编译cuda samples无法运行,提示错误:


这里写图片描述

应该是驱动版本没有更新
查看/etc/modprobe.d/目录下的文件,查看nvidia-graphics-drivers.conf:
将alias nvidia nvidia_352改为alias nvidia_367
将alias nvidia-uvm nvidia_352-uvm改为alias nvidia-uvm nvidia_367-uvm


这里写图片描述

问题解决。

Caffe matlab protobuf错误

https://github.com/BVLC/caffe/issues/1917
先卸载protobuf,然后下载源码,按照上文编译。
修改caffe的makefile,重新编译。解决。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268

推荐阅读更多精彩内容