提前说明:在开始之前我已经安装了opencv2.4.9以及cuda7.5.opencv 有很多安装的博客可以参考没什么好说的,cuda7.5建议看我之前博客 :ubuntu14.04+cuda7.5安装 官方步骤版,因为csdn中有不少关于cuda的安装,我之前也用了很简单的方法安装成功,但调用过程bug百出,中间重装了20+遍系统,所以还是按照官方步骤安装。
再研究deep learning中要用到很多python的库,需要我们搭建cuda与caffe等的链接,此文将详细介绍这一整套系统的安装过程方法
(接着我上一篇ubuntu14.04+cuda7.5安装 官方步骤版)
1、安装开发所需要的依赖包
sudo apt-get install build-essential # basic requirement
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 #required by caffe
2、下载安装cudnn
注:不建议到官方下载安装,因为他需要审批时间,得1到两天,可以直接再csdn上下载,地址:http://download.csdn.net/detail/eagelangel/9617094
将压缩包解压
出来一个cuda文件夹,然后cd到该cuda/lib64文件夹
sudo cp lib* /usr/local/cuda/lib64/
然后cd到另一个文件夹cuda/include
sudo cp cudnn.h /usr/local/cuda/include/
再依次执行,更新软连接:
cd /usr/local/cuda/lib64/
sudo chmod +r libcudnn.so.5.0.5
sudo ln -sf libcudnn.so.5.0.5 libcudnn.so.5
sudo ln -sf libcudnn.so.5 libcudnn.so
sudo ldconfig
3、设置环境变量(在profile中添加)
在终端
sudo gedit /etc/profile
在文件的最下方加入(可有可无,最好还是加上吧,当时没加也成功了)
PATH=/usr/local/cuda/bin:$PATH
export PATH
再执行source,是刚才操作立即生效
source /etc/profile
同时需要添加lib库路径: 在 /etc/ld.so.conf.d/建立文件 cuda.conf(方法为 sudo touch /etc/ld.so.conf.d/cuda.conf ), 内容如下
/usr/local/cuda/lib64
保存后在终端执行以下语句,使其立即生效
sudo ldconfig
4、安装cuda sample
进入/usr/local/cuda/samples, 执行下列命令来build samples
sudo make all -j4
整个过程大概10分钟左右, 全部编译完成后, 进入 samples/bin/x86_64/linux/release, 运行
./deviceQuery
如果看到以下信息即cuda已经cuda sample安装成功
(注:我的显卡是GT 650M,不同显卡信息不同只要能看到很多信息即可)
5、安装Atlas
sudo apt-get install libatlas-base-dev
6、安装caffe所需要的python包
建议安装Anaconda包,也可以用pip等安装方法,以下为Anaconda安装方式
Anaconda linux系统下载官网:https://www.continuum.io/downloads#all
下载download for linux 中的python2.7版本(建议)
下载好后cd到下载目录,运行:
bash Anaconda2-4.2.0-Linux-x86_64.sh
(注:Anaconda包一直再更新,所以上面的命令版本号可能会变,具体参考官网上的下载命令)
整个安装步骤一直选择默认(有一个选择为是否将Anaconda路径写如.bashrc文件,安装默认就是no,不要犹豫就是选no。。。如果选yes在之后的过程找不到caffe,血的教训。。)
7、添加Anaconda库路径
在/etc/ld.so.conf最后加入以下路径(具体方法 sudo gedit /etc/ld.so.conf) 注:username为你自己的计算机名字
/home/username/anaconda/lib
后在你自己的.bashrc文件中添加以下内容(注:username为你自己的计算机名字)
export LD_LIBRARY_PATH="/home/username/anaconda/lib:$LD_LIBRARY_PATH"
8、下载caffe包(github)
github网址 : https://github.com/BVLC/caffe
建议解压到你的home文件夹下面
之后cd到caffe-master的python目录下面
执行命令
for req in $(cat requirements.txt); do pip install $req; done
就是按照 requirements.txt检查你目前的系统配置符不符合caffe要求,不符合自动为你升级到合适的版本
9、编译caffe
进入caffe-master目录,复制一份Makefile.config.examples
cp Makefile.config.example Makefile.config
操作含义:本来caffe给的Makefile.config.example例子,复制到make操作需要的
Makefile.config中。下面需要修改该
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 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_50,code=compute_50
# 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
# 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 ?= @
注意:①打开cudnn②ANACONDA_HOME为自己的anaconda文件路径,我的为/home/anaconda2
再进行编译:
make all -j4
make test
make runtest
10、最后一步
make pycaffe
编译完成后测试:
用终端进入到caffe-master/python目录下,在终端输入:
python
import numpy
import caffe
有可能会出现的问题:
1、import caffe error:can‘t find module skimage.io
这需要你重新编译以下caffe
cd caffe-master
make clean
$ sudo apt-get install 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
$ sudo apt-get update
make all -j4
make test
make runtest
make pycaffe
或者
ImportError: numpy.core.multiarray failed to import
这个原因一般是caffe中的io.py作为关键字影响了python的运行环境,为什么这样说呢,因为安装这整套系统可能有的读者已经尝试的别人的方法,有的博客里可能要求读者添加caffe路径再.bashrc文件中,这有一个致命的错误,当你已经选定用anaconda或默认系统python,此时与caffe中的io.py有重定义.所以在之前的步骤也提了,一定不要把caffe路径放在.bashrc文件中.
解决方法:
查看你的.bashrc文件(操作在终端输入 sudo gedit .bashrc)里是否有你的caffe路径,如果有请注释掉
注释完之后运行
source .bashrc
使得其立即生效
但是这里注意一点,由于该命令为立即生效,但我的ubuntu14.04该命令并不能立即生效.我的经验是source语句只有增添路径时候立即生效,撤销地址只能通过重启系统来生效
所以下一步就重启电脑
用终端进入到caffe-master/python目录下,在终端输入:
python
import numpy
import caffe
就可以看见caffe加载成功了