6 #include "gtest/gtest.h"
8 #include "caffe/blob.hpp"
9 #include "caffe/common.hpp"
10 #include "caffe/layers/hdf5_data_layer.hpp"
11 #include "caffe/proto/caffe.pb.h"
13 #include "caffe/test/test_caffe_main.hpp"
17 template <typename TypeParam>
18 class HDF5DataLayerTest : public MultiDeviceTest<TypeParam> {
19 typedef typename TypeParam::Dtype Dtype;
24 blob_top_data_(new Blob<Dtype>()),
25 blob_top_label_(new Blob<Dtype>()),
26 blob_top_label2_(new Blob<Dtype>()) {}
27 virtual void SetUp() {
28 blob_top_vec_.push_back(blob_top_data_);
29 blob_top_vec_.push_back(blob_top_label_);
30 blob_top_vec_.push_back(blob_top_label2_);
32 // Check out generate_sample_data.py in the same directory.
33 filename = new string(
34 CMAKE_SOURCE_DIR "caffe/test/test_data/sample_data_list.txt" CMAKE_EXT);
35 LOG(INFO)<< "Using sample HDF5 data file " << filename;
38 virtual ~HDF5DataLayerTest() {
39 delete blob_top_data_;
40 delete blob_top_label_;
41 delete blob_top_label2_;
46 Blob<Dtype>* const blob_top_data_;
47 Blob<Dtype>* const blob_top_label_;
48 Blob<Dtype>* const blob_top_label2_;
49 vector<Blob<Dtype>*> blob_bottom_vec_;
50 vector<Blob<Dtype>*> blob_top_vec_;
53 TYPED_TEST_CASE(HDF5DataLayerTest, TestDtypesAndDevices);
55 TYPED_TEST(HDF5DataLayerTest, TestRead) {
56 typedef typename TypeParam::Dtype Dtype;
57 // Create LayerParameter with the known parameters.
58 // The data file we are reading has 10 rows and 8 columns,
59 // with values from 0 to 10*8 reshaped in row-major order.
61 param.add_top("data");
62 param.add_top("label");
63 param.add_top("label2");
65 HDF5DataParameter* hdf5_data_param = param.mutable_hdf5_data_param();
67 hdf5_data_param->set_batch_size(batch_size);
68 hdf5_data_param->set_source(*(this->filename));
73 // Test that the layer setup gives correct parameters.
74 HDF5DataLayer<Dtype> layer(param);
75 layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
76 EXPECT_EQ(this->blob_top_data_->num(), batch_size);
77 EXPECT_EQ(this->blob_top_data_->channels(), num_cols);
78 EXPECT_EQ(this->blob_top_data_->height(), height);
79 EXPECT_EQ(this->blob_top_data_->width(), width);
81 EXPECT_EQ(this->blob_top_label_->num_axes(), 2);
82 EXPECT_EQ(this->blob_top_label_->shape(0), batch_size);
83 EXPECT_EQ(this->blob_top_label_->shape(1), 1);
85 EXPECT_EQ(this->blob_top_label2_->num_axes(), 2);
86 EXPECT_EQ(this->blob_top_label2_->shape(0), batch_size);
87 EXPECT_EQ(this->blob_top_label2_->shape(1), 1);
89 layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
91 // Go through the data 10 times (5 batches).
92 const int data_size = num_cols * height * width;
93 for (int iter = 0; iter < 10; ++iter) {
94 layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
96 // On even iterations, we're reading the first half of the data.
97 // On odd iterations, we're reading the second half of the data.
98 // NB: label is 1-indexed
99 int label_offset = 1 + ((iter % 2 == 0) ? 0 : batch_size);
100 int label2_offset = 1 + label_offset;
101 int data_offset = (iter % 2 == 0) ? 0 : batch_size * data_size;
103 // Every two iterations we are reading the second file,
104 // which has the same labels, but data is offset by total data size,
105 // which is 2400 (see generate_sample_data).
106 int file_offset = (iter % 4 < 2) ? 0 : 2400;
108 for (int i = 0; i < batch_size; ++i) {
111 this->blob_top_label_->cpu_data()[i]);
114 this->blob_top_label2_->cpu_data()[i]);
116 for (int i = 0; i < batch_size; ++i) {
117 for (int j = 0; j < num_cols; ++j) {
118 for (int h = 0; h < height; ++h) {
119 for (int w = 0; w < width; ++w) {
121 i * num_cols * height * width +
125 file_offset + data_offset + idx,
126 this->blob_top_data_->cpu_data()[idx])
127 << "debug: i " << i << " j " << j