Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

test_memory_data_layer.cpp 11 KB

You have to be logged in to leave a comment. Sign In
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
  1. #ifdef USE_OPENCV
  2. #include <opencv2/core/core.hpp>
  3. #endif // USE_OPENCV
  4. #include <string>
  5. #include <vector>
  6. #include "caffe/filler.hpp"
  7. #include "caffe/layers/memory_data_layer.hpp"
  8. #include "caffe/test/test_caffe_main.hpp"
  9. namespace caffe {
  10. template <typename TypeParam>
  11. class MemoryDataLayerTest : public MultiDeviceTest<TypeParam> {
  12. typedef typename TypeParam::Dtype Dtype;
  13. protected:
  14. MemoryDataLayerTest()
  15. : data_(new Blob<Dtype>()),
  16. labels_(new Blob<Dtype>()),
  17. data_blob_(new Blob<Dtype>()),
  18. label_blob_(new Blob<Dtype>()) {}
  19. virtual void SetUp() {
  20. batch_size_ = 8;
  21. batches_ = 12;
  22. channels_ = 4;
  23. height_ = 7;
  24. width_ = 11;
  25. blob_top_vec_.push_back(data_blob_);
  26. blob_top_vec_.push_back(label_blob_);
  27. // pick random input data
  28. FillerParameter filler_param;
  29. GaussianFiller<Dtype> filler(filler_param);
  30. data_->Reshape(batches_ * batch_size_, channels_, height_, width_);
  31. labels_->Reshape(batches_ * batch_size_, 1, 1, 1);
  32. filler.Fill(this->data_);
  33. filler.Fill(this->labels_);
  34. }
  35. virtual ~MemoryDataLayerTest() {
  36. delete data_blob_;
  37. delete label_blob_;
  38. delete data_;
  39. delete labels_;
  40. }
  41. int batch_size_;
  42. int batches_;
  43. int channels_;
  44. int height_;
  45. int width_;
  46. // we don't really need blobs for the input data, but it makes it
  47. // easier to call Filler
  48. Blob<Dtype>* const data_;
  49. Blob<Dtype>* const labels_;
  50. // blobs for the top of MemoryDataLayer
  51. Blob<Dtype>* const data_blob_;
  52. Blob<Dtype>* const label_blob_;
  53. vector<Blob<Dtype>*> blob_bottom_vec_;
  54. vector<Blob<Dtype>*> blob_top_vec_;
  55. };
  56. TYPED_TEST_CASE(MemoryDataLayerTest, TestDtypesAndDevices);
  57. TYPED_TEST(MemoryDataLayerTest, TestSetup) {
  58. typedef typename TypeParam::Dtype Dtype;
  59. LayerParameter layer_param;
  60. MemoryDataParameter* md_param = layer_param.mutable_memory_data_param();
  61. md_param->set_batch_size(this->batch_size_);
  62. md_param->set_channels(this->channels_);
  63. md_param->set_height(this->height_);
  64. md_param->set_width(this->width_);
  65. shared_ptr<Layer<Dtype> > layer(
  66. new MemoryDataLayer<Dtype>(layer_param));
  67. layer->SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  68. EXPECT_EQ(this->data_blob_->num(), this->batch_size_);
  69. EXPECT_EQ(this->data_blob_->channels(), this->channels_);
  70. EXPECT_EQ(this->data_blob_->height(), this->height_);
  71. EXPECT_EQ(this->data_blob_->width(), this->width_);
  72. EXPECT_EQ(this->label_blob_->num(), this->batch_size_);
  73. EXPECT_EQ(this->label_blob_->channels(), 1);
  74. EXPECT_EQ(this->label_blob_->height(), 1);
  75. EXPECT_EQ(this->label_blob_->width(), 1);
  76. }
  77. // run through a few batches and check that the right data appears
  78. TYPED_TEST(MemoryDataLayerTest, TestForward) {
  79. typedef typename TypeParam::Dtype Dtype;
  80. LayerParameter layer_param;
  81. MemoryDataParameter* md_param = layer_param.mutable_memory_data_param();
  82. md_param->set_batch_size(this->batch_size_);
  83. md_param->set_channels(this->channels_);
  84. md_param->set_height(this->height_);
  85. md_param->set_width(this->width_);
  86. shared_ptr<MemoryDataLayer<Dtype> > layer(
  87. new MemoryDataLayer<Dtype>(layer_param));
  88. layer->DataLayerSetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  89. layer->Reset(this->data_->mutable_cpu_data(),
  90. this->labels_->mutable_cpu_data(), this->data_->num());
  91. for (int i = 0; i < this->batches_ * 6; ++i) {
  92. int batch_num = i % this->batches_;
  93. layer->Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  94. for (int j = 0; j < this->data_blob_->count(); ++j) {
  95. EXPECT_EQ(this->data_blob_->cpu_data()[j],
  96. this->data_->cpu_data()[
  97. this->data_->offset(1) * this->batch_size_ * batch_num + j]);
  98. }
  99. for (int j = 0; j < this->label_blob_->count(); ++j) {
  100. EXPECT_EQ(this->label_blob_->cpu_data()[j],
  101. this->labels_->cpu_data()[this->batch_size_ * batch_num + j]);
  102. }
  103. }
  104. }
  105. #ifdef USE_OPENCV
  106. TYPED_TEST(MemoryDataLayerTest, AddDatumVectorDefaultTransform) {
  107. typedef typename TypeParam::Dtype Dtype;
  108. LayerParameter param;
  109. MemoryDataParameter* memory_data_param = param.mutable_memory_data_param();
  110. memory_data_param->set_batch_size(this->batch_size_);
  111. memory_data_param->set_channels(this->channels_);
  112. memory_data_param->set_height(this->height_);
  113. memory_data_param->set_width(this->width_);
  114. MemoryDataLayer<Dtype> layer(param);
  115. layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  116. // We add batch_size*num_iter items, then for each iteration
  117. // we forward batch_size elements
  118. int num_iter = 5;
  119. vector<Datum> datum_vector(this->batch_size_ * num_iter);
  120. const size_t count = this->channels_ * this->height_ * this->width_;
  121. size_t pixel_index = 0;
  122. for (int i = 0; i < this->batch_size_ * num_iter; ++i) {
  123. datum_vector[i].set_channels(this->channels_);
  124. datum_vector[i].set_height(this->height_);
  125. datum_vector[i].set_width(this->width_);
  126. datum_vector[i].set_label(i);
  127. vector<char> pixels(count);
  128. for (int j = 0; j < count; ++j) {
  129. pixels[j] = pixel_index++ % 256;
  130. }
  131. datum_vector[i].set_data(&(pixels[0]), count);
  132. }
  133. layer.AddDatumVector(datum_vector);
  134. int data_index;
  135. // Go through the data 5 times
  136. for (int iter = 0; iter < num_iter; ++iter) {
  137. int offset = this->batch_size_ * iter;
  138. layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  139. const Dtype* data = this->data_blob_->cpu_data();
  140. size_t index = 0;
  141. for (int i = 0; i < this->batch_size_; ++i) {
  142. const string& data_string = datum_vector[offset + i].data();
  143. EXPECT_EQ(offset + i, this->label_blob_->cpu_data()[i]);
  144. for (int c = 0; c < this->channels_; ++c) {
  145. for (int h = 0; h < this->height_; ++h) {
  146. for (int w = 0; w < this->width_; ++w) {
  147. data_index = (c * this->height_ + h) * this->width_ + w;
  148. EXPECT_EQ(static_cast<Dtype>(
  149. static_cast<uint8_t>(data_string[data_index])),
  150. data[index++]);
  151. }
  152. }
  153. }
  154. }
  155. }
  156. }
  157. TYPED_TEST(MemoryDataLayerTest, AddMatVectorDefaultTransform) {
  158. typedef typename TypeParam::Dtype Dtype;
  159. LayerParameter param;
  160. MemoryDataParameter* memory_data_param = param.mutable_memory_data_param();
  161. memory_data_param->set_batch_size(this->batch_size_);
  162. memory_data_param->set_channels(this->channels_);
  163. memory_data_param->set_height(this->height_);
  164. memory_data_param->set_width(this->width_);
  165. MemoryDataLayer<Dtype> layer(param);
  166. layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  167. // We add batch_size*num_iter items, then for each iteration
  168. // we forward batch_size elements
  169. int num_iter = 5;
  170. vector<cv::Mat> mat_vector(this->batch_size_ * num_iter);
  171. vector<int> label_vector(this->batch_size_ * num_iter);
  172. for (int i = 0; i < this->batch_size_*num_iter; ++i) {
  173. mat_vector[i] = cv::Mat(this->height_, this->width_, CV_8UC4);
  174. label_vector[i] = i;
  175. cv::randu(mat_vector[i], cv::Scalar::all(0), cv::Scalar::all(255));
  176. }
  177. layer.AddMatVector(mat_vector, label_vector);
  178. int data_index;
  179. const size_t count = this->channels_ * this->height_ * this->width_;
  180. for (int iter = 0; iter < num_iter; ++iter) {
  181. int offset = this->batch_size_ * iter;
  182. layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  183. const Dtype* data = this->data_blob_->cpu_data();
  184. for (int i = 0; i < this->batch_size_; ++i) {
  185. EXPECT_EQ(offset + i, this->label_blob_->cpu_data()[i]);
  186. for (int h = 0; h < this->height_; ++h) {
  187. const unsigned char* ptr_mat = mat_vector[offset + i].ptr<uchar>(h);
  188. int index = 0;
  189. for (int w = 0; w < this->width_; ++w) {
  190. for (int c = 0; c < this->channels_; ++c) {
  191. data_index = (i*count) + (c * this->height_ + h) * this->width_ + w;
  192. Dtype pixel = static_cast<Dtype>(ptr_mat[index++]);
  193. EXPECT_EQ(static_cast<int>(pixel),
  194. data[data_index]);
  195. }
  196. }
  197. }
  198. }
  199. }
  200. }
  201. TYPED_TEST(MemoryDataLayerTest, TestSetBatchSize) {
  202. typedef typename TypeParam::Dtype Dtype;
  203. LayerParameter param;
  204. MemoryDataParameter* memory_data_param = param.mutable_memory_data_param();
  205. memory_data_param->set_batch_size(this->batch_size_);
  206. memory_data_param->set_channels(this->channels_);
  207. memory_data_param->set_height(this->height_);
  208. memory_data_param->set_width(this->width_);
  209. MemoryDataLayer<Dtype> layer(param);
  210. layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
  211. // first add data as usual
  212. int num_iter = 5;
  213. vector<cv::Mat> mat_vector(this->batch_size_ * num_iter);
  214. vector<int> label_vector(this->batch_size_ * num_iter);
  215. for (int i = 0; i < this->batch_size_*num_iter; ++i) {
  216. mat_vector[i] = cv::Mat(this->height_, this->width_, CV_8UC4);
  217. label_vector[i] = i;
  218. cv::randu(mat_vector[i], cv::Scalar::all(0), cv::Scalar::all(255));
  219. }
  220. layer.AddMatVector(mat_vector, label_vector);
  221. // then consume the data
  222. int data_index;
  223. const size_t count = this->channels_ * this->height_ * this->width_;
  224. for (int iter = 0; iter < num_iter; ++iter) {
  225. int offset = this->batch_size_ * iter;
  226. layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  227. const Dtype* data = this->data_blob_->cpu_data();
  228. for (int i = 0; i < this->batch_size_; ++i) {
  229. EXPECT_EQ(offset + i, this->label_blob_->cpu_data()[i]);
  230. for (int h = 0; h < this->height_; ++h) {
  231. const unsigned char* ptr_mat = mat_vector[offset + i].ptr<uchar>(h);
  232. int index = 0;
  233. for (int w = 0; w < this->width_; ++w) {
  234. for (int c = 0; c < this->channels_; ++c) {
  235. data_index = (i*count) + (c * this->height_ + h) * this->width_ + w;
  236. Dtype pixel = static_cast<Dtype>(ptr_mat[index++]);
  237. EXPECT_EQ(static_cast<int>(pixel), data[data_index]);
  238. }
  239. }
  240. }
  241. }
  242. }
  243. // and then add new data with different batch_size
  244. int new_batch_size = 16;
  245. layer.set_batch_size(new_batch_size);
  246. mat_vector.clear();
  247. mat_vector.resize(new_batch_size * num_iter);
  248. label_vector.clear();
  249. label_vector.resize(new_batch_size * num_iter);
  250. for (int i = 0; i < new_batch_size*num_iter; ++i) {
  251. mat_vector[i] = cv::Mat(this->height_, this->width_, CV_8UC4);
  252. label_vector[i] = i;
  253. cv::randu(mat_vector[i], cv::Scalar::all(0), cv::Scalar::all(255));
  254. }
  255. layer.AddMatVector(mat_vector, label_vector);
  256. // finally consume new data and check if everything is fine
  257. for (int iter = 0; iter < num_iter; ++iter) {
  258. int offset = new_batch_size * iter;
  259. layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
  260. EXPECT_EQ(new_batch_size, this->blob_top_vec_[0]->num());
  261. EXPECT_EQ(new_batch_size, this->blob_top_vec_[1]->num());
  262. const Dtype* data = this->data_blob_->cpu_data();
  263. for (int i = 0; i < new_batch_size; ++i) {
  264. EXPECT_EQ(offset + i, this->label_blob_->cpu_data()[i]);
  265. for (int h = 0; h < this->height_; ++h) {
  266. const unsigned char* ptr_mat = mat_vector[offset + i].ptr<uchar>(h);
  267. int index = 0;
  268. for (int w = 0; w < this->width_; ++w) {
  269. for (int c = 0; c < this->channels_; ++c) {
  270. data_index = (i*count) + (c * this->height_ + h) * this->width_ + w;
  271. Dtype pixel = static_cast<Dtype>(ptr_mat[index++]);
  272. EXPECT_EQ(static_cast<int>(pixel), data[data_index]);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. }
  279. #endif // USE_OPENCV
  280. } // namespace caffe
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...