Faster R-CNN在Window环境的目标检测

来源:互联网 发布:吾爱破解软件 编辑:程序博客网 时间:2024/04/27 18:59

Faster R-CNN在Linux环境下面的训练和检测相信很多感兴趣的人都可以根据下面

https://github.com/rbgirshick/py-faster-rcnn

获得python版本的环境搭建指导。

但是Faster R-CNN工作在Windows却很少看到有人能够成功搭建训练和检测环境。

本文,主要介绍使用Linux环境下自定义训练模型在Windows环境下Faster R-CNN的python版本的目标检测移植。

采用pascal_voc格式制作自己的数据集,模型使用中等网络模型,使用的端到端的训练方法,测试的使用CPU进行测试。

Faster R-CNN移植需要分两部分:

1、Faster R-CNN Caffe移植:

第一步:安装caffe的python依赖环境,可以依据faster rcnn caffe目录下面python的requirements.txt文件下载对应的python模块:

Cython>=0.19.2numpy>=1.7.1scipy>=0.13.2scikit-image>=0.9.3matplotlib>=1.3.1ipython>=3.0.0h5py>=2.2.0leveldb>=0.191networkx>=1.8.1nose>=1.3.0pandas>=0.12.0python-dateutil>=1.4,<2protobuf>=2.5.0python-gflags>=2.0pyyaml>=3.10Pillow>=2.3.0six>=1.1.0

也可以安装Anaconda 来更快速获得相应的科学计算工具库。由于该网站下载速度极慢,我采用 了python2.7的版本安装。

一般通过pip和wheel两种方式即可完成所有包的安装;

第二步:移植faster rcnn caffe,默认从https://github.com/rbgirshick/caffe-fast-rcnn/tree/faster-rcnn

下面的faster rcnn caffe本身不支持Windows的,需要基于该源码进行移植。

可以根据官方caffe-window版本进行对比移植。请使用VS2013工程进行编译。

官方caffe-window版本:https://github.com/BVLC/caffe/tree/windows

基本上移植难度很小。

第三步:生成pycaffe:

编译libcaffe,pycaffe。

检查Build\x64\Release\pycaffe\caffe目录下是否拷贝了pycaffe依赖库。若没有,则需要采用下面bat脚本生成:

进入当前caffe-windows\windows\scripts\\目录下执行下面命令:

ProtoCompile.cmd   D:\\Faster_RCNN\\caffe-windows\\windows\\  D:\\Faster_RCNN\\caffe-windows\\windows\\scripts\\PythonPreBuild  D:\\Faster_RCNN\\caffe-windows\\windows\\  D:\\Faster_RCNN\\caffe-windows\\windows\\scripts\\PythonPostBuild  D:\\Faster_RCNN\\caffe-windows\\windows\\  D:\\Faster_RCNN\\caffe-windows\\Build\\x64\\Release\\

2、Faster R-CNN目标检测移植:

第一步:

主要工作是移植nms,bbox两个模块,具体主要修改setup.py脚本和nms包装文件:
nms包装文件,全部采用CPU方式:
#from nms.gpu_nms import gpu_nms
def nms(dets, thresh, force_cpu=True):
setup.py修改:只留cpu的方式
ext_modules = [    # unix _compile: obj, src, ext, cc_args, extra_postargs, pp_opts    Extension(        "utils.cython_bbox",        sources=["utils\\bbox.pyx"],        extra_compile_args={'gcc': []},        include_dirs = [numpy_include]    ),    Extension(        "nms.cpu_nms",        sources=["nms\\cpu_nms.pyx"],        extra_compile_args={'gcc': []},        include_dirs = [numpy_include],    )]
注释掉cuda的依赖:
    def compile(sources, output_dir=None, macros=None, include_dirs=None, debug=0, extra_preargs=None, extra_postargs=None, depends=None):        postfix=os.path.splitext(sources[0])[1]        if postfix == '.cu':            # use the cuda for .cu files            #self.set_executable('compiler_so', CUDA['nvcc'])            # use only a subset of the extra_postargs, which are 1-1 translated            # from the extra_compile_args in the Extension class            postargs = extra_postargs['nvcc']        else:            postargs = extra_postargs['gcc']        return super(sources, output_dir, macros, include_dirs, debug, extra_preargs, postargs, depends)

 
 

由于本人机器不支持GPU,所以注释掉相关GPU模式的接口和模块的编译;
为了编译这两个模块,需要安装MinGW:https://sourceforge.net/projects/mingw-w64/
并下载gcc
在cmd中输入python setup.py build_ext --inplace编译。
我在编译nms报模型数据类型和nms定义的类型不匹配,需要修改cpu_nms.pyx,如下:
cdef np.ndarray[np.int64_t, ndim=1] order = scores.argsort()[::-1]
或参考官方问题解决:
https://github.com/rbgirshick/py-faster-rcnn/issues/36 
将编译输出的caffe目录拷贝到caffe-fast-rcnn\python目录下面。
可以尝试通过运行caffe程序,以确保caffe依赖库都已经拷贝进来。

第二步:修改demo

修改tool\demo.py中物种:
CLASSES = ('__background__',           'aeroplane', 'bicycle', 'bird', 'boat',           'bottle', 'bus', 'car', 'cat', 'chair',           'cow', 'diningtable', 'dog', 'horse',           'motorbike', 'person', 'pottedplant',           'sheep', 'sofa', 'train', 'tvmonitor')

为自己的物种:
CLASSES = ('__background__',           'flags')

训练模型修改:
NETS = {'vgg16': ('VGG16',                  'vgg16_fast_rcnn_iter_40000.caffemodel'),        'vgg_cnn_m_1024': ('VGG_CNN_M_1024',                  'vgg_cnn_m_1024_faster_rcnn_iter_40000.caffemodel'),        'zf': ('ZF',                  'ZF_faster_rcnn_final.caffemodel')}

第三步:修改测试模型

和训练模型修改是一样的,按照官方要求进行修改:
It looks like you have 3 classes.
In the train.prototxt and test.prototxt files that you're using,
you'll need to change  num_output  from 21 to 3 in the  cls_score  layer and from 84 to 12 in the  bbox_pred  layer.
You'll also need to change  num_classes  from 21 to 3 in the Python layer that provides data to the net (the very first layer).
将训练好的模型拷贝到指定位置,

运行python tools\demo.py --cpu --net vgg_cnn_m_1024即可输出检测的结果:
















在使用训练的模型测试的时候没有任何输出时就需要考虑Linux环境和Windows环境的差别了,此处我遇到过这种情况之一是由于模型加载不完整导致的该问题,具体修改可以参考如下:

http://blog.csdn.net/seasermy/article/details/51509571

修改内容:在io.cpp中增加windows的支持:

#ifdef _MSC_VER   int fd = open(filename, O_RDONLY|O_BINARY);#else   int fd = open(filename, O_RDONLY);#endif











0 0