stochastic pooling test
[jacinto-ai/caffe-jacinto.git] / src / caffe / test / test_pooling_layer.cpp
1 // Copyright 2013 Yangqing Jia
3 #include <cstring>
4 #include <cuda_runtime.h>
6 #include "gtest/gtest.h"
7 #include "caffe/blob.hpp"
8 #include "caffe/common.hpp"
9 #include "caffe/filler.hpp"
10 #include "caffe/vision_layers.hpp"
11 #include "caffe/test/test_gradient_check_util.hpp"
13 #include "caffe/test/test_caffe_main.hpp"
15 namespace caffe {
17 extern cudaDeviceProp CAFFE_TEST_CUDA_PROP;
19 template <typename Dtype>
20 class PoolingLayerTest : public ::testing::Test {
21  protected:
22   PoolingLayerTest()
23       : blob_bottom_(new Blob<Dtype>()),
24         blob_top_(new Blob<Dtype>()) {};
25   virtual void SetUp() {
26     Caffe::set_random_seed(1701);
27     blob_bottom_->Reshape(2, 3, 6, 5);
28     // fill the values
29     FillerParameter filler_param;
30     GaussianFiller<Dtype> filler(filler_param);
31     filler.Fill(this->blob_bottom_);
32     blob_bottom_vec_.push_back(blob_bottom_);
33     blob_top_vec_.push_back(blob_top_);
34   };
35   virtual ~PoolingLayerTest() { delete blob_bottom_; delete blob_top_; }
36   Blob<Dtype>* const blob_bottom_;
37   Blob<Dtype>* const blob_top_;
38   vector<Blob<Dtype>*> blob_bottom_vec_;
39   vector<Blob<Dtype>*> blob_top_vec_;
40 };
42 typedef ::testing::Types<float, double> Dtypes;
43 TYPED_TEST_CASE(PoolingLayerTest, Dtypes);
45 TYPED_TEST(PoolingLayerTest, TestSetup) {
46   LayerParameter layer_param;
47   layer_param.set_kernelsize(3);
48   layer_param.set_stride(2);
49   PoolingLayer<TypeParam> layer(layer_param);
50   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
51   EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num());
52   EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_->channels());
53   EXPECT_EQ(this->blob_top_->height(), 3);
54   EXPECT_EQ(this->blob_top_->width(), 2);
55 }
57 /*
58 TYPED_TEST(PoolingLayerTest, PrintGPUBackward) {
59   LayerParameter layer_param;
60   layer_param.set_kernelsize(3);
61   layer_param.set_stride(2);
62   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
63   Caffe::set_mode(Caffe::GPU);
64   PoolingLayer<TypeParam> layer(layer_param);
65   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
66   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
67   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
68     cout << "bottom data " << i << " " << this->blob_bottom_->cpu_data()[i] << endl;
69   }
70   for (int i = 0; i < this->blob_top_->count(); ++i) {
71     cout << "top data " << i << " " << this->blob_top_->cpu_data()[i] << endl;
72   }
74   for (int i = 0; i < this->blob_top_->count(); ++i) {
75     this->blob_top_->mutable_cpu_diff()[i] = 1.;
76   }
77   layer.Backward(this->blob_top_vec_, true, &(this->blob_bottom_vec_));
78   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
79     cout << "bottom diff " << i << " " << this->blob_bottom_->cpu_diff()[i] << endl;
80   }
81 }
82 */
84 TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) {
85   LayerParameter layer_param;
86   layer_param.set_kernelsize(3);
87   layer_param.set_stride(2);
88   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
89   Caffe::set_mode(Caffe::CPU);
90   PoolingLayer<TypeParam> layer(layer_param);
91   GradientChecker<TypeParam> checker(1e-4, 1e-2);
92   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
93 }
95 TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) {
96   LayerParameter layer_param;
97   layer_param.set_kernelsize(3);
98   layer_param.set_stride(2);
99   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
100   Caffe::set_mode(Caffe::GPU);
101   PoolingLayer<TypeParam> layer(layer_param);
102   GradientChecker<TypeParam> checker(1e-4, 1e-2);
103   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
107 TYPED_TEST(PoolingLayerTest, TestCPUGradientAve) {
108   LayerParameter layer_param;
109   layer_param.set_kernelsize(3);
110   layer_param.set_stride(2);
111   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
112   Caffe::set_mode(Caffe::CPU);
113   PoolingLayer<TypeParam> layer(layer_param);
114   GradientChecker<TypeParam> checker(1e-2, 1e-2);
115   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
119 TYPED_TEST(PoolingLayerTest, TestGPUGradientAve) {
120   LayerParameter layer_param;
121   layer_param.set_kernelsize(3);
122   layer_param.set_stride(2);
123   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
124   Caffe::set_mode(Caffe::GPU);
125   PoolingLayer<TypeParam> layer(layer_param);
126   GradientChecker<TypeParam> checker(1e-2, 1e-2);
127   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);