Caffe学习系列(13):数据可视化环境(python接口)配置
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键查看。
这里面好像已经编译过了pycaffe,是不是意味着已经安装了python接口配置?是不是就缺最后一步——安装jupyter呢? 期待您的解惑!谢谢!
尝试了
No module named google.protobuf.internal 的问题解决了,先用root权限,chmod 777 -R 解除anaconda2的文件夹保护,然后anaconda install protobuf ,就没有报错了。
但是显示
usage: anaconda [-h] [--show-traceback] [--hide-traceback] [-v] [-q] [--color]
[--no-color] [-V] [-t TOKEN] [-s SITE]
...
anaconda: error: argument : invalid choice: 'install' (choose from 'auth', u'label', u'channel', 'config', u'copy', u'download', 'groups', u'login', 'logout', u'notebook', 'package', 'remove', 'search', 'show', u'upload', u'whoami')
也尝试了您说的
sudo apt-get install libprotobuf-dev
显示为libprotobuf-dev is already the newest version.
求助啊 /(ㄒoㄒ)/~~
博主原文“输入conda list 就可以查询,你现在安装了哪些库,常用的numpy, scipy名列其中。如果你还有什么包没有安装上,可以运行
conda install *** 来进行安装”
对于这个问题:
cd anaconda2
conda install -c anaconda protobuf=2.6.1
就可以了。
附 https://anaconda.org
/opt/pb/binutils-2.24/bin/ld: skipping incompatible /usr/lib/libcudart.so when searching for -lcudart
/opt/pb/binutils-2.24/bin/ld: cannot find -lpython2.7
/opt/pb/binutils-2.24/bin/ld: cannot find -lcblas
/opt/pb/binutils-2.24/bin/ld: cannot find -latlas
/opt/pb/binutils-2.24/bin/ld: cannot find -lpython2.7
我后来安装了python3.5的版本,连接什么的我也尽量改了 但还是报这个错 求指教啊
安装好Anaconda后,在编译runtest的时候,会报这样的错误:
.build_release/test/test_all.testbin: error while loading shared libraries: libglog.so.0: cannot open shared object file: No such file or directory
但是我的电脑中没有/usr/lib/x86_64-linux-gnu文件夹,怎么办?我装的glog0.3.3版本解压后有libglog.so.0以及libglog.so.0.0.0文件。
ERROR:bus.cc(427)] Failed to connect to the bus: Failed to connect to socket /home/tuxiang/anaconda2/var/run/dbus/system_bus_socket: 没有那个文件或目录
但是 /var/run/dbus/下有对应的socket文件。。搜了一下不知如何解决。私以为anaconda下的dbus和系统的dbus冲突。不过不能卸载anaconda下的dbus,因为会顺带卸载很多其他东西包括matplotlib等
求教该如何解决
弄完之后报这个错误,allow-root是什么啊?大神。
[email protected]:/home/ubuntu# jupyter notebook
[I 14:36:05.718 NotebookApp] Writing notebook server cookie secret to /root/.local/share/jupyter/runtime/notebook_cookie_secret
[C 14:36:05.730 NotebookApp] Running as root is not recommended. Use --allow-root to bypass.
我在编译runtest的时候,也遇到了这个问题,不过用你方法后还是不行。另外,import caffe 时 报错:No module named google.protobuf.internal
我先尝试自己解决,如果解决不了,还是得麻烦你帮我看看哪里不对?