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

    css: <style type="text/css"> * { margin: 0; padding: 0; list-style: none; } .clearfi ...

  2. flex入门----基础知识

    传统的页面布局   在flex出现之前,双列布局,三列布局,动态盒居中,绝对居中布局等常见的布局均是采用dispaly+float+定位来布局的,一般包括以下几种布局策略: normal flow(文 ...

  3. memory prefix pre,pro,penta,pseudo out _p 1

    1● pre 前的,预先   2● pro 在前,很多,赞同       3● penta 5,五     4● pseaudo   伪,假 pseudo  

  4. window.open打开新窗口 参数

    1,基本描述 oNewWindow = window.open( sURL , sName , sFeatures, bReplace) window.open在打开一个窗口(其url为sURL)后, ...

  5. PAT-GPLT训练集 L2-001 紧急救援(最短路)

    PAT-GPLT训练集 L2-001 紧急救援 题目大意:求最短路的条数,最短路中的权重和的最大值和这条最短路的路线 分析:使用dijkstra算法求出最短路,并且对dijkstra算法进行变化,设起 ...

  6. sqlalchemy 模型中添加根据身份证号计算性别和年龄

    class Users(Base, BaseMixin): username = Column(String(24)) id_Num = Column(String(18) ) # 身份证号码 @pr ...

  7. mySQL 分组查询,根据分组的字段,取最小值

    今天看到别人问的问题,给别人写了一条sql! CREATE TEMPORARY TABLE tmp_table ( id INTEGER not NULL, uname VARCHAR(10) NOT ...

  8. Cracking The Coding Interview 1.4

    //Write a method to decide if two strings are anagrams or not. // // 变位词(anagrams)指的是组成两个单词的字符相同,但位置 ...

  9. ubuntu GUI程序开机自启设置

    在 主目录下,即 $HOME路径下新建 或编辑 .gnomerc 文件,将开机启动脚本写在这里如下:gedit ~/.gnomerc#!/bin/bashcd /home/xxxx/openUI./o ...

  10. static全局变量与普通全局变量的区别,static局部变量与普通局部变量的区别,static函数与普通函数的区别

    static全局变量与普通全局变量的区别 static全局变量和普通全局变量的存储方式没有不同,如果它们都已经初始化,那么存放在进程的数据段中,否则存放在bss段.这两者的不同点在于作用域不同,当一个 ...