安装caffe之前,你需要先做一些准备工作,包括:
关于这些步骤,网上已经有很多写得非常详细的教程,在此就不多赘述了。读者可以参考一下这些博文《Ubuntu16.04 14.04安装配置Caffe(GPU版)——天梦冰蚕》、《Ubuntu16.04 Caffe 安装步骤记录(超详尽)——王英豪》
这里主要介绍一下如何配置caffe
由于MobileNetv2-SSDlite需要使用ssd版本的caffe,所以我们需要到github上下载对应的源码
git clone https://github.com/weiliu89/caffe.git
cd caffe
若你要使用tensorflow提供的pre-trained model那么你需要安装chuanqi305提供的caffe-ssd。这个版本在weiliu89版本的基础上添加了ReLU6 layer
本文以weiliu89版本的caffe为例子
git clone https://github.com/chuanqi305/ssd.git
mv ssd caffe
cd caffe
首先,拷贝caffe提供的example文件
cp Makefile.config.example Makefile.config
打开Makefile.config
gedit Makefile.config
根据自己的情况修改对应的项目
(1)若要使用cudnn加速,则去掉第5行的注释
[4] # cuDNN acceleration switch (uncomment to build with cuDNN).
[5] USE_CUDNN := 1
(2)若仅使用CPU,则去掉第8行的注释
[7] # CPU-only switch (uncomment to build without GPU support).
[8] CPU_ONLY := 1
(3)若OpenCV版本为3.x,则去掉第21行的注释
[20] # Uncomment if you're using OpenCV 3
[21] OPENCV_VERSION := 3
(4)若你的cuda没有安装在默认位置,可以修改第28行的CUDA_DIR
[27] # CUDA directory contains bin/ and lib/ directories that we need.
[28] CUDA_DIR := /usr/local/cuda
(5)根据你的CUDA版本,注释35-41行的对应内容(caffe-ssd里没有相关的提示,这里提供了官方版caffe的说明)
# 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.
例如,我装的是CUDA9.0,因此要将code=sm_20和code=sm_21两行注释掉
[35] CUDA_ARCH := -gencode arch=compute_30,code=sm_30 \
[36] -gencode arch=compute_35,code=sm_35 \
[37] -gencode arch=compute_50,code=sm_50 \
[38] -gencode arch=compute_52,code=sm_52 \
[39] -gencode arch=compute_61,code=sm_61
[40] #-gencode arch=compute_20,code=sm_20 \
[41] #-gencode arch=compute_20,code=sm_21 \
(6)caffe默认使用python2.7,若要进行修改,需要先注释掉第66行和第67行
[64] # NOTE: this is required only if you will compile the python interface.
[65] # We need to be able to find Python.h and numpy/arrayobject.h.
[66] # PYTHON_INCLUDE := /usr/include/python2.7 \
[67] # /usr/lib/python2.7/dist-packages/numpy/core/include
然后,若想要使用python3,可以把第76行、第77行、第78行的注释去掉
[75] # Uncomment to use Python 3 (default is Python 2)
[76] PYTHON_LIBRARIES := boost_python3 python3.5m
[77] PYTHON_INCLUDE := /usr/include/python3.5m \
[78] /usr/lib/python3.5/dist-packages/numpy/core/include
若想要使用anaconda,可以把第70行、第71行、第72行的注释去掉,然后将ANACONDA_HOME修改为你的anaconda地址
[68] # Anaconda Python distribution is quite popular. Include path:
[69] # Verify anaconda location, sometimes it's in root.
[70] ANACONDA_HOME := $(HOME)/anaconda2
[71] PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
[72] $(ANACONDA_HOME)/include/python2.7 \
[73] $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
(7)若要使用python来编写layer,可以把第89行的注释去掉
[88] # Uncomment to support layers written in Python (will link against Python libs)
[89] WITH_PYTHON_LAYER := 1
(8)修改92行和93行,告诉caffe你的hdf5的地址
[91] # Whatever else you find you need goes here.
[92] INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
[93] 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)修改第409行
[409] NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
现在,所有前期工作都完成了,可以开始编译caffe了
make all -j8
make test -j8
make runtest -j8
如果执行make runtest后看到类似于这样的结果
[----------] Global test environment tear-down
[==========] 2361 tests from 309 test cases ran. (786565 ms total)
[ PASSED ] 2361 tests.
那么,恭喜,caffe-ssd编译成功了
make pycaffe -j8
配置环境变量
cd
gedit .bashrc
在.bashrc中添加
export PYTHONPATH=/home/bmelab/caffe/python:$PYTHONPATH
其中/home/bmelab/caffe请改成你的caffe地址
然后更新.bashrc
source ~/.bashrc
如果你在编译过程中看到google::ptorobuf或google::protoc这样的报错,那么你可能需要从github上下载合适版本的protobuf,并从源码编译。如果你要使用pycaffe,那么你要先build C++代码,再build python代码
另外,需要注意caffe不支持高版本的protobuf。我之前编译过3.6.1版本的protobuf,结果这次make all时报了一堆错误,并且提示我
error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
这应该是高版本的protobuf使用了C++ 2011标准,caffe默认的配置文件不支持
把protobuf换成2.6.1版本后再次make all,编译成功