From: Jeff Donahue Date: Sun, 25 May 2014 01:40:03 +0000 (-0700) Subject: add tests for maxpooling layer forward, and for maxpooling with top mask X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=48534870710ae828ed5388827f277ed5882f764b;p=platform%2Fupstream%2Fcaffe.git add tests for maxpooling layer forward, and for maxpooling with top mask --- diff --git a/src/caffe/test/test_pooling_layer.cpp b/src/caffe/test/test_pooling_layer.cpp index a26078e..6628fc5 100644 --- a/src/caffe/test/test_pooling_layer.cpp +++ b/src/caffe/test/test_pooling_layer.cpp @@ -22,7 +22,8 @@ class PoolingLayerTest : public ::testing::Test { protected: PoolingLayerTest() : blob_bottom_(new Blob()), - blob_top_(new Blob()) {} + blob_top_(new Blob()), + blob_top_mask_(new Blob()) {} virtual void SetUp() { Caffe::set_random_seed(1701); blob_bottom_->Reshape(2, 3, 6, 5); @@ -33,11 +34,88 @@ class PoolingLayerTest : public ::testing::Test { blob_bottom_vec_.push_back(blob_bottom_); blob_top_vec_.push_back(blob_top_); } - virtual ~PoolingLayerTest() { delete blob_bottom_; delete blob_top_; } + virtual ~PoolingLayerTest() { + delete blob_bottom_; + delete blob_top_; + delete blob_top_mask_; + } Blob* const blob_bottom_; Blob* const blob_top_; + Blob* const blob_top_mask_; vector*> blob_bottom_vec_; vector*> blob_top_vec_; + + void TestForward() { + LayerParameter layer_param; + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_MAX); + const int num = 2; + const int channels = 2; + blob_bottom_->Reshape(num, channels, 3, 5); + // Input: 2x 2 channels of: + // [1 2 5 2 3] + // [9 4 1 4 8] + // [1 2 5 2 3] + for (int i = 0; i < 15 * num * channels; i += 15) { + blob_bottom_->mutable_cpu_data()[i + 0] = 1; + blob_bottom_->mutable_cpu_data()[i + 1] = 2; + blob_bottom_->mutable_cpu_data()[i + 2] = 5; + blob_bottom_->mutable_cpu_data()[i + 3] = 2; + blob_bottom_->mutable_cpu_data()[i + 4] = 3; + blob_bottom_->mutable_cpu_data()[i + 5] = 9; + blob_bottom_->mutable_cpu_data()[i + 6] = 4; + blob_bottom_->mutable_cpu_data()[i + 7] = 1; + blob_bottom_->mutable_cpu_data()[i + 8] = 4; + blob_bottom_->mutable_cpu_data()[i + 9] = 8; + blob_bottom_->mutable_cpu_data()[i + 10] = 1; + blob_bottom_->mutable_cpu_data()[i + 11] = 2; + blob_bottom_->mutable_cpu_data()[i + 12] = 5; + blob_bottom_->mutable_cpu_data()[i + 13] = 2; + blob_bottom_->mutable_cpu_data()[i + 14] = 3; + } + PoolingLayer layer(layer_param); + layer.SetUp(blob_bottom_vec_, &blob_top_vec_); + EXPECT_EQ(blob_top_->num(), num); + EXPECT_EQ(blob_top_->channels(), channels); + EXPECT_EQ(blob_top_->height(), 2); + EXPECT_EQ(blob_top_->width(), 4); + if (blob_top_vec_.size() > 1) { + EXPECT_EQ(blob_top_mask_->num(), num); + EXPECT_EQ(blob_top_mask_->channels(), channels); + EXPECT_EQ(blob_top_mask_->height(), 2); + EXPECT_EQ(blob_top_mask_->width(), 4); + } + layer.Forward(blob_bottom_vec_, &blob_top_vec_); + // Expected output: 2x 2 channels of: + // [9 5 5 8] + // [9 5 5 8] + for (int i = 0; i < 8 * num * channels; i += 8) { + EXPECT_EQ(blob_top_->cpu_data()[i + 0], 9); + EXPECT_EQ(blob_top_->cpu_data()[i + 1], 5); + EXPECT_EQ(blob_top_->cpu_data()[i + 2], 5); + EXPECT_EQ(blob_top_->cpu_data()[i + 3], 8); + EXPECT_EQ(blob_top_->cpu_data()[i + 4], 9); + EXPECT_EQ(blob_top_->cpu_data()[i + 5], 5); + EXPECT_EQ(blob_top_->cpu_data()[i + 6], 5); + EXPECT_EQ(blob_top_->cpu_data()[i + 7], 8); + } + if (blob_top_vec_.size() > 1) { + // Expected mask output: 2x 2 channels of: + // [5 2 2 9] + // [5 12 12 9] + for (int i = 0; i < 8 * num * channels; i += 8) { + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 0], 5); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 1], 2); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 2], 2); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 3], 9); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 4], 5); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 5], 12); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 6], 12); + EXPECT_EQ(blob_top_mask_->cpu_data()[i + 7], 9); + } + } + } }; typedef ::testing::Types Dtypes; @@ -127,6 +205,28 @@ TYPED_TEST(PoolingLayerTest, PrintCPUBackward) { } */ +TYPED_TEST(PoolingLayerTest, TestCPUForwardMax) { + Caffe::set_mode(Caffe::CPU); + this->TestForward(); +} + +TYPED_TEST(PoolingLayerTest, TestGPUForwardMax) { + Caffe::set_mode(Caffe::GPU); + this->TestForward(); +} + +TYPED_TEST(PoolingLayerTest, TestCPUForwardMaxTopMask) { + Caffe::set_mode(Caffe::CPU); + this->blob_top_vec_.push_back(this->blob_top_mask_); + this->TestForward(); +} + +TYPED_TEST(PoolingLayerTest, TestGPUForwardMaxTopMask) { + Caffe::set_mode(Caffe::GPU); + this->blob_top_vec_.push_back(this->blob_top_mask_); + this->TestForward(); +} + TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) { LayerParameter layer_param; PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); @@ -153,6 +253,34 @@ TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) { &(this->blob_top_vec_)); } +TYPED_TEST(PoolingLayerTest, TestCPUGradientMaxTopMask) { + LayerParameter layer_param; + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_MAX); + this->blob_top_vec_.push_back(this->blob_top_mask_); + Caffe::set_mode(Caffe::CPU); + PoolingLayer layer(layer_param); + GradientChecker checker(1e-4, 1e-2); + checker.CheckGradientExhaustive(&layer, &(this->blob_bottom_vec_), + &(this->blob_top_vec_)); +} + +TYPED_TEST(PoolingLayerTest, TestGPUGradientMaxTopMask) { + LayerParameter layer_param; + PoolingParameter* pooling_param = layer_param.mutable_pooling_param(); + pooling_param->set_kernel_size(3); + pooling_param->set_stride(2); + pooling_param->set_pool(PoolingParameter_PoolMethod_MAX); + this->blob_top_vec_.push_back(this->blob_top_mask_); + Caffe::set_mode(Caffe::GPU); + PoolingLayer layer(layer_param); + GradientChecker checker(1e-4, 1e-2); + checker.CheckGradientExhaustive(&layer, &(this->blob_bottom_vec_), + &(this->blob_top_vec_)); +} + TYPED_TEST(PoolingLayerTest, TestCPUForwardAve) { LayerParameter layer_param;