object_detect

py-faster-rcnn

Faster R-CNN是一种object detection算法,这里记录下Faster R-CNN的Caffe、python实现。
git地址:https://github.com/rbgirshick/py-faster-rcnn

安装py-faster-rcnn

Clone the py-faster-rcnn

就是从github把源码下载下来,由于网络等原因,切记核对源代码是否下载完整。

Makefile.config

配置编译caffe所需的配置文件:
文件内容说明:
1、 将makefile.config里面的 WITH_PYTHON_LAYER :=1前面的注释去掉,这是因为faster R-CNN是要Python接口的,所以这一项要有,不能注释。
2、 # 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 /usr/lib/x86_64-linux-gnu/hdf5/serial
此处其实就是加上了hdf5的路径
3、 在编译caffe的Mkefile.config时遇到提示

nvcc warning : The ‘compute_20’, ‘sm_20’, and ‘sm_21’ architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).

原因是,Makefile.config中采用了CUDA的compute capability 2.0和2.1,这是两种计算能力。从CUDA 8.0开始compute capability 2.0和2.1被弃用了,所以可以将-gencode arch=compute_20,code=sm_20 和-gencode arch=compute_20,code=sm_21这两行删除即可。
4、 # CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
cuda路径不可错,若机器上安装过两个cuda,此处配置应与~/.bashrc中配置的路径一致

将 Makefile.config复制到py-faster-rcnn/目录下

修改caffe-fast-rcnn

但是由于代码提供者的caffe版本是老版本的,没有”与时俱进“,所以只兼容CUDNN 较老版本的。对于新的版本的,直接编译会报错。
参考链接:https://blog.csdn.net/u010733679/article/details/52221404
其中给出了两种方法:
一种是到github去下载新版的caffe并将它与py-faster-rcnn中的caffe-faster-rcnn合并,更换成新版的caffe,使其支持新的cuDNN版本;
另一种是自己手动更改一些文件,更改caffe的一些配置使其兼容新的cuDNN。
第一种方法较简单,但是一旦工程被修改过就很容易出错,不推荐;所以我采用了第二种方法。
1、用最新版本的caffe源码目录中的如下文件替换py-faster-rcnn/caffe-fast-rcnn中的对应文件。

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

2、用caffe源码中的这个文件替换掉faster rcnn 对应文件

include/caffe/util/cudnn.hpp

3、打开 py-faster-rcnn/caffe-fast-rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu文件,并将:

cudnnConvolutionBackwardData_v3
替换为
cudnnConvolutionBackwardData

cudnnConvolutionBackwardFilter_v3
替换为
cudnnConvolutionBackwardFilter

编译Cython modules

进入目录:py-faster-rcnn/lib
将Makefile文件中的python改为python2 因为此机器默认python版本为3.X
在终端中运行“make” 开始编译

编译 Caffe and pycaffe

打开py-faster-rcnn/caffe-fast-rcnn
终端执行“make -j8 && make pycaffe”

运行demo

获取faster_rcnn_models

参考https://blog.csdn.net/a8039974/article/details/77628805

获取imagenet_models

参考https://blog.csdn.net/a8039974/article/details/77628805

将这两个文件夹复制到/home/ubuntu/lihaizhou/py-faster-rcnn/data

回到路径/home/ubuntu/lihaizhou/py-faster-rcnn/
在终端执行“python2 tools/demo.py”
如果运行正常会自动弹出标注成功的图片