Caffe学习系列(13):数据可视化环境(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
先用链接下载安装包 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。执行的代码都是一样的,只是在不同的目录下执行。
二、安装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版本。
下载成功后,在终端执行(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键查看。
Caffe学习系列(13):数据可视化环境(python接口)配置的更多相关文章
- caffe(13) 数据可视化(python接口)配置
caffe程序是由c++语言写的,本身是不带数据可视化功能的.只能借助其它的库或接口,如opencv, python或matlab.大部分人使用python接口来进行可视化,因为python出了个比较 ...
- Caffe学习系列(13):对训练好的模型进行fine-tune
使用http://www.cnblogs.com/573177885qq/p/5804863.html中的图片进行训练和测试. 整个流程差不多,fine-tune命令: ./build/tools/c ...
- Caffe 学习系列
学习列表: Google protocol buffer在windows下的编译 caffe windows 学习第一步:编译和安装(vs2012+win 64) caffe windows学习:第一 ...
- Caffe学习系列(23):如何将别人训练好的model用到自己的数据上
caffe团队用imagenet图片进行训练,迭代30多万次,训练出来一个model.这个model将图片分为1000类,应该是目前为止最好的图片分类model了. 假设我现在有一些自己的图片想进行分 ...
- Caffe学习系列——工具篇:神经网络模型结构可视化
Caffe学习系列——工具篇:神经网络模型结构可视化 在Caffe中,目前有两种可视化prototxt格式网络结构的方法: 使用Netscope在线可视化 使用Caffe提供的draw_net.py ...
- 大数据学习系列之四 ----- Hadoop+Hive环境搭建图文详解(单机)
引言 在大数据学习系列之一 ----- Hadoop环境搭建(单机) 成功的搭建了Hadoop的环境,在大数据学习系列之二 ----- HBase环境搭建(单机)成功搭建了HBase的环境以及相关使用 ...
- Caffe学习系列(21):caffe图形化操作工具digits的安装与运行
经过前面一系列的学习,我们基本上学会了如何在linux下运行caffe程序,也学会了如何用python接口进行数据及参数的可视化. 如果还没有学会的,请自行细细阅读: caffe学习系列:http:/ ...
- Caffe学习系列(22):caffe图形化操作工具digits运行实例
上接:Caffe学习系列(21):caffe图形化操作工具digits的安装与运行 经过前面的操作,我们就把数据准备好了. 一.训练一个model 右击右边Models模块的” Images" ...
- Caffe学习系列(3):视觉层(Vision Layers)及参数
所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe学习系列(2):数据层及参数 本文只讲解视觉层(Vision La ...
随机推荐
- 获取byte数组的实际使用长度
背景:byte.length只能获取到初始化的byte数组长度,而不是实际使用的长度,因此想要获取到实际的使用长度只能靠其他方法实现. 方法一: public class ByteActualLeng ...
- Go对OO的选择
Go摒弃了许多OO的概念,但是还是很好的继承了OO的精髓——消息传递.我猜这个是学了Smalltalk的.通常我们说OO,我们会说这三大特性:对象,继承,多态. 1,Go中的对象 对于GO来说他的类型 ...
- CentOS 6.4安装配置LNMP服务器(Nginx+PHP+MySQL)
准备篇 1.配置防火墙,开启80端口.3306端口 vi /etc/sysconfig/iptables-A INPUT -m state --state NEW -m tcp -p tcp --dp ...
- 通过正则获取URL中的参数
闲着无聊用正则做了一个获取URL参数的小算法^_^ function getParam(name) { var objs = window.location.search.match("(\ ...
- dom4j操作xml
dom4j是一个Java的XML API,类似于jdom,用来读写XML文件.是一个非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点,同时它也是一个开放源工具.可以在这个 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- linux系统下设置oracle开机自动启动
在Linux系统中,安装好oracle数据库服务后,并不像在Windows系统下一样,oracle服务在默认情况下会随时系统的启动自动启动.Linux系统中,是需要用户去手动进行设置,才能实现orac ...
- C++ 基本知识
无论父类与子类的析构函数是否是virutal,子类的析构函数都会调用父类的析构函数 调用构造函数是与构造函数顺序相反,先子类后基类,否则如果基类先析构,子类的有些资源已经不存在了,会出错. 在C++中 ...
- Hadoop Shell命令字典(可收藏)
可以带着下面问题来阅读: 1.chmod与chown的区别是什麽?2.cat将路径指定文件的内容输出到哪里?3.cp能否是不同之间复制?4.hdfs如何查看文件大小?5.hdfs如何合并文件?6.如何 ...
- A + B Problem II
之前总是在查阅别人的文档,看着其他人的博客,自己心里总有一份冲动,想记录一下自己学习的经历.学习算法有一段时间了,于是想从算法开始自己的博客生涯O(∩_∩)O~~ 今天在网上看了一道大数相加(高精度) ...