caffe安装教程(Ubuntu14+GPU+pycaffe+anaconda2)

caffe安装教程

本文所使用的底层环境配置:cuda8、cudnn6、OpenCV2.4.5、anaconda2(Python2.7)。如使用其他版本的环境,如cuda,可安装自己的版本,但须在相应部分作出修改。
查看cuda版本`cat /usr/local/cuda/version.txt`
查看cudnn版本`cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2`

1.底层以及依赖环境搭建

本文所用的底层环境事先已经搭建好,主要包括有[Python2.7(anaconda版)](https://blog.csdn.net/qq_26898461/article/details/51485491),[cuda、cudnn(支持GPU)](https://blog.csdn.net/wanzhen4330/article/details/81699769)以及[opencv](https://blog.csdn.net/woainishifu/article/details/77449373),这些环境的安装不是本文的重点,读者可参考本文的链接或自寻他法,下文是建立在以上环境都成功搭建的前提下进行的。大家在选择底层依赖环境的时候可参考[caffe官网指导内容](http://caffe.berkeleyvision.org/installation.html)。

## 2.下载caffe源代码

caffe可使用`git clone https://github.com/BVLC/caffe/`指令直接下载,或者去GitHub上[https://github.com/BVLC/caffe](https://github.com/BVLC/caffe)下载,将下载下来的文件解压缩即可。使用上述两种方法之一成功后便会得到一个名为caffe的文件夹。

## 3. 修改配置文件

使用`cd caffe`指令进入caffe目录后,会看到这样的内容![caffe目录](https://img-blog.csdnimg.cn/20190329213159347.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM4OTA4ODE2,size_16,color_FFFFFF,t_70)
从目录中可以看到有一个==Makefile.config.example==文件,将其内容复制到==Makefile.config==文件中或者直接将前者的.example后缀去掉即可。打开==Makefile.config==文件,并对其依据个人情况进行修改。下面从前往后依次说明本人的改动。

```javascript
## 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
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 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
```
这段内容中将`USE_CUDNN := 1`这一行原本的注释去掉了,表明支持GPU版本,若只想使用CPU版本则将`# CPU_ONLY := 1`这一行的注释去掉。由于本人使用的是OpenCV2.4,所以关于OpenCV3的部分也不用改动。
```javascript
# 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.
# For CUDA >= 9.0, comment the *_20 and *_21 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
```
这一部分将`CUDA_DIR := /usr/local/cuda`改为自己的cuda路径,由于cuda允许多版本同时存在,为了方便起见,可在本人机器上安装多个cuda,然后在` /usr/local/`建立`cuda`软连接指定到当前实际使用的cuda目录即可。建立软连接代码`ln -s /usr/local/cuda /path/to/your/cuda`又由于本人使用的是cuda8,所以后面关于cuda的部分也不用修改,大家可参照注释根据自己的cuda版本进行修改。
```javascript
# 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
```
这一部分没有修改。
```javascript
# 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

```
这一部分如果使用Python接口的话就需要修改,本文编译了Python接口,所以着重对这一部分进行修改。首先,由于使用的是anaconda中的Python,所以得将原Python相关行给注释掉,然后将`ANACONDA_HOME`相关行解注释并且换成自己使用的anaconda路径。再下来同样要将`PYTHON_LIB`换成anaconda的,最后若要使用Python来写caffe的网络层(Python Layer),得将`WITH_PYTHON_LAYER := 1`这一行注释去掉。
```javascript
# 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 ?= @
```
在后面直至文档末的内容都没有改动。
## 编译caffe

修改完配置文件==Makefile.config==之后,在caffe目录下依次执行`make all -j8`、`make test -j8`、`make runtest -j8`、`make pycaffe`,如果都编译通过,则caffe编译完成。期间,可能会出现以下l类似错误
1.编译runtest的时候可能会出现
> error while loading shared libraries: libhdf5_hl.so.10: cannot open shared object file: No such file

此问题解决方法为在本人home目录下修改.bashrc配置文件,添加anaconda的lib库文件,具体操作为`vi ~/.bashrc`进入文件,然后在文末加上`export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/cz/anaconda2/lib`。这样我们的编译就能全部通过了,在Python中使用caffe的时候有可能会出现下面错误。
2.直接使用`import caffe`指令的时候报错

> ImportError: No module named caffe

解决方法,将caffe中的python路径添加到系统路径中,指令如下:
```javascript
import sys
import os
caffe_root = '/home/cz/enviroment/caffe/'
caffe_path = os.path.join(caffe_root,'python')
if caffe_path not in sys.path:
sys.path.insert(0,caffe_path)
import caffe
```
这里的caffe_root为你自己的caffe路径,或者更简单的
```javascript
import sys
sys.path.insert(0,'/home/cz/enviroment/caffe/python')
import caffe
```

3.解决完2之后还报错

> ImportError: No module named _caffe

此问题解决方法,重新编译一下pycaffe,指令`make pycaffe`
4.解决完3之后import caffe还是报错

> No module named google.protobuf.internal

解决方法,我装的是Anaconda2, 解决方法是在其中安装protobuf最新版本,我用的pip指令,`pip install protobuf`。在这里强调一下,由于本人机器上有多个版本的python,为了避免混淆,大家拥有多个python的时候可以使用`python2 -m pip install protobuf`来避免使用pip下载包的时候出现混淆。这里的`python2`是你实际使用的python。
至此,我就可以在python中正常import caffe啦!

## 总结

安装caffe的过程实属不易,期间可能会出现各种奇怪的错误,我这里给出的只是我成功安装的经历,由于大部分环境已经提前部署好,所以在安装caffe的时候还算比较顺利,大家在安装自己的caffe的时候很有可能还会出现其他错误,此时就要耐心看看错误提示是啥,结合网上资料,慢慢修正,最后,总会成功的!

============================================ 补充 ==============================================
## 第二次安装caffe时出现的错误(caffe可真是个神奇的东西呢)
1.最先出现的和hdf5有关的报错,编译CPU版本的也不行
>make: *** [.build_release/src/caffe/net.o] Error 1
>make: *** [.build_release/src/caffe/layers/data_layer.o] Error 1

解决方法:注释掉`USE_HDF5 := 0`(尝试过各种方法,还是这个见效)PS:也试了[Ubuntu14.04+caffe配置过程中出现的问题](https://blog.csdn.net/musechipin/article/details/74701779)中提到的方法,可能有用吧。(还是别用了吧,后面又出错了,如果用了,请见第3点!)

2.随后出现的和c++编译有关的错(网上很多说是和gcc版本有关,不要轻易相信!!!!!!!!)

> make: *** [.build_release/cuda/src/caffe/layers/split_layer.o] Error 1(类似这种)

这里在Makefile.config中添加了两句话,可能第二句更有用一点吧。

```
CXXFLAGS+=-std=c++11
CUSTOM_CXX := g++ -std=c++11
```
3.由于1中使用了不当方法,导致编译的hdf5库和链接的库不一致了,如果在`make runtest`时出现以下错误

> HDF5 library version mismatched error

则使用以下指令或相关指令强行安装hdf5 1.8.17版本。
`conda install -c anaconda hdf5=1.8.17`

posted @ 2019-04-29 17:45  ChenZhang  阅读(147)  评论(0编辑  收藏  举报