aeee81dd0a497ad6b202f4bdc52987607c741766
[platform/upstream/caffe.git] / src / caffe / test / test_concat_layer.cpp
1 // Copyright 2014 Sergio Guadarrama
2
3 #include <cstring>
4 #include <cuda_runtime.h>
5
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"
12
13 #include "caffe/test/test_caffe_main.hpp"
14
15 namespace caffe {
16
17 extern cudaDeviceProp CAFFE_TEST_CUDA_PROP;
18
19 template <typename Dtype>
20 class ConcatLayerTest : public ::testing::Test {
21  protected:
22   ConcatLayerTest()
23       : blob_bottom_0(new Blob<Dtype>(2, 3, 6, 5)),
24         blob_bottom_1(new Blob<Dtype>(2, 5, 6, 5)),
25         blob_bottom_2(new Blob<Dtype>(5, 3, 6, 5)),
26         blob_top_(new Blob<Dtype>()) {};
27   virtual void SetUp() {
28     // fill the values
29     FillerParameter filler_param;
30     filler_param.set_value(1.);
31     ConstantFiller<Dtype> filler(filler_param);
32     filler.Fill(this->blob_bottom_0);
33     filler_param.set_value(2.);
34     filler.Fill(this->blob_bottom_1);
35     filler_param.set_value(3.);
36     filler.Fill(this->blob_bottom_2);
37     blob_bottom_vec_0.push_back(blob_bottom_0);
38     blob_bottom_vec_0.push_back(blob_bottom_1);
39     blob_bottom_vec_1.push_back(blob_bottom_0);
40     blob_bottom_vec_1.push_back(blob_bottom_2);
41     blob_top_vec_.push_back(blob_top_);
42   };
43
44   virtual ~ConcatLayerTest() {
45     delete blob_bottom_0; delete blob_bottom_1; delete blob_bottom_2; delete blob_top_;
46   }
47
48   Blob<Dtype>* const blob_bottom_0;
49   Blob<Dtype>* const blob_bottom_1;
50   Blob<Dtype>* const blob_bottom_2;
51   Blob<Dtype>* const blob_top_;
52   vector<Blob<Dtype>*> blob_bottom_vec_0,blob_bottom_vec_1;
53   vector<Blob<Dtype>*> blob_top_vec_;
54 };
55
56 typedef ::testing::Types<float, double> Dtypes;
57 TYPED_TEST_CASE(ConcatLayerTest, Dtypes);
58
59 TYPED_TEST(ConcatLayerTest, TestSetupNum) {
60   LayerParameter layer_param;
61   layer_param.set_concat_dim(0);
62   ConcatLayer<TypeParam> layer(layer_param);
63   layer.SetUp(this->blob_bottom_vec_1, &(this->blob_top_vec_));
64   EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_0->num()+this->blob_bottom_2->num());
65   EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_0->channels());
66   EXPECT_EQ(this->blob_top_->height(), this->blob_bottom_0->height());
67   EXPECT_EQ(this->blob_top_->width(), this->blob_bottom_0->width());
68 }
69
70 TYPED_TEST(ConcatLayerTest, TestSetupChannels) {
71   LayerParameter layer_param;
72   ConcatLayer<TypeParam> layer(layer_param);
73   layer.SetUp(this->blob_bottom_vec_0, &(this->blob_top_vec_));
74   EXPECT_EQ(this->blob_top_->num(), this->blob_bottom_0->num());
75   EXPECT_EQ(this->blob_top_->channels(), this->blob_bottom_0->channels()+this->blob_bottom_1->channels());
76   EXPECT_EQ(this->blob_top_->height(), this->blob_bottom_0->height());
77   EXPECT_EQ(this->blob_top_->width(), this->blob_bottom_0->width());
78 }
79
80
81 TYPED_TEST(ConcatLayerTest, TestCPUNum) {
82   LayerParameter layer_param;
83   ConcatLayer<TypeParam> layer(layer_param);
84   Caffe::set_mode(Caffe::CPU);
85   layer.SetUp(this->blob_bottom_vec_0, &(this->blob_top_vec_));
86   layer.Forward(this->blob_bottom_vec_0, &(this->blob_top_vec_));
87   for (int n = 0; n < this->blob_top_->num(); ++n) {
88     for (int c = 0; c < this->blob_bottom_0->channels(); ++c) {
89       for (int h = 0; h < this->blob_top_->height(); ++h) {
90         for (int w = 0; w < this->blob_top_->width(); ++w) {
91           EXPECT_EQ(this->blob_top_->data_at(n, c, h, w), this->blob_bottom_vec_0[0]->data_at(n, c, h, w));
92         }
93       }
94     }
95     for (int c = 0; c < this->blob_bottom_1->channels(); ++c) {
96       for (int h = 0; h < this->blob_top_->height(); ++h) {
97         for (int w = 0; w < this->blob_top_->width(); ++w) {
98           EXPECT_EQ(this->blob_top_->data_at(n, c+3, h, w), this->blob_bottom_vec_0[1]->data_at(n, c, h, w));
99         }
100       }
101     }
102   }
103 }
104
105
106 TYPED_TEST(ConcatLayerTest, TestCPUGradient) {
107   LayerParameter layer_param;
108   Caffe::set_mode(Caffe::CPU);
109   ConcatLayer<TypeParam> layer(layer_param);
110   GradientChecker<TypeParam> checker(1e-2, 1e-3);
111   // it is too expensive to call curand multiple times, so we don't do an
112   // exhaustive gradient check.
113   checker.CheckGradient(&layer, &(this->blob_bottom_vec_0),
114     &(this->blob_top_vec_));
115 }
116
117 TYPED_TEST(ConcatLayerTest, TestGPUGradient) {
118   LayerParameter layer_param;
119   Caffe::set_mode(Caffe::GPU);
120   ConcatLayer<TypeParam> layer(layer_param);
121   GradientChecker<TypeParam> checker(1e-2, 1e-3);
122   // it is too expensive to call curand multiple times, so we don't do an
123   // exhaustive gradient check.
124   checker.CheckGradient(&layer, &(this->blob_bottom_vec_0),
125     &(this->blob_top_vec_));
126 }
127
128
129 }