安装步骤
首先安装各种依赖包: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
4USE_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 | NVCC src/caffe/layers/bnll_layer.cu |
解决方案
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 | src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory |
或者1
2
3
4
5
6
7AR -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 | In file included from src/caffe/net.cpp:10:0: |
解决方案
执行命令安装gflags
1
$ sudo apt-get install libgflags-dev
然后再重新编译。
glog错误
1 | In file included from src/caffe/net.cpp:10:0: |
解决方案
执行命令安装glog
1
$ sudo apt-get install libgoogle-glog-dev
然后再重新编译。
LMDB错误
1 | In file included from src/caffe/util/db.cpp:3:0: |
解决方案
执行命令安装lmdb
1
$ sudo apt-get install liblmdb-dev
然后再重新编译。
opencv_imgcodecs opencv_videoio错误
1 | /usr/bin/ld: cannot find -lopencv_imgcodecs |
解决方案
打开Makefile
文件,在164行(我的文件)加上opencv_imgcodecs
,如下:1
2
3LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
然后再重新编译。
numpy路径错误
1 | python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or |
解决方案
打开python
编辑器,通过命令得到numpy
的安装路径:1
2
3import numpy
dirs = numpy.get_include()
print(dirs)
然后就能看到numpy
的安装路径,打开caffe
目录下的Makefile.config
文件,将65行(我的文件)的路径:/usr/lib/python2.7/dist-packages/numpy/core/include
换成刚刚得到numpy
的安装路径,然后重新编译。