From aed992edc67d8bdf57e3ac836559d55b4a3aecdc Mon Sep 17 00:00:00 2001 From: dujianyi Date: Thu, 10 Sep 2015 11:25:56 -0400 Subject: [PATCH 1/2] Specify ceil/floor mode for MaxPooling --- include/caffe/vision_layers.hpp | 1 + src/caffe/layers/pooling_layer.cpp | 17 +++++++++++++---- src/caffe/proto/caffe.proto | 2 ++ 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index 211e3d9042d..e2f65775bf8 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -416,6 +416,7 @@ class PoolingLayer : public Layer { int height_, width_; int pooled_height_, pooled_width_; bool global_pooling_; + bool ceil_mode; Blob rand_idx_; Blob max_idx_; }; diff --git a/src/caffe/layers/pooling_layer.cpp b/src/caffe/layers/pooling_layer.cpp index c8d41499455..e71a6f38472 100644 --- a/src/caffe/layers/pooling_layer.cpp +++ b/src/caffe/layers/pooling_layer.cpp @@ -38,6 +38,7 @@ void PoolingLayer::LayerSetUp(const vector*>& 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(); + ceil_mode_ = pool_param.ceil_mode(); if (global_pooling_) { kernel_h_ = bottom[0]->height(); kernel_w_ = bottom[0]->width(); @@ -90,10 +91,18 @@ void PoolingLayer::Reshape(const vector*>& bottom, kernel_h_ = bottom[0]->height(); kernel_w_ = bottom[0]->width(); } - pooled_height_ = static_cast(ceil(static_cast( - height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1; - pooled_width_ = static_cast(ceil(static_cast( - width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1; + // Specify the structure by ceil or floor mode + if (ceil_mode_) { + pooled_height_ = static_cast(ceil(static_cast( + height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1; + pooled_width_ = static_cast(ceil(static_cast( + width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1; + } else { + pooled_height_ = static_cast(floor(static_cast( + height_ + 2 * pad_h_ - kernel_h_) / stride_h_)) + 1; + pooled_width_ = static_cast(floor(static_cast( + width_ + 2 * pad_w_ - kernel_w_) / stride_w_)) + 1; + } 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. diff --git a/src/caffe/proto/caffe.proto b/src/caffe/proto/caffe.proto index aa299f8660b..c23c2b393c7 100644 --- a/src/caffe/proto/caffe.proto +++ b/src/caffe/proto/caffe.proto @@ -744,6 +744,8 @@ 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]; + // Specify floor/ceil mode + optional bool ceil_mode = 13 [default = true]; } message PowerParameter { From b4259a1c77f2a0357ca5409924b10dcb49955f9d Mon Sep 17 00:00:00 2001 From: dujianyi Date: Thu, 10 Sep 2015 11:27:25 -0400 Subject: [PATCH 2/2] Specify ceil/floor mode for MaxPooling --- include/caffe/vision_layers.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/caffe/vision_layers.hpp b/include/caffe/vision_layers.hpp index e2f65775bf8..1335c3dccb5 100644 --- a/include/caffe/vision_layers.hpp +++ b/include/caffe/vision_layers.hpp @@ -416,7 +416,7 @@ class PoolingLayer : public Layer { int height_, width_; int pooled_height_, pooled_width_; bool global_pooling_; - bool ceil_mode; + bool ceil_mode_; Blob rand_idx_; Blob max_idx_; };