1. 程式人生 > >Ubuntu16.04 CUDA8.0+caffe+gpu執行環境配置

Ubuntu16.04 CUDA8.0+caffe+gpu執行環境配置

完成win10+ubuntu 16.04雙系統的安裝後,開始在ubuntu16.04系統上安裝nVidia驅動,配置cuda,cudnn執行環境,並完成caffe成功配置+MNIST資料庫測試。現在寫下部落格記錄安裝配置過程,以及對配置過程中遇到的錯誤提供解決辦法,避免今後再配置caffe時又踩坑。

電腦配置
系統:Ubuntu16.04
GPU:NVIDIA GTX1070
 
一、安裝nVidia驅動,配置cuda,cudnn執行環境
1.安裝相關依賴項
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  
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev  
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
 
如果安裝錯誤,重新執行程式碼即可,保證網路暢通。

2.安裝NVIDIA驅動
(1)查詢NVIDIA驅動
首先去官網(http://www.nvidia.com/Download/index.aspx?lang=en-us)檢視適合自己顯示卡的驅動:


圖1.顯示卡驅動查詢

驅動檔案字尾名應當是以.run結尾的。把這個檔案移動到home目錄下。

我的顯示卡型號是GTX1070,系統是linux 64位,按照要求選擇後點擊search.,點選下載就好了。我下載後的驅動檔案是:NVIDIA-Linux-x86_64-384.98.run。

更新:後面測試MNIST資料庫時發現報了一個錯,懷疑是驅動版本太新的問題,因此我又解除安裝了384.98,重新安裝了375.82。在www.geforce.cn/drivers 下可以選擇適合GTX1070的不同驅動版本,我選擇了375.82。

(2)安裝驅動
在終端下輸入:sudo gedit /etc/modprobe.d/blacklist.conf 
輸入密碼後在最後一行加上 blacklist nouveau 。這裡是將Ubuntu自帶的顯示卡驅動加入黑名單。 
在終端輸入:sudo update-initramfs -u 
重啟電腦~ 這裡要尤其注意,安裝顯示卡驅動要先切換到文字介面,(按Ctrl+Alt+F1~F6)。所以,啟動電腦後,先進入文字介面。 
然後,輸入命令sudo service lightdm stop,關閉X-Win視窗。

現在可以安裝驅動了,先進入家目錄 cd ~ ,然後:sudo ./NVIDIA-Linux-x86_64-384.98.run,按照提示一步步來。若是沒能安裝,可能是許可權問題, chmod 777NVIDIA-Linux-x86_64-384.98.run 修改許可權後再安裝。

完成後,再次重啟電腦。(或者輸入命令sudo service lightdm start,開啟X-Win視窗,然後ALT+CTRL+F7切換回圖形介面即可。) 
安裝完成之後輸入以下指令進行驗證:sudo nvidia-smi,若列出了GPU的資訊列表則表示驅動安裝成功。

tips: 
按ALT+CTRL+F1切換到字元介面(Linux實體機) 
按ALT+CTRL+F7切換到圖形介面(Linux實體機)

3.安裝CUDA
CUDA是NVIDIA的程式語言平臺,想使用GPU就必須要使用cuda。

(1)下載CUDA
首先在官網上(https://developer.nvidia.com/cuda-downloads)下載CUDA,選擇自己合適的版本。 
該連結介面只顯示最新版本。若需要下載以前的版本,可在頁面下方點選Legacy Releases,選擇自己需要的其他版本。 
 
圖2.CUDA下載

(2)安裝CUDA
下載完成後執行以下命令:

sudo chmod 777 cuda_8.0.44_linux.run 
sudo ./cuda_8.0.44_linux.run
1
2
(注意:執行後會先出現一個宣告,需要閱讀到100%,同意聲明後才會開始安裝。) 
安裝時首先會有一系列提示讓你確認,但是注意,有個讓你選擇是否安裝nvidia361驅動時,一定要選擇否:

Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 361.62?
1
因為前面我們已經安裝了更加新的nvidia384,所以這裡不要選擇安裝。其餘的都直接預設或者選擇是即可。

可能出現的錯誤: 
安裝cuda時可能有下面的資訊

Installing the CUDA Toolkit in /usr/local/cuda-8.0 … 
Missing recommended library: libGLU.so 
Missing recommended library: libX11.so 
Missing recommended library: libXi.so 
Missing recommended library: libXmu.so
 
原因是缺少相關的依賴庫,安裝相應庫就解決了:

sudo apt-get install freeglut3-dev build-essential libx11-dev libxmu-dev libxi-dev libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev
 
再次安裝,就不再提示了。

(3)環境變數配置
開啟~/.bashrc檔案: sudo gedit ~/.bashrc 將以下內容寫入到~/.bashrc尾部:

export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
 
(4)測試CUDA的samples
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery 
make 
sudo ./deviceQuery
 
如果顯示一些關於GPU的資訊,則說明安裝成功。

4.配置cuDNN
注意:首次配置cuDNN時我是下載的cuDNN6.0版本,但是後來在編譯fast rcnn及SSD時發現有很多問題是由於cuDNN版本不匹配引起的,因此後來又手動刪除掉了cuDNN6.0的include和lib檔案,重新下載cuDNN5.1版本並重新編譯caffe。

cuDNN是GPU加速計算深層神經網路的庫。 首先去官網https://developer.nvidia.com/rdp/cudnn-download 下載cuDNN,需要註冊一個賬號才能下載。


圖3.cuDNN下載

下載cuDNN5.1 之後進行解壓:

sudo tar -zxvf ./cudnn-8.0-linux-x64-v5.1.tgz
1
進入cuDNN5.1 解壓之後的include目錄,在命令列進行如下操作:

cd cuda/include
sudo cp cudnn.h /usr/local/cuda/include  #複製標頭檔案
 
再進入lib64目錄下的動態檔案進行復制和連結:

(這裡的libcudnn.so.5.1.10是固有檔案,而libcudnn.so.5是libcudnn.so.5.1.10連結得到的動態檔案,libcudnn.so是libcudnn.so.5連結得到的動態檔案。)

cd ..
cd lib64 
sudo cp lib* /usr/local/cuda/lib64/   #複製動態連結庫
 
cd /usr/local/cuda/lib64/
sudo rm -rf libcudnn.so libcudnn.so.5   #刪除原有動態檔案
 
sudo ln -s libcudnn.so.5.1.10 libcudnn.so.5  #生成軟銜接(注意這裡要和自己下載的cudnn版本對應,可以在/usr/local/cuda/lib64下檢視自己libcudnn的版本)
sudo ln -s libcudnn.so.5 libcudnn.so   #生成軟連結
 
注意:下面這個步驟不能缺少!!否則可能連結失敗!

執行

sudo ldconfig -v  #必須在/usr/local/cuda/lib64/目錄下執行,否則可能會報libcudnn.so.5: cannot open shared object file: No such file or directory
1
或者

sudo ldconfig /usr/local/cuda/lib64
1
至此,cudnn就配置完成了。

5.安裝opencv3.3.0
從opencv官網找到GitHub的下載連結,下載Opencv,並將其解壓到你要安裝的位置,假設解壓到了~/opencv。 
安裝前準備,建立編譯資料夾:

cd ~/opencv 
 mkdir build 
 cd build 
 
配置:

sudo apt install cmake 
  sudo cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local .. 
 
編譯:

sudo make -j8
1
-j8表示平行計算,根據自己電腦的配置進行設定,配置比較低的電腦可以將數字改小或不使用,直接輸make。

安裝: 以上只是將opencv編譯成功,還沒將opencv安裝,需要執行下面指令進行安裝:

sudo make install
1
二、安裝Anaconda*
python好用,但是python不同版本和各種包管理很讓人頭疼。Anaconda是一個python的一個包裝,它將一些python常用的package打包,方便pythoners直接使用。不但如此,他還有自己的安裝方式conda,這裡有提供各種pip裡面找不到的包。 
anaconda安裝參照官網下載安裝即可https://www.anaconda.com/download/,傻瓜式安裝非常方便 
安裝之後在命令終端輸入

$ python
Python 2.7.14 |Anaconda custom (64-bit)| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
 
出現上述資訊,說明安裝成功

重要提示:有一些軟體需要py2.7的環境,比如XX-Net, 最好是先安裝Anaconda2,這樣系統預設的環境是py2,然後再安裝Anaconda3。需要py3環境時,用 下面命令切換。

source activate py3
 
三、配置caffe*
(1)將終端cd到要安裝caffe的位置。 
(2)從github上獲取caffe:

git clone https://github.com/BVLC/caffe.git
1
注意:若沒有安裝Git,需要先安裝Git:

sudo apt-get install git
1
(3)因為make指令只能make Makefile.config檔案,而Makefile.config.example是caffe給出的makefile例子,因此,首先將Makefile.config.example的內容複製到Makefile.config:

sudo cp Makefile.config.example Makefile.config
1
(4)開啟並修改配置檔案:

sudo gedit Makefile.config #開啟Makefile.config檔案
1
根據個人情況修改檔案: 
a.若使用cudnn,則


#USE_CUDNN := 1
修改成: 
USE_CUDNN := 1
 
b.若使用的opencv版本是3的,則


#OPENCV_VERSION := 3 
修改為: 
OPENCV_VERSION := 3
 
c.若要使用python來編寫layer,則


#WITH_PYTHON_LAYER := 1 
修改為 
WITH_PYTHON_LAYER := 1
 
d. 重要的一項 : 
將# Whatever else you find you need goes here.下面的

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib 
修改為: 
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial    
 
這是因為ubuntu16.04的檔案包含位置發生了變化,尤其是需要用到的hdf5的位置,所以需要更改這一路徑。 
這裡貼出我修改後的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

# 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  #使用openCV3

# 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路徑
# 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 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 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_52,code=sm_52 \
        -gencode arch=compute_60,code=sm_60 \
        -gencode arch=compute_61,code=sm_61 \
        -gencode arch=compute_61,code=compute_61  #計算能力

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas  #使用哪種BLAS:atlas or mkl or openblas
# 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_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/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)/anaconda2  #使用anaconda,anaconda的路徑
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python2.7 \
        $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include  #anaconda的python標頭檔案路徑

# 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  #anaconda的python庫目錄

# 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  #使用python層

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial  #標頭檔案路徑
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial  #庫路徑

# 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

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# 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  #GPU編號

# enable pretty build (comment to see full commands)
Q ?= @
 
(5)修改makefile檔案 
開啟makefile檔案,做如下修改:

將:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替換為:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
 
(6)編輯/usr/local/cuda/include/host_config.h 
將其中的第115行註釋掉:


#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
改為
//#error-- unsupported GNU version! gcc versions later than 4.9 are not supported!
 
(7)編譯

make all -j8 #-j根據自己電腦配置決定
1
編譯過程中可能會出現如下錯誤: 
錯誤內容1:

"fatal error: hdf5.h: 沒有那個檔案或目錄"
1
解決辦法: 
step1:在Makefile.config檔案的第85行,新增/usr/include/hdf5/serial/ 到 INCLUDE_DIRS,也就是把下面第一行程式碼改為第二行程式碼。

將:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
替換為:
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
 
stept2:在Makefile檔案的第173行,把 hdf5_hl 和hdf5修改為hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行程式碼改為第二行程式碼。

將:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改為:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
 
錯誤內容2:

"libcudart.so.8.0 cannot open shared object file: No such file or directory"
1
解決辦法是將一些檔案複製到/usr/local/lib資料夾下:

#注意自己CUDA的版本號!
sudo cp /usr/local/cuda-8.0/lib64/libcudart.so.8.0 /usr/local/lib/libcudart.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/local/lib/libcublas.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcurand.so.8.0 /usr/local/lib/libcurand.so.8.0 && sudo ldconfig
 
(8)測試

make runtest
1
可能遇到的錯誤:

"libprotobuf.so.14 cannot open shared object file: No such file or directory"
1
輸入 protoc –version 發現可以輸出libprotoc的版本號,說明protobuf已經安裝成功,因此應該是安裝路徑沒有加入到配置中,找不到連結庫的路徑。檢視發現libprotobuf.so.14已經安裝在/usr/local/lib裡。 
注意:庫搜尋路徑的設定有兩種方式:在環境變數 LD_LIBRARY_PATH 中設定以及在 /etc/ld.so.conf 檔案中設定。 
一般有幾種解決辦法: 
a. sudo gedit /etc/ld.so.conf,在檔案末尾新增: 
/usr/local/lib 
然後:sudo ldconfig 
b. sudo gedit /etc/profile,在檔案末尾新增: 
LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH 
然後:sudo ldconfig 
c. 用ln將需要的so檔案連結到/usr/lib或者/lib這兩個預設的目錄下邊: 
ln -s /where/you/install/lib/*.so /usr/lib 
sudo ldconfig 
若是嘗試了這些方法後問題還沒解決,重啟電腦。

如果執行之後出現下圖,說明caffe配置成功。 

圖6.caffe測試結果 
到此caffe配置完畢!

四、MNIST資料集測試
配置caffe完成後,最好利用MNIST資料集對caffe進行測試,以確定caffe是否配置好能正常運行了,再去執行其他深度學習程式碼。MNIST資料集測試過程如下: 
1.將終端定位到Caffe根目錄

cd ~/learning/caffe
1
2.下載MNIST資料庫並解壓縮

./data/mnist/get_mnist.sh
1
3.將其轉換成Lmdb資料庫格式

./examples/mnist/create_mnist.sh
1
4.訓練網路

 ./examples/mnist/train_lenet.sh
1
訓練網路時可能報錯: 


關於這個錯誤,上網搜尋後發現,有人說“是因為顯示卡驅動版本不匹配導致的。也有可能是因為沒有許可權,加上sudo後重試若問題仍沒有解決,則要重灌低版本的驅動”。

加上sudo後發現,找不到libcudnn.so.5。 


不確定是驅動問題還是cudnn連結庫問題,我的解決辦法是:

解除安裝所有驅動,然後重灌375.82.版本驅動,如前面安裝顯示卡驅動部分所說。然後再測試MNIST,發現同樣的問題。最後,看到部落格裡有人的評論,嘗試命令

sudo ldconfig /usr/local/cuda/lib64  #重要!!指明瞭lib的路徑
1
問題解決。得到MNIST訓練結果。

原因猜測:這個問題應該不是驅動版本的問題,而是cudnn沒有找到連結庫的問題。雖然前面都按照要求把cudnn的include檔案和lib檔案加入到/usr/local/cuda/裡了,但是執行 sudo ldconfig -v 時命令沒有在/usr/local/cuda/lib64目錄下執行,因此沒能找到cudnn的連結庫。

訓練的時候可以看到損失與精度數值,如下圖: 

圖7.MNIST資料集訓練 
可以看到最終訓練精度是0.9914。

至此,執行環境配置完成。下次我們就可以開始安裝執行一些最近的深度學習程式碼了!