學(xué)習(xí)Caffe(一)安裝Caffe

Caffe是一個(gè)深度學(xué)習(xí)框架坚冀,本文講闡述如何在linux下安裝GPU加速的caffe脊另。
系統(tǒng)配置是:

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

安裝方法參見caffe的官方文檔:http://caffe.berkeleyvision.org/installation.html#compilation
依賴項(xiàng):

  • CUDA:推薦7.0以上的cuda和最新的顯卡驅(qū)動导狡。
  • BLAS:ATLAS, MKL, or OpenBLAS。C++矩陣運(yùn)算庫偎痛。
  • Boost >= 1.55旱捧。用到一些數(shù)學(xué)函數(shù)等。
  • protobuf:是一種輕便踩麦、高效的結(jié)構(gòu)化數(shù)據(jù)存儲格式枚赡,可以用于結(jié)構(gòu)化數(shù)據(jù)串行化,很適合做數(shù)據(jù)存儲或 RPC 數(shù)據(jù)交換格式靖榕。
  • glog&&gflags:谷歌的一個(gè)日志庫标锄;命令行參數(shù)解析庫顽铸。方便調(diào)試使用茁计。
  • hdf5:
  • lmdb,leveldb:數(shù)據(jù)庫IO。準(zhǔn)備數(shù)據(jù)時(shí)會用到。

可選依賴:

  • 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星压,統(tǒng)一計(jì)算架構(gòu))是由NVIDIA所推出的一種集成技術(shù)践剂,是該公司對于GPGPU的正式名稱。通過這個(gè)技術(shù)娜膘,用戶可利用NVIDIA的GeForce 8以后的GPU和較新的Quadro GPU進(jìn)行計(jì)算逊脯。亦是首次可以利用GPU作為C-編譯器的開發(fā)環(huán)境。

安裝過程

1.下載Cuda

下載CUDA:https://developer.nvidia.com/cuda-downloads 選擇下載deb包(或者runfile)竣贪,下載完后用mu5sum檢查一下文件是否完整军洼。按照cuda官方文檔安裝cuda.

2.安裝

先關(guān)閉桌面顯示管理器lightdm,進(jìn)入字符界面演怎,在字符界面安裝cuda匕争。(這是因?yàn)閏uda的安裝包里包含了顯卡驅(qū)動,安裝驅(qū)動前要先關(guān)閉桌面顯示管理器)
(也可分別安裝顯卡驅(qū)動與cuda庫)

sudo service stop

切換到deb包目錄爷耀,執(zhí)行下面的命令

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

然后重啟電腦:sudo reboot
注意甘桑,cuda的安裝包中已經(jīng)包含了較新版本的顯卡驅(qū)動。

3.配置環(huán)境變量

將cuda安裝目錄下的bin路徑導(dǎo)出到系統(tǒng)的搜索路徑path


這里寫圖片描述

并使之生效


這里寫圖片描述

添加動態(tài)庫查找路徑:在 /etc/ld.so.conf.d/加入文件 cuda.conf, 內(nèi)容如下

/usr/local/cuda/lib64

保存后歹叮,執(zhí)行下列命令使之立刻生效:

sudo ldconfig

4.驗(yàn)證

查看Cuda的C編譯器NVCC的版本:

nvcc -V

這里寫圖片描述

編譯并運(yùn)行例子跑杭,進(jìn)入cuda目錄下的samples目錄,然后在該目錄下make咆耿,等待十來分鐘德谅。編譯完成后,可以在Samples里面找到bin/x86_64/linux/release/目錄票灰,并切換到該目錄
運(yùn)行deviceQuery程序女阀,查看輸出結(jié)果如下(重點(diǎn)關(guān)注最后一行,Pass表示通過測試)屑迂。


這里寫圖片描述

5.gcc編譯器版本

該版本cuda不支持gcc5.0的編譯器

參考文獻(xiàn):
[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目錄分別復(fù)制到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依賴庫

進(jìn)入caffe/python目錄惹盼,安裝依賴項(xiàng):

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

caffe官網(wǎng)推薦使用Anaconda http://continuum.io/downloads#all Anaconda是一個(gè)和Canopy類似的科學(xué)計(jì)算環(huán)境庸汗,但用起來更加方便。自帶的包管理器conda也很強(qiáng)大手报。

MatCaffe

安裝matlabR2014a

編譯caffe

復(fù)制并修改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 ?= @

進(jìn)入caffe目錄蚯舱,執(zhí)行:

make all
make test
make runtest

無錯(cuò)誤,編譯完成掩蛤。

編譯pycaffe與matcaffe

進(jìn)入caffe目錄枉昏,執(zhí)行

make pycaffe
make matcaffe

Caffe python接口

復(fù)制caffe/python/caffe 到/usr/local/lib/python2.7/dist-packages/目錄下。
復(fù)制caffe/build/lib/下的庫文件到/usr/local/lib

$ sudo ldconfig

打開python揍鸟,import caffe兄裂,無錯(cuò)誤。

Caffe C++接口

分別將include,lib目錄復(fù)制。

測試

測試mnist http://caffe.berkeleyvision.org/gathered/examples/mnist.html

準(zhǔn)備數(shù)據(jù)

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

LeNet: the MNIST Classification Model

...


升級cuda8.0

安裝cuda8.0晰奖,重啟
編譯cuda samples無法運(yùn)行谈撒,提示錯(cuò)誤:


這里寫圖片描述

應(yīng)該是驅(qū)動版本沒有更新
查看/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錯(cuò)誤

https://github.com/BVLC/caffe/issues/1917
先卸載protobuf,然后下載源碼啃匿,按照上文編譯。
修改caffe的makefile蛆楞,重新編譯溯乒。解決。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末豹爹,一起剝皮案震驚了整個(gè)濱河市橙数,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌帅戒,老刑警劉巖灯帮,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異逻住,居然都是意外死亡钟哥,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進(jìn)店門瞎访,熙熙樓的掌柜王于貴愁眉苦臉地迎上來腻贰,“玉大人,你說我怎么就攤上這事扒秸〔パ荩” “怎么了?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵伴奥,是天一觀的道長写烤。 經(jīng)常有香客問我,道長拾徙,這世上最難降的妖魔是什么洲炊? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任,我火速辦了婚禮尼啡,結(jié)果婚禮上暂衡,老公的妹妹穿的比我還像新娘。我一直安慰自己崖瞭,他們只是感情好狂巢,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著书聚,像睡著了一般唧领。 火紅的嫁衣襯著肌膚如雪代态。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天疹吃,我揣著相機(jī)與錄音,去河邊找鬼西雀。 笑死萨驶,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的艇肴。 我是一名探鬼主播腔呜,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼再悼!你這毒婦竟也來了核畴?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤冲九,失蹤者是張志新(化名)和其女友劉穎谤草,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體莺奸,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡丑孩,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了灭贷。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片温学。...
    茶點(diǎn)故事閱讀 40,561評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖甚疟,靈堂內(nèi)的尸體忽然破棺而出仗岖,到底是詐尸還是另有隱情,我是刑警寧澤览妖,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布轧拄,位于F島的核電站,受9級特大地震影響讽膏,放射性物質(zhì)發(fā)生泄漏紧帕。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一桅打、第九天 我趴在偏房一處隱蔽的房頂上張望是嗜。 院中可真熱鬧,春花似錦挺尾、人聲如沸鹅搪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽丽柿。三九已至恢准,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間甫题,已是汗流浹背馁筐。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留坠非,地道東北人敏沉。 一個(gè)月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像炎码,于是被迫代替她去往敵國和親盟迟。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內(nèi)容