Caffe学习系列(13):数据可视化环境(python接口)配置

caffe程序是由c++语言写的,自己是不带数据可视化功能的。只能借助其它的库或接口,如opencv, python或matlab。大部分人使用python接口来进行可视化,由于python出了个比较强大的东西:ipython notebook, 如今的最新版本更名叫jupyter notebook,它能将python代码搬到浏览器上去执行,以富文本方式显示,使得整个工做能够以笔记的形式展示、存储,对于交互编程、学习很是方便。 html

   python环境不能单独配置,必需要先编译好caffe,才能编译python环境。python

    python环境的配置提及来简单,作起来很是复杂。在安装的过程当中,可能老是出现这样那样的问题。所以强烈建议你们用anaconda来进行安装,anaconda把不少与python有关的库都收集在一块儿了,包括numpy,scipy等等,所以,咱们只须要下载对应系统,对应版本的anaconda来安装就能够了。linux

若是你想经过anaconda来安装,请跳过第1、二步,直接进入第三步开始:c++

1、安装python和pipgit

通常linux系统都自带python,因此不须要安装。若是没有的,安装起来也很是方便。安装完成后,可用version查看版本github

# python --version

pip是专门用于安装python各类依赖库的,因此咱们这里安装一下pip1.5.6shell

先用连接下载安装包 https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz,而后解压,里面有一个setup.py的文件,执行这个文件就能够安装pip了编程

# sudo python setup.py install

有些电脑可能会提示 no moudle name setuptools 的错误,这是没有安装setuptools的缘由。那就须要先安装一下setuptools, 到https://pypi.python.org/packages/source/s/setuptools/setuptools-19.2.tar.gz 下载安装包setuptools-19.2.tar.gz,而后解压执行浏览器

# sudo python setup.py install

就要以安装setuptools了,而后再回头去从新安装pip。执行的代码都是同样的,只是在不一样的目录下执行。bash

2、安装pyhon接口依赖库

在caffe根目录的python文件夹下,有一个requirements.txt的清单文件,上面列出了须要的依赖库,按照这个清单安装就能够了。

在安装scipy库的时候,须要fortran编译器(gfortran),若是没有这个编译器就会报错,所以,咱们能够先安装一下。

首先回到caffe的根目录,而后执行安装代码:

# cd ~/caffe
# sudo apt-get install gfortran
# for req in $(cat requirements.txt); do sudo pip install $req; done

安装完成之后,咱们能够执行:

# sudo pip install -r python/requirements.txt

就会看到,安装成功的,都会显示Requirement already satisfied, 没有安装成功的,会继续安装。

在安装的时候,也许问题会有一大堆。这时候你就知道anaconda的好处了。

3、利用anaconda来配置python环境

若是你上面两步已经没有问题了,那么这一步能够省略。

若是你想简单一些,利用anaconda来配置python环境,那么直接从这一步开始,能够省略上面两步。

先到https://www.continuum.io/downloads 下载anaconda, 如今的版本有python2.7版本和python3.5版本,下载好对应版本、对应系统的anaconda,它其实是一个sh脚本文件,大约280M左右。我下载的是linux版的python 2.7版本。

下载成功后,在终端执行(2.7版本):

# bash Anaconda2-2.4.1-Linux-x86_64.sh

或者3.5 版本:

# bash Anaconda3-2.4.1-Linux-x86_64.sh

在安装的过程当中,会问你安装路径,直接回车默认就能够了。有个地方问你是否将anaconda安装路径加入到环境变量(.bashrc)中,这个必定要输入yes

安装成功后,会有当前用户根目录下生成一个anaconda2的文件夹,里面就是安装好的内容。

输入conda list 就能够查询,你如今安装了哪些库,经常使用的numpy, scipy名列其中。若是你还有什么包没有安装上,能够运行

conda install ***  来进行安装,

若是某个包版本不是最新的,运行 conda update *** 就能够了。

4、编译python接口

首先,将caffe根目录下的python文件夹加入到环境变量

打开配置文件bashrc

# sudo vi ~/.bashrc

在最后面加入

export PYTHONPATH=/home/xxx/caffe/python:$PYTHONPATH

注意 /home/xxx/caffe/python 是个人路径,这个地方每一个人都不一样,须要修改

保存退出,更新配置文件

# sudo ldconfig

而后修改编译配置文件Makefile.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_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)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        $(ANACONDA_HOME)/include/python2.7 \
        $(ANACONDA_HOME)/lib/python2.7/site-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 ?= @

修改完编译配置文件后,最后进行编译:

# sudo make pycaffe

编译成功后,不能重复编译,不然会提示 Nothing to be done for "pycaffe"的错误。

防止其它意外的错误,最好还编译一下:

# sudo make test -j8
# sudo make runtest -j8

也许你在编译runtest的时候,会报这样的错误:

.build_release/test/test_all.testbin: error while loading shared libraries: libhdf5.so.10: cannot open shared object file: No such file or directory

这是由于 libhdf5.so的版本问题,你能够进入/usr/lib/x86_64-linux-gnu看一下,你的libhdf5.so.x中的那个x是多少,好比个人是libhdf5.so.7

 所以能够执行下面几行代码解决:

# cd /usr/lib/x86_64-linux-gnu
# sudo ln -s libhdf5.so.7 libhdf5.so.10
# sudo ln -s libhdf5_hl.so.7 libhdf5_hl.so.10
# sudo ldconfig

最终查看python接口是否编译成功:

进入python环境,进行import操做

# python
>>> import caffe

若是没有提示错误,则编译成功。

5、安装jupyter

安装了python还不行,还得安装一下ipython,后者更加方便快捷,更有自动补全功能。而ipython notebook是ipython的最好展示方式。最新的版本更名为jupyter notebook,咱们先来安装一下。(若是安装了anaconda, jupyter notebook就已经自动装好,不须要再安装)

# sudo pip install jupyter

安装成功后,运行notebook

# jupyter notebook

就会在浏览器中打开notebook,  点击右上角的New-python2, 就能够新建一个网页同样的文件,扩展名为ipynb。在这个网页上,咱们就能够像在命令行下面同样运行python代码了。输入代码后,按shift+enter运行,更多的快捷键,可点击上方的help-Keyboard shortcuts查看,或者先按esc退出编辑状态,再按h键查看。

相关文章
相关标签/搜索