ubuntu 16.04LTS + cuda +caffe

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_36835368/article/details/55213774

首先进入terminal下
1.查看显卡型号:lspci
00:00.0 Host bridge: Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor DRAM Controller (rev 06)
00:01.0 PCI bridge: Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor PCI Express x16 Controller (rev 06)
00:02.0 VGA compatible controller: Intel Corporation 4th Gen Core Processor Integrated Graphics Controller (rev 06)
00:03.0 Audio device: Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor HD Audio Controller (rev 06)
00:14.0 USB controller: Intel Corporation 8 Series/C220 Series Chipset Family USB xHCI (rev 05)
00:16.0 Communication controller: Intel Corporation 8 Series/C220 Series Chipset Family MEI Controller #1 (rev 04)
00:1a.0 USB controller: Intel Corporation 8 Series/C220 Series Chipset Family USB EHCI #2 (rev 05)
00:1b.0 Audio device: Intel Corporation 8 Series/C220 Series Chipset High Definition Audio Controller (rev 05)
00:1c.0 PCI bridge: Intel Corporation 8 Series/C220 Series Chipset Family PCI Express Root Port #4 (rev d5)
00:1c.4 PCI bridge: Intel Corporation 8 Series/C220 Series Chipset Family PCI Express Root Port #5 (rev d5)
00:1d.0 USB controller: Intel Corporation 8 Series/C220 Series Chipset Family USB EHCI #1 (rev 05)
00:1f.0 ISA bridge: Intel Corporation HM86 Express LPC Controller (rev 05)
00:1f.2 SATA controller: Intel Corporation 8 Series/C220 Series Chipset Family 6-port SATA Controller 1 [AHCI mode] (rev 05)
00:1f.3 SMBus: Intel Corporation 8 Series/C220 Series Chipset Family SMBus Controller (rev 05)
01:00.0 3D controller: NVIDIA Corporation GM107M [GeForce GTX 850M] (rev a2)
07:00.0 Ethernet controller: Qualcomm Atheros QCA8171 Gigabit Ethernet (rev 10)
08:00.0 Network controller: Broadcom Corporation BCM43142 802.11b/g/n (rev 01)

显卡型号:NVIDIA Corporation GM107M [GeForce GTX 850M] (rev a2)

查看显卡驱动是否安装: glxinfo |head
name of display: :0
display: :0 screen: 0
direct rendering: Yes
server glx vendor string: SGI
server glx version string: 1.4
server glx extensions:
GLX_ARB_create_context, GLX_ARB_create_context_profile,
GLX_ARB_create_context_robustness, GLX_ARB_fbconfig_float,
GLX_ARB_framebuffer_sRGB, GLX_ARB_multisample,
GLX_EXT_create_context_es2_profile, GLX_EXT_create_context_es_profile,
显示YES表明已安装。

查看显卡驱动型号:sudo lshw -c video|grep configuration
[sudo] password for yingyingli:
configuration: driver=nouveau latency=0
configuration: driver=i915 latency=0

其中两个驱动中第一个时ubuntu自带的,想要使用NVIDIA驱动进行图像处理时需要将Nouveau禁用,禁用过程见下。
重启系统,在选择系统界面按“e”,将倒数第二行的quiet splash – 改为 quiet splash acpi=off
进入Ubuntu系统后输入lsmod | grep nouveau ,没有任何输出就是禁用成功

检查硬件加速:启用基于硬件的3D加速可以在绘制3D图形时直接使用硬件进行处理,这大大加快了3D渲染的速度,使用该功能,必须显卡支持硬件加速并安装了正确的驱动,
可以使用glxinfo 命令来查看OpenGL的详细信息:glxinfo|grep OpenGL

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel® Haswell Mobile
OpenGL core profile version string: 3.3 (Core Profile) Mesa 11.2.0
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 11.2.0
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 11.2.0
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00
OpenGL ES profile extensions:

2.安装nvidia驱动

首先去官网上查看适合你GPU的驱动(http://www.nvidia.com/Download/index.aspx?lang=en-us),若电脑中安装的有JAVE的程序网页可以完成自动查找,若没有安装也可以手动添加自己的系统型号然后查找对应的GPU,详细内容可在网页中自行查找。
然后执行如下命令进行安装:
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt-get update
sudo apt-get install nvidia-XXX(XXX表对应型号)
sudo apt-get install mesa-common-dev
sudo apt-get install freeglut3-dev

执行完上述后,重启(reboot)。
重启后输入: nvidia-smi
这里写图片描述

如果出现了你的GPU列表,则说明驱动安装成功了。另外也可以通过

nvidia-settings

查看自己机器上详细的GPU信息,下面是我自己的
这里写图片描述

3.安装CUDA

cuda是nvidia的编程语言平台,想使用GPU就必须要使用cuda。
从这里下载cuda的安装文件
https://developer.nvidia.com/cuda-release-candidate-download
注意这里下载的是cuda8.0的runfile(local)文件。
这里是nvidia给出的官方安装指南(遇到问题时可以查阅):
http://docs.nvidia.com/cuda/cuda-installation-guide-Linux/#axzz4HIBXnwyt
下载完cuda8.0后,执行如下语句,运行runfile文件:

sudo sh cuda_8.0.44_linux.run

执行后会有一系列提示让你确认,但是注意,有个让你选择是否安装nvidia361驱动时,一定要选择否,因为前面我们已经安装了更加新的nvidia375,所以这里不要选择安装。
安装程序中
Enter Toolkit Location
[default is /usr/local/cuda-8.0]: ( :后填写提示内容/usr/local/cuda-8.0)

Enter CUDA Samples Location
[default is /home/yingyingli ]: (: 后填提示内容/home/yingyingli)
其余的都直接默认或者选择是即可。
安装成功后会出现如下界面:

============ Summary ============
Driver: Not Selected
Toolkit: Installed in /usr/local/cuda-8.0
Samples: Installed in /home/textminerPlease
make sure that– PATH includes /usr/local/cuda-8.0/bin– LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as rootTo uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/binPlease see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.To install the driver using this installer, run the following command, replacing with the name of this run file:sudo .run -silent -driverLogfile is /opt/temp//cuda_install_6583.log

安装完毕后,再声明一下环境变量,并将其写入到 ~/.bashrc 的尾部:
echo 'export PATH=/usr/local/cuda-8.0/binKaTeX parse error: Expected '}', got 'EOF' at end of input: {PATH:+:{PATH}}>>~/.bashrc

echo 'export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64KaTeX parse error: Expected '}', got 'EOF' at end of input: …LIBRARY_PATH:+:{LD_LIBRARY_PATH}} ’ >>~/.bashrc

然后设置环境变量和动态链接库,在命令行输入:

$ sudo gedit /etc/profile

在打开的文件末尾加入:

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

保存之后,创建链接文件:

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

在打开的文件中添加如下语句:

/usr/local/cuda/lib64

然后执行

sudo ldconfig

使链接立即生效。

其中我的Samples安装时显示Missing…,所以在测试CUDA的样本时出现错误,希望大家可以帮忙指导一下,如果没有提示错误安装成功后可以测试一下。

4.测试cuda的Samples

cd /usr/local/cuda-8.0/samples/1_Utilities/deviceQuery

sudo make

sudo ./deviceQuery

如果显示的是一些关于GPU的信息,则说明安装成功了。

5、使用cudnn

首先去官网下载你需要的cudnn,下载的时候需要注册账号。选择对应你cuda版本的cudnn下载。这里我下载的是cudnn5.1,是个压缩文件(.tgz)
这里写图片描述
下载完cudnn5.1之后进行解压,
tar -zxvf cudnn-8.0-5.1.tgz
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.0.5 libcudnn.so.5 #生成软衔接
sudo ln -s libcudnn.so.5 libcudnn.so #生成软链接
6、安装opencv3.1.0

从官网上下载opencv3.1.0
http://OpenCV.org/downloads.html
并将其解压到你要安装的位置,假设解压到了/home
首先安装Ubuntu系统和OpenCV需要的依赖项,虽然我也不知道有些依赖项是干啥的,但是只管装就行,也不会占据很多空间的。

sudo apt-get install --assume-yes libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip

在终端中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

上面是将opencv编译成功,但是并没有安装到我们的系统中,有很多的设置都没有写入到系统中,因此还要进行install。

sudo make install
sudo /bin/bash -c ‘echo “/usr/local/lib” > /etc/ld.so.conf.d/opencv.conf’
sudo ldconfig

重启系统,重启系统后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即可。

7、安装caffe

首先安装各种依赖包

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–no-install-recommends libboost-all-dev
sudo apt-get install -y python-pip
sudo apt-get install -y python-numpy python-scipy

将终端cd到你要安装caffe的位置,执行如下指令,从github上clone caffe。

sudo git clone https://github.com/BVLC/caffe.git
//从github上git caffecd caffe //打开到刚刚git下来的caffe
sudo cp Makefile.config.example Makefile.config
sudo gedit Makefile.config //打开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
//重要的一项 将# 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的位置,所以需要更改这一路径//若使用MATLAB接口的话,则要讲MATLAB_DIR换成你自己的MATLAB安装路径MATLAB_DIR := /usr/localMATLAB_DIR := /usr/local/matlab2014a

cd caffe 打开makefile文件,(415行)

将NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC ( C O M M O N F L A G S ) N V C C F L A G S + = D F O R C E I N L I N E S c c b i n = (COMMON_FLAGS)替换NVCCFLAGS += -D_FORCE_INLINES -ccbin= (CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

以管理员的身份编辑/usr/local/cuda/include/host_config.h,
sudo emacs /usr/local/cuda/include/host_config.h
将其中的第119行注释掉:

#error-- unsupported GNU version! gcc versions later than 5 are not supported!改为//#error-- unsupported GNU version! gcc versions later than 5 are not supported!

之后再

make all -j8make runtestmake pycaffemake matcaffe

猜你喜欢

转载自blog.csdn.net/weixin_36835368/article/details/55213774