GammaPerf

Caffe Pooling层对ceil mode选择的支持

2019-01-14  本文已影响0人  馆长撒嘛

转Pytorch框架下ResNet到caffe的时候遇到的问题:
Pytorch中池化层默认的ceil mode是false,而caffe只实现了ceil mode= true的。
github上搜到大神自己增加了Pooling层ceil mode的配置,需要修改源码,记录在此。
include/caffe/layers/pooling_layers.hpp中:

  int height_, width_;
  int pooled_height_, pooled_width_;
  bool global_pooling_;
  bool ceil_mode_;//+
  Blob<Dtype> rand_idx_;
  Blob<int> max_idx_;
};

src/caffe/layers/pooling_layer.cpp中:

  //modify for ceil mode
  //pooled_height_ = static_cast<int>(ceil(static_cast<float>(
  //    height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
  //pooled_width_ = static_cast<int>(ceil(static_cast<float>(
  //    width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
  // Specify the structure by ceil or floor mode
  if (ceil_mode_) {
    pooled_height_ = static_cast<int>(ceil(static_cast<float>(
        height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
    pooled_width_ = static_cast<int>(ceil(static_cast<float>(
        width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
  } else {
    pooled_height_ = static_cast<int>(floor(static_cast<float>(
        height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1;
    pooled_width_ = static_cast<int>(floor(static_cast<float>(
        width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1;
  }

src/caffe/proto/caffe.proto中:

  // If global_pooling then it will pool over the size of the bottom by doing
  // kernel_h = bottom->height and kernel_w = bottom->width
  optional bool global_pooling = 12 [default = false];
  // Specify floor/ceil mode
  optional bool ceil_mode = 13 [default = true];//+
}
上一篇下一篇

猜你喜欢

热点阅读