Caffe + Ubuntu 14.04 + CUDA 8 + cudnn 8.0+Ananconda3+opencv3.0.0
由于我的机子使用GPU是GTX1080,本身已经安装英伟达显卡的驱动,故下面的安装过程不包括显卡驱动的安装
1 CUDA 安装
官网下载CUDA,我选的是CUDA8.0,官网附带安装方法如下:
https://developer.nvidia.com/cuda-downloads
sudo apt-get update ---------- error
在apt-get更新的时候,可能提示找不到‘ppa’源之类的错误.
可以修改软件源镜向的更新地址,也可以直接忽略(我就是直接忽略了,好像没问题)
ubuntu 修改软件源地址
http://jingyan.baidu.com/article/75ab0bcbea7e43d6864db2f1.html
2 cuDNN library 安装
官方说明如下
LINUX cd <installpath> export LD_LIBRARY_PATH=`pwd`:$LD_LIBRARY_PATH Add <installpath> to your build and link process by adding -I<installpath> to your compile line and -L<installpath> -lcudnn to your link line.
从官方网站下载cudnn
后解压。得到的文件是.h
和.so
文件。所以,直接把他们拷贝到/usr/local/include
和/usr/local/lib/
下就好了。
还要记得做自己那个版本对应的软链接,我使用的版本是这样写的
sudo cp cudnn.h /usr/local/include sudo cp libcudnn.so.* /usr/local/lib sudo ln -sf /usr/local/lib/libcudnn.so.5.0.5 /usr/local/lib/libcudnn.so.5 sudo ln -sf /usr/local/lib/libcudnn.so.5 /usr/local/lib/libcudnn.so sudo ldconfig//软连接
3 Ananconda 安装
这里有一点点需要注意的,anaconda的官网已经更新到了ananconda3.4以上了。
但是caffe的python接口官网给的是python2.0的 , 如果你安装anaconda2.x版本应该会和caffe衔接的很好.
我当时脑残了,一下子装了3.4的anaconda,后来编译caffe的python接口真是搞了好久
anaconda安装很简单,就是运行安装包,然后一路选择next就行。
最后一定要记得加上环境变量。
机子重启绝对就生效了。
Verify your install
验证安装是否成功,命令行输入 conda list
4. install opencv3.0
opencv 是非常难装
我换了好几个opencv安装方法,大神都建议用写好的shell脚本来装,最开始我打算自己装,结果遇见无数error,果断弃坑
最后还是靠 宇宙骑士欧老师 的shell安装成功了 感谢欧老师,链接如下
http://blog.csdn.net/shiorioxy/article/details/52652831
opencv编译到72%的时候会出现一个错误,大神给出了下面的解决办法
http://blog.csdn.net/frank_zrh/article/details/52298909
5 安装依赖项
可以参照官网说明的依赖项来安装
http://caffe.berkeleyvision.org/install_apt.html
http://coldmooon.github.io/2015/08/03/caffe_install/
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler sudo apt-get install --no-install-recommends libboost-all-dev
但是这个libboost的安装其实是有讲究的
如果按照官网给的apt-get方法
sudo apt-get libboost-all-dev
安装的是libboost 1.54
如果使用的是python3.5以前的版本,那么这样安装是可以的
如何查看python的版本呢? 你可以直接命令行输入 python
我由于使用的是python3.5的版本,发现libboost 1.54不支持 python3.5
如果你使用的是python3.5及更新的版本,那么请安装libboost 1.55以上
sudo apt-get install libboost1.55-all-dev
除此之外还需要做一个链接
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py34.so.1.55.0 /usr/local/lib/libboost_python3.so
在这里记住,一定要选择py34版本来作为链接,如果你选择py33,那么你后面make pycaffe将会遇到问题
6 安装 caffe-master
在安装和编译caffe的时候,有不少需要注意的。
首先是编辑caffe 的makefile.config文件
先复制一份
cd caffe-master cp 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 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 := 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_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版本进行修改,官方默认的是python2.7版本,如果你和官方一样,那就不要改变了
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.5m \
$(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)下面这个地方要注意,如果你用的是python3,那么一定要把这个注释打开,否则后面会有问题
PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include/python3.5m \
#$(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/local/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
# 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
$ make all -j16 $ make test -j16 $ make runtest -j16 $ make pycaffe -j16 $ make matcaffe -j16 我没有装matlab,这一步我没做
make runtest -j16 花的时间特别久,其实可以先把 make python -j16 做成功了再 make runtest
跑一跑MNIST,测试是否安装成功
这个可以直接参考官网
http://caffe.berkeleyvision.org/gathered/examples/mnist.html
按照正常的流程来说,到这个地方就已经结束了,但是我想补充一点内容,那就是从ipython里面调用caffe
官网的教程里面,ipython是这样导入caffe的
# The caffe module needs to be on the Python path; # we'll add it here explicitly. import sys caffe_root = '../' # this file should be run from {caffe_root}/examples (otherwise change this line) sys.path.insert(0, caffe_root + 'python') import caffe # If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.
但是这个时候,你很容易收获到以下几个错误
No module named _caffe : 没有编译pycaffe接口,或者没有成功 make pycaffe 或者 里的ipython运行目录不是 caffe-master下面的examples
ImportError: /home/zero/Documents/caffe-master/python/caffe/_caffe.so:undefined symbol:
_ZN5boost6python6detail11init_moduleER11PyModuleDefPFvvE
这个导入错误指的是boost的版本不匹配,如果你使用的是python3的代码,但是你在makefile里面没有打开 PYTHON_LIBRARIES := boost_python3 python3.5m 就会这样报错
当你遇到上面那个错误,并且回去打开那个注释的时候,如果你用的是python3.5及其以上的版本,请参考这个链接
在 python3.5 下使用 Caffe Using Caffe with Python3.5
最后,还有一小丢丢的东西,
如果安装了 libboost1.54 想要删除 然后安装 libboost1.55 有可能在删除 libboost1.54 的时候会删除CUDA
重新装上就行了.
GEMO OVER
感谢以下几位博主,贴上链接
http://coldmooon.github.io/2015/08/03/caffe_install/
http://blog.csdn.net/shiorioxy/article/details/52652831
http://blog.csdn.net/lien0906/article/details/51784191