2021-05-12 14:32:11
Ubuntu 16.04+CUDA7.5+Caffe深度學習環境搭建
詳細介紹在Ubuntu 16.04下搭建CUDA7.5+Caffe深度學習環境的過程步驟。
1.安裝Ubuntu 16.04
省略。不懂可以自行百度,系統安裝後安裝必要的更新和工具。
sudo apt update
sudo apt-get upgrade
sudo apt-get install vim
sudo apt-get install cmake
2.安裝顯示卡驅動
進入all setting->Software Update,更換英偉達361.42驅動,重新啟動電腦,使用nvidia-smi測試是否成功。
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
② 設定環境變數
vi /home/xxx/.bashrc
新增如下內容:
export PATH=/usr/local/cuda/bin:$PATH
執行如下命令使環境變數生效
source /home/xxx/.bashrc
將cuda動態庫新增到動態庫管理器
sudo vi /etc/ld.so.conf.d/cuda.conf
新增:
/usr/local/cuda/lib64
執行ldconfig使新加的庫生效
sudo ldconfig
③ 強制使用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例子與測試
進入到/usr/local/cuda/NVIDIA_CUDA-7.5_Samples/1_Utilities/deviceQuery目錄執行:
sudo make
./deviceQuery
4.安裝cudnn庫
(1)解壓
tar xzvf cudnn-xxx-ga.tgz
得到cuda資料夾裡面含有的lib64和include兩個資料夾
(2)拷貝到cuda安裝目錄
sudo cp cuda/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
注意:拷貝後將連結刪除重新建立連結,否則,拷貝是多個多個不同名字的相同檔案,連結關係參見cudnn解壓後的資料夾。也可以分別拷貝每一個文??,連結檔案拷貝使用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過程中過程中,可能會出現下面的錯誤:
– ICV: Downloading ippicv_linux_20151201.tgz…
在直接下載該檔案的過程中,會因為超時而失敗,需要收到下載,將其拷貝至opencv-3.1.0/3rdparty/ippicv/downloads/linux-8b449a536a2157bcad08a2b9f266828b目錄內,重新執行設定命令。
(3)編譯opencv
make -j8
此時可能會出現另一個錯誤:
/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;
這是因為ubuntu的g++版本過高造成的,只需要在opencv-3.1.0目錄下的CMakeList.txt 檔案的開頭加入:
set(CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -D_FORCE_INLINES”)
新增之後再次進行編譯連結即可。
(4)檢視版本號
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
如果這些庫都能順利安裝,會大大減少後面遇到的問題。
(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
這種方法一般不會出現問題。
方法2:直接使用gcc編譯
make -j8
錯誤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
錯誤2:
error -- unsupported GNU version! gcc versions later than 5.3 are not supported!
目前caffe不支援高於5.3的gcc,理論上可通過對gcc,g++降級解決,但是降級後還會引起其他相容性問題,因此並不能解決實際問題,下面附上降級方法。解決方法在後面。
① 安裝低版本gcc、g++
sudo apt-get install gcc-4.7 gcc-4.7-multilib
sudo apt-get install g++-4.7 g++-4.7-multilib
② 設定優先順序
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
錯誤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)
錯誤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++降級完成。
相關文章