在配置caffe-ssd的CPU版本时,遇到很多坑,查阅了相关资料,并结合自己实际的配置过程,梳理出现在的这个流程,对于初次接触Caffe的CPU版本编译的朋友或许有帮助。
anaconda下载地址:清华镜像https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/
caffe下载地址:https://github.com/weiliu89/caffe.git
第一步:使用apt-get安装caffe的依赖库
sudo apt-get install git #git用于clone网站上的文件
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler # 利用root用户安装protobuf, leveldb, opencv, hdf5-serial. protobuf, opencv, leveldb改为手动源码安装.
sudo apt-get install –no-install-recommends libboost-all-dev # 安装boost库, 改为手动源码安装.
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev # 安装OPENBLAS, LAPACK, ATLAS库.
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev# gflags, glog改为手动源码安装.
第二步:安装Python环境
由清华镜像https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/ 下载anaconda2(Linux64位版本)
双击anaconda2-2.5.0-linux-64.sh或者切换至anaconda2-2.5.0-linux-64.sh所在的路径运行命令:
sh anaconda2-2.5.0-linux-64.sh进行安装,默认安装在Home目录下
安装时会自动将anaconda2/bin添加到环境变量
第三步:下载caffe的SSD源码
git clone https://github.com/weiliu89/caffe.git #默认下载至Home目录下
cd caffe
git checkout ssd #出现“分支”则说明copy-check成功
第四步:修改Makefile.config和Makefile的配置参数
首先在Caffe目录下执行命令:cp Makefile.config.example Makefile.config 生成Makefile.config文件
执行命令:gedit Makefile.config 打开Makefile.config文本,对其进行修改
本文是配置CPU版本,修改如下:
CPU_ONLY :=1 #去掉注释
USE_OPENCV := 1 #去掉注释,并将其值改为1
USE_LEVELDB := 1 #去掉注释,并将其值改为1
USE_LMDB := 1 #去掉注释,并将其值改为1
注释掉CUDA有关的行:
#USE_CUDNN := 1
#CUDA_DIR := /usr/local/cuda
#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_50,code=compute_50
修改python的路径,因为Python环境用的anaconda2,所以使用anaconda的路径:
ANACONDA_HOME := $(HOME)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \
PYTHON_LIB := $(ANACONDA_HOME)/lib
WITH_PYTHON_LAYER := 1的注释去掉
INCLUDE_DIRS和LIBRARY_DIRS分别添加上hdf5的serial路径,其中,i386-linux-gnu/hdf5/serial是32位的版本,
x86_64-linux-gnu/hdf5/serial是64位的版本,Ubuntu系统是64位的,所以添加上64位的hdf5如下:
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
#TEST_GPUID := 0
Makefile的修改如下:
为避免各类undefined reference错误,需要将LIBRARIES修改为以下形式:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem boost_regex m hdf5_serial_hl hdf5_serial
第五步:编译caffe和pycaffe
依次执行以下命令:
make all -j8
make test -j8
make runtest -j8
make pycaffe
Caffe编译时报错:
protobuf版本的问题
Makefile:582: recipe for target '.build_release/src/caffe/layers/rnn_layer.o' failed
make: *** [.build_release/src/caffe/layers/rnn_layer.o] Error 1
In file included from ./include/caffe/util/cudnn.hpp:8:0,
from ./include/caffe/util/device_alternate.hpp:40,
from ./include/caffe/common.hpp:19,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/layers/exp_layer.hpp:6,
from src/caffe/layers/exp_layer.cpp:3:
.build_release/src/caffe/proto/caffe.pb.h:17:2: error: #error This file was generated by an older version of protoc which is
#error This file was generated by an older version of protoc which is
^
.build_release/src/caffe/proto/caffe.pb.h:18:2: error: #error incompatible with your Protocol Buffer headers. Please
#error incompatible with your Protocol Buffer headers. Please
^
.build_release/src/caffe/proto/caffe.pb.h:19:2: error: #error regenerate this file with a newer version of protoc.
#error regenerate this file with a newer version of protoc.
下载最新的protobuf版本,下载地址:https://github.com/google/protobuf/releases 此处可尝试protobuf2.6.0版本
报错:
CXX .build_release/src/caffe/proto/caffe.pb.cc
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0-rc3
/usr/bin/ld: 找不到 -lsnappy
/usr/bin/ld: 找不到 -lopenblas
collect2: error: ld returned 1 exit status
Makefile:567: recipe for target '.build_release/lib/libcaffe.so.1.0.0-rc3' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0-rc3] Error 1
解决方案:重新安装libsnappy-dev和libopenblas-dev
安装命令:sudo apt-get install libsnappy-dev
sudo apt-get install libopenblas-dev
报错:
Warning! ***HDF5 library version mismatched error***
The HDF5 header files used to compile this application do not match
the version used by the HDF5 library to which this application is linked.
Data corruption or segmentation faults may occur if the application continues.
This can happen when an application was compiled by one version of HDF5 but
linked with a different version of static or shared HDF5 library.
You should recompile the application or check your shared library related
settings such as 'LD_LIBRARY_PATH'.
You can, at your own risk, disable this warning by setting the environment
variable 'HDF5_DISABLE_VERSION_CHECK' to a value of '1'.
Setting it to 2 or higher will suppress the warning messages totally.
Headers are 1.10.2, library is 1.8.16
编译时的hdf5版本与链接时的版本不一致,重新编译,错误消除
报错:
/usr/lib/x86_64-linux-gnu/libopencv_highgui.so:对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用
Makefile:620: recipe for target '.build_release/tools/caffe.bin' failed
权限不够导致:sudo su
make all -j8
make test -j8
make runtest -j8
make pycaffe
编译pycaffe时报错:
Makefile:576: recipe for target '.build_release/src/caffe/layer_factory.o' failed
/usr/include/boost/python/detail/wrap_python.hpp:50:23: fatal error: pyconfig.h: 没有那个文件或目录
python的路径问题:在.bashrc文件里添加CPLUS_INCLUDE_PATH:export CPLUS_INCLUDE_PATH="/home/cw/anaconda2/include/python2.7:$CPLUS_INCLUDE_PATH"
报错:
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: 没有那个文件或目录
compilation terminated.
Makefile:502: recipe for target 'python/caffe/_caffe.so' failed
python-numpy没有安装到位,安装numpy:sudo apt-get install python-numpy
import caffe时报错:
ImportError: No module named google.protobuf.internal
我装的是anaconda2, protobuf的版本问题,解决方法是安装protobuf2.6.0版本
命令如下:sudo su
conda uninstall protobuf
pip install protobuf==2.6.0
注:在编译过程中若报错,在重新编译之前需要先清除之前的编译,使用命令:make clear