forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_spp_layer.cpp
More file actions
131 lines (116 loc) · 4.58 KB
/
test_spp_layer.cpp
File metadata and controls
131 lines (116 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <algorithm>
#include <cstring>
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/vision_layers.hpp"
#include "caffe/test/test_caffe_main.hpp"
#include "caffe/test/test_gradient_check_util.hpp"
namespace caffe {
template <typename TypeParam>
class SPPLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
SPPLayerTest()
: blob_bottom_(new Blob<Dtype>()),
blob_bottom_2_(new Blob<Dtype>()),
blob_bottom_3_(new Blob<Dtype>()),
blob_top_(new Blob<Dtype>()) {}
virtual void SetUp() {
Caffe::set_random_seed(1701);
blob_bottom_->Reshape(2, 3, 9, 8);
blob_bottom_2_->Reshape(4, 3, 1024, 765);
blob_bottom_3_->Reshape(10, 3, 7, 7);
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_bottom_vec_2_.push_back(blob_bottom_2_);
blob_bottom_vec_3_.push_back(blob_bottom_3_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~SPPLayerTest() { delete blob_bottom_; delete blob_top_; }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_bottom_2_;
Blob<Dtype>* const blob_bottom_3_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_bottom_vec_2_;
vector<Blob<Dtype>*> blob_bottom_vec_3_;
vector<Blob<Dtype>*> blob_top_vec_;
};
TYPED_TEST_CASE(SPPLayerTest, TestDtypesAndDevices);
TYPED_TEST(SPPLayerTest, TestSetup) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
layer_param.mutable_spp_param()->set_pyramid_height(3);
SPPLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
// expected number of pool results is geometric sum
// (1 - r ** n)/(1 - r) where r = 4 and n = pyramid_height
// (1 - 4 ** 3)/(1 - 4) = 21
// multiply bottom num_channels * expected_pool_results
// to get expected num_channels (3 * 21 = 63)
EXPECT_EQ(this->blob_top_->num(), 2);
EXPECT_EQ(this->blob_top_->channels(), 63);
EXPECT_EQ(this->blob_top_->height(), 1);
EXPECT_EQ(this->blob_top_->width(), 1);
}
TYPED_TEST(SPPLayerTest, TestEqualOutputDims) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
layer_param.mutable_spp_param()->set_pyramid_height(5);
SPPLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_2_, this->blob_top_vec_);
// expected number of pool results is geometric sum
// (1 - r ** n)/(1 - r) where r = 4 and n = pyramid_height
// (1 - 4 ** 5)/(1 - 4) = 341
// multiply bottom num_channels * expected_pool_results
// to get expected num_channels (3 * 341 = 1023)
EXPECT_EQ(this->blob_top_->num(), 4);
EXPECT_EQ(this->blob_top_->channels(), 1023);
EXPECT_EQ(this->blob_top_->height(), 1);
EXPECT_EQ(this->blob_top_->width(), 1);
}
TYPED_TEST(SPPLayerTest, TestEqualOutputDims2) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
layer_param.mutable_spp_param()->set_pyramid_height(3);
SPPLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_3_, this->blob_top_vec_);
// expected number of pool results is geometric sum
// (1 - r ** n)/(1 - r) where r = 4 and n = pyramid_height
// (1 - 4 ** 3)/(1 - 4) = 21
// multiply bottom num_channels * expected_pool_results
// to get expected num_channels (3 * 21 = 63)
EXPECT_EQ(this->blob_top_->num(), 10);
EXPECT_EQ(this->blob_top_->channels(), 63);
EXPECT_EQ(this->blob_top_->height(), 1);
EXPECT_EQ(this->blob_top_->width(), 1);
}
TYPED_TEST(SPPLayerTest, TestForwardBackward) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
layer_param.mutable_spp_param()->set_pyramid_height(3);
SPPLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
vector<bool> propagate_down(this->blob_bottom_vec_.size(), true);
layer.Backward(this->blob_top_vec_, propagate_down,
this->blob_bottom_vec_);
}
TYPED_TEST(SPPLayerTest, TestGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
SPPParameter* spp_param = layer_param.mutable_spp_param();
spp_param->set_pyramid_height(3);
SPPLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-4, 1e-2);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
checker.CheckGradientExhaustive(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
} // namespace caffe