Ubuntu16.04+CUDA8.0+Caffe配置指南

本文旨在Ubuntu16.04下的caffe環境搭建。

顯卡:TESLA K80


一、系統安裝

這里與大多系統安裝步驟無異,因而略過。大抵的一些注意事項是

1.時區選擇的時候需要斷網;

2.大陸地區的需要換源

3.apt-get update

選擇的鏡像:Ubuntu16.04-desktop

二、驅動安裝

之前安裝Ubuntu14.04下的驅動時,都是直接用CUDA安裝包里自帶的顯卡驅動。但在當前目標配置下,安裝驅動的時候會報couldn't locate the kernel files之類的錯誤。反復無法解決。遂選擇手動安裝顯卡驅動。

NVIDIA官網下載對應的驅動版本。這里選擇用runfile安裝。

1.禁用nouveau

vi /etc/modprobe.d/blacklist-nouveau.conf

加入文本

blacklist nouveau

options nouveau modeset=0

更新內核

update-initramfs -u

重啟之后,查看nouveau是否禁用成功

lsmod | grep nouveau

2.禁用x server

如是lightdm,則如下命令,其他以此類推

service lightdm stop

如果該命令之后x server沒有成功關閉,可嘗試

pkill x

3.安裝驅動

注意不要安裝opengl libs(否則桌面環境不正常)

chmod a+x NVIDIA.run

./NVIDIA.run —no-opengl-files

調用以下命令,若顯示顯卡信息,則安裝驅動成功。

nvidia-smi

三、CUDA8.0

CUDA歷史版本,這里選擇CUDA Toolkit 8.0 GA2

同樣別安裝opengl libs,其余選項皆為默認或yes

chmod a+x cuda.run

./cuda.run —no-opengl-libs

設置環境變量和動態鏈接庫

vi? ?/etc/profile

在文件末尾加入:

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

再創建鏈接文件

vi /etc/ld.so.conf.d/cuda.conf

加入文本

/usr/local/cuda/lib64

再執行以下命令使鏈接生效

ldconfig

編譯測試程序,若出現顯卡信息,則安裝成功。

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

make

?./deviceQuery

四、CUDNN6.0

CUDNN下載地址,這里選擇6.0版本。

tar zxvf cudnn-8.0-linux-x64-v6.0.tgz

cd cuda

cp include/cudnn.h /usr/local/include

cp lib64/libcudnn.* /usr/local/lib

ln -sf /usr/local/lib/libcudnn.so.6.0.21 /usr/local/lib/libcudnn.so.6

ln -sf /usr/local/lib/libcudnn.so.6 /usr/local/lib/libcudnn.so

ldconfig -v

若出現gcc版本過高,而編譯caffe-master報錯的話,

vi /usr/local/cuda/include/host_config.h

搜索: #error -- unsupported GNU version! gcc versions later than 5.3 are not supported!

修改為: //#error -- unsupported GNU version! gcc versions later than 5.3 are not supported!

五、Matlab2016b

關于 GCC 和 G++ 版本問題

Matlab 2014a gcc/g++ 4.7.x, Matlab 2016a gcc/g++ 4.9.x

Ubuntu 15.04 gcc/g++ 4.9.x, Ubuntu 16.04 gcc/g++ 5.4.x

原則上Matlab需要和Ubuntu版本一致,由于CUDA 8只支持16.04,而且需要GCC 5.4.x 進行編譯,而CUDA 7.5不支持 Ubuntu 16.04 因此Matlab會有一些奇葩,有時按照降級(或強制安裝)的方法可以正常使用,有時卻會報錯,懷疑和顯卡驅動有關。

引自宇宙騎士歐老師

因而選擇了2016b版本。

將鏡像文件掛載到image monitor上。將所有文件拷貝到Home/Matlab。

chmod a+x Matlab -R

./install

相關選項:

-?不使用Internet安裝

-?密鑰:09806-07443-53955-64350-21751-41297

-?安裝完成后,運行Matlab安裝目錄下bin/glx64/activate_matlab.sh激活

- Crack文件夾下license_standalone.lic為許可證文件

-?復制MATLAB Production Server\R2016b\bin\glx64下的文件到對應目錄下,并替換源文件

-?激活完畢,可運行matlab

六、BLAS

安裝MKL,以學生身份下載Student版,填好各種信息,可以直接下載,同時會給你一個郵件告知序列號。下載完之后,要把文件解壓到home文件夾(注意任何一級文件夾不能包含空格,否則安裝會失敗)。這里下載的是2017版本。

tar zxvf parallel_studio_xe_2017.tar.gz?

chmod a+x parallel_studio_xe_2017-R

sh install_GUI.sh

環境配置,新建conf文件

vi /etc/ld.so.conf.d/intel_mkl.conf

并鍵入

/opt/intel/lib/intel64

/opt/intel/mkl/lib/intel64

七、OpenCV3.1

安裝依賴項

apt-get install build-essentialsudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-devsudo apt-get install –assume-yes libopencv-dev 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

apt-get install ffmpeg libopencv-dev libgtk-3-dev python-numpy python3-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff5-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine2-dev libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev libv4l-dev libtbb-dev qtbase5-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev

下載OpneCV源文件

wget -O opencv-3.1.0.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/3.1.0/opencv-3.1.0.zip/download

或者從Github clone

mkdir opencv

cd opencv ?

git clone?https://github.com/opencv/opencv.git?

git clone?https://github.com/opencv/opencv_contrib.git

編譯和安裝

mkdir build

cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..

make -j4

sudo make install

sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'

sudo ldconfig

這里可能遇到的問題包括:

- ippicv無法下載

- cuda8.0不支持

解決方案:

- 自行下載ippicv_linux_20151201,將文件拷貝到opencv-3.1.0/3rdparty/ippicv/downloads/linux-808b791a6eac9ed78d32a7666804320e/路徑下

-?modules/gpu/src/graphcuts.cpp

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)

改成

#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) || (CUDART_VERSION >= 8000)

八、其他依賴項

1. Google Logging Library(glog)下載地址

tar zxvf glog-0.3.3.tar.gz

./configure

make

sudo make install

如果沒有權限就

chmod a+x glog-0.3.3 -R

2.其他依賴項

sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler protobuf-c-compiler protobuf-compiler python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags cython ipython

九、Caffe

下載源文件,進行配置

cp Makefile.config.example Makefile.config

vi? Makefile.config

1.啟用CUDNN

USE_CUDNN := 1

2.配置引用文件(Ubuntu16.04下,文件位置發生變化)

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/lib/x86_64-linux-gnu/hdf5/serial/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

3.啟用Intel Parallel Studio XE 2017

BLAS := mkl

4.配置路徑,實現caffe對Python和Matlab接口的支持

PYTHON_LIB := /usr/local/lib

MATLAB_DIR := /usr/local/MATLAB/R2016b

5.啟用Opencv

OPENCV_VERSION =3


這里列出一份配置參考

## Refer to http://caffe.berkeleyvision.org/installation.html

# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).

USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).

# CPU_ONLY := 1

# uncomment to disable IO dependencies and corresponding data layers

# USE_OPENCV := 0

# USE_LEVELDB := 0

# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)

# You should not set this flag if you will be reading LMDBs with any

# possibility of simultaneous read and write

# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3

OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.

# N.B. the default for Linux is g++ and the default for OSX is clang++

# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.

CUDA_DIR := /usr/local/cuda

# On Ubuntu 14.04, if cuda tools are installed via

# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:

# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.

# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.

# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.

CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \

-gencode arch=compute_20,code=sm_21 \

-gencode arch=compute_30,code=sm_30 \

-gencode arch=compute_35,code=sm_35 \

-gencode arch=compute_50,code=sm_50 \

-gencode arch=compute_52,code=sm_52 \

-gencode arch=compute_60,code=sm_60 \

-gencode arch=compute_61,code=sm_61 \

-gencode arch=compute_61,code=compute_61

# BLAS choice:

# atlas for ATLAS (default)

# mkl for MKL

# open for OpenBlas

BLAS := mkl

# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.

# Leave commented to accept the defaults for your choice of BLAS

# (which should work)!

# BLAS_INCLUDE := /path/to/your/blas

# BLAS_LIB := /path/to/your/blas

# Homebrew puts openblas in a directory that is not on the standard search path

# BLAS_INCLUDE := $(shell brew --prefix openblas)/include

# BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.

# MATLAB directory should contain the mex binary in /bin.

MATLAB_DIR := /usr/local/MATLAB/R2016b

# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# NOTE: this is required only if you will compile the python interface.

# We need to be able to find Python.h and numpy/arrayobject.h.

PYTHON_INCLUDE := /usr/include/python2.7 \ /usr/lib/python2.7/dist-packages/numpy/core/include

# Anaconda Python distribution is quite popular. Include path: # Verify anaconda location, sometimes it's in root.

# ANACONDA_HOME := $(HOME)/anaconda

# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \

# $(ANACONDA_HOME)/include/python2.7 \ # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)

# PYTHON_LIBRARIES := boost_python3 python3.5m

# PYTHON_INCLUDE := /usr/include/python3.5m \

# /usr/lib/python3.5/dist-packages/numpy/core/include

# We need to be able to find libpythonX.X.so or .dylib.

PYTHON_LIB := /usr/lib

# PYTHON_LIB := $(ANACONDA_HOME)/lib

# Homebrew installs numpy in a non standard path (keg only)

# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include

# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)

# 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/lib/x86_64-linux-gnu/hdf5/serial/include

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies

# INCLUDE_DIRS += $(shell brew --prefix)/include

# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)

# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)

# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.

# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)

# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean` BUILD_DIR := build DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171

# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests. TEST_GPUID := 0

# enable pretty build (comment to see full commands) Q ?= @

編譯caffe-master

make all -j16

make test -j16

make runtest -j16

make pycaffe -j16

make matcaffe -j16

十、測試

這里用mnist數據集進行測試。如若下載數據集不方便,附上云盤下載

不做具體說明。

sh data/mnist/get_mnist.sh

sh examples/mnist/create_mnist.sh

sh examples/mnist/train_lenet.sh

至此,配置完畢。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容