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

至此,配置完毕。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 148,637评论 1 318
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 63,443评论 1 266
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 99,164评论 0 218
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 42,075评论 0 188
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 50,080评论 1 266
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 39,365评论 1 184
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 30,901评论 2 283
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 29,649评论 0 176
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 33,122评论 0 223
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 29,734评论 2 225
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 31,093评论 1 236
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 27,548评论 2 222
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,028评论 3 216
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 25,765评论 0 9
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,291评论 0 178
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 34,162评论 2 239
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 34,293评论 2 242

推荐阅读更多精彩内容