Ink One

配置并运行 faster-RCNN

安装python库依赖

cython,python-opencveasydict

1
2
3
sudo apt-get install python-opencv
pip install cython
pip install easydict

克隆py-faster-rcnn源代码

1
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git

编译cython模块

1
2
cd $FRCN_ROOT/lib
make

修改配置文件

1
2
3
cd $FRCN_ROOT/caffe-faster-rcnn
cp Makefile.config.example Makefile.config # make指令只能make Makefile.config
gedit Makefile.config #编辑Makefile.config文件

编辑Makefile.config配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
USE_CUDNN := 1 # Build with CuDNN
WITH_PYTHON_LAYER := 1 # 支持使用Python编写layers
#去掉不合适的cuda计算能力参数
CUDA_ARCH := -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
##
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
##

编译caffe-faster-rcnn

需要先更新用到的caffe:原版py-faster-rcnn依赖的caffe比较老,不支持cudnn v5和v6。

方法1:利用git修改

1
2
3
4
cd caffe-fast-rcnn
git remote add caffe https://github.com/BVLC/caffe.git
git fetch caffe
git merge -X theirs caffe/master

合并后注释掉include/caffe/layers/python_layer.hpp文件中的如下行:

1
//self_.attr(“phase”) = static_cast(this->phase_)

方法2:手工修改

用最新caffe源码的以下文件替换掉caffe-faster-rcnn的对应文件

include/caffe/util/cudnn.hpp

include/caffe/layers/cudnn_relu_layer.hpp, src/caffe/layers/cudnn_relu_layer.cpp, src/caffe/layers/cudnn_relu_layer.cu

include/caffe/layers/cudnn_sigmoid_layer.hpp, src/caffe/layers/cudnn_sigmoid_layer.cpp, src/caffe/layers/cudnn_sigmoid_layer.cu

include/caffe/layers/cudnn_tanh_layer.hpp, src/caffe/layers/cudnn_tanh_layer.cpp, src/caffe/layers/cudnn_tanh_layer.cu

caffe-faster-rcnnsrc/caffe/layers/cudnn_conv_layer.cu 文件中的所有
cudnnConvolutionBackwardData_v4 函数名替换为 cudnnConvolutionBackwardData
cudnnConvolutionBackwardFilter_v4函数名替换为 cudnnConvolutionBackwardFilter

执行编译

1
2
make -j8
make pycaffe

Demo

下载训练好的Faster R-CNN模型(在 VOC 2007 trainval 上训练)

1
2
cd $FRCN_ROOT
./data/scripts/fetch_faster_rcnn_models.sh

运行 Demo

1
./tools/demo.py

在VOC2007上训练

下载训练集、验证集、测试集数据和VOCdevkit

1
2
3
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar

解压缩

1
2
3
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar

创建数据集链接

1
2
cd $FRCN_ROOT/data
ln -s <VOCdevkit_path> VOCdevkit2007

下载ImageNet预训练模型

1
2
cd $FRCN_ROOT
./data/scripts/fetch_imagenet_models.sh

执行训练

1
2
3
4
5
cd $FRCN_ROOT
./experiments/scripts/faster_rcnn_end2end.sh [GPU_ID] [NET] [DATASET]
# GPU_ID is the GPU you want to train on
# NET in {ZF, VGG_CNN_M_1024, VGG16} is the network arch to use
# DATASET is either pascal_voc or coco.

Error

Problem1

1
AttributeError: 'module' object has no attribute 'text_format'

Solution:

py-faster-rcnn/lib/fast_rcnn/train.py的头文件导入部分加上 :

1
import google.protobuf.text_format

Problem2

1
TypeError: 'numpy.float64' object cannot be interpreted as an index

这里是因为numpy版本不兼容导致的问题,可以卸载numpy,安装numpy1.11.0,pip install numpy=1.11。但这样需要重新编译OpenCV和Caffe。

Other Solution:

修改如下几个地方的code:

1) py-faster-rcnn/lib/roi_data_layer/minibatch.py

1
2
3
4
# line 26:
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
# ==>
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)

2) py-faster-rcnn/lib/datasets/ds_utils.py

1
2
3
4
# line 12:
hashes = np.round(boxes * scale).dot(v)
# ==>
hashes = np.round(boxes * scale).dot(v).astype(np.int)

3) py-faster-rcnn/lib/fast_rcnn/test.py

1
2
3
4
# line 129:
hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v)
# ==>
hashes = np.round(blobs['rois'] * cfg.DEDUP_BOXES).dot(v).astype(np.int)

4) py-faster-rcnn/lib/rpn/proposal_target_layer.py

1
2
3
4
# line 60:
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image)
# ==>
fg_rois_per_image = np.round(cfg.TRAIN.FG_FRACTION * rois_per_image).astype(np.int)

Problem3

1
TypeError: slice indices must be integers or None or hava an __index__ method

还是由于numpy版本的原因,同上,可以更换到低版本numpy。

Other Solution:

修改 py-faster-rcnn/lib/rpn/proposal_target_layer.py,转到123行:

1
2
3
4
5
6
7
for ind in inds:
cls = clss[ind]
start = 4 * cls
end = start + 4
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
return bbox_targets, bbox_inside_weights

这里的start,end都是 numpy.int 类型,这种类型的数据不能作为索引,所以必须对其进行强制类型转换,如下修改:

1
2
3
4
5
6
7
for ind in inds:
cls = clss[ind]
start = int(4 * cos)
end = start + 4
bbox_targets[ind, start:end] = bbox_target_data[ind, 1:]
bbox_inside_weights[ind, start:end] = cfg.TRAIN.BBOX_INSIDE_WEIGHTS
return bbox_targets, bbox_inside_weights