Caffe學習系列:資料視覺化環境(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
# 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鍵檢視。