Linux(Ubuntu14.04)下安裝Anaconda和Spyder

Linux(Ubuntu14.04)下安裝Anaconda是爲了安裝Python所需要的各種庫以及他們的環境配置。

Spyder是使用python的IDE

安裝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
利用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
 
 

在安裝的過程中,會問你安裝路徑,直接回車默認就可以了。有個地方問你是否將anaconda安裝路徑加入到環境變量(.bashrc)中,這個一定要輸入yes,不然沒有將anaconda加入到環境變量,裝完anaconda之後,輸入conda list 命令系統會報錯:未找到命令。

安裝成功後,會有當前用戶根目錄下生成一個anaconda2的文件夾,裏面就是安裝好的內容。

輸入conda list 就可以查詢,你現在安裝了哪些庫,常用的numpy, scipy名列其中。如果你還有什麼包沒有安裝上,可以運行

conda install ***  來進行安裝,

如果某個包版本不是最新的,運行 conda update *** 就可以了。

 

編譯python接口

首先,將caffe根目錄下的python文件夾加入到環境變量

打開配置文件bashrc

# sudo vi ~/.bashrc
*************************************************************************************
PS:
保存命令

按ESC鍵 跳到<a target=_blank target="_blank" rel="nofollow" href="https://www.baidu.com/s?wd=%E5%91%BD%E4%BB%A4%E6%A8%A1%E5%BC%8F&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dWPAmYmW0dnv7Wry7-nANb0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjfkP1D1PWRd" class="baidu-highlight" style="color: rgb(51, 102, 153); text-decoration: none;">命令模式</a>,然後:

:w   保存文件但不退出vi
:w file 將修改另外保存到file中,不退出vi
:w!   強制保存,不推出vi
:wq  保存文件並退出vi
:wq! 強制保存文件,並退出vi
q:  不保存文件,退出vi
:q! 不保存文件,強制退出vi
:e! 放棄所有修改,從上次保存文件開始再編輯
*************************************************************************************

在最後面加入

export PYTHONPATH=/home/leo/caffe/python:$PYTHONPATH

注意 /home/leo/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

如果沒有提示錯誤,則編譯成功。

我出現的問題是ImportError: No module named google.protobuf.internal

解決方法:直接在終端中安裝protobuf,代碼:conda install protobuf (用pip install protobuf安裝會一直半途出錯,無法完成安裝)

配置環境和安裝spyder:

在github上https://github.com/spyder-ide/spyder下載軟件包

 

sudo pip install spyderconda install QtPyconda install Pylint

 

test code:

from skimage import ioimg=io.imread('d:/dog.jpg')io.imshow(img)

將其中的d:/dog.jpg 改成你的圖片位置

然後點擊上面工具欄裏的綠色三角進行運行,最終顯示

如果右下角“ Ipython console" 能顯示出圖片,說明我們的運行環境安裝成功。

我們可以選擇右上角的 ” variable explorer" 來查看圖片信息,如

我們可以把這個程序保存起來,注意python腳本文件的後綴名爲py.

四、skimage包的子模塊

skimage包的全稱是scikit-image SciKit (toolkit for SciPy) ,它對scipy.ndimage進行了擴展,提供了更多的圖片處理功能。它是由python語言編寫的,由scipy 社區開發和維護。skimage包由許多的子模塊組成,各個子模塊提供不同的功能。主要子模塊列表如下:

子模塊名稱 主要實現功能
io讀取、保存和顯示圖片或視頻
data提供一些測試圖片和樣本數據
color顏色空間變換
filters圖像增強、邊緣檢測、排序濾波器、自動閾值等
draw操作於numpy數組上的基本圖形繪製,包括線條、矩形、圓和文本等
transform幾何變換或其它變換,如旋轉、拉伸和拉東變換等
morphology形態學操作,如開閉運算、骨架提取等
exposure圖片強度調整,如亮度調整、直方圖均衡等
feature特徵檢測與提取等
measure圖像屬性的測量,如相似性或等高線等
segmentation圖像分割
restoration圖像恢復
util通用函數

用到一些圖片處理的操作函數時,需要導入對應的子模塊,如果需要導入多個子模塊,則用逗號隔開,如:

from skimage import io,data,color

參考:http://www.cnblogs.com/denny402/p/5088399.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章