編譯caffe-Makefile.config解析

配置cuDNN

原始代碼:

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

如果要使用GPU版本的caffe并且準(zhǔn)備使用cuDNN加速庫,那就將

# USE_CUDNN := 1

改為

USE_CUDNN := 1

CPU or GPU

原始代碼:

# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1

這兩行代碼決定是否配置CPU版本的caffe。配置文件默認是編譯GPU版本的caffe,如果電腦上沒有英偉達GPU或者只準(zhǔn)備用caffe做簡單練習(xí),則可以只編譯CPU版本的caffe,將

# CPU_ONLY := 1

改成

CPU_ONLY := 1

這樣,文件中所有關(guān)于CUDA和cuDNN的配置都將無效。

配置基本I/O依賴項

原始代碼:

# 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

這幾行代碼是配置caffe的基本輸入/輸出中用到的3個模塊:opencv、LEVELDB和LMDB。

opencv是世界上最流行的開源計算機視覺庫,caffe使用opencv完成一些圖像存取和預(yù)處理功能,《21天實戰(zhàn)caffe》里面說到,其實caffe里面用到的opencv模塊非常有限,僅限于圖片讀寫、圖片縮放等CPU上的模塊,編譯配置時的配置選項其實不用選太多,可以禁用很多模塊節(jié)省編譯時間。

LMDB(Lightning Memory-Mapped Database Manager,閃電般的內(nèi)存映射型數(shù)據(jù)庫管理器),在caffe中的主要作用是提供數(shù)據(jù)管理,將形形色色的原始數(shù)據(jù)(圖片、二進制數(shù)據(jù)等)轉(zhuǎn)換為統(tǒng)一的Key-Value存儲,便于caffe的DataLayer獲取這些數(shù)據(jù)。

LEVELDB是caffe早期版本使用的數(shù)據(jù)存儲方式,目前大部分例程都已經(jīng)使用LMDB代替了LEVELDB,但是為了與以前的版本兼容,默認還是將LEVELDB依賴庫編譯到caffe中。

默認情況下,這三個模塊都開啟的,不需要修改什么。

只需要注意的是,如果你使用的是opencv3.x版本,則需要將

# OPENCV_VERSION := 3

改為

OPENCV_VERSION := 3

如果安裝的opencv2.x則無需修改。

配置編譯器

原始代碼:

# 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++

這幾行代碼是選擇使用哪種編譯器,linux默認使用g++,這里一般不用修改。

配置CUDA

原始代碼:

# 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 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
# For CUDA >= 9.0, comment the *_20 and *_21 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_52,code=sm_52 \
        -gencode arch=compute_60,code=sm_60 \
        -gencode arch=compute_61,code=sm_61 \
        -gencode arch=compute_61,code=compute_61

配置BLAS

原始代碼:

# 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

這一段配置代碼的功能是選擇BLAS庫。什么是BLAS庫?BLAS(Basic Linear Algebra Subprograms, 基本線性代數(shù)子程序)是caffe在實現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)中的矩陣/向量等線性運算中使用的數(shù)學(xué)庫,最常用的BLAS庫有三個:Intel MKL、ATLAS和OpenBLAS。caffe可以通過上面這段配置代碼,可以選擇其中任何一種BLAS:

配置代碼默認選擇的是ATLAS,所以如果不修改上面這段代碼我們就默認使用ATLAS庫。

如果想使用MKL庫,則需要將

BLAS := atlas

修改為

BLAS := mkl

如果要使用OpenBlas庫,則需將

BLAS := atlas

修改為

BLAS := open

此外,如果不適用默認的ATLAS,同時還需要添加相應(yīng)模塊的lib路徑和include路徑:

BLAS_INCLUDE := /path/to/your/blas
BLAS_LIB := /path/to/your/blas

例如,在py-faster-rcnn的python源代碼中,作者提供的配置文件中就選擇的是OpenBlas模塊,下面是其對應(yīng)配置代碼:

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# 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/OpenBLAS/include
BLAS_LIB := /opt/OpenBLAS/lib

最下面的Homebrew是MAC蘋果系統(tǒng)中的包管理系統(tǒng),類似于apt-get。我們使用的linux系統(tǒng),所以這一段不用管它。

配置caffe-matlab接口

原始代碼:

# 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

默認情況下,配置文件只編譯caffe的python接口,不編譯matlab接口,如果你要使用matlab進行開發(fā),需要對這一部分進行修改。

配置caffe-python接口

原始代碼:

# 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)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
        # $(ANACONDA_HOME)/include/python2.7 \
        # $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/lib/python3.5/dist-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

注意到上面第三行的PYTHON_INCLUDE中的/usr/lib/python2.7/dist-packages/numpy/core/include,如果你使用的是系統(tǒng)自帶的python2.7,同時安裝numpy使用的是

sudo -H pip install numpy #此時numpy會被安裝到/usr/local/lib/python2.7/dist-packages/目錄下

而不是

sudo apt install python-numpy #此時numpy會被安裝到/usr/lib/python2.7/dist-packages/目錄下

的話,得注意了,應(yīng)該將

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/lib/python2.7/dist-packages/numpy/core/include

改成

PYTHON_INCLUDE := /usr/include/python2.7 \
        /usr/local/lib/python2.7/dist-packages/numpy/core/include

配置其他依賴項的lib路徑和include路徑

原始代碼:

# 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

除了上面提到的幾個依賴項,caffe還有其他幾個必須的依賴項,這里的include路徑和lib路徑就是給出其他caffe依賴項的對應(yīng)位置。

一般需要將

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

改為

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial

Homebrew下面部分屬于蘋果系統(tǒng)的配置選項,同樣不用管它。

其他配置

原始代碼:

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# 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

# N.B. both build and distribute dirs are cleared on `make clean`
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 ?= @

。。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容