Ubuntu14.04 Caffe安裝

Caffe簡介

Caffe全稱Convolutional Architecture for Fast Feature Embedding,是一個計算CNN相關算法的框架。就目前來說是deeplearning最流行的開源的框架,所以使用的人也是最多的。
caffe網址:http://caffe.berkeleyvision.org/
github網址:https://github.com/BVLC/caffe/

安裝環境

系統:Ubuntu14.04 Desktop x64(建議使用剛安裝好的系統,同時不要更新,不然會影響後面的某些環節)
顯卡:支持CUDA的顯卡,當然這個不是必須的

依賴程序

基本包

安裝開發需要的一些基本包

sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install gfortran ## python中安裝scipy需要依賴gfortran

:Ubuntu默認環境雖然已經有gcc,但並沒有提供C/C++的編譯環境,單獨安裝編譯環境比較複雜,所以提供了build-essential命令一鍵來構建所需的編譯環境。

CUDA(可選)

cuda在caffe編譯和使用中不是必須的,如果實在沒有支持cuda的顯卡或者只想嘗試一下caffe,不需要用它來進行復雜的訓練,不安裝也可以。
安裝鏈接:http://blog.csdn.net/honyniu/article/details/46387429

CuDNN(可選)

這個庫是基於cuda,當然也是可選的。
安裝鏈接:http://blog.csdn.net/honyniu/article/details/46388241

BLAS(必選)

這個庫提供了caffe需要的一些基本的矩陣和向量庫,這個是必選的。
安裝鏈接:http://blog.csdn.net/honyniu/article/details/46388915

OpenCV(必選)

OpenCV提供了圖像處理和計算機視覺的庫,在caffe使用中是必須的,同時最好自己編譯安裝。
安裝鏈接:http://blog.csdn.net/honyniu/article/details/46390097

其他依賴項

caffe需要一些特定的數據庫的操作, 因此需要安裝leveldb, lmdb, hdf5等庫。 此外, caffe採用了google的一些庫, 比如protobuf以及glog, 這些也需要安裝,命令如下。

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

Matlab和python

這兩個不是必須的,但是caffe提供了bash、matlab和python的接口,同時caffe許多的可視化接口都是通過python實現的,推薦安裝。
matlab在這裏使用的是Matlab-R2014A,這裏就不講怎麼安裝得了,自己去百度教程,默認安裝即可,安裝在’/usr/local/MATLAB’目錄下。
ubuntu默認安裝的2.7的版本但是爲了安裝Python開發環境,方便今後編譯其他擴展庫,命令如下

sudo apt-get install python-dev

python是使用的虛擬環境,具體虛擬環境的構建見http://blog.csdn.net/honyniu/article/details/46382929
激活虛擬環境

source ENV/bin/activate

安裝編譯pycaffe的依賴包, 進入caffe安裝目錄中的python文件夾, 你會看到一個requirements.txt, 首先, 修改protobuf那一行爲protobuf==2.5.0, 然後執行下面命令:

for req in $(cat requirements.txt); do pip install $req; done

這裏我們沒安裝caffe推薦的anaconda包, 因爲安裝之後進不去桌面。而且anaconda包只是一個自帶了requirements.txt中所有選項的python集合庫, 將上面的庫安裝好之後, 不裝anaconda沒有任何影響。

配置編譯

caffe程序

可以去上面的caffe的github網址把caffe最新代碼下載下來,也可以使用git命令獲取,如下

git clone https://github.com/BVLC/caffe.git

caffe配置

終於到了最後的一步了,如果安裝好了上面的所有的caffe的依賴,那麼下面就可以對caffe進行配置和編譯了。
首先拷貝生成所需要的’Makefile.config’文件,caffe安裝包下面給提供了example,注意直接拷貝下面的可能會報錯,因爲每一行命令的後面不能有空白的字符串,有可能會導致編譯不通過。

cp Makefile.config.example Makefile.config

然後對該’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   #如果沒有安裝cudnn的注掉

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1   #如果沒有安裝cuda,不是要GPU的設置開啓  

# 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   #cuda默認安裝目錄(其實是軟鏈接),一般不需要修改,但如果沒有安裝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 := mkl   #這裏使用的是Intel MKL,如果使用的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 := /opt/intel/mkl/include   #如果是atlas,這兩個路徑要注掉
BLAS_LIB := /opt/intel/mkl/lib/intel64

# 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/R2014a   #Matlab的路徑,沒安裝Matlab或者不需要編譯該接口可以注掉
# 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庫的路徑,使用的是虛擬環境capy這裏就是一直提到的ENV,有時候可能不起作用,還是會報錯,不知道爲什麼,那就設置下面的export,一般就沒問題了。
PYTHON_INCLUDE := /home/huayong/capy/include/python2.7 \
                /home/huayong/capy/lib/python2.7/site-packages/numpy/core/include

#If it doesn't work above, you can try this below.
export CPLUS_INCLUDE_PATH=/home/huayong/capy/include/python2.7:/home/huayong/capy/lib/python2.7/site-packages/numpy/core/include

# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := /home/wenzheng/anaconda
# 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/x86_64-linux-gnu
# PYTHON_LIB := $(ANACONDA_HOME)/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

# 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 ?= @

caffe編譯

基本的編譯,j代表是多線程編譯,一般前兩個命令不會出錯,最後一個可能會出錯,但有時候也不影響使用。

make all -j8
make alltest -j8
make runtest

編譯matlab和python接口,這個當然不是必須的,看你自己的需求。

make pycaffe
make matcaffe

結束語

到這來我們就成功編譯好了caffe,那麼下面就要開始真正的caffe使用之旅了。下面有時間會把自己學習使用caffe的一些過程寫下來,包括訓練測試caffe提供的examples,caffe的可視化,運行一些caffe經典的例子(如R-CNN)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章