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蛆楞,重新編譯溯乒。解決。