Ubuntu16.04中Caffe的安装

1. 首先安装一些依赖库

$ sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install -y libatlas-base-dev
$ sudo apt-get install -y --no-install-recommends libboost-all-dev
$ sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo apt-get install -y python-pip python-dev python-numpy python-scipy
$ sudo apt-get install -y libopencv-dev

2.下载caffe.git 到download目录

$ git clone https://github.com/BVLC/caffe.git
$ cd ~/download
$ cp -r ./caffe ~/

3.进入 caffe 中复制配置文件并重命名

$ cd ~/caffe
$ sudo cp Makefile.config.example Makefile.config

4.修改配置文件

$ sudo vim Makefile.config

根据个人需要进行修改

a.若使用 cudnn,则将#USE_CUDNN := 1
修改成:USE_CUDNN := 1
注意:GPU运算能力(GPU Compute Capability )3.0以上才支持CUDNN

b.若使用的 opencv 版本是 3 的,则将#OPENCV_VERSION := 3
修改为:OPENCV_VERSION := 3
c.若要使用 python 来编写 layer,则将#WITH_PYTHON_LAYER := 1
修改为:WITH_PYTHON_LAYER := 1
d.重要的一项 :将# 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/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial
e.还要更改
USE_OPENCV: = 1
USE_LEVELDB: = 1
USE_LMDB: = 1

5. 修改 makefile 文件

打开 Makefile 文件,在 415 行,将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

6.修改 host_config.h 文件

sudo vim /usr/local/cuda/include/host_config.h

将其中的第 119 行注释掉:

#error-- unsupported GNU version! gcc versions later than 5 are not supported!

改为:

//#error-- unsupported GNU version! gcc versions later than 5 are not supported!

7.执行编译和测试命令

在~/ caffe/目录下

$ make all -j4
$ make test -j4
$ make runtest -j4

问题:
Makefile:532: recipe for target 'runtest' failed
make: *** [runtest] 已放弃 (core dumped)

这个问题就是,电脑不支持cudnn,再前面的Makefile.config里面去掉CUDNN就可以了。

另外,
编译过程中出现警告:
nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).
解决:修改Makefile.config文件

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

中的

 -gencode arch=compute_20,code=sm_20 \
-gencode arch=compute_20,code=sm_21 \

去掉即可。

重新clean再编译一边

$ make clean
$ make all -j4
$ make test -j4
$ make runtest -j4


[----------] 2 tests from BilinearFillerTest/1, where TypeParam = double
[ RUN      ] BilinearFillerTest/1.TestFillEven
[       OK ] BilinearFillerTest/1.TestFillEven (5 ms)
[ RUN      ] BilinearFillerTest/1.TestFillOdd
[       OK ] BilinearFillerTest/1.TestFillOdd (8 ms)
[----------] 2 tests from BilinearFillerTest/1 (13 ms total)

[----------] 1 test from SolverTest/1, where TypeParam = caffe::CPUDevice<double>
[ RUN      ] SolverTest/1.TestInitTrainTestNets
[       OK ] SolverTest/1.TestInitTrainTestNets (1 ms)
[----------] 1 test from SolverTest/1 (1 ms total)

[----------] Global test environment tear-down
[==========] 2063 tests from 271 test cases ran. (605597 ms total)
[  PASSED  ] 2063 tests.

如上面所示,说明编译成功了。

8. 配置 pycaffe 接口

在 caffe 根目录的 python 文件夹下,有一个 requirements.txt 的清单文件,上面列出了需要的依赖库,按照这个清单安装就可以了。在安装 scipy 库的时候,需要先安装 fortran 编译器
(gfortran),如果没有这个编译器就会报错,因此,我们可以先安装一下。
a.首先回到 caffe 的根目录,然后执行安装代码:

$ sudo apt-get install gfortran
$cd ./python

b.在终端输入下面一段即可

for req in $(cat requirements.txt); do pip install $req; done

错误:
IOError: [Errno 13] 权限不够: '/home/fc/anaconda2/lib/python2.7/site-packages/leveldb.so'
换root用户重新执行上面的命令

c.安装完成以后,再次回到 caffe 根目录我们可以执行:

$ cd ~/caffe
$ sudo pip install -r python/requirements.txt

问题:
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-Io7ETq/ipython/
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

$ sudo python -m pip install --upgrade --force pip
$ sudo pip install setuptools==33.1.1

9.编译 python 接口

$ sudo make pycaffe

10. 配置环境变量,以便 python 调用

$sudo vim ~/.bashrc

加入
export PYTHONPATH=/home/fc/caffe/python:$PYTHONPATH

$ source ~/.bashrc