forked from OAID/Caffe-HRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch_norm_layer.cpp
More file actions
133 lines (113 loc) · 3.93 KB
/
Copy pathtest_batch_norm_layer.cpp
File metadata and controls
133 lines (113 loc) · 3.93 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
132
133
#include <algorithm>
#include <cstring>
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/layers/batch_norm_layer.hpp"
#include "caffe/test/test_caffe_main.hpp"
#include "caffe/test/test_gradient_check_util.hpp"
#define BATCH_SIZE 2
#define INPUT_DATA_SIZE 3
namespace caffe {
template <typename TypeParam>
class BatchNormLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
BatchNormLayerTest()
: blob_bottom_(new Blob<Dtype>(5, 2, 3, 4)),
blob_top_(new Blob<Dtype>()) {
// fill the values
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_bottom_);
blob_bottom_vec_.push_back(blob_bottom_);
blob_top_vec_.push_back(blob_top_);
}
virtual ~BatchNormLayerTest() { delete blob_bottom_; delete blob_top_; }
Blob<Dtype>* const blob_bottom_;
Blob<Dtype>* const blob_top_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
};
TYPED_TEST_CASE(BatchNormLayerTest, TestDtypesAndDevices);
TYPED_TEST(BatchNormLayerTest, TestForward) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
BatchNormLayer<Dtype> layer(layer_param);
layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
// Test mean
int num = this->blob_bottom_->num();
int channels = this->blob_bottom_->channels();
int height = this->blob_bottom_->height();
int width = this->blob_bottom_->width();
for (int j = 0; j < channels; ++j) {
Dtype sum = 0, var = 0;
for (int i = 0; i < num; ++i) {
for ( int k = 0; k < height; ++k ) {
for ( int l = 0; l < width; ++l ) {
Dtype data = this->blob_top_->data_at(i, j, k, l);
sum += data;
var += data * data;
}
}
}
sum /= height * width * num;
var /= height * width * num;
const Dtype kErrorBound = 0.001;
// expect zero mean
EXPECT_NEAR(0, sum, kErrorBound);
// expect unit variance
EXPECT_NEAR(1, var, kErrorBound);
}
}
TYPED_TEST(BatchNormLayerTest, TestForwardInplace) {
typedef typename TypeParam::Dtype Dtype;
Blob<Dtype> blob_inplace(5, 2, 3, 4);
vector<Blob<Dtype>*> blob_bottom_vec;
vector<Blob<Dtype>*> blob_top_vec;
LayerParameter layer_param;
FillerParameter filler_param;
GaussianFiller<Dtype> filler(filler_param);
filler.Fill(&blob_inplace);
blob_bottom_vec.push_back(&blob_inplace);
blob_top_vec.push_back(&blob_inplace);
BatchNormLayer<Dtype> layer(layer_param);
layer.SetUp(blob_bottom_vec, blob_top_vec);
layer.Forward(blob_bottom_vec, blob_top_vec);
// Test mean
int num = blob_inplace.num();
int channels = blob_inplace.channels();
int height = blob_inplace.height();
int width = blob_inplace.width();
for (int j = 0; j < channels; ++j) {
Dtype sum = 0, var = 0;
for (int i = 0; i < num; ++i) {
for ( int k = 0; k < height; ++k ) {
for ( int l = 0; l < width; ++l ) {
Dtype data = blob_inplace.data_at(i, j, k, l);
sum += data;
var += data * data;
}
}
}
sum /= height * width * num;
var /= height * width * num;
const Dtype kErrorBound = 0.001;
// expect zero mean
EXPECT_NEAR(0, sum, kErrorBound);
// expect unit variance
EXPECT_NEAR(1, var, kErrorBound);
}
}
TYPED_TEST(BatchNormLayerTest, TestGradient) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter layer_param;
BatchNormLayer<Dtype> layer(layer_param);
GradientChecker<Dtype> checker(1e-2, 1e-4);
checker.CheckGradientExhaustive(&layer, this->blob_bottom_vec_,
this->blob_top_vec_);
}
} // namespace caffe