a5d0c9fb2294fadf20a2c9c41a78a98f69c83676
[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 /*
60 TYPED_TEST(PoolingLayerTest, PrintGPUBackward) {
61   LayerParameter layer_param;
62   layer_param.set_kernelsize(3);
63   layer_param.set_stride(2);
64   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
65   Caffe::set_mode(Caffe::GPU);
66   PoolingLayer<TypeParam> layer(layer_param);
67   layer.SetUp(this->blob_bottom_vec_, &(this->blob_top_vec_));
68   layer.Forward(this->blob_bottom_vec_, &(this->blob_top_vec_));
69   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
70     cout << "bottom data " << i << " " << this->blob_bottom_->cpu_data()[i] << endl;
71   }
72   for (int i = 0; i < this->blob_top_->count(); ++i) {
73     cout << "top data " << i << " " << this->blob_top_->cpu_data()[i] << endl;
74   }  
76   for (int i = 0; i < this->blob_top_->count(); ++i) {
77     this->blob_top_->mutable_cpu_diff()[i] = 1.;
78   }
79   layer.Backward(this->blob_top_vec_, true, &(this->blob_bottom_vec_));
80   for (int i = 0; i < this->blob_bottom_->count(); ++i) {
81     cout << "bottom diff " << i << " " << this->blob_bottom_->cpu_diff()[i] << endl;
82   }  
83 }
84 */
86 TYPED_TEST(PoolingLayerTest, TestCPUGradientMax) {
87   LayerParameter layer_param;
88   layer_param.set_kernelsize(3);
89   layer_param.set_stride(2);
90   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
91   Caffe::set_mode(Caffe::CPU);
92   PoolingLayer<TypeParam> layer(layer_param);
93   GradientChecker<TypeParam> checker(1e-4, 1e-2);
94   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
95 }
97 TYPED_TEST(PoolingLayerTest, TestGPUGradientMax) {
98   LayerParameter layer_param;
99   layer_param.set_kernelsize(3);
100   layer_param.set_stride(2);
101   layer_param.set_pool(LayerParameter_PoolMethod_MAX);
102   Caffe::set_mode(Caffe::GPU);
103   PoolingLayer<TypeParam> layer(layer_param);
104   GradientChecker<TypeParam> checker(1e-4, 1e-2);
105   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
109 TYPED_TEST(PoolingLayerTest, TestCPUGradientAve) {
110   LayerParameter layer_param;
111   layer_param.set_kernelsize(3);
112   layer_param.set_stride(2);
113   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
114   Caffe::set_mode(Caffe::CPU);
115   PoolingLayer<TypeParam> layer(layer_param);
116   GradientChecker<TypeParam> checker(1e-2, 1e-2);
117   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);
121 TYPED_TEST(PoolingLayerTest, TestGPUGradientAve) {
122   LayerParameter layer_param;
123   layer_param.set_kernelsize(3);
124   layer_param.set_stride(2);
125   layer_param.set_pool(LayerParameter_PoolMethod_AVE);
126   Caffe::set_mode(Caffe::GPU);
127   PoolingLayer<TypeParam> layer(layer_param);
128   GradientChecker<TypeParam> checker(1e-2, 1e-2);
129   checker.CheckGradientExhaustive(layer, this->blob_bottom_vec_, this->blob_top_vec_);