背景是这样的,项目需要,必须将训练的模型通过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. Script to Collect Log File Sync Diagnostic Information (lfsdiag.sql) (文档 ID 1064487.1)

    the article from :http://m.blog.itpub.net/31393455/viewspace-2130875/ Script to Collect Log File Syn ...

  2. 在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true

    Boolean 表达式 一个值为 true 或者 false 的表达式.如果需要,非 Boolean 表达式也可以被转换为 Boolean 值,但是要遵循下列规则: 所有的对象都被当作 true. 当 ...

  3. gradle set

    gradle安装   1◆ gradle下载 http://services.gradle.org/distributions/       2◆ 配置环境 =====>D:\envs\grad ...

  4. 【Query】使用java对mysql数据库进行查询操作

    操作步骤: 1.加载数据库驱动(先在工程里加载数据库对应的驱动包) 2.获取连接 3.根据连接建立一个可执行sql的对象 4.执行sql语句 5.关闭连接 代码: package database; ...

  5. 登录验证码实现(Captcha)

    登录验证码 登录验证是一般系统都会有的功能,验证的方式也多种多样,比如输入式验证码,拖动式验证条,拖动式验证拼图等等. 我们这里先实现常规的输入验证码的方式,右边显示验证码图片,点击可刷新,左边输入验 ...

  6. UVa LA 3029 City Game 状态拆分,最大子矩阵O(n2) 难度:2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  7. [HDU3726]Graph and Queries

    Problem 给你一张图,点的权值,边和几个操作: D x: 删除第x条边 Q x y: 询问包含x的联通块中权值第y大的权值 C x y: 将x这个点的权值改为y Solution 一看就要离线处 ...

  8. IDEA使用GitHub托管代码

    该方法基本也适用于JetBrains公司的其他IDE产品,如phpStorm,PyCharm等. 首先,在github官网注册一个账号,参考:http://stormzhang.com/github/ ...

  9. Linux学习 : Socket 网络编程入门

    一.socket()函数 int socket(int domain, int type, int protocol); domain:即协议域,又称为协议族(family).常用的协议族有,AF_I ...

  10. 5.4 C++重载输入与输出操作符

    参考:http://www.weixueyuan.net/view/6382.html 总结: 在C++中,系统已经对左移操作符“<<”和右移操作符“>>”分别进行了重载,使其 ...