ubuntu 14.04 && 16.04 安装caffe+cuda8.0+pycafee总结 OSError: libcudnn.so.7.0: cannot open shared object file: No such file or directory错误

从开学到现在,caffe装了有4-5次了。在这里做个总结,以防那天,自己的电脑又操作失误,又跪!

建议,如果是自己的电脑,能用网线,可以这样搞,因为到最后关机重启后,不知道是什么原因,系统的设置中,好多软件打不开了。 建议主要看下边Ubuntu16.04的安装,我又重装的,效果很好。

总体思路:

1、先装ubuntu14.04。用UltralSO搞个刻录U盘就好(不知道怎么回事,电脑开不了机,尝试过16.04版本,但是感觉没有14.04好搞。。。)

2、禁用Ubuntu自带显卡驱动

Ubuntu的nouveau禁用方法: 
      在/etc/modprobe.d中创建文件blacklist-nouveau.conf,在文件中输入一下内容
blacklist nouveau options nouveau modeset=0

3、安装cuda

  说明:

  (a)可以先安装NVIDIA显卡驱动,再安装cuda,但是先装显卡驱动之后,就要注意,在安装cuda时,就不要重装cuda里带的显卡驱动了。

  (b)我的流程就是,不自己去装显卡驱动,用cuda里的。

  (c)在安装cuda时,需要关闭图形化界面

使用alt ctrl f1-f6中的任意一个,进入黑屏命令行中,使用用户名和密码登录

service lightdm stop    (使用root账户执行该命令)

将下载好的进行安装,我这里用的是 cuda_8.0.61_375.26_linux.run
所以执行

sh cuda_8.0.61_375.26_linux.run

然后根据提示进行安装,当遇到提示是否安装openGL时,选择no(不明所以,只是其他人这么说)

重启电脑
配置环境变量

      终端中输入 $ sudo gedit /etc/profile
      在打开的文件末尾,添加以下两行。
export PATH=/usr/local/cuda-8.0/bin:$PATH 
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH

使用命令 nvidia-smi 查看当前显卡状态

4、安装cudnn

将cuDNN文件copy到和cuda同一目录下,然后进行解压,解压之后,执行以下命令

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
sudo chmod a+r /usr/local/cuda/include/cudnn.h
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*

然后通过执行cuda中samples的deviceQuery来验证。

5、安装openCV 使用github上的 OpenCV安装脚本,操作超级简单,超级好用。(不过,可能需要很长时间,我单颗 i7 跑了将近1h)

https://github.com/jayrambhia/Install-OpenCV

最后出现OpenCV-3.3.0 ready to be used类似的话,就说明安装成功了

6、安装caffe

安装依赖
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

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

BLAS安装
sudo apt-get install libatlas-base-dev

安装anaconda之后pycaffe依赖好像就不用装了,我忘了,这里附上(所有python库依赖,本来是一行,这里为了排版,分成多行了)
sudo apt-get install -y 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

然后从github上把caffe clone下来
git clone https://github.com/BVLC/caffe.git
然后复制一份Makefile.config文件,自己去修改(这里修改,可以参照网上的一些教程,按需修改,下边附上我修改的)
cp Makefile.config.example Makefile.config

然后在 caffe文件根目录下进行编译
make all -j8  (这里的 -j8  意思是使用8核进行编译, 如果电脑是4核,用 -j4.类似,电脑有几核,最多就可以用几核编译)
make test -j8
make runtest -j8

进行这三步应该会遇到一些问题,请自行Google
如果出错,要重新编译,使用 
make clean (但注意,一使用该命令,所以编译操作就要全部重做一次)

需要使用python caffe接口,使用
make pycaffe -j8
验证 在caffe文件根目录下 cd python 切换到 ./python 目录中然后在终端下输入
python (进入python 命令行中)
import caffe (如果不报错,就说明没问题了)


需要使用matlab caffe接口,使用
make matcaffe -j8

到这里所有编译全部完成。

7、安装anaconda(推荐使用anaconda,因为这里集合了python中常用的科学工具包,不用自己之后一个一个pip install,不足之处就是,可能动态链接库会报错,不过好解决。caffe的issue中有相关回答,或者直接Google)

推荐使用pycharm 配合使用,感觉超好。

pycharm中import caffe/caffe2 ---> http://blog.csdn.net/u013010889/article/details/70808866

至此,caffe 和 python环境应该就没问题了,但具体其他操作呢??

这里推荐一个大牛的博客,自己去翻他的文章学习吧,文章的可看度还是挺高的。

http://www.cnblogs.com/denny402/tag/caffe/

matlab2017a安装教程

http://blog.csdn.net/m0_37407756/article/details/73187654

附的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.
# 注意,如果用anaconda的话,下边两行都要注释掉
# 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的目录根据你安装的目录设置,如果是按默认安装的,因为默认安装目录为/root目录下,所以为  ANACONDA_HOME := $(HOME)/anaconda
ANACONDA_HOME := /home/unicoe/anaconda2
# 将下边的#号注释都删除掉,共3行
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 ?= @

之后的错误记录,可能还有遗漏,自行Google吧

装完anaconda2之后,记得设置一下环境变量
export PATH="/home/tom/anaconda2/bin:$PATH" 



./build/tools/caffe: error while loading shared libraries: libhdf5_hl.so.8: cannot open shared object file: No such file or directory

在 /etc/profile 加入环境变量
export LD_LIBRARY_PATH="/usr/local/cuda/lib64"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/xxx/anaconda2/lib"

注意,这里的  /home/xxx/anaconda2 是你装anaconda2的目录


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
  File "caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "caffe/io.py", line 8, in <module>
    from caffe.proto import caffe_pb2
  File "caffe/proto/caffe_pb2.py", line 4, in <module>
    from google.protobuf.internal import enum_type_wrapper
ImportError: No module named google.protobuf.internal

解决方法:

我是用anaconda2/bin 目录下的pip 进行  pip install protobuf 解决问题

  

安装Ubuntu16.04 caffe 和 py faster rcnn是参考一下几篇博客搞定的

用当前最新的 Ubuntu16.04.3

Ubuntu16.04 Caffe 安装步骤记录(超详尽) http://blog.csdn.net/yhaolpz/article/details/71375762

我的做法:

1、安装完Ubuntu之后,先换显卡驱动,

  (a)换显卡之前,先执行 sudo apt-get update sudo apt-get upgrade ,将系统更新之后, 在System Settings -> Software & Updates -> Additional Drivers 中选择要换的显卡驱动(我当前的是NVIDIA-384),安装完成之后。照着上边的教程把默认显卡驱动禁用了,然后重启查看有没有问题(看能不能进入系统,能进入的话,再进行后续步骤)

  之后我的步骤是

  (b)安装依赖包,

  (c)配置环境变量,

  (d)安装cuda(这里要注意的是,因为不用cuda带的驱动,所以安装的时候,不用关闭图形化界面),

  (e)安装cudnn也没用他的方法,我是将cudnn安装包放在了/usr/local/ 目录下,直接解压,然后用我博客上边的操作,然后用nvcc -V验证一下。

  (f)装openCV也是直接用安装脚本,直接搞定,自己做的很少。 (装完之后,重启看看)

  (g)装caffe(我装在了 /home/unicoe/ 目录下,所以要用 sudo make all -j8 && sudo make runtest -j8 )

  (h)装anaconda2(注意,在装caffe的时候,不要用anaconda2,因为会有蜜汁错误,装了caffe之后再装也是一样的)

  (i)装pycaffe(装了anaconda2之后,配置一下环境变量,就不用想博主那样装一堆Python库了)

  (j)进入 caffe/python 中 python 然后 import caffe,这里会出现一些问题,总结来说,就是缺啥装啥

  好像要装 conda install easydict

      conda install opencv-python

Traceback (most recent call last)

File ImportError: /home/../anaconda2/lib/python2.7/site-packages/zmq/backend/cython/../../../../.././libstdc++.so.6: versionGLIBCXX_3.4.21\' not found

解决:

https://github.com/BVLC/caffe/issues/4953

https://gitter.im/BVLC/caffe/archives/2015/08/20

cd /home/unicoe/caffe

pip install protobuf

sudo apt-get install python-protobuf

conda install libgcc

如果还有其他错误,自行Google吧

装faster rcnn

参照 http://blog.csdn.net/u012841667/article/details/53436615#reply

(a)先下载 py-faster-rcnn

  git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git

(b) cd py-faster-rcnn/lib 然后 make

    1. cd py-faster-rcnn/lib
    2. make

(c)编译/py-faster-rcnn/caffe-fast-rcnn

  改动的地方,和装caffe时一样

  (I)改Makefile.config (这个可以复制装完的caffe中的Makefile.config)

  (II)改Makefile(这个手动改,照着装caffe时的方法,不要直接复制caffe中的Makefile文件,不然会有各种问题)

(d) make -j8 && make pycaffe

  编译的时候,就会报关于cudnn的错,上边的博客中说了,是这里的cudnn版本低,需要换成,现在caffe版本的cudnn,

(e)参照上边的博客,cudnn依赖要改动三个地方(使用 cp命令 cp 源文件位置/源文件 目的文件位置, 具体使用,请自行查看)

  (I)将/py-faster-rcnn/caffe-fast-rcnn/include/caffe/util/cudnn.hpp 换成最新版的caffe里的cudnn的实现,即相应的cudnn.hpp

  (II)将/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layer里的,所有以cudnn开头的文件,例如cudnn_lrn_layer.cu,cudnn_pooling_layer.cpp,cudnn_sigmoid_layer.cu。 都替换成最新版的caffe里的相应的同名文件

  (III)将./include/caffe/layers的,所有以cudnn开头的文件,例如cudnn_conv_layer.hpp,cudnn_lcn_laye.hpp 都替换成最新版的caffe里的相应的同名文件

(f)然后就可以运行demo.py 了

    1. cd py-faster-rcnn/tools
    2. ./demo.py

如果有下列错误,请参照下边博客中所说

http://blog.csdn.net/u014696921/article/details/60140264

可供参考的blog还有 Ubuntu16.04 caffe安装记录 http://www.cnblogs.com/peiyuYang/p/7784787.html

如有问题,请留言,或者发邮件到unicoe@163.com 中。研究生之路刚开始,希望能和大家多多交流。