choice python接口,数据可视化环境(Python接口)配置

数据可视化环境(Python接口)配置

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

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

python环境的配置说起来简单,做起来非常复杂。在安装的过程中,可能总是出现这样那样的问题。因此强烈建议大家用anaconda来进行安装,anaconda把很多与python有关的库都收集在一起了,包括numpy,scipy等等,因此,我们只需要下载对应系统,对应版本的anaconda来安装就可以了。

如果你想通过anaconda来安装,请跳过第一、二步,直接进入第三步开始:

一、安装python和pip

一般linux系统都自带python,所以不需要安装。如果没有的,安装起来也非常方便。安装完成后,可用version查看版本

#python --version

pip是专门用于安装python各种依赖库的,所以我们这里安装一下pip1.5.6

#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。执行的代码都是一样的,只是在不同的目录下执行。

二、安装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的好处了。

三、利用anaconda来配置python环境

如果你上面两步已经没有问题了,那么这一步可以省略。

如果你想简单一些,利用anaconda来配置python环境,那么直接从这一步开始,可以省略上面两步。

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

664bb6f826e9b5ae869a88ef750cc174.png

下载成功后,在终端执行(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 *** 就可以了。

四、编译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

如果没有提示错误,则编译成功。

五、安装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键查看。

186ccb8acb9f53a493b7cbb4ea9b9d29.png0b1331709591d260c1c78e86d0c51c18.png

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值