/
test_data_transformer.cpp
344 lines (303 loc) · 11.1 KB
/
test_data_transformer.cpp
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#ifdef USE_OPENCV
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/data_transformer.hpp"
#include "caffe/filler.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
#include "caffe/test/test_caffe_main.hpp"
namespace caffe {
void FillDatum(const int label, const int channels, const int height,
const int width, const bool unique_pixels, Datum * datum) {
datum->set_label(label);
datum->set_channels(channels);
datum->set_height(height);
datum->set_width(width);
int size = channels * height * width;
std::string* data = datum->mutable_data();
for (int j = 0; j < size; ++j) {
int datum = unique_pixels ? j : label;
data->push_back(static_cast<uint8_t>(datum));
}
}
template <typename Dtype>
class DataTransformTest : public ::testing::Test {
protected:
DataTransformTest()
: seed_(1701),
num_iter_(10) {}
int NumSequenceMatches(const TransformationParameter transform_param,
const Datum& datum, Phase phase) {
// Get crop sequence with Caffe seed 1701.
DataTransformer<Dtype> transformer(transform_param, phase);
const int crop_size = transform_param.crop_size();
Caffe::set_random_seed(seed_);
transformer.InitRand();
Blob<Dtype> blob(1, datum.channels(), datum.height(), datum.width());
if (transform_param.crop_size() > 0) {
blob.Reshape(1, datum.channels(), crop_size, crop_size);
}
vector<vector<Dtype> > crop_sequence;
for (int iter = 0; iter < this->num_iter_; ++iter) {
vector<Dtype> iter_crop_sequence;
transformer.Transform(datum, &blob);
for (int j = 0; j < blob.count(); ++j) {
iter_crop_sequence.push_back(blob.cpu_data()[j]);
}
crop_sequence.push_back(iter_crop_sequence);
}
// Check if the sequence differs from the previous
int num_sequence_matches = 0;
for (int iter = 0; iter < this->num_iter_; ++iter) {
vector<Dtype> iter_crop_sequence = crop_sequence[iter];
transformer.Transform(datum, &blob);
for (int j = 0; j < blob.count(); ++j) {
num_sequence_matches += (crop_sequence[iter][j] == blob.cpu_data()[j]);
}
}
return num_sequence_matches;
}
int seed_;
int num_iter_;
};
TYPED_TEST_CASE(DataTransformTest, TestDtypes);
TYPED_TEST(DataTransformTest, TestEmptyTransform) {
TransformationParameter transform_param;
const bool unique_pixels = false; // all pixels the same equal to label
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
Blob<TypeParam> blob(1, channels, height, width);
DataTransformer<TypeParam> transformer(transform_param, TEST);
transformer.InitRand();
transformer.Transform(datum, &blob);
EXPECT_EQ(blob.num(), 1);
EXPECT_EQ(blob.channels(), datum.channels());
EXPECT_EQ(blob.height(), datum.height());
EXPECT_EQ(blob.width(), datum.width());
for (int j = 0; j < blob.count(); ++j) {
EXPECT_EQ(blob.cpu_data()[j], label);
}
}
TYPED_TEST(DataTransformTest, TestEmptyTransformUniquePixels) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
Blob<TypeParam> blob(1, 3, 4, 5);
DataTransformer<TypeParam> transformer(transform_param, TEST);
transformer.InitRand();
transformer.Transform(datum, &blob);
EXPECT_EQ(blob.num(), 1);
EXPECT_EQ(blob.channels(), datum.channels());
EXPECT_EQ(blob.height(), datum.height());
EXPECT_EQ(blob.width(), datum.width());
for (int j = 0; j < blob.count(); ++j) {
EXPECT_EQ(blob.cpu_data()[j], j);
}
}
TYPED_TEST(DataTransformTest, TestCropSize) {
TransformationParameter transform_param;
const bool unique_pixels = false; // all pixels the same equal to label
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int crop_size = 2;
transform_param.set_crop_size(crop_size);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
DataTransformer<TypeParam> transformer(transform_param, TEST);
transformer.InitRand();
Blob<TypeParam> blob(1, channels, crop_size, crop_size);
for (int iter = 0; iter < this->num_iter_; ++iter) {
transformer.Transform(datum, &blob);
EXPECT_EQ(blob.num(), 1);
EXPECT_EQ(blob.channels(), datum.channels());
EXPECT_EQ(blob.height(), crop_size);
EXPECT_EQ(blob.width(), crop_size);
for (int j = 0; j < blob.count(); ++j) {
EXPECT_EQ(blob.cpu_data()[j], label);
}
}
}
TYPED_TEST(DataTransformTest, TestCropTrain) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int crop_size = 2;
const int size = channels * crop_size * crop_size;
transform_param.set_crop_size(crop_size);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
int num_matches = this->NumSequenceMatches(transform_param, datum, TRAIN);
EXPECT_LT(num_matches, size * this->num_iter_);
}
TYPED_TEST(DataTransformTest, TestCropTest) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int crop_size = 2;
const int size = channels * crop_size * crop_size;
transform_param.set_crop_size(crop_size);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
int num_matches = this->NumSequenceMatches(transform_param, datum, TEST);
EXPECT_EQ(num_matches, size * this->num_iter_);
}
TYPED_TEST(DataTransformTest, TestMirrorTrain) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int size = channels * height * width;
transform_param.set_mirror(true);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
int num_matches = this->NumSequenceMatches(transform_param, datum, TRAIN);
EXPECT_LT(num_matches, size * this->num_iter_);
}
TYPED_TEST(DataTransformTest, TestMirrorTest) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int size = channels * height * width;
transform_param.set_mirror(true);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
int num_matches = this->NumSequenceMatches(transform_param, datum, TEST);
EXPECT_LT(num_matches, size * this->num_iter_);
}
TYPED_TEST(DataTransformTest, TestCropMirrorTrain) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int crop_size = 2;
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
transform_param.set_crop_size(crop_size);
int num_matches_crop = this->NumSequenceMatches(
transform_param, datum, TRAIN);
transform_param.set_mirror(true);
int num_matches_crop_mirror =
this->NumSequenceMatches(transform_param, datum, TRAIN);
// When doing crop and mirror we expect less num_matches than just crop
EXPECT_LE(num_matches_crop_mirror, num_matches_crop);
}
TYPED_TEST(DataTransformTest, TestCropMirrorTest) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int crop_size = 2;
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
transform_param.set_crop_size(crop_size);
int num_matches_crop = this->NumSequenceMatches(transform_param, datum, TEST);
transform_param.set_mirror(true);
int num_matches_crop_mirror =
this->NumSequenceMatches(transform_param, datum, TEST);
// When doing crop and mirror we expect less num_matches than just crop
EXPECT_LT(num_matches_crop_mirror, num_matches_crop);
}
TYPED_TEST(DataTransformTest, TestMeanValue) {
TransformationParameter transform_param;
const bool unique_pixels = false; // pixels are equal to label
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int mean_value = 2;
transform_param.add_mean_value(mean_value);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
Blob<TypeParam> blob(1, channels, height, width);
DataTransformer<TypeParam> transformer(transform_param, TEST);
transformer.InitRand();
transformer.Transform(datum, &blob);
for (int j = 0; j < blob.count(); ++j) {
EXPECT_EQ(blob.cpu_data()[j], label - mean_value);
}
}
TYPED_TEST(DataTransformTest, TestMeanValues) {
TransformationParameter transform_param;
const bool unique_pixels = false; // pixels are equal to label
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
transform_param.add_mean_value(0);
transform_param.add_mean_value(1);
transform_param.add_mean_value(2);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
Blob<TypeParam> blob(1, channels, height, width);
DataTransformer<TypeParam> transformer(transform_param, TEST);
transformer.InitRand();
transformer.Transform(datum, &blob);
for (int c = 0; c < channels; ++c) {
for (int j = 0; j < height * width; ++j) {
EXPECT_EQ(blob.cpu_data()[blob.offset(0, c) + j], label - c);
}
}
}
TYPED_TEST(DataTransformTest, TestMeanFile) {
TransformationParameter transform_param;
const bool unique_pixels = true; // pixels are consecutive ints [0,size]
const int label = 0;
const int channels = 3;
const int height = 4;
const int width = 5;
const int size = channels * height * width;
// Create a mean file
string mean_file;
MakeTempFilename(&mean_file);
BlobProto blob_mean;
blob_mean.set_num(1);
blob_mean.set_channels(channels);
blob_mean.set_height(height);
blob_mean.set_width(width);
for (int j = 0; j < size; ++j) {
blob_mean.add_data(j);
}
LOG(INFO) << "Using temporary mean_file " << mean_file;
WriteProtoToBinaryFile(blob_mean, mean_file);
transform_param.set_mean_file(mean_file);
Datum datum;
FillDatum(label, channels, height, width, unique_pixels, &datum);
Blob<TypeParam> blob(1, channels, height, width);
DataTransformer<TypeParam> transformer(transform_param, TEST);
transformer.InitRand();
transformer.Transform(datum, &blob);
for (int j = 0; j < blob.count(); ++j) {
EXPECT_EQ(blob.cpu_data()[j], 0);
}
}
} // namespace caffe
#endif // USE_OPENCV