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`

caffe安装教程(Ubuntu14+GPU+pycaffe+anaconda2)的更多相关文章

  1. windows10下基于vs2015的 caffe安装教程及python接口实现

    啦啦啦:根据网上的教程前一天安装失败,第二天才安装成功.其实caffe的安装并不难,只是网上的教程不是很全面,自己写一个,留作纪念. 准备工作 Windows10 操作系统 vs2015(c++编译器 ...

  2. Caffe安装教程(原创)

    转载请注明地址 说明:本文档参考自Caffe官网的安装说明,http://caffe.berkeleyvision.org/installation.html 如果对安装过程中,需要用到的依赖不明,请 ...

  3. CAFFE安装 CentOS无GPU

    前记 由于是在一台用了很久的机器上安装caffe,过程比较复杂,网上说再干净的机器上装比较简单.如果能有干净的机器,就不用再过这么多坑了,希望大家好运!介绍这里就不说了,直接进入正题: Caffe 主 ...

  4. GNU Radio安装教程: Ubuntu14.04 + uhd3.10.0 + gnuradio3.7.10.1

    1. 更新和安装依赖项 在编译安装uhd和gnuradio之前,确保已安装所需依赖项.Ubuntu系统运行: sudo apt-get update 安装UHD和GNURadio所需依赖项: On U ...

  5. UBUNTU 15.10 CAFFE安装教程(测试可用)

    转帖:https://github.com/BVLC/caffe/wiki/Ubuntu-15.10-Installation-Guide Ubuntu 15.10 have been release ...

  6. OpenLTE安装教程

    安装需求: USB3 interface Modern multicore CPU (Intel Core i5, Core i7 or equivalent with SSE4.1 SSE4.2 a ...

  7. ubuntu16.04+caffe+GPU+cuda+cudnn安装教程

    步骤简述: 1.安装GPU驱动(系统适配,不采取手动安装的方式) 2.安装依赖(cuda依赖库,caffe依赖) 3.安装cuda 4.安装cudnn(只是复制文件加链接,不需要编译安装的过程) 5. ...

  8. 转 Windows+VS2013爆详细Caffe编译安装教程

    1. 安装cuda Cuda是英伟达推出的GPU加速运算平台 我这里安装的是cuda7.5,已经安装过的忽略,还没有安装过的这里有安装教程.windows下面安装还是非常简单的. 点击打开链接    ...

  9. Caffe学习系列(1):安装配置ubuntu14.04+cuda7.5+caffe+cudnn

    一.版本 linux系统:Ubuntu 14.04 (64位) 显卡:Nvidia K20c cuda: cuda_7.5.18_linux.run cudnn: cudnn-7.0-linux-x6 ...

随机推荐

  1. Linux设置开放一个端口

    修改防火墙配置需要修改 /etc/sysconfig/iptables 这个文件,如果要开放哪个端口,在里面添加一条. -A RH-Firewall-1-INPUT -m state --state ...

  2. swap分析及其使用

    什么是swap swap主要是在内存不够用的时候,将部分内存上的数据交换到swap空间上,以便让系统不会因为内存不够用而导致oom或者更致命的情况出现.当内存使用存在压力的时候,开始触发内存回收行为, ...

  3. checkbox事件的变化

    <input type="checkbox" checked={this.state.checked} onChange={this.checkedChangeHandler ...

  4. FreeRTOS数据结构(一)--链表和链表项

    结构体定义 /*链表结构体*/ typedef struct xLIST { listFIRST_LIST_INTEGRITY_CHECK_VALUE /*用于链表完整性检查*/ configLIST ...

  5. 借助HTML中的特殊符号在markdown中打印出来

    转自:https://blog.csdn.net/html5_/article/details/21639475 HTML中的特殊符号 原创 2014年03月20日 18:35:23 标签: HTML ...

  6. testng生成自定义html报告

    转自:https://blog.csdn.net/kdslkd/article/details/51198433 testng原生的或reportng的报告总有些不符合需要,尝试生成自定义测试报告,用 ...

  7. MVC之图片验证码

    MVC之图片验证码 controller中的action方法public ActionResult GetValidateCode() { ValidateCode vCode = new Valid ...

  8. dqname_widnows.go

    package nsqd // On Windows, file names cannot contain colons. func getBackendName(topicName, channel ...

  9. BZOJ_3932_[CQOI2015]任务查询系统_主席树

    BZOJ_3932_[CQOI2015]任务查询系统_主席树 题意: 最近实验室正在为其管理的超级计算机编制一套任务管理系统,而你被安排完成其中的查询部分.超级计算机中的 任务用三元组(Si,Ei,P ...

  10. ArchLinux 安装五笔输入法

    说明 自己的笔记本已经全盘做成了ArchLinux系统了,用着还好,苦于常用的五笔输入法在Arch下有点不太好装,参考wiki弄好了,这里简单记录下 这里使用ibus-rime 原因有二: ibus- ...