]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - jacinto-ai/caffe-jacinto.git/blob - src/caffe/test/test_pooling_layer.cpp
copyright message
[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   void ReferenceLRNForward(const Blob<Dtype>& blob_bottom,
37       const LayerParameter& layer_param, Blob<Dtype>* blob_top);
38   Blob<Dtype>* const blob_bottom_;
39   Blob<Dtype>* const blob_top_;
40   vector<Blob<Dtype>*> blob_bottom_vec_;
41   vector<Blob<Dtype>*> blob_top_vec_;
42 };
44 typedef ::testing::Types<float, double> Dtypes;
45 TYPED_TEST_CASE(PoolingLayerTest, Dtypes);
47 TYPED_TEST(PoolingLayerTest, TestSetup) {
48   LayerParameter layer_param;
49   layer_param.set_kernelsize(3);
50   layer_param.set_stride(2);
51   PoolingLayer<TypeParam> layer(layer_param);
52   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
53   EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_->num());
54   EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_->channels());
55   EXPECT_EQ(this->blob_top_->height(), 3);
56   EXPECT_EQ(this->blob_top_->width(), 2);
57 }
59 TYPED_TEST(PoolingLayerTest, TestGPUMax) {
60   LayerParameter layer_param;
61   layer_param.set_kernelsize(3);
62   layer_param.set_stride(2);
63   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
64   Caffe::set_mode(Caffe::CPU);
65   PoolingLayer<TypeParam> layer(layer_param);
66   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
67   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
68   Blob<TypeParam> blob_reference(*this->blob_top_);
69   Caffe::set_mode(Caffe::GPU);
70   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
71   for (int i = 0; i < blob_reference.count(); ++i) {
72     EXPECT_EQ(blob_reference.cpu_data()[i], this->blob_top_->cpu_data()[i])
73         << "debug: index " << i;
74   }
75 }
77 TYPED_TEST(PoolingLayerTest, TestGPUAve) {
78   LayerParameter layer_param;
79   layer_param.set_kernelsize(3);
80   layer_param.set_stride(2);
81   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
82   Caffe::set_mode(Caffe::CPU);
83   PoolingLayer<TypeParam> layer(layer_param);
84   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
85   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
86   Blob<TypeParam> blob_reference(*this->blob_top_);
87   Caffe::set_mode(Caffe::GPU);
88   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
89   for (int i = 0; i < blob_reference.count(); ++i) {
90     EXPECT_GE(blob_reference.cpu_data()[i], this->blob_top_->cpu_data()[i] - 1e-4)
91         << "debug: index " << i;
92     EXPECT_LE(blob_reference.cpu_data()[i], this->blob_top_->cpu_data()[i] + 1e-4)
93         << "debug: index " << i;
94   }
95 }
97 /*
98 TYPED_TEST(PoolingLayerTest, PrintGPUBackward) {
99   LayerParameter layer_param;
100   layer_param.set_kernelsize(3);
101   layer_param.set_stride(2);
102   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
103   Caffe::set_mode(Caffe::GPU);
104   PoolingLayer<TypeParam> layer(layer_param);
105   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
106   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
107   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
108     cout << "bottom data " << i << " " << this->blob_bottom_->cpu_data()[i] << endl;
109   }
110   for (int i = 0; i < this->blob_top_->count(); ++i) {
111     cout << "top data " << i << " " << this->blob_top_->cpu_data()[i] << endl;
112   }  
114   for (int i = 0; i < this->blob_top_->count(); ++i) {
115     this->blob_top_->mutable_cpu_diff()[i] = 1.;
116   }
117   layer.Backward(this->blob_top_vec_, true, &(this->blob_bottom_vec_));
118   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
119     cout << "bottom diff " << i << " " << this->blob_bottom_->cpu_diff()[i] << endl;
120   }  
122 */
124 TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) {
125   LayerParameter layer_param;
126   layer_param.set_kernelsize(3);
127   layer_param.set_stride(2);
128   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
129   Caffe::set_mode(Caffe::CPU);
130   PoolingLayer<TypeParam> layer(layer_param);
131   GradientChecker<TypeParam> checker(1e-4, 1e-2);
132   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
135 TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) {
136   LayerParameter layer_param;
137   layer_param.set_kernelsize(3);
138   layer_param.set_stride(2);
139   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
140   Caffe::set_mode(Caffe::GPU);
141   PoolingLayer<TypeParam> layer(layer_param);
142   GradientChecker<TypeParam> checker(1e-4, 1e-2);
143   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
147 TYPED_TEST(PoolingLayerTest, TestCPUGradientAve) {
148   LayerParameter layer_param;
149   layer_param.set_kernelsize(3);
150   layer_param.set_stride(2);
151   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
152   Caffe::set_mode(Caffe::CPU);
153   PoolingLayer<TypeParam> layer(layer_param);
154   GradientChecker<TypeParam> checker(1e-2, 1e-2);
155   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
159 TYPED_TEST(PoolingLayerTest, TestGPUGradientAve) {
160   LayerParameter layer_param;
161   layer_param.set_kernelsize(3);
162   layer_param.set_stride(2);
163   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
164   Caffe::set_mode(Caffe::GPU);
165   PoolingLayer<TypeParam> layer(layer_param);
166   GradientChecker<TypeParam> checker(1e-2, 1e-2);
167   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);