记录编译caffe的心酸历程
踩过了好多坑,经过千辛万苦终于编译好了caffe,import caffe 没有报错.我怀着激动的心情记录这个博客,为大家排一些我遇到的坑.
0. 环境
我的环境是ubuntu16.04+cuda9.0+cudnn7.0.5+opencv3.3+anaconda3(python3.5),建议大家先多参考几个博客,确认操作的过程中发生尽量少的失误,减少不必要的麻烦.先把我参考的博客放在这里,大家可以适当食用.
https://blog.csdn.net/zyazky/article/details/82716940
https://blog.csdn.net/ifisher999/article/details/84494315
https://www.jianshu.com/p/8d309dcc8b99
https://www.jianshu.com/p/0cad02ad323b
http://www.mamicode.com/info-detail-2310767.html
https://www.cnblogs.com/fanwendi2312/p/8438575.html
http://www.bubuko.com/infodetail-2616182.html
https://cloud.tencent.com/developer/article/1384531
https://blog.csdn.net/OliverLee456/article/details/83023566
由于每个人设备环境的差异性,不能完全照搬别人的操作.我综合参考了以上几篇博客,根据自己电脑的环境进行调整和修改,期间遇到bug就百度查资料.下面是我编译caffe的过程.
1. 创建虚拟环境
首先创建一个虚拟环境名为caffe35,我这里选择了python=3.5
conda create -n caffe35 python=3.5
接着进入到这个虚拟环境中
source activate caffe35
2. 安装必要的依赖库
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler libopenblas-dev
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libopenblas-dev
3. clone github源码到本地
我的安装目录为 home/lx/caffe
cd home/lx
git clone https://github.com/BVLC/caffe.git
4. 修改Makefile.config配置文件
进入到你的caffe目录下
cd caffe
拷贝一个安装配置文件,并打开它
cp Makefile.config.example Makefile.config
sudo gedit Makefile.config
以下是我修改后的Makefile.config文件,我在这个部分花费了大量的时间,因为编译错误大部分是由于这里的配置有问题.这里可以仔细看它给的英文注释,提示了每行代码的作用.此外,除了修改路径配置,我在编译过程中遇到的一些问题后额外增加了几行.每个人遇到的问题可能会不一样,我在编译的过程中也是出现一个问题解决一个问题.
这里的修改可以参考我上面给的几篇博客.值得注意的是,我是在anaconda的虚拟环境caffe35中配置的caffe,这里的第74行应该改为:ANACONDA_HOME := $(HOME)/lx/software/anaconda3/envs/caffe35
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# 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_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
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
# BLAS := atlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas
# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
# PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
ANACONDA_HOME := $(HOME)/lx/software/anaconda3/envs/caffe35
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.5m \
$(ANACONDA_HOME)/lib/python3.5/site-packages/numpy/core/include \
/home/lx/software/anaconda3/envs/caffe35/include/python3.5m
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib
# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
# Whatever else you find you need goes here.
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
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib
# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1
# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see full commands)
Q ?= @
LINKFLAGS := -Wl,-rpath,$(HOME)/software/anaconda3/lib
5. 修改Makefile文件
还是在caffe这个文件夹里
sudo gedit Makefile
用ctrl+f查询并修改
# Debugging
ifeq ($(DEBUG), 1)
COMMON_FLAGS += -DDEBUG -g -O0
NVCCFLAGS += -G
else
COMMON_FLAGS += -DNDEBUG -O2
NVCCFLAGS += -G
endif
将:
NVCCFLAGS +=-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)
替换为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
将:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial opencv_core opencv_imgproc opencv_imgcodecs opencv_highgui
修改 /usr/local/cuda-9.0/include/crt/host_config.h 文件 :
将
#error -- unsupported GNU version! gcc versions later than 6 are not supported!
改为
//#error -- unsupported GNU version! gcc versions later than 6 are not supported!
6. 编译
编译失败了,重新编译时,记得sudo make clean
开始编译:
sudo make clean
sudo make all -j12
此时可能会报一大堆错误,只有慢慢排错.我在这说一下我遇到的问题.
错误:
/usr/bin/ld: warning: libjpeg.so.9, needed by /usr/local/lib/libopencv_imgcodecs.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: warning: libpng16.so.16, needed by /usr/local/lib/libopencv_imgcodecs.so, not found (try using -rpath or -rpath-link)
解决办法:
cd /usr/lib/x86_64-linux-gnu
sudo ln -s ~/anaconda2/lib/libpng16.so.16 /usr/lib/
sudo ln -s ~/anaconda2/lib/libjpeg.so.9 /usr/lib/
sudo ldconfig # 运行完记得回到caffe文件夹下
错误:
/sbin/ldconfig.real: /usr/local/cuda-9.0/targets/x86_64-linux/lib/libcudnn.so.7 不是符号连接
解决办法:
sudo ln -sf /usr/local/cuda-9.0/lib64/libcudnn.so.7.0.5 /usr/local/cuda-9.0/lib64/libcudnn.so.7
错误:
//usr/lib/libpng16.so.16:对‘inflateValidate@ZLIB_1.2.9’未定义的引用
解决办法:
在Makefile.config中加入下列信息:
LINKFLAGS := -Wl,-rpath,{path to your anaconda}/anaconda3/lib
我这里是:
LINKFLAGS := -Wl,-rpath,$(HOME)/software/anaconda3/lib
排除全部错误后编译成功!
接着
sudo make test -j12
成功后输入
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/lx/software/anaconda3/lib:/home/lx/software/anaconda3/pkgs/python-3.6.1-2/lib
接着运行测试:
sudo make runtest -j12
运行时间大概有5分钟,运行成功
继续进入caffe下python目录,修改requirements.txt里为python-dateutil>=2.0:
sudo gedit requirements.txt
修改后运行
for req in $(cat requirements.txt); do pip install $req; done
最后对pycaffe配置:
cd ..
sudo make pycaffe
7. 测试
更改环境变量
sudo gedit /etc/profile
加入:export PYTHONPATH={path to your caffe}/caffe/python/:$PYTHONPATH
本文中是export PYTHONPATH=home/lx/caffe/python/:$PYTHONPATH
source一下使之生效
source /etc/profile
结束后测试
python
import caffe
没有报错就成功配置好了caffe!