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鍵查看。

相關文章
相關標籤/搜索