]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - src/caffe/test/test_pooling_layer.cpp
pooling layer
[jacinto-ai/caffe-jacinto.git] / src / caffe / test / test_pooling_layer.cpp
1 #include <cstring>
2 #include <cuda_runtime.h>
4 #include "gtest/gtest.h"
5 #include "caffe/blob.hpp"
6 #include "caffe/common.hpp"
7 #include "caffe/filler.hpp"
8 #include "caffe/vision_layers.hpp"
9 #include "caffe/test/test_gradient_check_util.hpp"
11 #include "caffe/test/test_caffe_main.hpp"
13 namespace caffe {
15 extern cudaDeviceProp CAFFE_TEST_CUDA_PROP;
17 template <typename Dtype>
18 class PoolingLayerTest : public ::testing::Test {
19  protected:
20   PoolingLayerTest()
21       : blob_bottom_(new Blob<Dtype>()),
22         blob_top_(new Blob<Dtype>()) {};
23   virtual void SetUp() {
24     Caffe::set_random_seed(1701);
25     blob_bottom_->Reshape(2, 3, 6, 5);
26     // fill the values
27     FillerParameter filler_param;
28     GaussianFiller<Dtype> filler(filler_param);
29     filler.Fill(this->blob_bottom_);
30     blob_bottom_vec_.push_back(blob_bottom_);
31     blob_top_vec_.push_back(blob_top_);
32   };
33   virtual ~PoolingLayerTest() { delete blob_bottom_; delete blob_top_; }
34   void ReferenceLRNForward(const Blob<Dtype>& blob_bottom,
35       const LayerParameter& layer_param, Blob<Dtype>* 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 TYPED_TEST(PoolingLayerTest, TestGPUMax) {
58   LayerParameter layer_param;
59   layer_param.set_kernelsize(3);
60   layer_param.set_stride(2);
61   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
62   Caffe::set_mode(Caffe::CPU);
63   PoolingLayer<TypeParam> layer(layer_param);
64   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
65   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
66   Blob<TypeParam> blob_reference(*this->blob_top_);
67   Caffe::set_mode(Caffe::GPU);
68   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
69   for (int i = 0; i < blob_reference.count(); ++i) {
70     EXPECT_EQ(blob_reference.cpu_data()[i], this->blob_top_->cpu_data()[i])
71         << "debug: index " << i;
72   }
73 }
75 TYPED_TEST(PoolingLayerTest, TestGPUAve) {
76   LayerParameter layer_param;
77   layer_param.set_kernelsize(3);
78   layer_param.set_stride(2);
79   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
80   Caffe::set_mode(Caffe::CPU);
81   PoolingLayer<TypeParam> layer(layer_param);
82   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
83   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
84   Blob<TypeParam> blob_reference(*this->blob_top_);
85   Caffe::set_mode(Caffe::GPU);
86   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
87   for (int i = 0; i < blob_reference.count(); ++i) {
88     EXPECT_GE(blob_reference.cpu_data()[i], this->blob_top_->cpu_data()[i] - 1e-4)
89         << "debug: index " << i;
90     EXPECT_LE(blob_reference.cpu_data()[i], this->blob_top_->cpu_data()[i] + 1e-4)
91         << "debug: index " << i;
92   }
93 }
95 /*
96 TYPED_TEST(PoolingLayerTest, PrintGPUBackward) {
97   LayerParameter layer_param;
98   layer_param.set_kernelsize(3);
99   layer_param.set_stride(2);
100   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
101   Caffe::set_mode(Caffe::GPU);
102   PoolingLayer<TypeParam> layer(layer_param);
103   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
104   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
105   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
106     cout << "bottom data " << i << " " << this->blob_bottom_->cpu_data()[i] << endl;
107   }
108   for (int i = 0; i < this->blob_top_->count(); ++i) {
109     cout << "top data " << i << " " << this->blob_top_->cpu_data()[i] << endl;
110   }  
112   for (int i = 0; i < this->blob_top_->count(); ++i) {
113     this->blob_top_->mutable_cpu_diff()[i] = 1.;
114   }
115   layer.Backward(this->blob_top_vec_, true, &(this->blob_bottom_vec_));
116   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
117     cout << "bottom diff " << i << " " << this->blob_bottom_->cpu_diff()[i] << endl;
118   }  
120 */
122 TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) {
123   LayerParameter layer_param;
124   layer_param.set_kernelsize(3);
125   layer_param.set_stride(2);
126   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
127   Caffe::set_mode(Caffe::CPU);
128   PoolingLayer<TypeParam> layer(layer_param);
129   GradientChecker<TypeParam> checker(1e-4, 1e-2);
130   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
133 TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) {
134   LayerParameter layer_param;
135   layer_param.set_kernelsize(3);
136   layer_param.set_stride(2);
137   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
138   Caffe::set_mode(Caffe::GPU);
139   PoolingLayer<TypeParam> layer(layer_param);
140   GradientChecker<TypeParam> checker(1e-4, 1e-2);
141   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
145 TYPED_TEST(PoolingLayerTest, TestCPUGradientAve) {
146   LayerParameter layer_param;
147   layer_param.set_kernelsize(3);
148   layer_param.set_stride(2);
149   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
150   Caffe::set_mode(Caffe::CPU);
151   PoolingLayer<TypeParam> layer(layer_param);
152   GradientChecker<TypeParam> checker(1e-2, 1e-2);
153   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
157 TYPED_TEST(PoolingLayerTest, TestGPUGradientAve) {
158   LayerParameter layer_param;
159   layer_param.set_kernelsize(3);
160   layer_param.set_stride(2);
161   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
162   Caffe::set_mode(Caffe::GPU);
163   PoolingLayer<TypeParam> layer(layer_param);
164   GradientChecker<TypeParam> checker(1e-2, 1e-2);
165   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);