Caffe數(shù)據(jù)可視化環(huán)境python接口配置教程示例
引言
caffe程序是由c++語言寫的,本身是不帶數(shù)據(jù)可視化功能的。只能借助其它的庫(kù)或接口,如opencv, python或matlab。大部分人使用python接口來進(jìn)行可視化,因?yàn)閜ython出了個(gè)比較強(qiáng)大的東西:ipython notebook, 現(xiàn)在的最新版本改名叫jupyter notebook,它能將python代碼搬到瀏覽器上去執(zhí)行,以富文本方式顯示,使得整個(gè)工作可以以筆記的形式展現(xiàn)、存儲(chǔ),對(duì)于交互編程、學(xué)習(xí)非常方便。
python環(huán)境不能單獨(dú)配置,必須要先編譯好caffe,才能編譯python環(huán)境。
python環(huán)境的配置說起來簡(jiǎn)單,做起來非常復(fù)雜。在安裝的過程中,可能總是出現(xiàn)這樣那樣的問題。因此強(qiáng)烈建議大家用anaconda來進(jìn)行安裝,anaconda把很多與python有關(guān)的庫(kù)都收集在一起了,包括numpy,scipy等等,因此,我們只需要下載對(duì)應(yīng)系統(tǒng),對(duì)應(yīng)版本的anaconda來安裝就可以了。
如果你想通過anaconda來安裝,請(qǐng)?zhí)^第一、二步,直接進(jìn)入第三步開始:
一、安裝python和pip
一般linux系統(tǒng)都自帶python,所以不需要安裝。如果沒有的,安裝起來也非常方便。安裝完成后,可用version查看版本
# python --version
pip是專門用于安裝python各種依賴庫(kù)的,所以我們這里安裝一下pip1.5.6
先點(diǎn)擊下載安裝包,然后解壓,里面有一個(gè)setup.py的文件,執(zhí)行這個(gè)文件就可以安裝pip了
# sudo python setup.py install
有些電腦可能會(huì)提示 no moudle name setuptools 的錯(cuò)誤,這是沒有安裝setuptools的原因。那就需要先安裝一下setuptools, 然后解壓執(zhí)行
# sudo python setup.py install
就要以安裝setuptools了,然后再回頭去重新安裝pip。執(zhí)行的代碼都是一樣的,只是在不同的目錄下執(zhí)行。
二、安裝pyhon接口依賴庫(kù)
在caffe根目錄的python文件夾下,有一個(gè)requirements.txt的清單文件,上面列出了需要的依賴庫(kù),按照這個(gè)清單安裝就可以了。
在安裝scipy庫(kù)的時(shí)候,需要fortran編譯器(gfortran),如果沒有這個(gè)編譯器就會(huì)報(bào)錯(cuò),因此,我們可以先安裝一下。
首先回到caffe的根目錄,然后執(zhí)行安裝代碼:
# cd ~/caffe # sudo apt-get install gfortran # for req in $(cat requirements.txt); do sudo pip install $req; done
安裝完成以后,我們可以執(zhí)行:
# sudo pip install -r python/requirements.txt
就會(huì)看到,安裝成功的,都會(huì)顯示Requirement already satisfied, 沒有安裝成功的,會(huì)繼續(xù)安裝。
在安裝的時(shí)候,也許問題會(huì)有一大堆。這時(shí)候你就知道anaconda的好處了。
三、利用anaconda來配置python環(huán)境
如果你上面兩步已經(jīng)沒有問題了,那么這一步可以省略。
如果你想簡(jiǎn)單一些,利用anaconda來配置python環(huán)境,那么直接從這一步開始,可以省略上面兩步。
先到https://www.anaconda.com/products/distribution 下載anaconda, 現(xiàn)在的版本有python2.7版本和python3.5版本,下載好對(duì)應(yīng)版本、對(duì)應(yīng)系統(tǒng)的anaconda,它實(shí)際上是一個(gè)sh腳本文件,大約280M左右。我下載的是linux版的python 2.7版本。
下載成功后,在終端執(zhí)行(2.7版本):
# bash Anaconda2-2.4.1-Linux-x86_64.sh
或者3.5 版本:
# bash Anaconda3-2.4.1-Linux-x86_64.sh
在安裝的過程中,會(huì)問你安裝路徑,直接回車默認(rèn)就可以了。有個(gè)地方問你是否將anaconda安裝路徑加入到環(huán)境變量(.bashrc)中,這個(gè)一定要輸入yes
安裝成功后,會(huì)有當(dāng)前用戶根目錄下生成一個(gè)anaconda2的文件夾,里面就是安裝好的內(nèi)容。
輸入conda list 就可以查詢,你現(xiàn)在安裝了哪些庫(kù),常用的numpy, scipy名列其中。如果你還有什么包沒有安裝上,可以運(yùn)行
conda install *** 來進(jìn)行安裝,
如果某個(gè)包版本不是最新的,運(yùn)行 conda update *** 就可以了。
四、編譯python接口
首先,將caffe根目錄下的python文件夾加入到環(huán)境變量
打開配置文件bashrc
# sudo vi ~/.bashrc
在最后面加入
export PYTHONPATH=/home/xxx/caffe/python:$PYTHONPATH
注意 /home/xxx/caffe/python 是我的路徑,這個(gè)地方每個(gè)人都不同,需要修改
保存退出,更新配置文件
# 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 ?= @
修改完編譯配置文件后,最后進(jìn)行編譯:
# sudo make pycaffe
編譯成功后,不能重復(fù)編譯,否則會(huì)提示 Nothing to be done for "pycaffe"的錯(cuò)誤。
防止其它意外的錯(cuò)誤,最好還編譯一下:
# sudo make test -j8 # sudo make runtest -j8
也許你在編譯runtest的時(shí)候,會(huì)報(bào)這樣的錯(cuò)誤:
.build_release/test/test_all.testbin: error while loading shared libraries: libhdf5.so.10: cannot open shared object file: No such file or directory
這是因?yàn)?nbsp;libhdf5.so的版本問題,你可以進(jìn)入/usr/lib/x86_64-linux-gnu看一下,你的libhdf5.so.x中的那個(gè)x是多少,比如我的是libhdf5.so.7
因此可以執(zhí)行下面幾行代碼解決:
# 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接口是否編譯成功:
進(jìn)入python環(huán)境,進(jìn)行import操作
# python >>> import caffe
如果沒有提示錯(cuò)誤,則編譯成功。
五、安裝jupyter
安裝了python還不行,還得安裝一下ipython,后者更加方便快捷,更有自動(dòng)補(bǔ)全功能。而ipython notebook是ipython的最好展現(xiàn)方式。最新的版本改名為jupyter notebook,我們先來安裝一下。(如果安裝了anaconda, jupyter notebook就已經(jīng)自動(dòng)裝好,不需要再安裝)
# sudo pip install jupyter
安裝成功后,運(yùn)行notebook
# jupyter notebook
就會(huì)在瀏覽器中打開notebook, 點(diǎn)擊右上角的New-python2, 就可以新建一個(gè)網(wǎng)頁(yè)一樣的文件,擴(kuò)展名為ipynb。在這個(gè)網(wǎng)頁(yè)上,我們就可以像在命令行下面一樣運(yùn)行python代碼了。輸入代碼后,按shift+enter運(yùn)行,更多的快捷鍵,可點(diǎn)擊上方的help-Keyboard shortcuts查看,或者先按esc退出編輯狀態(tài),再按h鍵查看。
以上就是Caffe數(shù)據(jù)可視化環(huán)境python接口配置教程示例的詳細(xì)內(nèi)容,更多關(guān)于Caffe環(huán)境python接口配置的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python mplfinance庫(kù)繪制金融圖表實(shí)現(xiàn)數(shù)據(jù)可視化實(shí)例探究
- Python報(bào)表自動(dòng)化之從數(shù)據(jù)到可視化一站式指南
- Python對(duì)中國(guó)500強(qiáng)排行榜數(shù)據(jù)進(jìn)行可視化分析實(shí)戰(zhàn)
- python數(shù)據(jù)可視化pygal模擬擲骰子實(shí)現(xiàn)示例
- python數(shù)據(jù)可視化繪制火山圖示例
- python數(shù)據(jù)可視化繪制世界人口地圖
- python數(shù)據(jù)可視化matplotlib繪制折線圖示例
- python?AutoViz庫(kù)一行代碼實(shí)現(xiàn)可視化數(shù)據(jù)集
相關(guān)文章
Python實(shí)現(xiàn)的刪除重復(fù)文件或圖片功能示例【去重】
這篇文章主要介紹了Python實(shí)現(xiàn)的刪除重復(fù)文件或圖片功能,結(jié)合實(shí)例形式分析了Python基于os與hashlib模塊針對(duì)文件的讀取、hash計(jì)算及重復(fù)性判定等相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Django項(xiàng)目打包完整步驟以及可能出現(xiàn)的問題
django項(xiàng)目的結(jié)構(gòu)大體上都是類似,打包主要的功能就是把一些不需要部署的文件剔除,把需要部署的文件直接壓縮打包,下面這篇文章主要給大家介紹了關(guān)于Django項(xiàng)目打包完整步驟以及可能出現(xiàn)問題的相關(guān)資料,需要的朋友可以參考下2023-06-06在Qt中正確的設(shè)置窗體的背景圖片的幾種方法總結(jié)
今天小編就為大家分享一篇在Qt中正確的設(shè)置窗體的背景圖片的幾種方法總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06pandas 實(shí)現(xiàn)某一列分組,其他列合并成list
這篇文章主要介紹了pandas 實(shí)現(xiàn)某一列分組,其他列合并成list的案例。具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-03-03PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè)
這篇文章主要為大家介紹了PyTorch搭建LSTM實(shí)現(xiàn)時(shí)間序列負(fù)荷預(yù)測(cè),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05Python實(shí)現(xiàn)將一段話txt生成字幕srt文件
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)將一段話txt生成字幕srt文件,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-02-02python中關(guān)于時(shí)間和日期函數(shù)的常用計(jì)算總結(jié)(time和datatime)
python中關(guān)于時(shí)間和日期函數(shù)有time和datatime使用介紹,需要的朋友可以參考下2013-03-03selenium3+python3環(huán)境搭建教程圖解
這篇文章主要介紹了selenium3+python3環(huán)境搭建教程圖解,需要的朋友可以參考下2018-12-12Python編寫通訊錄通過數(shù)據(jù)庫(kù)存儲(chǔ)實(shí)現(xiàn)模糊查詢功能
數(shù)據(jù)庫(kù)存儲(chǔ)通訊錄,要求按姓名/電話號(hào)碼查詢,查詢條件只有一個(gè)輸入入口,自動(dòng)識(shí)別輸入的是姓名還是號(hào)碼,允許模糊查詢。這篇文章主要介紹了Python編寫通訊錄,支持模糊查詢,利用數(shù)據(jù)庫(kù)存儲(chǔ),需要的朋友可以參考下2019-07-07