0. 本文目的
历经多次安装caffe后,总结一下安装的经验。总结一下使用anaconda下的python来编译caffe的坑。
1. 在anaconda下创建新环境
说明:假设anaconda安装在${HOME}下,即/home/XXX/下。anaconda2或者anaconda3并无影响。建议使用python2来编译,因此在anaconda2/3下创建环境py2Caffe。安装caffe所需要的包.(对于protobuf采用2.5.0或者2.6.1版本,对于protobuf2.5.0采用pip install protobuf==2.5.0安装, protobuf2.6.1采用conda install protobuf==2.6.1). 对于opencv3 采用conda install -c menpo opencv3来安装(opencv2 安装为: conda install -c menpo opencv).
命令如下:
conda create --name py2Caffe python=2.7
2. 修改caffe的Makefile.config文件
1) 下载caffe
命令如下:
git clone https://github.com/BVLC/caffe.git
cp Makefile.config.example Makefile.config
2) 修改Makefile.config文件
修改的部分在下面(GPU + opencv3)
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 3
# 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)/anaconda2/envs/py2Caffe
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# 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
# 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
#INCLUDE_DIRS := /usr/local/opencv2.4.13/include /usr/include/hdf5/serial
#LIBRARY_DIRS := /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 ?= @
3 Make Caffe
1) 出现needed by /home/XXX/anaconda2/envs/py2Caffe/lib/libopencv_imgcodecs.so, not found (try using -rpath or -rpath-link)
/home/XXX/anaconda2/envs/py2Caffe/lib/libopencv_imgcodecs.so:对‘jpeg_set_quality@LIBJPEG_9.0’未定义的引用问题
将/home/XXX/anaconda2/envs/py2Caffe/lib下的对应文件链接到/usr/lib下
sudo ln -s ~/anaconda2/envs/py2Caffe/lib/libpng16.so.16 /usr/lib
sudo ln -s ~/anaconda2/envs/py2Caffe/lib/libjpeg.so.9 /usr/lib
2) 可能出现../build/tools/get_image_size: error while loading shared libraries: libhdf5.so.101: cannot open shared object file: No such file or directory 问题
此时,需要将/home/XXX/anaconda2/envs/py2Caffe/lib/下的文件:
libhdf5_hl.so.100
libhdf5.so.101
拷贝到/usr/local/lib文件下.
3) 出现问题: build/tools/get_image_size: error while loading shared libraries: libhdf5_hl.so.10: cannot open shared object file: No such file or directory
将/home/XXX/anaconda2/envs/py2Caffe/lib下的文件
libhdf5_hl.so.10
拷贝至/usr/local/llib文件下
4) 出现问题: build/tools/get_image_size: error while loading shared libraries: libopencv*: cannot open shared object file: No such file or directory
将/home/XXX/anaconda2/envs/py2Caffe/lib下的文件的对应的文件拷贝至/usr/local/lib下
5) 出现问题 Check failed: status == CUDNN_STATUS_SUCCESS (4 vs. 0) CUDNN_STATUS_INTERNA
可能是由于CUDNN不匹配导致的,因此将Makefile.config文件中的 USE_CUDNN := 1 注释即可.
There may be some mistakes in this blog. So, any suggestions and comments are welcome!
说明:如果转载,请著名出处.