博主电脑的配置:
系统:Ubuntu16.04
GPU:NVIDIA GTX1080
Table of Contents
一、安装NVIDIA驱动
首先去官网查适合自己的GPU驱动(http://www.nvidia.com/Download/index.aspx?lang=en-us)
在Product Type一栏选择自己的产品类型,其它的类型也相应选择,上图是博主的GPU的相关信息。填写完成后,点击右边的SEARC按钮,会出现以下的界面,以下界面主要关注Version,这个是后面安装驱动需要用的数据。
安装之前先卸载已经存在的驱动版本:
sudo apt-get remove --purge nvidia*
若电脑是集成显卡(NVIDIA独立显卡忽略此步骤),需要在安装之前禁止一项:
sudo service lightdm stop
执行如下语句安装:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-410 #注意在这里指定自己的驱动版本!
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev
执行完上述后,重启(reboot)。
重启后输入: sudo nvidia-smi
如果出现了你的GPU列表,如下,则说明驱动安装成功了。
另外也可以通过
nvidia-settings
查看自己机器上详细的GPU信息,博主机器的信息如下:
二、安装CUDA
cuda是nvidia的编程语言平台,想使用GPU就必须要使用cuda
CUDA下载地址:https://developer.nvidia.com/cuda-downloads
下载完cuda8.0后,去对应的目录,执行如下语句,运行runfile文件:
sudo sh cuda_8.0.61_375.26_linux.run
执行后会有一系列提示让你确认,但是注意,有个让你选择是否安装nvidia375驱动时,一定要选择否,因为前面我们已经安装了更加新的nvidia410,所以这里不要选择安装。其余的都直接默认或者选择是即可。
Do you accept the previously read EULA? (accept/decline/quit):
这里选择acceot
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 375.37? ((y)es/(n)o/(q)uit):
这里选择NO(n)
然后剩下出现的都是确认使用默认目录 ENTER 或者是 YES (y),然后就等着安装吧,很快就会好
安装成功后会出现如下界面:
安装完毕后,再声明一下环境变量,并将其写入到 ~/.bashrc 的尾部:
打开~/.bashrc文件:
sudo vim ~/.bashrc
将以下内容写入到~/.bashrc尾部:
export PATH=/usr/local/cuda-8.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda8.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
三、测试cuda的Samples
cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery
sudo make
sudo ./deviceQuery
如果显示以下关于GPU的信息,则说明安装成功了。
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce GTX 1080 Ti"
CUDA Driver Version / Runtime Version 10.0 / 8.0
CUDA Capability Major/Minor version number: 6.1
Total amount of global memory: 11175 MBytes (11718230016 bytes)
(28) Multiprocessors, (128) CUDA Cores/MP: 3584 CUDA Cores
GPU Max Clock rate: 1658 MHz (1.66 GHz)
Memory Clock rate: 5505 Mhz
Memory Bus Width: 352-bit
L2 Cache Size: 2883584 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) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 10.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 1080 Ti
Result = PASS
四、cudnn安装
cuDNN是GPU加速计算深层神经网络的库。
首先去官网下载(https://developer.nvidia.com/rdp/cudnn-download)你需要的cudnn,下载的时候需要注册账号。选择对应你cuda版本的cudnn下载。这里我下载的是cudnn5.1,是个压缩文件(.tgz)
下载完cudnn5.1之后进行解压,cd进入cudnn5.1解压之后的include目录,在命令行进行如下操作:
sudo cp cudnn.h /usr/local/cuda/include/ #复制头文件
再将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 #生成软衔接
sudo ln -s libcudnn.so.5 libcudnn.so #生成软链接
五、 opencv3.1安装
opencv3.1.0下载地址:https://opencv.org/releases.html
下载对应版本的source(source是对应的linux版本)
并将其解压到你要安装的位置,假设解压到了/home/root1,其中root1是博主使用的用户名字,同时我将解压后的文件改名为opencv(可以不用改名)。
首先安装需要的依赖项。
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
在终端中cd到opencv文件夹下,然后
mkdir build #新建一个build文件夹,编译的工程都在这个文件夹里
cd build/
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D WITH_V4L=ON -D WITH_QT=ON -D WITH_OPENGL=ON -DCUDA_NVCC_FLAGS="-D_FORCE_INLINES" ..
cmake成功后,会出现如下结果,提示配置和生成成功:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ise/software/opencv-3.1.0/build
然后make编译就可以了
make -j8
编译最后可能出现以下错误:'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all' failed[5]
这是由于CUDA 8.0不支持OpenCV的 GraphCut算法引起的
解决办法:
修改 ~/opencv-3.1.0/modules/cudalegacy/src/graphcuts.cpp
文件
把第45
行修改成:
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)
即在 #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
加上 || (CUDART_VERSION >= 8000)
重启系统,重启系统后cd到build文件夹下:(这一步可以不做)
sudo apt-get install checkinstall
sudo checkinstall
然后按照提示安装就可以了。
使用checkinstall的目的是为了更好的管理我安装的opencv,因为opencv的安装很麻烦,卸载更麻烦,其安装的时候修改了一大堆的文件,当我想使用别的版本的opencv时,将当前版本的opencv卸载就是一件头疼的事情,因此需要使用checkinstall来管理我的安装。
执行了checkinstall后,会在build文件下生成一个以backup开头的.tgz的备份文件和一个以build开头的.deb安装文件,当你想卸载当前的opencv时,直接执行dpkg -r build即可。
六、安装caffe
1. 下载caffe源码
下载地址:https://github.com/BVLC/caffe
git clone https://github.com/BVLC/caffe.git
2. 首先安装各种依赖包
sudo apt-get update
sudo apt-get install -y build-essential cmake git pkg-config
sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install -y libatlas-base-dev
sudo apt-get install -y --no-install-recommends libboost-all-dev
sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install -y python-pip
sudo apt-get install -y python-dev
sudo apt-get install -y python-numpy python-scipy
2. 修改Make.config和Makefile两个文件
- Make.config文件修改
进入 caffe ,将 Makefile.config.example 文件复制一份并更名为 Makefile.config ,也可以在 caffe 目录下直接调用以下命令完成复制操作 :
然后修改 Makefile.config 文件,在 caffe 目录下打开该文件:sudo cp Makefile.config.example Makefile.config
修改 Makefile.config 文件内容:sudo gedit Makefile.config
//若使用cudnn,则将# USE_CUDNN := 1 修改成: USE_CUDNN := 1 //若使用的opencv版本是3的,则将# OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3 //若要使用python来编写layer,则需要将# WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1 具体修改如下
(1) 应用 cudnn
将 #USE_CUDNN := 1 修改成: USE_CUDNN := 1
(2) 应用 opencv 版本
将 #OPENCV_VERSION := 3 修改为: OPENCV_VERSION := 3
(3) 使用 python 接口
将 #WITH_PYTHON_LAYER := 1 修改为 WITH_PYTHON_LAYER := 1
(4) 修改 python 路径
// 因为ubuntu16.04的文件包含位置发生了变化,尤其是需要用到的hdf5的位置,所以需要更改这一路径,将 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
-
Makefile文件修改
进入 caffe,然后修改 caffe 目录下的 Makefile 文件:
将: NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS) 替换为: NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
将: 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
-
修改 /usr/local/cuda/include/host_config.h 文件 :
将 #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!
3. 开始编译
在 caffe 目录下执行 :
make all -j8
可能出现以下错误:
- 错误1
CXX src/caffe/layer_factory.cpp NVCCsrc/caffe/solvers/adam_solver.cu make: /usr/local/cuda/bin/nvcc: Command not found Makefile:589: recipe for target'.build_release/cuda/src/caffe/solvers/adam_solver.o' failed make: *** [.build_release/cuda/src/caffe/solvers/adam_solver.o]Error 127 make: *** Waiting for unfinished jobs....
解决方案:
可以看到刚刚运行到NVCC时就出错了,也就是跟cuda相关。这其实是cuda的路径没有设置正确,我们打开caffe中的Makefile.config,ctrl+f直接搜索CUDA_DIR,更改cuda的路径为自己的路径就可以,我更改后如下:
CUDA_DIR := /usr/local/cuda-8.0
参考:Ubuntu16.04安装ssd的错误及解决方法
4. 编译成功后可运行测试:
sudo make runtest -j8
在此过程中,可能出现以下错误
- 错误1
.build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libcudart.so.8.0: cannot open shared object file: No such file or directory
Makefile:542: recipe for target 'runtest' failed
make: *** [runtest] Error 127
貌似是cuda8.0链接的问题
解决办法:运行以下命令
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
参考: error while loading shared libraries: libcudart.so.8.0: cannot open shared object file: No such file
- 错误2
.build_release/tools/caffe
.build_release/tools/caffe: error while loading shared libraries: libcudnn.so.5: cannot open shared object file: No such file or directory
Makefile:542: recipe for target 'runtest' failed
make: *** [runtest] Error 127
由于权限的原因原来的两个符号链接文件(libcudnn.so,libcudnn.so.5)已经失效了,需要手动生成软链接
解决办法:
#下面的操作在/usr/local/cuda/lib64/目录下进行
sudo rm -rf libcudnn.so libcudnn.so.5
sudo ln -s libcudnn.so.5.1.10 libcudnn.so.5
sudo ln -s libcudnn.so.5 libcudnn.so
参考:
ubuntu14.04下cuDNN配置(caffe+cuda7.5) 文章中的第4点
"libcudnn.so.5 cannot open shared object file: No such file or directory"(这种方法我没有试过)
5. 最后,如果显示结果为上图所示,则表示 caffe 已经成功安装(Congratulation)。
6. 安装caffe的python接口
首先编译 pycaffe :
cd caffe
sudo make pycaffe -j8
编译 pycaffe 成功后,验证一下是否可以在 python 中导入 caffe 包,首先进入 python 环境,执行
import caffe
若不报错则表示 caffe 的 python 接口已正确编译,如下图:
可能出现的错误:
- 错误1
解决方法:
打开~/.bashrc
sudo gedit ~/.bashrc
在文件下方写入
export PYTHONPATH=~/caffe/python
关闭文件,在终端写入下面语句,使环境变量生效
source ~/.bashrc
七、编译py-faster-rcnn
1. 下载py-faster-rcnn源码
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
2. 生成Cython模块
cd py-faster-rcnn/lib
make -j8
3.生成Caffe和pycaffe
cd py-faster-rcnn/caffe-fast-rcnn
- 修改Makefile.config和Makefile两个文件(我用的是方法2)
方法1:打开Makefile.config,修改方式和caffe中的修改方式一样。
打开Makefile文件,也按照caffe中的方式进行修改。
方法2:
直接将第六步中caffe中修改好的Makefile.config和Makefile复制到py-faster-rcnn/caffe-fast-rcnn下。 - 将第六步中caffe中所有cudnn的文件复制到caffe-fast-rcnn对应的目录下
因为caffe-fast-rcnn这个版本所用的cudnn为旧版本的,可能与新环境的cudnn不兼容,所以需要替换。复制以下caffe路径下所有以cudnn开头的文件 /caffe/include/caffe/util/ /caffe/include/caffe/layers/ /caffe/src/caffe/layers/ /caffe/src/caffe/util/ 到以下py-faster-rcnn中对应的路径下 /py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/ /py-faster-rcnn/caffe-fast-rcnn/include/caffe/layers/ /py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers/ /py-faster-rcnn/caffe-fast-rcnn/src/caffe/util/
- 开始编译
可能出现以下错误:make -j8 && make pycaffe
错误1:
ImportError: No module named google.protobuf.internal
解决办法:
首先你需要知道你所使用的是ubuntu自带的python,还是anaconda中带有的python,选用以下不同的命令
解决方案:pip install protobuf # 使用Ubuntu系统自带的python conda install protobuf # 使用Anaconda中带有的python
错误2:
解决方案:fatal error: caffe/proto/caffe.pb.h: No such file or directory
首先需要进入 第六步caffe 根目录,运行:
错误3:(针对这个错误,博主以后有时间会写一个关于protoc多版本选择的博客)$ protoc src/caffe/proto/caffe.proto --cpp_out=. $ sudo mkdir include/caffe/proto $ sudo mv src/caffe/proto/caffe.pb.h include/caffe/proto
解决方案:#error This file was generated by a newer version of protoc which is
产生的原因是可能是系统中有多个版本的protoc,但是默认使用的protoc版本比较高。
我的是因为系统安装了anaconda, 而且默认使用的就anaconda环境中的ptotoc版本,但是这个版本产生了冲突,如果使用Ubuntu中自带的protoc版本,一般是没有问题的。我们只需要切换到Ubuntu下的版本即可。具体的做法是:sudo vim ~/.bashrc
将 export PATH="/home/root1/anaconda2/bin:$PATH" 注释掉,我这里是位于120行,结果如下
上面操作完成后,一定要
同时要make clean后,再执行source ~/.bashrc
make -j8 && make pycaffe
八、运行py-faster-rcnn中的demo.py
1. 下载模型文件
方法一(下载比较慢)
进入到py-faster-rcnn的目录下,运行以下命令,然后等待下载完成
cd py-faster-rcnn/
./data/scripts/fetch_faster_rcnn_models.sh
方法二
用gedit打开fetch_faster_rcnn_models.sh文件
cd py-faster-rcnn/data/scripts
gedit fetch_faster_rcnn_models.sh
复制其中的URL地址,去网页上下载,如果Ubuntu下载太慢,可以在window上下载再拷贝过来。
下载完成后,将下载的文件faster_rcnn_models.tgz放至py-faster-rcnn/data目录下进行解压 或者用以下命令进行解压
tar zxvf faster_rcnn_models.tgz -C ./
2. 运行faster-rcnn里的demo.py
cd py-faster-rcnn/tools
./demo.py
3. 现象(只截取了部分图片)
参考引用
- Ubuntu16.04+CUDA8.0+caffe配置
- Ubuntu16.04+cuda8.0+caffe安装教程
- Ubuntu16.04环境下安装caffe及运行faster-rcnn
- 搭建faster-rcnn进行目标检测的环境
- 'modules/cudalegacy/CMakeFiles/opencv_cudalegacy.dir/all' failed
- Ubuntu16.04安装ssd的错误及解决方法
- Ubuntu16.04 Caffe 安装步骤记录(超详尽)
- error while loading shared libraries: libcudart.so.8.0: cannot open shared object file: No such file
- ubuntu14.04下cuDNN配置(caffe+cuda7.5)
- "libcudnn.so.5 cannot open shared object file: No such file or directory"
- 解决No module named google.protobuf.internal