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
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
./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
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
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/
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
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
make -j8
编译过程中如果出现如下错误:
/usr/include/string.h: In function ‘void* __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
sudo make install
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
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 function ‘void* **__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
make pycaffe
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
sh data/mnist/get_mnist.sh
sh examples/mnist/create_mnist.sh
(3)执行训练脚本
sh examples/mnist/train_lenet.sh
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