Skip to content

Commit

Permalink
PoolingLayer customizable output shape rounding mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Noiredd committed Mar 7, 2018
1 parent 7b3ac40 commit d7da092
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
1 change: 1 addition & 0 deletions include/caffe/layers/pooling_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class PoolingLayer : public Layer<Dtype> {
int height_, width_;
int pooled_height_, pooled_width_;
bool global_pooling_;
PoolingParameter_RoundMode round_mode_;
Blob<Dtype> rand_idx_;
Blob<int> max_idx_;
};
Expand Down
21 changes: 17 additions & 4 deletions src/caffe/layers/pooling_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void PoolingLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
|| (!pool_param.has_stride_h() && !pool_param.has_stride_w()))
<< "Stride is stride OR stride_h and stride_w are required.";
global_pooling_ = pool_param.global_pooling();
round_mode_ = pool_param.round_mode();
if (global_pooling_) {
kernel_h_ = bottom[0]->height();
kernel_w_ = bottom[0]->width();
Expand Down Expand Up @@ -87,10 +88,22 @@ void PoolingLayer<Dtype>::Reshape(const vector<Blob<Dtype>*>& bottom,
kernel_h_ = bottom[0]->height();
kernel_w_ = bottom[0]->width();
}
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;
switch (round_mode_) {
case PoolingParameter_RoundMode_CEIL:
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;
break;
case PoolingParameter_RoundMode_FLOOR:
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;
break;
default:
LOG(FATAL) << "Unknown rounding mode.";
}
if (pad_h_ || pad_w_) {
// If we have padding, ensure that the last pooling starts strictly
// inside the image (instead of at the padding); otherwise clip the last.
Expand Down
6 changes: 6 additions & 0 deletions src/caffe/proto/caffe.proto
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,12 @@ message PoolingParameter {
// 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];
// How to calculate the output size - using ceil (default) or floor rounding.
enum RoundMode {
CEIL = 0;
FLOOR = 1;
}
optional RoundMode round_mode = 13 [default = CEIL];
}

message PowerParameter {
Expand Down

0 comments on commit d7da092

Please sign in to comment.