新聞中心
詳細(xì)介紹在ubuntu 16.04下搭建CUDA7.5+Caffe深度學(xué)習(xí)環(huán)境的過程步驟。
1.安裝Ubuntu 16.04
省略。不懂可以自行百度,系統(tǒng)安裝后安裝必要的更新和工具。

創(chuàng)新互聯(lián)主營(yíng)西陵網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),西陵h5重慶小程序開發(fā)公司搭建,西陵網(wǎng)站營(yíng)銷推廣歡迎西陵等地區(qū)企業(yè)咨詢
sudo apt update
sudo apt-get upgrade
sudo apt-get install vim
sudo apt-get install cmake
2.安裝顯卡驅(qū)動(dòng)
進(jìn)入all setting->Software Update,更換英偉達(dá)361.42驅(qū)動(dòng),重啟電腦,使用nvidia-smi測(cè)試是否成功。
3.安裝cuda
(1)安裝必要的依賴庫
ca-certificates-java
default-jre
default-jre-headless
fonts-dejavu-extra
freeglut3
freeglut3-dev
java-common
libatk-wrapper-java
libatk-wrapper-java-jni
libdrm-dev
libgl1-mesa-dev
libglu1-mesa-dev
libgnomevfs2-0
libgnomevfs2-common
libice-dev
libpthread-stubs0-dev
libsctp1
libsm-dev
libx11-dev
libx11-doc
libx11-xcb-dev
libxau-dev
libxcb-dri2-0-dev
libxcb-dri3-dev
libxcb-glx0-dev
libxcb-present-dev
libxcb-randr0-dev
libxcb-render0-dev
libxcb-shape0-dev
libxcb-sync-dev
libxcb-xfixes0-dev
libxcb1-dev
libxdamage-dev
libxdmcp-dev
libxext-dev
libxfixes-dev
libxi-dev
libxmu-dev
libxmu-headers
libxshmfence-dev
libxt-dev
libxxf86vm-dev
lksctp-tools
mesa-common-dev
openjdk-7-jre
openjdk-7-jre-headless
tzdata-java
x11proto-core-dev
x11proto-damage-dev
x11proto-dri2-dev
x11proto-fixes-dev
x11proto-gl-dev
x11proto-input-dev
x11proto-kb-dev
x11proto-xext-dev
x11proto-xf86vidmode-dev
xorg-sgml-doctools
xtrans-dev
libgles2-mesa-dev
nvidia-modprobe
build-essential
(2)安裝cuda-toolkit
① 安裝cuda_7.5.18_linux.run
sudo ./cuda_7.5.18_linux.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 352.39? ((y)es/(n)o/(q)uit): n Install the CUDA 7.5 Toolkit? ((y)es/(n)o/(q)uit): y Enter Toolkit Location [ default is /usr/local/cuda-7.5 ]: Do you want to install a symbolic link at /usr/local/cuda? ((y)es/(n)o/(q)uit): y Install the CUDA 7.5 Samples? ((y)es/(n)o/(q)uit): y Enter CUDA Samples Location [ default is /home/kinghorn ]: /usr/local/cuda-7.5 Installing the CUDA Toolkit in /usr/local/cuda-7.5 ... Finished copying samples. =========== = Summary = =========== Driver: Not Selected Toolkit: Installed in /usr/local/cuda-7.5 Samples: Installed in /usr/local/cuda-7.5
② 設(shè)置環(huán)境變量
vi /home/xxx/.bashrc
添加如下內(nèi)容:
export PATH=/usr/local/cuda/bin:$PATH
執(zhí)行如下命令使環(huán)境變量生效
source /home/xxx/.bashrc
將cuda動(dòng)態(tài)庫添加到動(dòng)態(tài)庫管理器
sudo vi /etc/ld.so.conf.d/cuda.conf
添加:
/usr/local/cuda/lib64
執(zhí)行l(wèi)dconfig使新加的庫生效
sudo ldconfig
③ 強(qiáng)制使用gcc5
編輯/usr/local/cuda/include/host_config.h文件,注釋掉115行
#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)編譯cuda例子與測(cè)試
進(jìn)入到/usr/local/cuda/NVIDIA_CUDA-7.5_Samples/1_Utilities/deviceQuery目錄執(zhí)行:
sudo make
./deviceQuery
4.安裝cudnn庫
(1)解壓
tar xzvf cudnn-xxx-ga.tgz
得到cuda文件夾里面含有的lib64和include兩個(gè)文件夾
(2)拷貝到cuda安裝目錄
sudo cp cuda/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
注意:拷貝后將鏈接刪除重新建立鏈接,否則,拷貝是多個(gè)多個(gè)不同名字的相同文件,鏈接關(guān)系參見cudnn解壓后的文件夾。也可以分別拷貝每一個(gè)文??,鏈接文件拷貝使用cp -d命令。
5.安裝opencv3.1.0
(1)安裝基本必要庫
sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
(2)配置opencv,生成Makefile
cd opencv-3.1.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
在configure過程中過程中,可能會(huì)出現(xiàn)下面的錯(cuò)誤:
– ICV: Downloading ippicv_linux_20151201.tgz…
在直接下載該文件的過程中,會(huì)因?yàn)槌瑫r(shí)而失敗,需要收到下載,將其拷貝至opencv-3.1.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b目錄內(nèi),重新執(zhí)行配置命令。
(3)編譯opencv
make -j8
此時(shí)可能會(huì)出現(xiàn)另一個(gè)錯(cuò)誤:
/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;
這是因?yàn)閡buntu的g++版本過高造成的,只需要在opencv-3.1.0目錄下的CMakeList.txt 文件的開頭加入:
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -D_FORCE_INLINES”)
添加之后再次進(jìn)行編譯鏈接即可。
(4)查看版本號(hào)
pkg-config --modversion opencv
(5)安裝
sudo make install
6.安裝caffe與配置
(1)安裝必要的依賴庫
sudo apt-get install build-essential
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev 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
如果這些庫都能順利安裝,會(huì)大大減少后面遇到的問題。
(2)下載caffe-master并解壓得到源碼包
解壓:
unzip caffe-master.zip
(3)修改配置文件Make.config
cd caffe-master
cp Makefile.config.example Makefile.config
vi Makefile.config
將# USE_CUDNN := 1前得#注釋去掉,表示使用cuDNN,如果不是使用GPU,可以將# CPU_ONLY := 1前得注釋去掉。這里我使用cuDNN來加速。
(4)編譯caffe
方法1:使用cmake編譯
mkdir build
cd build
cmake ..
make all -j8
這種方法一般不會(huì)出現(xiàn)問題。
方法2:直接使用gcc編譯
make -j8
錯(cuò)誤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
修改Makefile.config
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/hdf5/serial
錯(cuò)誤2:
error -- unsupported GNU version! gcc versions later than 5.3 are not supported!
目前caffe不支持高于5.3的gcc,理論上可通過對(duì)gcc,g++降級(jí)解決,但是降級(jí)后還會(huì)引起其他兼容性問題,因此并不能解決實(shí)際問題,下面附上降級(jí)方法。解決方法在后面。
① 安裝低版本gcc、g++
sudo apt-get install gcc-4.7 gcc-4.7-multilib
sudo apt-get install g++-4.7 g++-4.7-multilib
② 設(shè)置優(yōu)先級(jí)
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.7 40
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 50
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.7 40
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 50
③ 選擇版本
sudo update-alternatives --config gcc
There are 2 choices for the alternative gcc (providing /usr/bin/gcc)
Selection Path Priority Status ------------------------------------------------------------
0 /usr/bin/gcc-5 50 auto mode
* 1 /usr/bin/gcc-4.7 40 manual mode
2 /usr/bin/gcc-5 50 manual mode
sudo update-alternatives --config g++
There are 2 choices for the alternative g++ (providing /usr/bin/g++).
Selection Path Priority Status ------------------------------------------------------------
0 /usr/bin/g++-5 50 auto mode
* 1 /usr/bin/g++-4.7 40 manual mode
2 /usr/bin/g++-5 50 manual mode
錯(cuò)誤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;
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
改為:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
錯(cuò)誤3:
/usr/bin/ld: cannot find -lippicv
cp opencv-3.1.0/3rdparty/ippicv/unpack/ippicv_lnx/lib/intel64/libippicv.a /usr/local/lib
再次編譯即可。
至此,gcc、g++降級(jí)完成。
下面是錯(cuò)誤2 的真正解決方法(紅色字體):
sudo vi /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!
我的gcc版本為5.4.0,可根據(jù)具體情況修改。
(5)編譯caffe的python接口
make pycaffe
出錯(cuò):
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
原因是numpy路徑配置錯(cuò)誤將:
PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/lib/python2.7/dist-packages/numpy/core/include
改為:
PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/local/lib/python2.7/dist-packages/numpy/core/include
(6)測(cè)試caffe
make runtest
這個(gè)時(shí)間有點(diǎn)長(zhǎng)。
7.運(yùn)行手寫體例程
caffe自帶手寫體識(shí)別的測(cè)試?yán)?。每一步caffe都已經(jīng)寫好腳本,執(zhí)行幾個(gè)簡(jiǎn)單命令就可以將第一個(gè)深度學(xué)習(xí)程序跑起來。
(1)獲取數(shù)據(jù)(并完成數(shù)據(jù)標(biāo)注)
sh data/mnist/get_mnist.sh
(2)將標(biāo)簽數(shù)據(jù)轉(zhuǎn)換成caffe使用的LMDB數(shù)據(jù)格式
sh examples/mnist/create_mnist.sh
(3)網(wǎng)絡(luò)求解文件修改
vi caffe-master/examples/mnist/lenet_solver.prototxt
# The train/test net protocol buffer definition
net: "examples/mnist/lenet_train_test.prototxt"
# test_iter specifies how many forward passes the test should carry out.
# In the case of MNIST, we have test batch size 100 and 100 test iterations,
# covering the full 10,000 testing images.
test_iter: 100
# Carry out testing every 500 training iterations.
test_interval: 500
# The base learning rate, momentum and the weight decay of the network.
base_lr: 0.01
momentum: 0.9
weight_decay: 0.0005
# The learning rate policy
lr_policy: "inv"
gamma: 0.0001
power: 0.75
# Display every 100 iterations
display: 100
# The maximum number of iterations
max_iter: 10000
# snapshot intermediate results
snapshot: 5000
snapshot_prefix: "examples/mnist/lenet"
# solver mode: CPU or GPU
solver_mode: GPU
最后一行,訓(xùn)練過程采用CPU、GPU選擇,如果不使用GPU,修改solver_mode: GPU為solver_mode: CPU即可,這里我使用GPU。
(4)執(zhí)行訓(xùn)練腳本
sh examples/mnist/train_lenet.sh
大約10分鐘左右,模型訓(xùn)練完成
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以上。
至此,Caffe+Linux深度學(xué)習(xí)環(huán)境搭建完成。
本文題目:Ubuntu16.04+CUDA7.5+Caffe深度學(xué)習(xí)環(huán)境搭建
文章路徑:http://www.5511xx.com/article/djhhcgh.html


咨詢
建站咨詢
