Fast rcnn cpu 训练自己的数据

本文介绍如何在 cpu 模式下使用 Faster RCNN demo,以及在cpu 模式下训练自己的数据。

Install Faster-rcnn

源码地址:https://github.com/rbgirshick/py-faster-rcnn

由于 faster rcnn 依赖是基于 caffe 的,所以需要先安装 caffe,所以前提是你已经在本机上装过 caffe ,然后直接复制该 Makefile.config 到目录 $FRCN_ROOT/caffe-fast-rcnn 下然后执行 make -j8 && make pycaffe 即可。

Run demo in cpu

安装完后可以跑个 demo 试试

cd $FRCN_ROOT
./tools/demo.py

如果出现如下错误:

ImportError: No module named gpu_nms

说明 demo.py 脚本默认使用 gpu 检测物体,如果想要使用 cpu 修要做如下修改:

  • $FRCN_ROOT/lib/setup.py 中含有nms.gpu_nms的部分注释掉,注释后的内容如下。同时需要将该文件中 58 行左右的 CUDA = locate_cuda() 也注释掉。
ext_modules = [
    Extension(
        "utils.cython_bbox",
        ["utils/bbox.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    ),
    Extension(
        "nms.cpu_nms",
        ["nms/cpu_nms.pyx"],
        extra_compile_args={'gcc': ["-Wno-cpp", "-Wno-unused-function"]},
        include_dirs = [numpy_include]
    ),
    #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']]
    #),
    Extension(
        'pycocotools._mask',
        sources=['pycocotools/maskApi.c', 'pycocotools/_mask.pyx'],
        include_dirs = [numpy_include, 'pycocotools'],
        extra_compile_args={
            'gcc': ['-Wno-cpp', '-Wno-unused-function', '-std=c99']},
    ),
]
  • $FRCN_ROOT/lib/fast_rcnn/config.py中 205 行的 __C.USE_GPU_NMS = True 改成 __C.USE_GPU_NMS = False
  • $FRCN_ROOT/lib/fast_rcnn/nms_wrapper.py中的第 9 行 from nms.gpu_nms import gpu_nms 注释掉

现在执行 ./tool/demo.py --cpu就可以看到物体检测的效果了。

Train in cpu

  1. 首先需要对$/FRCN_ROOT/caffe-faster-rcnn/src/caffe/layers/内的roi_pooling_layer.cppsmooth_L1_loss_layer.cpp进行替换并重新编译,替换文件在 github-faster-rcnn-cpu
  2. 将该数据集放在py-faster-rcnn\data下,用你的数据集替换VOC2007数据集(即用你的Annotations,ImagesSets和JPEGImages替换 py-faster-rcnn\data\VOCdevkit2007\VOC2007 中对应文件夹)
  3. 下载ImageNet数据集下预训练得到的模型参数(用来初始化),解压,然后将该文件放在py-faster-rcnn\data下。(提供一个百度云地址:http://pan.baidu.com/s/1hsxx8OW)
  4. 对模型的配置文件进行一些修改,主要有:
    • 修改 $/FRCN_ROOT/lib/datasets/pascal_voc.py 中待检测物体的类别名,主要时第 30 行 self._classes = ('__background__', ) 中加入你自己想要分类的物体名。
    • 修改文件stage1_rpn_train.pt,stage2_rpn_train.pt,stage1_fast_rcnn_train.pt,stage1_fast_rcnn_train.pt 中待分类的个数。
  5. 修改 $/FRCN_ROOT/experiments/scripts/faster_rcnn_alt_opt.sh 脚本,去掉 46 行 time ./tools/train_faster_rcnn_alt_opt.py --gpu ${GPU_ID} \ 中的 --gpu ${GPU_ID},对 57 行做同样的操作。

执行命令 ./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc 就可以使用预训练的 ImageNet 数据 finetune 自己的数据了。

但此时会出现错误:

WARNING: Logging before InitGoogleLogging() is written to STDERR
F1209 11:40:45.697101   535 common.cpp:66] Cannot use GPU in CPU-only Caffe: check mode.
*** Check failure stack trace: ***

主要原因时文件 $/FRCN_ROOT/tool/train_faster_rcnn_alt_opt.py 默认使用了 caffe gpu mode,需要对该文件做如下修改:

  1. 注释掉 34-36 行,注释后的结果如下:
33     parser = argparse.ArgumentParser(description='Train a Faster R-CNN network')
34     # parser.add_argument('--gpu', dest='gpu_id',
35     #                     help='GPU device id to use [0]',
36     #                     default=0, type=int)
37     parser.add_argument('--net_name', dest='net_name',
  1. 注释掉 102-103 行 的 caffe.set_mode_gpu()caffe.set_device(cfg.GPU_ID),并在后面加上 caffe.set_mode_cpu()
  2. 注释掉出现 gpu_id 的地方

再次执行命令 ./experiments/scripts/faster_rcnn_alt_opt.sh 0 ZF pascal_voc 就可以看到训练效果了。

扫描二维码关注公众号,回复: 3866599 查看本文章

Reference

http://www.cnblogs.com/justinzhang/p/5386837.html

http://www.aichengxu.com/view/11065139

猜你喜欢

转载自blog.csdn.net/u012675539/article/details/53537271
今日推荐