protected:
PoolingLayerTest()
: blob_bottom_(new Blob<Dtype>()),
- blob_top_(new Blob<Dtype>()) {}
+ blob_top_(new Blob<Dtype>()),
+ blob_top_mask_(new Blob<Dtype>()) {}
virtual void SetUp() {
Caffe::set_random_seed(1701);
blob_bottom_->Reshape(2, 3, 6, 5);
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<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
+ Blob<Dtype>* const blob_top_mask_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> 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<Dtype> 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<float, double> Dtypes;
}
*/
+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();
&(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<TypeParam> layer(layer_param);
+ GradientChecker<TypeParam> 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<TypeParam> layer(layer_param);
+ GradientChecker<TypeParam> checker(1e-4, 1e-2);
+ checker.CheckGradientExhaustive(&layer, &(this->blob_bottom_vec_),
+ &(this->blob_top_vec_));
+}
+
TYPED_TEST(PoolingLayerTest, TestCPUForwardAve) {
LayerParameter layer_param;