序
在博文开始之前,首先声明一下这是自己写的第一篇博文,其中不乏用词不准确或冗长多余之处,还请各位谅解,有好的建议或修改之处欢迎联系我的邮箱lyxok1@sjtu.edu.cn进行指正~
好的,话不多说,进入正题。这学期由于实验室工作,需要自己在电脑上配置Caffe环境,由于自己机器配置不够,于是先是用的好心室友提供的笔记本,后来又自己买了主机,先后两次配置了Caffe环境。虽然网络上的配置教程林林总总,但机器个体差异不同导致自己不可避免地踩了不少的坑。于是决定配置完后把自己的踩坑经历总结一下,也希望后来配置环境的朋友们能够避免重蹈覆辙。
这次我主要以后来在台式机上配置Caffe的经历为主,显卡为GTX 1080,配置环境为Ubuntu14.04+CUDA8.0+cudnn8.0+OpenCV2.4.13,安装并编译了Pycaffe接口,没有编译Matlab接口。配置过程中主要参考了以下几篇博文:
- http://blog.csdn.net/ubunfans/article/details/47724341/
- http://blog.csdn.net/fengbingchun/article/details/53844852
- http://coldmooon.github.io/2015/08/03/caffe_install/
顺便把Caffe的官网配置教程贴在这里:
安装基本依赖库
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
下载并安装CUDA8.0
这里我采用的是离线.deb安装的方式,首先进入Nvidia官网https://developer.nvidia.com/cuda-downloads下载CUDA的安装包,这里需要选择合适的操作系统和安装方式,由于我是离线.deb安装,因此选择的deb(local)一项。
这里默认所有下载文件以及压缩包都下载到了/home/username/Download目录下,进入该目录,解压并安装:
sudo dpkg -i cuda-repo-ubuntu1404-8-0-local-ga2_8.0.61-1_amd64.deb
sudo apt-get update
sudo apt-get install cuda
注意事项
- 下载的CUDA包自带Nvidia驱动,有的教程推荐驱动与CUDA分开安装,但是我是直接使用的自带驱动,并没有太大问题。
- 很多教程中安装完CUDA后直接重启系统
sudo reboot,但这里是自己遇到的第一个大坑,重启后直接卡在Ubuntu界面了,登录界面都进不去,百度谷歌也找不到解决方法,后来在知乎的一个角落里终于找到了原因以及不是办法的办法:由于我的电脑是双显卡的,当当前显卡切换为Nvidia独立显卡时,会导致以上进入不了登录界面的情况,因此在不跑程序时,或者是关机重启前,要确保当前显卡为intel集显。安装的CUDA包中有自带的显卡查询工具prime-select,通过指令prime-select query查询当前显卡,若是Nvidia,则用sudo prime-select intel切换回集显,再关机。如果一不小心在独显状态下关机导致又卡在Ubuntu界面或登录界面时,可以ctrl+alt+F1进入tty,在执行显卡切换并重启。 - 上面解压的CUDA包是CUDA8.0的版本,不同版本的名字可能不同,下文的cuDNN文件名以及库
libcudnn.so.5.1.10同理。
安装cuDNN
cuDNN相当于一个CAFFE加速引擎,同样需要到Nvidia官网进行申请:https://developer.nvidia.com/cudnn,这里申请需要注册账号并回答一些调查问卷。下好后同样进入下载目录,解压并更新软连接:
tar -zxvf cudnn-8.0-linux-x64-v5.1-ga.tgz
cd cuda
sudo cp lib/lib* /usr/local/cuda/lib64/
sudo cp include/cudnn.h /usr/local/cuda/include/
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.5.1.10
sudo ln -sf libcudnn.so.5.1.10 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig
安装完成后还需要设置一下环境变量,首先sudo gedit /etc/profile 进入/etc/profile文件,在末尾添加:
PATH=/usr/local/cuda/bin:$PATH
export PATH
然后source /etc/profile使之生效,同时添加文件sudo touch /etc/ld.so.conf.d/cuda.conf, 并在其中添加
/usr/local/cuda/lib64
通过sudo ldconfig使之生效
编译CUDA Sample并检测CUDA是否安装成功
进入文件夹/usr/local/cuda/samples,并执行指令sudo make all -j4进行编译,其中-j4指定了并行执行任务的CPU核数为4,因为我的机器为4核。
编译完成后,进入/usr/local/cuda/samples/bin/x86_64/linux/release并运行./deviceQuery查看设备信息,得到如下结果:
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce GTX 1080"
CUDA Driver Version / Runtime Version 8.0 / 8.0
CUDA Capability Major/Minor version number: 6.1
Total amount of global memory: 8114 MBytes (8508145664 bytes)
(20) Multiprocessors, (128) CUDA Cores/MP: 2560 CUDA Cores
GPU Max Clock rate: 1835 MHz (1.84 GHz)
Memory Clock rate: 5005 Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 2097152 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(131072), 2D=(131072, 65536), 3D=(16384, 16384, 16384)
Maximum Layered 1D Texture Size, (num) layers 1D=(32768), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(32768, 32768), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 2 copy engine(s)
Run time limit on kernels: No
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 8.0, CUDA Runtime Version = 8.0, NumDevs = 1, Device0 = GeForce GTX 1080
Result = PASS
注意核对CUDA版本与显卡型号,正确无误则CUDA安装正确, 至此Caffe配置的大部头已经完成了一半了。
安装Atlas
sudo apt-get install libatlas-base-dev
安装OpenCV
安装OpenCV的依赖包:
sudo apt-get update
sudo apt-get install -y --no-install-recommends build-essential cmake libavcodec-dev libavformat-dev libgtk2.0-dev libgtkglext1 libgtkglext1-dev libjpeg-dev libpng-dev libswscale-dev libtbb2 libtbb-dev libtiff-dev pkg-config unzip wget
在OpenCV官网下载2.4.13的源码包,并解压,进入解压文件夹,执行命令:
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=ON -D ENABLE_FAST_MATH=ON -D CUDA_FAST_MATH=ON -D WITH_CUBLAS=1 -D WITH_NVCUVID=on -D CUDA_GENERATION=Auto ..
make -j
sudo make install
#配置环境变量并使之生效
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
sudo ldconfig
注意事项
安装OpenCV是我在配置的时候遇到的第二个大坑,首先执行cmake指令时要注意安装配置,WITH_CUDA=ON表明安装的OpenCV支持CUDA,因此CUDA应该在OpenCV之前安装,其次OpenCV版本很重要,CUDA8.0和OpenCV2.4.13版本是兼容性较好的,之前试过OpenCV2.4.9与2.4.10,均以失败告终,编译的时候报错说某些头文件中缺少变量的声明。
安装Python环境
OpenCV安装完成过后,Caffe的配置可以说是度过了最艰难困苦的一关,接下来就是配置Python环境了。虽然Ubuntu自带Python2.7,但是很多教程推荐使用Anaconda发行版的Python,因此我也采用了这个方式。首先在Anaconda的官网下载对应的安装脚本https://www.continuum.io/downloads,注意分清是Python2 还是 Python3的版本。
下载好后进入下载目录执行脚本即可:sh ./Anaconda2-4.4.0-Linux-x86_64,注意安装最后会提示你是否自动设置环境变量,这里选择No,之后手动设置环境变量。
编辑文件/etc/ld.so.conf,在其末尾添加路径/home/username/anaconda2/lib,同时在~/.bashrc中添加export LD_LIBRARY_PATH="/home/username/anaconda2/lib:$LD_LIBRARY_PATH,并且执行source ~/.bashrc使之立即生效。
安装Caffe
到Github上Caffe的官方发行版本仓库中克隆项目https://github.com/BVLC/caffe,之后进入文件夹/home/username/caffe/python中,如果打开requirements.txt可以看到运行需要的python包以及版本信息。因此需要通过pip来将其安装:
for req in $(cat requirements.txt); do pip install $(req); done
注意事项
很多情况下这些包都不能一次性全部安装成功,因为有的包需要Fortran编译环境的支持,因此安装前可以先安装Fortran依赖包:
sudo apt-get update
sudo apt-get install gfortran
编译Caffe
进入/home/username/caffe目录下,复制一份Makefile.config.example并命名为Makefile.config,并进入该文件进行编辑,根据我之前的配置路径,编辑内容为:
## 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 := atlas
# 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_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)/anaconda2
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
# 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 ?= @
这里注意几点:
- USE_CUDNN前的注释去掉,CPU_ONLY注释掉
- CUDA_DIR变量设置成CUDA配置时include和lib所在的路径
- CUDA版本在8.0以下的均需要注释掉CUDA_ARCH中的一部分内容
- Python相关路径均设置成Anaconda中的
- 由于没有安装Matlab接口,Matlab相关路径需要注释掉
- 总之照着上方的注释进行配置都不会有太大问题
配置完成之后,执行命令进行编译并测试:
make all -j4
make test
make runtest
make pycaffe
至此,我们的Caffe环境终于配置。。。。。失!!败!!!了!!!!,没有想到居然死在了黎明前的黑暗中。。。在make编译的时候没有问题,问题在于编译完成后连接库文件的时候报错,具体内容记不清了,但大致就是undefined referrence to XXXX,同样是百度谷歌无果,最后在Github的提问中找到了问题所在,主要原因是因为我早早地把C/C++编译器从gcc-4.8.4版本升级到了4.9(当然,很多教程也推荐这样),之前在笔记本上编译是没有问题的,但是回到台式机上就出现了这样的问题,强行更改g++/gcc的软连接也没有办法,无奈之下,只好删掉编译器重新安装了一遍,便大功告成了。这个问题的具体原因我也没有搞清楚,但是我的建议还是先不要升级gcc,等到CUDA编译或者最后面编译出问题了再升级试试,这也是我配置过程中遇到的最后一个大坑。