ubuntu 16.04LTS + cuda +caffe

首先進入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禁用,禁用過程見下。

檢查硬件加速:啓用基於硬件的3D加速可以在繪製3D圖形時直接使用硬件進行處理,這大大加快了3D渲染的速度,使用該功能,必須顯卡支持硬件加速並安裝了正確的驅動,
可以使用glxinfo 命令來查看OpenGL的詳細信息:glxinfo|grep OpenGL

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) 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/bin{PATH:+: {PATH}}>>~/.bashrc

echo ‘export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64{LD_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 := (PYTHONINCLUDE)/usr/local/includeLIBRARYDIRS:= (PYTHON_LIB) /usr/local/lib /usr/lib 修改爲: INCLUDE_DIRS := (PYTHONINCLUDE)/usr/local/include/usr/include/hdf5/serialLIBRARYDIRS:= (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)XcompilerfPIC (COMMON_FLAGS)替換NVCCFLAGS += -D_FORCE_INLINES -ccbin=(CXX)XcompilerfPIC (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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章