forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_blob.cpp
More file actions
294 lines (263 loc) · 9.32 KB
/
test_blob.cpp
File metadata and controls
294 lines (263 loc) · 9.32 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <cstring>
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/test/test_caffe_main.hpp"
namespace caffe {
template <typename Dtype>
class BlobSimpleTest : public ::testing::Test {
protected:
BlobSimpleTest()
: blob_(new Blob<Dtype>()),
blob_preshaped_(new Blob<Dtype>(2, 3, 4, 5)) {}
virtual ~BlobSimpleTest() { delete blob_; delete blob_preshaped_; }
Blob<Dtype>* const blob_;
Blob<Dtype>* const blob_preshaped_;
};
TYPED_TEST_CASE(BlobSimpleTest, TestDtypes);
TYPED_TEST(BlobSimpleTest, TestInitialization) {
EXPECT_TRUE(this->blob_);
EXPECT_TRUE(this->blob_preshaped_);
EXPECT_EQ(this->blob_preshaped_->num(), 2);
EXPECT_EQ(this->blob_preshaped_->channels(), 3);
EXPECT_EQ(this->blob_preshaped_->height(), 4);
EXPECT_EQ(this->blob_preshaped_->width(), 5);
EXPECT_EQ(this->blob_preshaped_->count(), 120);
EXPECT_EQ(this->blob_->num_axes(), 0);
EXPECT_EQ(this->blob_->count(), 0);
}
TYPED_TEST(BlobSimpleTest, TestPointersCPUGPU) {
EXPECT_TRUE(this->blob_preshaped_->gpu_data());
EXPECT_TRUE(this->blob_preshaped_->cpu_data());
EXPECT_TRUE(this->blob_preshaped_->mutable_gpu_data());
EXPECT_TRUE(this->blob_preshaped_->mutable_cpu_data());
}
TYPED_TEST(BlobSimpleTest, TestReshape) {
this->blob_->Reshape(2, 3, 4, 5);
EXPECT_EQ(this->blob_->num(), 2);
EXPECT_EQ(this->blob_->channels(), 3);
EXPECT_EQ(this->blob_->height(), 4);
EXPECT_EQ(this->blob_->width(), 5);
EXPECT_EQ(this->blob_->count(), 120);
}
TYPED_TEST(BlobSimpleTest, TestLegacyBlobProtoShapeEquals) {
BlobProto blob_proto;
// Reshape to (3 x 2).
vector<int> shape(2);
shape[0] = 3;
shape[1] = 2;
this->blob_->Reshape(shape);
// (3 x 2) blob == (1 x 1 x 3 x 2) legacy blob
blob_proto.set_num(1);
blob_proto.set_channels(1);
blob_proto.set_height(3);
blob_proto.set_width(2);
EXPECT_TRUE(this->blob_->ShapeEquals(blob_proto));
// (3 x 2) blob != (0 x 1 x 3 x 2) legacy blob
blob_proto.set_num(0);
blob_proto.set_channels(1);
blob_proto.set_height(3);
blob_proto.set_width(2);
EXPECT_FALSE(this->blob_->ShapeEquals(blob_proto));
// (3 x 2) blob != (3 x 1 x 3 x 2) legacy blob
blob_proto.set_num(3);
blob_proto.set_channels(1);
blob_proto.set_height(3);
blob_proto.set_width(2);
EXPECT_FALSE(this->blob_->ShapeEquals(blob_proto));
// Reshape to (1 x 3 x 2).
shape.insert(shape.begin(), 1);
this->blob_->Reshape(shape);
// (1 x 3 x 2) blob == (1 x 1 x 3 x 2) legacy blob
blob_proto.set_num(1);
blob_proto.set_channels(1);
blob_proto.set_height(3);
blob_proto.set_width(2);
EXPECT_TRUE(this->blob_->ShapeEquals(blob_proto));
// Reshape to (2 x 3 x 2).
shape[0] = 2;
this->blob_->Reshape(shape);
// (2 x 3 x 2) blob != (1 x 1 x 3 x 2) legacy blob
blob_proto.set_num(1);
blob_proto.set_channels(1);
blob_proto.set_height(3);
blob_proto.set_width(2);
EXPECT_FALSE(this->blob_->ShapeEquals(blob_proto));
}
template <typename TypeParam>
class BlobMathTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
BlobMathTest()
: blob_(new Blob<Dtype>(2, 3, 4, 5)),
epsilon_(1e-6) {}
virtual ~BlobMathTest() { delete blob_; }
Blob<Dtype>* const blob_;
Dtype epsilon_;
};
TYPED_TEST_CASE(BlobMathTest, TestDtypesAndDevices);
TYPED_TEST(BlobMathTest, TestSumOfSquares) {
typedef typename TypeParam::Dtype Dtype;
// Uninitialized Blob should have sum of squares == 0.
EXPECT_EQ(0, this->blob_->sumsq_data());
EXPECT_EQ(0, this->blob_->sumsq_diff());
FillerParameter filler_param;
filler_param.set_min(-3);
filler_param.set_max(3);
UniformFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_);
Dtype expected_sumsq = 0;
const Dtype* data = this->blob_->cpu_data();
for (int i = 0; i < this->blob_->count(); ++i) {
expected_sumsq += data[i] * data[i];
}
// Do a mutable access on the current device,
// so that the sumsq computation is done on that device.
// (Otherwise, this would only check the CPU sumsq implementation.)
switch (TypeParam::device) {
case Caffe::CPU:
this->blob_->mutable_cpu_data();
break;
case Caffe::GPU:
this->blob_->mutable_gpu_data();
break;
default:
LOG(FATAL) << "Unknown device: " << TypeParam::device;
}
EXPECT_NEAR(expected_sumsq, this->blob_->sumsq_data(),
this->epsilon_ * expected_sumsq);
EXPECT_EQ(0, this->blob_->sumsq_diff());
// Check sumsq_diff too.
const Dtype kDiffScaleFactor = 7;
caffe_cpu_scale(this->blob_->count(), kDiffScaleFactor, data,
this->blob_->mutable_cpu_diff());
switch (TypeParam::device) {
case Caffe::CPU:
this->blob_->mutable_cpu_diff();
break;
case Caffe::GPU:
this->blob_->mutable_gpu_diff();
break;
default:
LOG(FATAL) << "Unknown device: " << TypeParam::device;
}
EXPECT_NEAR(expected_sumsq, this->blob_->sumsq_data(),
this->epsilon_ * expected_sumsq);
const Dtype expected_sumsq_diff =
expected_sumsq * kDiffScaleFactor * kDiffScaleFactor;
EXPECT_NEAR(expected_sumsq_diff, this->blob_->sumsq_diff(),
this->epsilon_ * expected_sumsq_diff);
}
TYPED_TEST(BlobMathTest, TestAsum) {
typedef typename TypeParam::Dtype Dtype;
// Uninitialized Blob should have asum == 0.
EXPECT_EQ(0, this->blob_->asum_data());
EXPECT_EQ(0, this->blob_->asum_diff());
FillerParameter filler_param;
filler_param.set_min(-3);
filler_param.set_max(3);
UniformFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_);
Dtype expected_asum = 0;
const Dtype* data = this->blob_->cpu_data();
for (int i = 0; i < this->blob_->count(); ++i) {
expected_asum += std::fabs(data[i]);
}
// Do a mutable access on the current device,
// so that the asum computation is done on that device.
// (Otherwise, this would only check the CPU asum implementation.)
switch (TypeParam::device) {
case Caffe::CPU:
this->blob_->mutable_cpu_data();
break;
case Caffe::GPU:
this->blob_->mutable_gpu_data();
break;
default:
LOG(FATAL) << "Unknown device: " << TypeParam::device;
}
EXPECT_NEAR(expected_asum, this->blob_->asum_data(),
this->epsilon_ * expected_asum);
EXPECT_EQ(0, this->blob_->asum_diff());
// Check asum_diff too.
const Dtype kDiffScaleFactor = 7;
caffe_cpu_scale(this->blob_->count(), kDiffScaleFactor, data,
this->blob_->mutable_cpu_diff());
switch (TypeParam::device) {
case Caffe::CPU:
this->blob_->mutable_cpu_diff();
break;
case Caffe::GPU:
this->blob_->mutable_gpu_diff();
break;
default:
LOG(FATAL) << "Unknown device: " << TypeParam::device;
}
EXPECT_NEAR(expected_asum, this->blob_->asum_data(),
this->epsilon_ * expected_asum);
const Dtype expected_diff_asum = expected_asum * kDiffScaleFactor;
EXPECT_NEAR(expected_diff_asum, this->blob_->asum_diff(),
this->epsilon_ * expected_diff_asum);
}
TYPED_TEST(BlobMathTest, TestScaleData) {
typedef typename TypeParam::Dtype Dtype;
EXPECT_EQ(0, this->blob_->asum_data());
EXPECT_EQ(0, this->blob_->asum_diff());
FillerParameter filler_param;
filler_param.set_min(-3);
filler_param.set_max(3);
UniformFiller<Dtype> filler(filler_param);
filler.Fill(this->blob_);
const Dtype asum_before_scale = this->blob_->asum_data();
// Do a mutable access on the current device,
// so that the asum computation is done on that device.
// (Otherwise, this would only check the CPU asum implementation.)
switch (TypeParam::device) {
case Caffe::CPU:
this->blob_->mutable_cpu_data();
break;
case Caffe::GPU:
this->blob_->mutable_gpu_data();
break;
default:
LOG(FATAL) << "Unknown device: " << TypeParam::device;
}
const Dtype kDataScaleFactor = 3;
this->blob_->scale_data(kDataScaleFactor);
EXPECT_NEAR(asum_before_scale * kDataScaleFactor, this->blob_->asum_data(),
this->epsilon_ * asum_before_scale * kDataScaleFactor);
EXPECT_EQ(0, this->blob_->asum_diff());
// Check scale_diff too.
const Dtype kDataToDiffScaleFactor = 7;
const Dtype* data = this->blob_->cpu_data();
caffe_cpu_scale(this->blob_->count(), kDataToDiffScaleFactor, data,
this->blob_->mutable_cpu_diff());
const Dtype expected_asum_before_scale = asum_before_scale * kDataScaleFactor;
EXPECT_NEAR(expected_asum_before_scale, this->blob_->asum_data(),
this->epsilon_ * expected_asum_before_scale);
const Dtype expected_diff_asum_before_scale =
asum_before_scale * kDataScaleFactor * kDataToDiffScaleFactor;
EXPECT_NEAR(expected_diff_asum_before_scale, this->blob_->asum_diff(),
this->epsilon_ * expected_diff_asum_before_scale);
switch (TypeParam::device) {
case Caffe::CPU:
this->blob_->mutable_cpu_diff();
break;
case Caffe::GPU:
this->blob_->mutable_gpu_diff();
break;
default:
LOG(FATAL) << "Unknown device: " << TypeParam::device;
}
const Dtype kDiffScaleFactor = 3;
this->blob_->scale_diff(kDiffScaleFactor);
EXPECT_NEAR(asum_before_scale * kDataScaleFactor, this->blob_->asum_data(),
this->epsilon_ * asum_before_scale * kDataScaleFactor);
const Dtype expected_diff_asum =
expected_diff_asum_before_scale * kDiffScaleFactor;
EXPECT_NEAR(expected_diff_asum, this->blob_->asum_diff(),
this->epsilon_ * expected_diff_asum);
}
} // namespace caffe