py-Faster-RCNN在windows下的pycaffe编译

来源:互联网 发布:百亿彩票计划软件 编辑:程序博客网 时间:2024/05/21 06:55

20170714更新

前两天重新编译了一遍caffe,发现caffe官网的项目进行了更新。除了最后一步不需要添加环境变量之外,最主要的改变还是在build_win.cmd的修改上,需要在下面教程的基础上再将WITH_NINJA设为0。VS版本修改的行数也有一些出入,希望大家能够自行寻找到需要修改的地方。

前言

刚开始接触FRCNN的代码时,发现网上关于windows下编译caffe的资料并不多,所以自己花了一些时间探索出了解决方法,但一直没有上传。

最近发现很多人都需要这个教程,所以干脆直接传上来,就不用我再一个个发邮件了~≧▽≦~

在windows下跑FRCNN的难点在于pycaffe的编译,因为FRCNN使用的pycaffe是在caffe自带的层中添加了两个新的网络层类型后重新编译的得到的。所以本文主要介绍的就是如何在windows对caffe添加新层并编译。

添加新层

  • 首先完全按照官网上的指示来 https://github.com/BVLC/caffe/tree/windows
  • 环境需求:
    • Visual Studio 2013 or 2015
    • CMake 3.4 or higher (Visual Studio and Ninja generators are supported)
    • Python 2.7 Anaconda x64 (or Miniconda).
    • CUDA 7.5 or 8.0 (optional) (use CUDA 8 if using Visual Studio 2015)
    • cuDNN v5 (optional)
  • 将cmake.exe的路径添加到系统变量PATH中。
  • 在cmd中执行下面代码(找到一个路径ROOT下):

    ROOT > git clone https://github.com/BVLC/caffe.gitROOT > cd caffeROOT \caffe> git checkout windows
  • 在执行下面的代码前需要修改ROOT\scripts\build_win.cmd(推荐用Notepad++打开)

    • Line 21: PATH改成自己的Anaconda的路径
    • 如果使用的不是vs2015而是2013,Line7和Line47:把14改成12(记得保存)
  • 由于faster rcnn在caffe的基础上添加了自己的layer,下面对caffe文件夹做一些修改。

    • 将caffe\src\caffe\proto下的caffe.proto文件中增加与roi_pooling_layer和smooth_L1_loss_layer相关的参数配置等,参考其他layer的参数配置。(找好位置后,将下面的代码直接复制到caffe.proto文件中):
optional ROIPoolingParameter roi_pooling_param = 8266711;optional SmoothL1LossParameter smooth_l1_loss_param = 8266712;// Message that stores parameters used by ROIPoolingLayermessage ROIPoolingParameter {  // Pad, kernel size, and stride are all given as a single value for equal  // dimensions in height and width or as Y, X pairs.  optional uint32 pooled_h = 1 [default = 0]; // The pooled output height  optional uint32 pooled_w = 2 [default = 0]; // The pooled output width  // Multiplicative spatial scale factor to translate ROI coords from their  // input scale to the scale used when pooling  optional float spatial_scale = 3 [default = 1];}message SmoothL1LossParameter {  // SmoothL1Loss(x) =  //   0.5 * (sigma * x) ** 2    -- if x < 1.0 / sigma / sigma  //   |x| - 0.5 / sigma / sigma -- otherwise  optional float sigma = 1 [default = 1];}
注:这里不能用caffe-faster-rcnn对应文件夹下的proto去替换caffecaffe\src\caffe\proto下的caffe.proto文件,因为新的caffe里支持RNN,新增了关于RNN的参数设置,而原来faster rcnn中没有这些设置。所以只能把faster rcnn需要的layer参数添加到caffe.proto中。
  • 在caffe\src\caffe\layers下添加caffe-faster-rcnn对应文件夹下的
    • smooth_L1_loss_layer.cpp
    • smooth_L1_loss_layer.cu
    • roi_pooling_layer.cpp
    • roi_pooling_layer.cu
      四个文件。
  • 在caffe\include\caffe下添加caffe-faster-rcnn对应文件夹下的fast_rcnn_layers.hpp。

编译caffe

  • 接着在之前的cmd执行
ROOT \caffe> scripts\build_win.cmd
  • 成功后如图所示:

    这里写图片描述

  • 上步成功后手动把

    • ROOT\build\libraries\bin
    • ROOT\build\libraries\lib
    • ROOT\build\libraries\x64\vc12\bin
    • ROOT\build\libraries

    加到系统变量里。

编译nms库

  • 到https://github.com/MrGF/py-faster-rcnn-windows中下载lib文件进行替换
  • cd到ROOT/lib下,输入python setup.py install即可
  • (这一部分我这边没有显示编译成功,但运行后还是生成了gpu_nms.pyd,对程序执行没有影响。)gpu_nms需要单独编译, 只是用来编译gpu_nms,GPU的nms比较难编译单独分出来编译,其余的编译过程在上面的setup.py已完成。将setup_cuda.py文件中的路径改为你自己的路径之后,输入python setup_cuda.py install进行编译。
  • 如果还有问题,可以到http://www.unjeep.com/q/894288692.htm 下看看有没有解决方案。

Faster rcnn的Python接口

  • 将caffe\python文件夹放到caffe-fast-rcnn文件夹下即可。
  • 成功后结果如图:
    这里写图片描述
    这里写图片描述

希望对大家有用处!~( ̄▽ ̄)~

0 0
原创粉丝点击