背景是这样的,项目需要,必须将训练的模型通过C++进行调用,所以必须使用caffe或者mxnet,而caffe是用C++实现,所以有时候简单的加载一张图片然后再进行预测十分不方便
用caffe写prototxt比较容易,写solver也是很容易,但是如何根据传入的lmdb数据来predict每一个样本的类别,抑或如何得到样本预测为其他类的概率?这看起来是一个简单的问题,实际上,在pytorch中很容易实现,在caffe中可能需要修改c++代码,用起来不是很方便直观,所以能否通过python调用已经训练完的caffemodel以及deploy.prototxt来实现类别的预测?
这个时候需要在ubuntu上配置caffe,在ubuntu上配置caffe我主要参考了这篇博客,http://www.cnblogs.com/denny402/p/5088399.html
其实主要是有两部分,第一部分是修改Make.config文件,第二部分是解决so库找不到的问题
1.修改Makefile.config
关键点在于修改配置文件Make.config然后进行编译,我的Make.config文件如下,
## 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 # 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 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_50,code=compute_50 # BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# 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/R2014a
# 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)/anaconda
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 # 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
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib # 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 # 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 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 ?= @
主要是注意PYTHON_INCLUDE这一块怎么写,因为我系统中安装了anaconda2,所以我修改PYTHON_INCLUDE这一块为anaconda的路径
修改完成之后,进入caffe根目录,运行
sudo make pycaffe
,编译成功后,如果重复编译则会提示Nothing to be done for "pycaffe"
为了防止其他错误,还是编译一下test
sudo make test -j8
sudo make runtest -j8
2,解决so库找不到的问题
在编译的时候我倒是没有遇到什么问题,但是在进入到python环境中去的时候,我import caffe的时候倒是遇到了各种各样的问题,但是这种问题大致可以归结为一种类型,就是
error while loading shared libraries: libhdf5.so.10: cannot open shared object file: No such file or directory
就是找不到caffe想要的库文件,这个时候这个链接 (http://www.cnblogs.com/denny402/p/5088399.html给了一种解决的方法,原因大概是缺少动态链接库,这些库基本上我们之前都已经安装了,安装的路径是
/use/lib/x86_64-linux-gnu,ll libhdf*的话能够列出所有的libhdf相关的库文件,如下图
如上图所示,基本上系统里面有很多我们自己的库,只不过caffe依赖的版本与系统中的版本号不一致,这一点儿与caffe在包含cudnn库文件的时候类似,只不过caffe的cudnn貌似是在/usr/local/lib下
对已有的库创建软链接,能够解决找不到so库的问题,所以
cd /usr/lib/x86_64-linux-gnu
sudo ln -s libhdf5.so.(我文件夹中的so库的版本号) libhdf5.so.(caffe需要的版本号)
sudo ldconfig
可能还会遇到其他的有关羽so库找不到的问题,基本上都是按照这个套路来解决
然后import caffe就不会报错,保险起见,可以再编译运行一下test