Ubuntu16.04 安装 Caffe

安装步骤

首先安装各种依赖包:

1
2
$ sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev python-dev libgflags-dev libatlas-base-dev libhdf5-serial-dev protobuf-compiler
$ sudo apt-get install --no-install-recommends libboost-all-dev

github上面拷贝下来caffe项目:

1
2
$ git clone https://github.com/BVLC/caffe.git
$ cd caffe

安装caffe版的SSD拷贝步骤为:

1
2
3
$ git clone https://github.com/weiliu89/caffe.git
$ cd caffe
$ git checkout ssd

然后将caffe主目录下面的Makefile.config.example拷贝更名为Makefile.config,打开操作:

1
2
$ cp Makefile.config.example Makefile.config
$ gedit Makefile.config

将其中的:

1
2
3
4
#USE_CUDNN := 1
#WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

分别更改为:

1
2
3
4
USE_CUDNN := 1
WITH_PYTHON_LAYER := 1
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

打开 Makefile文件:

1
$ gedit Makefile

将其中的:

1
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

更改为:

1
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

打开/usr/local/cuda/include/crt/host_config.h文件:

1
$ sudo gedit /usr/local/cuda/include/crt/host_config.h

将其中的:

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

更改为:

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

下面就是编译caffe并测试:

1
2
3
$ make clean -j8
$ make all -j8
$ make runtest -j8

最后输出PASS说明测试成功。
配置环境变量:

1
$ vim ~/.bashrc

在文件末尾写入caffe-pathon的安装路径:

1
export PYTHONPATH=caffe安装路径/caffe/python:$PYTHONPATH

上述语句中的~表示caffe所在的根目录。
是环境变量生效:

1
$ source ~/.bashrc

然后执行:

1
$ make pycaffe

常见问题

CUDA9错误
1
2
3
4
5
NVCC src/caffe/layers/bnll_layer.cu
nvcc fatal : Unsupported gpu architecture 'compute_20'
Makefile:594: recipe for target '.build_release/cuda/src/caffe/layers/bnll_layer.o' failed
make: *** [.build_release/cuda/src/caffe/layers/bnll_layer.o] Error 1
make: *** Waiting for unfinished jobs....
解决方案

cuda9不支持‘ compute-20 ’,需要修改Makefile.config文件中CUDA_ARCH设置,将

1
2
3
4
5
6
7
8
9
10
11
12
13
# 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

中的

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

删除即可重新编译。

HDF5错误
1
2
3
4
src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1

或者

1
2
3
4
5
6
7
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0
/usr/bin/ld: cannot find -lhdf5_hl
/usr/bin/ld: cannot find -lhdf5
collect2: error: ld returned 1 exit status
Makefile:572: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1

解决方案

执行命令安装libhdf5-dev

1
$ sudo apt-get install libhdf5-dev

然后再重新编译。

gflags错误
1
2
3
4
5
In file included from src/caffe/net.cpp:10:0:
./include/caffe/common.hpp:5:27: fatal error: gflags/gflags.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
解决方案

执行命令安装gflags

1
$ sudo apt-get install libgflags-dev

然后再重新编译。

glog错误
1
2
3
4
5
In file included from src/caffe/net.cpp:10:0:
./include/caffe/common.hpp:6:26: fatal error: glog/logging.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
解决方案

执行命令安装glog

1
$ sudo apt-get install libgoogle-glog-dev

然后再重新编译。

LMDB错误
1
2
3
4
5
In file included from src/caffe/util/db.cpp:3:0:
./include/caffe/util/db_lmdb.hpp:8:18: fatal error: lmdb.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/util/db.o' failed
make: *** [.build_release/src/caffe/util/db.o] Error 1
解决方案

执行命令安装lmdb

1
$ sudo apt-get install liblmdb-dev

然后再重新编译。

opencv_imgcodecs opencv_videoio错误
1
2
3
4
5
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_videoio
collect2: error: ld returned 1 exit status
Makefile:579: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc5' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc5] Error 1
解决方案

打开Makefile文件,在164行(我的文件)加上opencv_imgcodecs,如下:

1
2
3
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

然后再重新编译。

numpy路径错误
1
2
3
4
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or
directory
compilation terminated.
make: *** [python/caffe/_caffe.so] Error 1`
解决方案

打开python编辑器,通过命令得到numpy的安装路径:

1
2
3
>>> import numpy
>>> dirs = numpy.get_include()
>>> print(dirs)

然后就能看到numpy的安装路径,打开caffe目录下的Makefile.config文件,将65行(我的文件)的路径:/usr/lib/python2.7/dist-packages/numpy/core/include换成刚刚得到numpy的安装路径,然后重新编译。