首頁 > 軟體

Ubuntu16.04+GTX1080+CUDA8.0+OpenCV3.1.0+cuDNN-v5+Caffe安裝教學

2020-06-16 17:04:51

1.本教學對應的環境

system:ubuntu-16.04-desktop-amd64.iso
cuda:cuda_8.0.44_linux-16.04.run
cudnn:cudnn-8.0-linux-x64-v5.1.tgz
caffe:https://github.com/BVLC/caffe

2.安裝Ubuntu-16.04

略。安裝基本更新。

sudo apt-get update
sudo apt-get upgrade

3.安裝cuda-8.0

3.1 安裝顯示卡驅動

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-367

重新啟動系統,使新驅動生效。使用nvidia-smi測試是否安裝成功。

3.2 安裝cuda-Toolkit

3.2.1 執行安裝檔案

./cuda_8.0.44_linux-16.04.run --override

安裝過程如下:
Do you accept the previously read EULA? (accept/decline/quit): accept
You are attempting to install on an unsupported configuration. Do you wish to continue? ((y)es/(n)o) [ default is no ]: y
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 367.48? ((y)es/(n)o/(q)uit): n
Install the CUDA 8.0 Toolkit? ((y)es/(n)o/(q)uit): y
Enter Toolkit Location [ default is /usr/local/cuda-8.0 ]:
Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): y
Install the CUDA 8.0 Samples? ((y)es/(n)o/(q)uit): y
Enter CUDA Samples Location [ default is /home/kinghorn ]: /usr/local/cuda-8.0
Installing the CUDA Toolkit in /usr/local/cuda-8.0 ...
Finished copying samples.
===========
= Summary =
===========
Driver:   Not Selected
Toolkit:  Installed in /usr/local/cuda-8.0
Samples:  Installed in /usr/local/cuda-8.0

②設定環境變數

vi /home/xxx/.bashrc

內容如下:

export PATH=/usr/local/cuda-8.0/bin:$PATH

使環境變數生效

source /home/xxx/.bashrc

③將cuda庫新增到系統動態庫管理器

sudo vi /etc/ld.so.conf.d/cuda.conf

新增:

/usr/local/cuda/lib64

執行ldconfig使新加的庫生效

sudo ldconfig

④編譯cuda例子與測試

進入到/usr/local/cuda/NVIDIA_CUDA-8.0_Samples/1_Utilities/deviceQuery目錄執行:

sudo make
./deviceQuery

列印出類似如下資訊,說明裝成功

./deviceQuery Starting...
 CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 2 CUDA Capable device(s)
Device 0: "GeForce GTX 1080"
  CUDA Driver Version / Runtime Version          8.0 / 8.0
  CUDA Capability Major/Minor version number:    6.1
  Total amount of global memory:                 8110 MBytes (8504279040 bytes)
  (20) Multiprocessors, (128) CUDA Cores/MP:     2560 CUDA Cores
  GPU Max Clock rate:                            1772 MHz (1.77 GHz)
  Memory Clock rate:                             5005 Mhz
  Memory Bus Width:                              256-bit
  L2 Cache Size:                                 2097152 bytes
  Maximum Texture Dimension Size (x,y,z)         1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
  Maximum Layered 1D Texture Size, (num) layers  1D=(32768), 2048 layers
  Maximum Layered 2D Texture Size, (num) layers  2D=(32768, 32768), 2048 layers
  Total amount of constant memory:               65536 bytes
  Total amount of shared memory per block:       49152 bytes
  Total number of registers available per block: 65536
  Warp size:                                     32
  Maximum number of threads per multiprocessor:  2048
  Maximum number of threads per block:           1024
  Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
  Max dimension size of a grid size    (x,y,z): (2147483647, 65535, 65535)
  Maximum memory pitch:                          2147483647 bytes
  Texture alignment:                             512 bytes
  Concurrent copy and kernel execution:          Yes with 2 copy engine(s)
  Run time limit on kernels:                     Yes
  Integrated GPU sharing Host Memory:            No
  Support host page-locked memory mapping:       Yes
  Alignment requirement for Surfaces:            Yes
  Device has ECC support:                        Disabled
  Device supports Unified Addressing (UVA):      Yes
  Device PCI Domain ID / Bus ID / location ID:   0 / 1 / 0
  Compute Mode:
     < Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >

(3)安裝cudnn-v5.1庫

①解壓

tar xzvf cudnn-8.0-linux-x64-v5.1.tgz

得到cuda資料夾裡面含有lib64和include兩個資料夾

②拷貝到cuda安裝目錄

sudo cp cuda/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/

拷貝後將連結刪除重新建立連結,否則,拷貝是多個多個不同名字的相同檔案,連結關係使用ls -l檢視cudnn解壓後的lib64資料夾。也可以分別拷貝每一個檔案,連結檔案拷貝使用cp -d命令。

4.安裝opencv3.1.0

(1)解壓,建立build目錄

unzip opencv-3.1.0.zip
cd opencv-3.1.0
mkdir build

(2)修改opencv原始碼,使其相容cuda8.0

vi opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp

修改如下:

將:

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)```

改為:

#if !defined(HAVE_CUDA)||defined(CUDA_DISABLER)||(CUDART_VERSION>=8000)

(3)設定opencv,生成Makefile

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..

如果因為ippicv_linux_20151201.tgz包下載失敗而導致Makefile生成失敗,可通過手動下載ippicv_linux_20151201.tgz安裝包,將其拷貝至
opencv-3.1.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b目錄內,重新執行設定命令即可。

(4)編譯

make -j8

編譯過程中如果出現如下錯誤:

/usr/include/string.h: In functionvoid* __mempcpy_inline(void*, const void*, size_t)’: /usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope return (char *) memcpy (__dest, __src, __n) + __n;

這是因為ubuntu的g++版本過高造成的,只需要在opencv-3.1.0目錄下的CMakeList.txt 檔案的開頭加入:

set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -D_FORCE_INLINES”)

新增之後再次進行編譯即可。

(5)安裝

sudo make install

(6)檢視版本號

pkg-config --modversion opencv

5.安裝caffe

(1)安裝必要的依賴庫

sudo apt-get install build-essential
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev 
sudo apt-get libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install Python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

(2)解壓修改組態檔

unzip caffe-master.zip
cp Makefile.config.example Makefile.config
vi Makefile.config

主要設定修改如下:

USE_CUDNN := 1
OPENCV_VERSION := 3
CUDA_DIR :=/usr/local/cuda-8.0
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
/usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
WITH_PYTHON_LAYER := 1
USE_PKG_CONFIG := 1

(3)編譯caffe

make -j8

可能遇到的錯誤1:src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory
解決方法:

cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5_serial.so.10.1.0 libhdf5_serial.so
sudo ln -s libhdf5_serial_hl.so.10.0.2 libhdf5_serial_hl.so

可能遇到的錯誤2:error – unsupported GNU version! gcc versions later than 5.3 are not supported!
解決方法:修改/usr/local/cuda/include/host_config.h檔案

#if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 3)
#error -- unsupported GNU version! gcc versions later than 5.3 are not supported!

改為:

 #if __GNUC__ > 5 || (__GNUC__ == 5 && __GNUC_MINOR__ > 4)
 #error -- unsupported GNU version! gcc versions later than 5.4 are not supported!

可能遇到的錯誤3:

/usr/include/string.h: In functionvoid* **__mempcpy_inline(void*, const void*, size_t)’: /usr/include/string.h:652:42: error: ‘memcpy’ was not declared in this scope return (char *) memcpy (__dest, __src, __n) + __n;**

解決方法:修改caffe-master的Makefile

NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

改為:

NVCCFLAGS +=-D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

可能遇到的錯誤4:

caffe/proto/caffe.pb.h: No such file or directory

使用如下方法生成caffe.pb.h

protoc src/caffe/proto/caffe.proto --cpp_out=.  
mkdir include/caffe/proto  
mv src/caffe/proto/caffe.pb.h include/caffe/proto 

(4)編譯caffe的python介面

make pycaffe

(5)執行caffe runtest

make runtest

這裡時間有點長。

6.執行手寫體例程

進入到caffe根目錄下,執行指令碼

(1)獲取資料

sh data/mnist/get_mnist.sh

(2)將標籤資料轉換成caffe使用的LMDB資料格式

sh examples/mnist/create_mnist.sh

(3)執行訓練指令碼

sh examples/mnist/train_lenet.sh

訓練時間不同的顯示卡訓練時間不同,gtx1080疊代10000次大約需要20s,最終結果如下所示:

I0716 14:46:01.360709 27985 solver.cpp:404]     Test net output #0: accuracy = 0.9908
I0716 14:46:01.360750 27985 solver.cpp:404]     Test net output #1: loss = 0.0303895 (* 1 = 0.0303895 loss)
I0716 14:46:01.360755 27985 solver.cpp:322] Optimization Done.
I0716 14:46:01.360757 27985 caffe.cpp:222] Optimization Done.

模型精度在0.99以上。至此,在ubuntu16.04系統下使用gtx1080顯示卡+cudnn-v5的開發環境就搭建完成了。

Ubuntu 14.04 安裝設定CUDA  http://www.linuxidc.com/Linux/2014-10/107501.htm

Ubuntu 14.04下CUDA8.0 + cuDNN v5 + Caffe  安裝設定  http://www.linuxidc.com/Linux/2017-01/139300.htm

Caffe設定簡明教學 ( Ubuntu 14.04 / CUDA 7.5 / cuDNN 5.1 / OpenCV 3.1 ) http://www.linuxidc.com/Linux/2016-09/135016.htm

Ubuntu 16.04 安裝設定MATLAB+Python +CUDA8.0+cuDNN+OpenCV3.1的Caffe環境  http://www.linuxidc.com/Linux/2017-06/145087.htm

在Ubuntu 14.04上設定CUDA+Caffe+cuDNN+Anaconda+DIGITS  http://www.linuxidc.com/Linux/2016-11/136775.htm

深度學習環境設定Ubuntu16.04+CUDA8.0+CUDNN5  http://www.linuxidc.com/Linux/2017-09/147180.htm

本文永久更新連結地址http://www.linuxidc.com/Linux/2017-10/147609.htm


IT145.com E-mail:sddin#qq.com