Skip to content

Commit

Permalink
fix auto padding calculation again
Browse files Browse the repository at this point in the history
  • Loading branch information
PETER_GAO committed May 12, 2015
1 parent 1a70336 commit c7e2dfc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/caffe/layers/spp_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,16 @@ LayerParameter SPPLayer<Dtype>::GetPoolingParam(const int pyramid_level,
// performed across the entire image
int kernel_h = ceil(bottom_h / static_cast<double>(num_bins));
int pad_h = 0;
int remainder_h = kernel_h - bottom_h % num_bins + 1;
int remainder_h = kernel_h * num_bins - bottom_h;
if (bottom_h % num_bins > 0) {
pad_h = (remainder_h + 1) / 2;
kernel_h = (bottom_h + 2 * pad_h) / num_bins;
}

int kernel_w = ceil(bottom_w / static_cast<double>(num_bins));
int pad_w = 0;
int remainder_w = kernel_w - bottom_w % num_bins + 1;
int remainder_w = kernel_w * num_bins - bottom_w;
if (bottom_w % num_bins > 0) {
pad_w = (remainder_w + 1) / 2;
kernel_w = (bottom_w + 2 * pad_w) / num_bins;
}

pooling_param.mutable_pooling_param()->set_pad_h(pad_h);
Expand Down
22 changes: 22 additions & 0 deletions src/caffe/test/test_spp_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,31 @@ class SPPLayerTest : public MultiDeviceTest<TypeParam> {
SPPLayerTest()
: blob_bottom_(new Blob<Dtype>()),
blob_bottom_2_(new Blob<Dtype>()),
blob_bottom_3_(new Blob<Dtype>()),
blob_top_(new Blob<Dtype>()) {}
virtual void SetUp() {
Caffe::set_random_seed(1701);
blob_bottom_->Reshape(2, 3, 9, 8);
blob_bottom_2_->Reshape(4, 3, 1024, 765);
blob_bottom_3_->Reshape(10, 3, 7, 7);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_bottom_vec_2_.push_back(blob_bottom_2_);
blob_bottom_vec_3_.push_back(blob_bottom_3_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~SPPLayerTest() { delete blob_bottom_; delete blob_top_; }

Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_bottom_2_;
Blob<Dtype>* const blob_bottom_3_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_bottom_vec_2_;
vector<Blob<Dtype>*> blob_bottom_vec_3_;
vector<Blob<Dtype>*> blob_top_vec_;
};

Expand Down Expand Up @@ -81,6 +86,23 @@ TYPED_TEST(SPPLayerTest, TestEqualOutputDims) {
EXPECT_EQ(this->blob_top_->width(), 1);
}

TYPED_TEST(SPPLayerTest, TestEqualOutputDims2) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
layer_param.mutable_spp_param()->set_pyramid_height(3);
SPPLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_3_, this->blob_top_vec_);
// expected number of pool results is geometric sum
// (1 - r ** n)/(1 - r) where r = 4 and n = pyramid_height
// (1 - 4 ** 3)/(1 - 4) = 21
// multiply bottom num_channels * expected_pool_results
// to get expected num_channels (3 * 21 = 63)
EXPECT_EQ(this->blob_top_->num(), 10);
EXPECT_EQ(this->blob_top_->channels(), 63);
EXPECT_EQ(this->blob_top_->height(), 1);
EXPECT_EQ(this->blob_top_->width(), 1);
}

TYPED_TEST(SPPLayerTest, TestForwardBackward) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
Expand Down

0 comments on commit c7e2dfc

Please sign in to comment.