本文旨在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
至此,配置完毕。