参考:https://blog.csdn.net/u010094199/article/details/54380086
参考:https://blog.csdn.net/jonms/article/details/79318566
参考:https://blog.csdn.net/jonms/article/details/79318566
cuDnn的配置可以参考:https://blog.csdn.net/lucifer_zzq/article/details/76675239
首先介绍一下我的电脑配置,我的显卡是NVIDIA GTX970
1. 安装双系统(Ubuntu16.04 + Windows 7)全都是64位的操作系统
我用U盘制作系统盘安装Ubuntu16.04的时候,遇到如下问题:无法将启动引导正常安装
重新安装了好几次都是这样,找不到解决方案,有同学知道怎么解决的可以安利一下我~
由于Ubuntu14.04安装cuda的时候坑太多,看好几个帖子都这么说的,我还是坚定地想装Ubunt16.04。
然后参考:从Ubuntu 14.04 LTS版升级到Ubuntu 16.04 LTS。到此,Ubuntu16.04安装成功!
2. 安装NVIDIA显卡驱动
这里要引用PPA第三方库,因为直接从NVIDIA官方安装,会有显示器黑屏、进入不了tty1界面等一系列问题,没办法,Ubuntu对于NVIDIA显卡驱动的支持不太好
sudo add-apt-repository ppa:graphics-drivers/ppa //引入PPA库里的显卡驱动
如果引用成功,则会显示如下图所示:
Fresh drivers from upstream, currently shipping Nvidia.
## Current Status
Current official release: `nvidia-370` (370.28)
Current long-lived branch release: `nvidia-367` (367.57)
For GeForce 8 and 9 series GPUs use `nvidia-340` (340.98)
For GeForce 6 and 7 series GPUs use `nvidia-304` (304.132)
## What we're working on right now:
- Normal driver updates
- Help Wanted: Mesa Updates for Intel/AMD users, ping us if you want to help do this work, we're shorthanded.
接下来安装当前的长期稳定版nvidia-367驱动
sudo service lightdm stop
sudo apt-get install nvidia-367
sudo service lightdm start
sudo reboot
nvidia-smi
这里需要先关闭图形桌面,如果不关闭,可能会在安装显卡驱动的时候提示X server未关闭的错误,从而导致安装失败
如果显卡驱动安装成功,则在执行完nvidia-smi语句后,输出如下:
Sat Jan 14 10:41:03 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 367.57 Driver Version: 367.57 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 970 Off | 0000:01:00.0 Off | N/A |
| 30% 30C P8 19W / 200W | 121MiB / 4036MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1386 G /usr/lib/xorg/Xorg 111MiB |
| 0 2341 G compiz 8MiB |
+-----------------------------------------------------------------------------+
若安装失败,卸载未安装成功的显卡驱动,再重新安装
$ sudo apt-get remove --purge nvidia-* #卸载显卡驱动
3. Cuda安装
Cuda官方下载地址:https://developer.nvidia.com/cuda-downloads 我用的是 cuda_8.0.44_linux.run 版本
进入cuda_8.0.44_linux.run 所在目录,执行下面的语句开始安装cuda
$ sudo sh cuda_8.0.44_linux.run
可能遇到的选项:
是否接受许可条款: accept
是否安装NVIDIA driver:no #因为我们已经安装了NVIDIA显卡驱动
是否安装cuda toolkit : yes
是否安装cuda samples:yes
中间会有提示是否确认选择默认路径当作安装路径,按Enter键即可。
若安装失败,且最后错误的提示为:
Not enough space on parition mounted at /tmp.Need 5091561472 bytes.
Disk space check has failed. Installation cannot continue.
即错误提示为/tmp空间不足,可执行下面的操作:
====如果执行$ sudo sh cuda_8.0.44_linux.run 时提示/tmp空间不足,则执行下面的操作===============
$ sudo mkdir /opt/tmp #在根目录下的opt文件夹中新建tmp文件夹,用作安装文件的临时文件夹
$ sudo sh cuda_8.0.44_linux.run --tmpdir=/opt/tmp/
====如果执行$ sudo sh cuda_8.0.44_linux.run 时提示/tmp空间不足,则执行上面的操作================
配置环境变量
$ sudo vim ~/.bashrc #打开配置文件,如果没安装vim,可执行 $ sudo apt-get install vim #安装vim
按 i 键,在文件末尾插入下面两行,按esc键,输入 :wq ,保存退出。
export PATH=/usr/local/cuda-8.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
立即使配置的环境变量生效
source ~/.bashrc
判断cuda是否安装成功
执行:
$ nvcc --version
输出:
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Sun_Sep__4_22:14:01_CDT_2016
Cuda compilation tools, release 8.0, V8.0.44
则表示安装成功。
===========若不幸安装失败,执行下面的命令卸载cuda,然后重新安装=========
$ sudo /usr/local/cuda-8.0/bin/uninstall_cuda_8.0.pl
测试cuda的Samples
$ cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
$ make
$ sudo ./deviceQuery
输出的最后两行类似这样的信息:
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 970
Result = PASS
4.使用Cudnn加速
我们去官网下载与cuda8.0匹配的cudnn,https://developer.nvidia.com/cudnn ,我下载的是cudnn v5.05 for cuda8.0
直接将文件解压,拷贝到cuda相应的文件夹下即可
$ tar xvzf cudnn-8.0-linux-x64-v5.0-ga.tgz
$ sudo cp cuda/include/cudnn.h /usr/local/cuda/include
$ sudo cp cuda/lib64/*.* /usr/local/cuda/lib64
$ sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
5. 安装编译Caffe
下载caffe
$ sudo git clone https://github.com/BVLC/caffe.git
安装第三方库
$ sudo apt-get install libatlas-base-dev
$ sudo apt-get install libprotobuf-dev
$ sudo apt-get install libleveldb-dev
$ sudo apt-get install libsnappy-dev
$ sudo apt-get install libopencv-dev
$ sudo apt-get install libboost-all-dev
$ sudo apt-get install libhdf5-serial-dev
$ sudo apt-get install libgflags-dev
$ sudo apt-get install libgoogle-glog-dev
$ sudo apt-get install liblmdb-dev
$ sudo apt-get install protobuf-compiler
安装OpenCV
当前最新版OpenCV是3.2.0版本的
$ cd caffe
$ sudo git clone https://github.com/jayrambhia/Install-OpenCV
$ cd Install-OpenCV/Ubuntu
$ sudo chmod +x *
$ sudo ./opencv_latest.sh
我们可以通过如下命令查看OpenCV安装版本
$ pkg-config --modversion opencv
编译caffe
编译前,先配置变量
$ sudo cp Makefile.config.example Makefile.config
$ sudo vim Makefile.config
设置以下内容:
USE_CUDNN := 1 #取消该句注释
PYTHON_INCLUDE := /usr/include/python2.7
/usr/lib/python2.7/dist-packages/numpy/core/include
WITH_PYTHON_LAYER := 1 #取消注释
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
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
保存退出
$ sudo make clean #每次需要重新编译Caffe的时候,在caffe文件夹下清除掉之前的编译结果
$ cd build
$ sudo cmake ..
$ sudo make all
$ sudo make install
$ sudo make runtest
$ sudo make -j8
$ sudo make runtest
$ sudo make pycaffe
配置环境
caffe运行时需要调用cuda的库,我们在/etc/ld.so.conf.d目录下新建一个caffe.conf文件,将所需要用的库的目录写入
$ sudo vim /etc/ld.so.conf.d/caffe.conf
添加: /usr/local/cuda/lib64
保存并退出 :wq
更新配置 $ sudo ldconfig
6.测试caffe
下载mnist数据集
$ cd ~/caffe #切换到caffe目录
# 注意:执行命令的时候最好在当前的caffe目录下,否则会报错,会找不到XXX文件
$ sudo sh data/mnist/get_mnist.sh #获取mnist数据集
$ sudo sh examples/mnist/create_mnist.sh
开始训练
$ sudo sh examples/mnist/train_lenet.sh
训练结果
Test net output #0: accuracy = 0.9908
I0114 13:41:23.117681 4189 solver.cpp:404] Test net output #1: loss = 0.0286537 (* 1 = 0.0286537 loss)
I0114 13:41:23.117684 4189 solver.cpp:322] Optimization Done.
I0114 13:41:23.117687 4189 caffe.cpp:254] Optimization Done.
因为一些原因还是需要使用别人基于Caffe的代码,但是代码比较老,默认不支持高版本的cuda或者cudnn
怎么办呢?基本上就是把最新官方Caffe-BVLC的几个关键文件拿过来替换即可。
脚本如下:
#########################################################################
# File Name: xxx.sh
# Author: ChrisZZ
# mail: imzhuo AT foxmail.com
# Created Time: 2018年05月18日 星期五 16时20分20秒
#########################################################################
#!/bin/bash
# 先准备用到的别人的老本的caffe,比如放在了~/work/caffe_xxx
cd ~/work
MY_CAFFE=~/work/caffe_xxx
# 下载官方的最新Caffe
git clone https://github.com/BVLC/caffe caffe-BVLC --depth=1
BVLC_CAFFE=~/work/caffe-BVLC
# 现在执行如下文件替换。直接执行即可。
cp $BVLC_CAFFE/include/caffe/layers/cudnn_relu_layer.hpp $MY_CAFFE/include/caffe/layers/cudnn_relu_layer.hpp
cp $BVLC_CAFFE/include/caffe/layers/cudnn_sigmoid_layer.hpp $MY_CAFFE/include/caffe/layers/cudnn_sigmoid_layer.hpp
cp $BVLC_CAFFE/include/caffe/layers/cudnn_tanh_layer.hpp $MY_CAFFE/include/caffe/layers/cudnn_tanh_layer.hpp
cp $BVLC_CAFFE/include/caffe/util/cudnn.hpp $MY_CAFFE/include/caffe/util/cudnn.hpp
cp $BVLC_CAFFE/src/caffe/layers/cudnn_relu_layer.cpp $MY_CAFFE/src/caffe/layers/cudnn_relu_layer.cpp
cp $BVLC_CAFFE/src/caffe/layers/cudnn_relu_layer.cu $MY_CAFFE/src/caffe/layers/cudnn_relu_layer.cu
cp $BVLC_CAFFE/src/caffe/layers/cudnn_sigmoid_layer.cpp $MY_CAFFE/src/caffe/layers/cudnn_sigmoid_layer.cpp
cp $BVLC_CAFFE/src/caffe/layers/cudnn_sigmoid_layer.cu $MY_CAFFE/src/caffe/layers/cudnn_sigmoid_layer.cu
cp $BVLC_CAFFE/src/caffe/layers/cudnn_tanh_layer.cpp $MY_CAFFE/src/caffe/layers/cudnn_tanh_layer.cpp
cp $BVLC_CAFFE/src/caffe/layers/cudnn_tanh_layer.cu $MY_CAFFE/src/caffe/layers/cudnn_tanh_layer.cu
然后,再编译你的caffe_xxx时,CUDA和CuDNN都用起来,都可以编译了。