編譯py-faster-rcnn全過程
編譯py-faster-rcnn,花費了好幾天,中間遇到好多問題,今天終於成功編譯。下面詳述我的整個編譯過程。
【註記:】其實下面的依賴庫可以安裝在統一的一個本地目錄下,相關安裝指南,可以參考《深度學習:21天實戰caffe》的第5天那一章。 (這也是我後來才知道的。)
部分軟體見百度雲盤:連結: https://pan.baidu.com/s/1i4DNpU9 密碼: shgt
硬體情況
- centos6.9
- 8塊Titan xp顯示卡
[關於nvidia顯示卡的對比,可以檢視《nvidia顯示卡對比分析》]
軟體情況
boost 1.41
關於boost的情況,雖然caffe官網要求boost>=1.55,但是似乎對於低版本的也沒有出現什麼問題,如果要安裝boost,可以參考
一般安裝好以後,標頭檔案在/usr/include/boost,lib檔案在/usr/lib64/boost 。OpenBlas
CUDA8.0
- cudnn v6 (如果沒有安裝,可以自己裝在home目錄下,可參考< cuda、cudnn相關問題連結>)
檢視相應版本號:
cuda 版本
cat /usr/local/cuda/version.txt
cudnn 版本
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
- glog
- gflags
- leveldb
安裝可以使用yum install
sudo yum install leveldb-devel
- lmdb
使用pip安裝:
pip install lmdb
- snappy
首先下載snappy-1.1.1,然後解壓,配置:./configure –prefix=/home/**/
最後編譯安裝: make && make install
安裝依賴庫
- anaconda2
首先安裝anaconda2,安裝方法很簡單,就是下載軟體,然後執行bash Anaconda2-5.0.0.1-Linux-x86_64.sh
- opencv-python
使用pip安裝:
pip install opencv-python ,最新版為opencv3
安裝後
>>> import cv2
>>> print cv2.__version__
注意:在caffe中不用麻煩地安裝原始碼裝opencv,會很麻煩,要裝一堆依賴庫,而且很多需要sudo許可權,
請參考< linux 安裝python-opencv>
3. easydict
pip install easydict
使用上述命令即可安裝成功。
4. protobuf 2.6.1
首先先確認系統是否有裝protobuf ,
檢視方法:
$ protoc --version
libprotoc 2.3.0
如果沒有顯示,則說明沒有裝,另外我們期望裝2.6.1(caffe-master使用的protobuf版本是2.5.0及以上),所以版本不符,就需要重灌。
注意要裝兩個probobuf,一個是python的,一個是系統的。而且最好安裝2.6.1,比較穩定。
由於anaconda預設沒有裝protobuf,所以可以使用pip安裝指定版本的protobuf。
[python版本]
pip install protobuf==2.6.1
$./autogen.sh(在csdn下載的可以省略)
$./configure --prefix=/home/**/protobuf (自己的目錄下)
$ make
$ make check
$ make install
編譯成功後將export PATH=/home/**/protobuf/bin:$PATH加入到環境變數中
輸入 protoc –version命令,如顯示protobuf 2.6.1則安裝成功。
$ protoc --version
libprotoc 2.6.1
最後進入caffe根目錄,修改Makefile.config,在INCLUDE_DIRS後面加入/home/*/protobuf/include
LIBRARY_DIRS 後面加入 /home/*/protobuf/lib。
【注意:】
在向path中新增變數的時候,我總結一個原則,即:如果系統的路徑,比如/usr/local/bin等,就這樣寫:
export PATH=$PATH:/usr/local/bin
or
export PATH=/usr/local/bin:$PATH
如果是自己安裝的路徑,就:
export PATH=/home/**/protobuf/bin:$PATH
也就是一個在前一個在後的問題。之所以強調這個,是因為有時候系統已經安裝了某個軟體,如果你想覆蓋它,則在path中必須寫在它的前面。因此對於自己安裝的路徑,一般我們都想覆蓋原來的軟體,所以寫在path前面。,而系統安裝的軟體只有一個,寫在前後都可以。我們可以檢視echo $PATH
來看最後的path路徑結果,並在需要的時候,在~/.bashrc中做相應地調整。一定要注意:
對於含有兩個安裝位置的軟體,如果我們想先讀某一個,一定要注意在path的位置。
make check的結果:
5. hdf5
由於前面安裝過 anaconda ,所以hdf5已經預設安裝。只需要把/*/anaconda2/lib 加入~/.bashrc中,即:
export LD_LIBRARY_PATH=/*/anaconda2/lib:$LD_LIBRARY_PATH
便於搜尋hdf5的lib,尤其是 libhdf5_hl.so.100 ,若在後面的編譯中仍然找不到此lib的話,只能在home目錄下重新安裝,參考:http://blog.csdn.net/xiamentingtao/article/details/78266153
6. 關於numpy的版本問題
根據py-faster-rcnn的要求:
numpy>=1.7.1
pip install -U numpy==1.11.0
下載py-faster-rcnn
克隆Faster R-CNN倉庫:
# Make sure to clone with --recursive
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
–recursive :是為了遞迴下載caffe-master
繼續安裝python依賴庫
將上面的克隆的檔案放在安裝目錄下,這裡我們用FRCN_ROOT指 py-faster-rcnn的路徑,也就是
進入根目錄,然後進入py-faster-rcnn/caffe-fast-rcnn/python中,caffe-fast-rcnn/python/requirement.txt檔案列出了pycaffe的所有依賴庫,要把requirements下的依賴都裝一遍
Cython>=0.19.2
numpy>=1.7.1
scipy>=0.13.2
scikit-image>=0.9.3
matplotlib>=1.3.1
ipython>=3.0.0
h5py>=2.2.0
leveldb>=0.191
networkx>=1.8.1
nose>=1.3.0
pandas>=0.12.0
python-dateutil>=1.4,<2
protobuf>=2.5.0
python-gflags>=2.0
pyyaml>=3.10
Pillow>=2.3.0
six>=1.1.0
執行bash命令:
$for req in $(cat requirements.txt); do pip install $req; done
如果安裝完成,可以再執行一遍上述命令,發現全部裝好:
執行pip list可以列出相應地版本號,要注意與上面說的相符,尤其是:protobuf 2.6.1
到目前為止,我們可以對照caffe 官網的install,檢測所有必須的依賴庫。
合版本
編譯Cython模組
cd $FRCN_ROOT/lib
make
編譯caffe& pycaffe
正如caffe官網-install所說,在編譯前有兩種方法可以配置build,
第一種就是常見的手動設定Makefile.(我所採用的)
cp Makefile.config.example Makefile.config
# Adjust Makefile.config (for example, if using Anaconda Python, or if cuDNN is desired)
make all
make test
make runtest
make pycaffe
另一種就是先用cmake(版本>=2.8.7)自動檢測各依賴項位置,生成Makefile.也可以自己設定。使用cmake-gui可能更容易些。
mkdir build
cd build
#編譯CPU版本
#cmake -DCPU_ONLY=ON -DBLAS=Open .
#編譯GPU版本
$cmake -DBLAS=Open -DCUDA_NVCC_FLAGS=--Wno-deprecated-gpu-targets ..
make all
make install
make runtest
make pycaffe
下面給出我自己配置的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 := 1
USE_LMDB := 1
# 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 := 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 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 := /data1/caiyong.wang/bin/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_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 := /data1/caiyong.wang/bin/anaconda2
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
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /data1/caiyong.wang/bin/hdf5-1.10.1/hdf5/include /data1/caiyong.wang/bin/protobuf/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /data1/caiyong.wang/bin/hdf5-1.10.1/hdf5/lib /data1/caiyong.wang/bin/protobuf/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 ?= @
需要注意的是我禁掉了opencv,並且 加入了一些額外的搜尋路徑,
至於到底是否需要增加,我也不清楚。也許不用新增也可以。你可以自己嘗試。
下面再貼出我的~/.bashrc的內容:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
alias matlab='/data1/caiyong.wang/bin/matlab/bin/matlab'
export PYTHONPATH=/data1/caiyong.wang/program/faster_rcnn/py-faster-rcnn/caffe-fast-rcnn/python:$PYTHONPATH
export LD_LIBRARY_PATH=/usr/local/cuda-8.0/lib64:$LD_LIBRARY_PATH
# libglog
export LD_LIBRARY_PATH=/usr/local/lib/:$LD_LIBRARY_PATH
# hdf5 (其實可以省略,因為anaconda的lib含有,而且在下面一句已經加上,且加在了自安裝的HDF5的前面,所以實際上使用的仍然是anaconda的 版本,如果不行就加上唄。)
#export LD_LIBRARY_PATH=/data1/caiyong.wang/bin/hdf5-1.10.1/hdf5/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=/data1/caiyong.wang/bin/anaconda2/lib:$LD_LIBRARY_PATH
export PATH=/usr/local/cuda-8.0/bin:$PATH
######## add protobuf lib path ########
##(動態庫搜尋路徑) 程式載入執行期間查詢動態連結庫時指定除了系統預設路徑之外的其他路徑
#export LD_LIBRARY_PATH=/data1/caiyong.wang/bin/protobuf2/lib/:$LD_LIBRARY_PATH
##(靜態庫搜尋路徑) 程式編譯期間查詢動態連結庫時指定查詢共享庫的路徑
#export LIBRARY_PATH=/data1/caiyong.wang/bin/protobuf2/lib/:$LIBRARY_PATH
##執行程式搜尋路徑
#export PATH=/data1/caiyong.wang/bin/protobuf2/bin/:$PATH
##c程式標頭檔案搜尋路徑
#export C_INCLUDE_PATH=/data1/caiyong.wang/bin/protobuf2/include/:$C_INCLUDE_PATH
##c++程式標頭檔案搜尋路徑
#export CPLUS_INCLUDE_PATH=/data1/caiyong.wang/bin/protobuf2/include/:$CPLUS_INCLUDE_PATH
##pkg-config 路徑
#export PKG_CONFIG_PATH=/data1/caiyong.wang/bin/protobuf2/lib/pkgconfig/
#######################################
#alias protoc='/data1/caiyong.wang/bin/protobuf2/bin/protoc'
export PATH=/data1/caiyong.wang/bin/protobuf/bin:$PATH
export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/data1/caiyong.wang/bin/protobuf/lib:$LD_LIBRARY_PATH
# added by Anaconda2 installer
export PATH=/data1/caiyong.wang/bin/anaconda2/bin:$PATH
一般情況下,這樣編譯就可以通過。這裡make runtest花費時間很多,我用了近9個小時。如果不想花費這麼長時間,也可以不執行這個指令。
最後為了在python中可以載入caffe模組,我們需要在bashrc設定PYTHONPATH。
export PYTHONPATH=/*/py-faster-rcnn/caffe-fast-rcnn/python:$PYTHONPATH
然後source ~/.bashrc生效。
最終檢視python介面是否編譯成功:
進入python環境,進行import操作
# python
>>> import caffe
>>> print caffe.__version__
1.0.0
如果沒有提示錯誤,則編譯成功。
測試demo
- 下載預計算的R-CNN檢測器
cd $FRCN_ROOT
./data/scripts/fetch_faster_rcnn_models.sh
這個模型解壓出來750M,下載的話大概695M,而且很慢。。
為了方便大家,我把模型上傳到了百度雲 連結: https://pan.baidu.com/s/1eSKoVAi 密碼: 59ga
注意下載後,解壓到data目錄下。
2. 執行
cd $FRCN_ROOT
./tools/demo.py
出現結果: (共5個圖片來源)
成功~~~