背景是这样的,项目需要,必须将训练的模型通过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

python调用caffe环境配置的更多相关文章

  1. python+selenium的环境配置

    以前写过关于python和selenium加myeclipse的环境配置,但是myeclipse启动时过于费时,虽然myeclipse有很好的提示功能,但是作为初学者,我还是直接用python的idl ...

  2. Python selenium chrome 环境配置

    Python selenium chrome 环境配置 一.参考文章: 1. 记录一下python easy_install和pip安装地址和方法 http://heipark.iteye.com/b ...

  3. VS2015调用Matlab2017a环境配置(转载)

    VS2015调用Matlab2017a环境配置 一定要在Debug+x64平台下进行配置,x64,x64,x64!!!* 1.配置环境变量 右键计算机—–>属性——>高级系统设置——> ...

  4. 自己的Qt GUI 项目+vs2013+opencv+caffe环境配置

    由于深度学习的种种优势,使我们对于深度学习的使用越来越频繁.很多时候,我们都需要在自己的项目中配置caffe环境,来调用caffe网络模型完成自己的任务.今天我主要讲的关于"在自己的项目中配 ...

  5. Python 爬虫2——环境配置

    关于环境配置的操作,其实非常简单,假如不使用第三方的框架的话,只需要安装Python即可完成后续的操作. 一.Python的安装和配置: windows系统的安装配置过程如下,假如是Mac系统,可参考 ...

  6. python调用caffe实现预测

    对于已经训练完成的caffemodel,对于单个的图片预测,用python接口来调用是一件非常方便的事情,下面就来讲述如何用python调用已经训练完成的caffemodel,以及prototxt,网 ...

  7. Python Flask 多环境配置

    Python里取配置文件的时候,之前是使用的ini文件和python里configparser 模块: 可参考:https://www.cnblogs.com/feeland/p/4514771.ht ...

  8. Python沙盒环境配置

    一.简介 本文介绍配置python沙盒环境的方法步骤. 二.安装步骤 1.安装pyenv http://www.cnblogs.com/274914765qq/p/4948530.html 2.安装v ...

  9. 代码编辑器[0] -> Vim/gVim[0] -> 基于 Python 的 gVim 环境配置(Windows)

     环境配置 / Environment Setup 基于Python开发的 gVim 环境配置(Windows) 使用方式参考 Vim 的使用. 1 基于vundle进行配置 Vim有多个扩展管理器, ...

随机推荐

  1. 一、I/O操作(流的概念)

    一.流(Stream) 所谓流(Stream),就是一系列的数据. 当不同的介质之间有数据交互的时候,java就会使用流来实现. 数据源可以使文件,还可以是数据库,网络,甚至是其他的程序 不如读取文件 ...

  2. 记一次搭建vsftp服务器坑

    避免踩坑,特此记录... yum -y install vsftpd useradd -d /www -s /sbin/nologin sui # 修改vsftpd配置文件/etc/vsftpd/vs ...

  3. Hexo+Github 搭建属于自己的博客(Mac下安装 其他操作系统大同小异)

    安装前提 参考博客:http://blog.csdn.net/gdutxiaoxu/article/details/53576018#t5(写的很好,不用看我的了.....) 这篇:http://ww ...

  4. volatile原理解析

    Java并发编程:volatile关键字解析 volatile 有序性.可见性 volatile可以保证一定程度上有序性,即volatile前面的代码先于后面的代码先执行. 但是前.后代码,各自里面的 ...

  5. QT和JS的互相调用例子

    转自: http://blog.163.com/qimo601@126/blog/static/15822093201682185819623/ Qt 4.8.4 感谢原作者,我只转载. 看看作者如何 ...

  6. Talend 数据转换

    2个系统都有客户信息,产品信息. 要从一个系统a导出数据给另一个系统b用. 有2个方法. 1.在a系统的客户表,产品表加一个字段,记录b系统对应的ID,导出时直接用sql转换了. 2. 用ETL工具转 ...

  7. Mockito:一个强大的用于Java开发的模拟测试框架

    https://blog.csdn.net/zhoudaxia/article/details/33056093 介绍 本文将介绍模拟测试框架Mockito的一些基础概念, 介绍该框架的优点,讲解应用 ...

  8. HTML(四)Form标签

    <form>…</form>    定义供用户输入的 HTML 表单 例子 <html> <body> <form method="ge ...

  9. dede织梦调取一二三级栏目名及栏目下的内容列表的方法

    网站根据需要,把地区划成省-市-文章的层级结构,栏目首页需要显示的是 复制代码代码如下: {dede:channelarclist} <!--省显示--> <a href=" ...

  10. μC/OS-II在Microblaze上的移植与使用专题--“安富利杯”赛灵思FPGA设计技巧与应用创新博文大赛参赛作品

    reference:http://xilinx.eetrend.com/d6-xilinx/blog/2010-05/682.html   随着集成电路设计与制造技术的发展,FPGA芯片的容量越来越大 ...