forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_image_data_layer.cpp
More file actions
144 lines (131 loc) · 4.93 KB
/
test_image_data_layer.cpp
File metadata and controls
144 lines (131 loc) · 4.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
134
135
136
137
138
139
140
141
142
143
144
#include <map>
#include <string>
#include <vector>
#include "gtest/gtest.h"
#include "caffe/blob.hpp"
#include "caffe/common.hpp"
#include "caffe/filler.hpp"
#include "caffe/proto/caffe.pb.h"
#include "caffe/util/io.hpp"
#include "caffe/vision_layers.hpp"
#include "caffe/test/test_caffe_main.hpp"
namespace caffe {
template <typename TypeParam>
class ImageDataLayerTest : public MultiDeviceTest<TypeParam> {
typedef typename TypeParam::Dtype Dtype;
protected:
ImageDataLayerTest()
: seed_(1701),
blob_top_data_(new Blob<Dtype>()),
blob_top_label_(new Blob<Dtype>()) {}
virtual void SetUp() {
MakeTempFilename(&filename_);
blob_top_vec_.push_back(blob_top_data_);
blob_top_vec_.push_back(blob_top_label_);
Caffe::set_random_seed(seed_);
// Create a Vector of files with labels
std::ofstream outfile(filename_.c_str(), std::ofstream::out);
LOG(INFO) << "Using temporary file " << filename_;
for (int i = 0; i < 5; ++i) {
outfile << EXAMPLES_SOURCE_DIR "images/cat.jpg " << i;
}
outfile.close();
}
virtual ~ImageDataLayerTest() {
delete blob_top_data_;
delete blob_top_label_;
}
int seed_;
string filename_;
Blob<Dtype>* const blob_top_data_;
Blob<Dtype>* const blob_top_label_;
vector<Blob<Dtype>*> blob_bottom_vec_;
vector<Blob<Dtype>*> blob_top_vec_;
};
TYPED_TEST_CASE(ImageDataLayerTest, TestDtypesAndDevices);
TYPED_TEST(ImageDataLayerTest, TestRead) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(5);
image_data_param->set_source(this->filename_.c_str());
image_data_param->set_shuffle(false);
ImageDataLayer<Dtype> layer(param);
layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
EXPECT_EQ(this->blob_top_data_->num(), 5);
EXPECT_EQ(this->blob_top_data_->channels(), 3);
EXPECT_EQ(this->blob_top_data_->height(), 360);
EXPECT_EQ(this->blob_top_data_->width(), 480);
EXPECT_EQ(this->blob_top_label_->num(), 5);
EXPECT_EQ(this->blob_top_label_->channels(), 1);
EXPECT_EQ(this->blob_top_label_->height(), 1);
EXPECT_EQ(this->blob_top_label_->width(), 1);
// Go through the data twice
for (int iter = 0; iter < 2; ++iter) {
layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(i, this->blob_top_label_->cpu_data()[i]);
}
}
}
TYPED_TEST(ImageDataLayerTest, TestResize) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(5);
image_data_param->set_source(this->filename_.c_str());
image_data_param->set_new_height(256);
image_data_param->set_new_width(256);
image_data_param->set_shuffle(false);
ImageDataLayer<Dtype> layer(param);
layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
EXPECT_EQ(this->blob_top_data_->num(), 5);
EXPECT_EQ(this->blob_top_data_->channels(), 3);
EXPECT_EQ(this->blob_top_data_->height(), 256);
EXPECT_EQ(this->blob_top_data_->width(), 256);
EXPECT_EQ(this->blob_top_label_->num(), 5);
EXPECT_EQ(this->blob_top_label_->channels(), 1);
EXPECT_EQ(this->blob_top_label_->height(), 1);
EXPECT_EQ(this->blob_top_label_->width(), 1);
// Go through the data twice
for (int iter = 0; iter < 2; ++iter) {
layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
for (int i = 0; i < 5; ++i) {
EXPECT_EQ(i, this->blob_top_label_->cpu_data()[i]);
}
}
}
TYPED_TEST(ImageDataLayerTest, TestShuffle) {
typedef typename TypeParam::Dtype Dtype;
LayerParameter param;
ImageDataParameter* image_data_param = param.mutable_image_data_param();
image_data_param->set_batch_size(5);
image_data_param->set_source(this->filename_.c_str());
image_data_param->set_shuffle(true);
ImageDataLayer<Dtype> layer(param);
layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
EXPECT_EQ(this->blob_top_data_->num(), 5);
EXPECT_EQ(this->blob_top_data_->channels(), 3);
EXPECT_EQ(this->blob_top_data_->height(), 360);
EXPECT_EQ(this->blob_top_data_->width(), 480);
EXPECT_EQ(this->blob_top_label_->num(), 5);
EXPECT_EQ(this->blob_top_label_->channels(), 1);
EXPECT_EQ(this->blob_top_label_->height(), 1);
EXPECT_EQ(this->blob_top_label_->width(), 1);
// Go through the data twice
for (int iter = 0; iter < 2; ++iter) {
layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
map<Dtype, int> values_to_indices;
int num_in_order = 0;
for (int i = 0; i < 5; ++i) {
Dtype value = this->blob_top_label_->cpu_data()[i];
// Check that the value has not been seen already (no duplicates).
EXPECT_EQ(values_to_indices.find(value), values_to_indices.end());
values_to_indices[value] = i;
num_in_order += (value == Dtype(i));
}
EXPECT_EQ(5, values_to_indices.size());
EXPECT_GT(5, num_in_order);
}
}
} // namespace caffe