參考https://haoyu.love/blog404.html
獲取並修改代碼
首先,我們需要獲取源代碼:
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
消除一個編譯錯誤
找到下面兩個文件
$FRCN_ROOT/caffe-fast-rcnn/src/caffe/test/test_smooth_L1_loss_layer.cpp $FRCN_ROOT/caffe-fast-rcnn/src/caffe/test/test_roi_pooling_layer.cpp
去掉最前面的
typedef ::testing::Types<GPUDevice<float>, GPUDevice<double> > TestDtypesGPU;
並將文件里面的 TestDtypesGPU 改為 TestDtypesAndDevices
另外,在 $FRCN_ROOT/caffe-fast-rcnn/src/caffe/test/test_smooth_L1_loss_layer.cpp 中,我們還需要去掉下面這行代碼才能通過編譯:
#include "caffe/vision_layers.hpp"
修正幾個 Typo
- 在
$FRCN_ROOT/lib/fast_rcnn/train.py中添加import google.protobuf.text_format (雖然我認為沒有用) $FRCN_ROOT/lib/roi_data_layer/minibatch.py里面的約 25 行:fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)$FRCN_ROOT/lib/datasets/ds_utils.py里面的約 12 行:hashes = np.round(boxes * scale).dot(v).astype(np.int)$FRCN_ROOT/lib/fast_rcnn/test.py里面的約 129 行:hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v).astype(np.int)$FRCN_ROOT/lib/rpn/proposal_target_layer.py里面:- 約 60 行:
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int) - 約 124 行:
cls = int(clss[ind]) - 約 166 行:
size=int(fg_rois_per_this_image) - 約 177 行:
size=int(bg_rois_per_this_image) - 約 184 行:
labels[int(fg_rois_per_this_image):] = 0
- 約 60 行:
添加 CPU 支持
因為網絡比較大,rbg 大神壓根沒想讓你用 cpu 來跑。不過為了完整一點,我們還是加上 CPU 的支持吧。
在源代碼的 pull-request 里面可以找到幾個 cpu 的實現。經過測試, 這個版本 的代碼可以拿來直接使用。其他幾個版本,例如 這個版本 ,就需要把 base_lr 設置得非常非常低,特被難以訓練。
如果希望使用純 CPU
這是個奇怪的需求…… 對…… 而且特別麻煩。也就是說,我們得剔除一些 GPU 的代碼。 在 $FRCN_ROOT/lib/setup.py 中,注釋掉
CUDA = locate_cuda()
self.set_executable('compiler_so', CUDA['nvcc'])
Extension('nms.gpu_nms',
['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
library_dirs=[CUDA['lib64']],
libraries=['cudart'],
language='c++',
runtime_library_dirs=[CUDA['lib64']],
# this syntax is specific to this build system
# we're only going to use certain compiler args with nvcc and not with
# gcc the implementation of this trick is in customize_compiler() below
extra_compile_args={'gcc': ["-Wno-unused-function"],
'nvcc': ['-arch=sm_35',
'--ptxas-options=-v',
'-c',
'--compiler-options',
"'-fPIC'"]},
include_dirs = [numpy_include, CUDA['include']]
),
在 $FRCN_ROOT/lib/fast_rcnn/config.py 中,將 __C.USE_GPU_NMS = True 改為 False
將 $FRCN_ROOT/lib/fast_rcnn/nms_wrapper.py 替換成如下代碼
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
from fast_rcnn.config import cfg
def nms(dets, thresh, force_cpu=False):
"""Dispatch to either CPU or GPU NMS implementations."""
if dets.shape[0] == 0:
return []
if cfg.USE_GPU_NMS and not force_cpu:
from nms.gpu_nms import gpu_nms
return gpu_nms(dets, thresh, device_id=cfg.GPU_ID)
else:
import pyximport
pyximport.install()
from nms.cpu_nms import cpu_nms
return cpu_nms(dets, thresh)
這里再加一個小 trick:打開下面幾個文件
$FRCN_ROOT/tools/test_net.py $FRCN_ROOT/tools/train_net.py
找到
caffe.set_mode_gpu() caffe.set_device(args.gpu_id)
改成
if args.gpu_id>=0 :
caffe.set_mode_gpu()
caffe.set_device(args.gpu_id)
else:
caffe.set_mode_cpu()
打開
$FRCN_ROOT/tools/train_faster_rcnn_alt_opt.py
找到
caffe.set_mode_gpu() caffe.set_device(cfg.GPU_ID)
改成
if cfg.GPU_ID>=0 :
caffe.set_mode_gpu()
caffe.set_device(cfg.GPU_ID)
else:
caffe.set_mode_cpu()
由於 GPU_ID 是一個必須填寫的參數,這樣修改的話,我們只要把 GPU_ID 填寫成一個負數就可以使用純 CPU 來跑了,代碼更改量比較少。雖然我知道有些地方的規定是「-1 means all」。
Let's 編譯 it !
編譯 Caffe 這個坑算是跳出來了。直接按照 這個筆記 來進行編譯就好了。 在這里有幾點需要注意:
- 必須開啟
USE_PYTHON_LAYER = 1,py-faster-rcnn 的有幾個層是拿 Python 寫的,不開啟的話 一定 會出問題。 - Python 請使用 Python2 而不是 Python3。
- 如果沒有升級 Caffe,那么請不要使用 CUDA8.0。
- 如果使用 GPU,必須使用
USE_CUDNN := 1,否則無論你顯存多大,都會報 “顯存溢出” 的錯誤。
在DIR/caffe-fast-rcnn/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 := /usr/include/atlas BLAS_LIB := /usr/lib/atlas-base LIBRARIES += glog gflags protobuf leveldb snappy lmdb boost_system hdf5_hl hdf5 m opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs # 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)/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 # Whatever else you find you need goes here. 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/hdf5/serial # 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 ?= @
重點是:要用python2.7,用3.5各種報錯,
接下來就是make 了 ,運行:
make all -j4 make test make runtest -j4 make pycaffe
測試網絡配置(只要能跑完就沒有問題)
發現問題詳見caffe網絡的生成:
Ubuntu16.04下配置caffe(僅CPU)
還有
接下來在.bashrc 中添加
119 export rcnn_path=/home/pis/py-faster-rcnn/caffe-fast-rcnn 120 export PYTHONPATH=$rcnn_path/python:$PYTHONPATH
當然source .bashrc
跑一下測試 Demo
這個是必須的!用來檢驗上面的成果。 首先下載訓練好的模型
./data/scripts/fetch_faster_rcnn_models.sh
不過應該是直接下載不了的,我放到了百度雲:
鏈接:https://pan.baidu.com/s/1Vab0mSvSCyxWFeSjkFW1_A 密碼:r4ty
然后
./tools/demo.py --cpu
純 CPU(--cpu)的話,應該不到五分鍾就能出來結果了…… 嗯……
這一階段完啦,下一階段見!
