首页 > 技术文章 > ubuntu16+caffe fast-rcnnCPU运行步骤

shuimuqingyang 2018-12-13 20:35 原文

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

一:初步进行检测

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

1.首先opencv是需要安装的,我用的ubuntu16,opencv3.4,具体安装教程可以参考网上很多,不想多提。

 

2.安装几个依赖包:cython,python-opencv和easydict,直接用sudo apt-get安装,网上很多用pip安装,bug比较多。

 

3.从github上clone项目文件,注意:一定要在clone时加入--recursive参数,不然会很麻烦,也不要直接下载:

 

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

 

 

4.Cython模块的编译

cd py-faster-rcnn/lib

make

 

5.编译caffe-fast-rcnn

cd ..

cd caffe-fast-rcnn

 

修改这个目录下的Makefile.config(如果没有这个文件,就直接cp Makefile.config.example Makefile.config)

将CPU_ONLY := 1开关和WITH_PYTHON_LAYER开关打开:

image

image

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

 

6.运行demo.py

 

在环境一切就绪的情况下,将faster的模型下载下来:

在py-faster-rcnn/data/scripts 目录下,执行./fetch_faster_rcnn_models.sh 将数据下载,可能需要一定时间,慢慢等吧~~~

能成功吗?如果现在直接运行肯定是万万不可以成功的!

https://blog.csdn.net/u012675539/article/details/53537271

 

 

 

源码地址: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

 

  • 1

 

说明 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

    首先需要对$/FRCN_ROOT/caffe-faster-rcnn/src/caffe/layers/内的roi_pooling_layer.cpp和smooth_L1_loss_layer.cpp进行替换并重新编译,替换文件在 github-faster-rcnn-cpu
    将该数据集放在py-faster-rcnn\data下,用你的数据集替换VOC2007数据集(即用你的Annotations,ImagesSets和JPEGImages替换 py-faster-rcnn\data\VOCdevkit2007\VOC2007 中对应文件夹)
    下载ImageNet数据集下预训练得到的模型参数(用来初始化),解压,然后将该文件放在py-faster-rcnn\data下。(提供一个百度云地址:http://pan.baidu.com/s/1hsxx8OW)
    对模型的配置文件进行一些修改,主要有:
        修改 $/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 中待分类的个数。
    修改 $/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: ***

    1
    2
    3

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

    注释掉 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
    2
    3
    4
    5

    注释掉 102-103 行 的 caffe.set_mode_gpu() 和 caffe.set_device(cfg.GPU_ID),并在后面加上 caffe.set_mode_cpu()
    注释掉出现 gpu_id 的地方

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

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

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

推荐阅读