8884ce95a2310829613da4a3f2eb5e830ef890e7
[platform/upstream/caffeonacl.git] / src / caffe / test / test_hdf5data_layer.cpp
1 #include <string>
2 #include <vector>
3
4 #include "hdf5.h"
5
6 #include "gtest/gtest.h"
7
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"
12
13 #include "caffe/test/test_caffe_main.hpp"
14
15 namespace caffe {
16
17 template <typename TypeParam>
18 class HDF5DataLayerTest : public MultiDeviceTest<TypeParam> {
19   typedef typename TypeParam::Dtype Dtype;
20
21  protected:
22   HDF5DataLayerTest()
23       : filename(NULL),
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_);
31
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;
36   }
37
38   virtual ~HDF5DataLayerTest() {
39     delete blob_top_data_;
40     delete blob_top_label_;
41     delete blob_top_label2_;
42     delete filename;
43   }
44
45   string* filename;
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_;
51 };
52
53 TYPED_TEST_CASE(HDF5DataLayerTest, TestDtypesAndDevices);
54
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.
60   LayerParameter param;
61   param.add_top("data");
62   param.add_top("label");
63   param.add_top("label2");
64
65   HDF5DataParameter* hdf5_data_param = param.mutable_hdf5_data_param();
66   int batch_size = 5;
67   hdf5_data_param->set_batch_size(batch_size);
68   hdf5_data_param->set_source(*(this->filename));
69   int num_cols = 8;
70   int height = 6;
71   int width = 5;
72
73   // Test that the layer setup got the 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);
80
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);
84
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);
88
89   layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
90
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_);
95
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;
102
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;
107
108     for (int i = 0; i < batch_size; ++i) {
109       EXPECT_EQ(
110         label_offset + i,
111         this->blob_top_label_->cpu_data()[i]);
112       EXPECT_EQ(
113         label2_offset + i,
114         this->blob_top_label2_->cpu_data()[i]);
115     }
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) {
120             int idx = (
121               i * num_cols * height * width +
122               j * height * width +
123               h * width + w);
124             EXPECT_EQ(
125               file_offset + data_offset + idx,
126               this->blob_top_data_->cpu_data()[idx])
127               << "debug: i " << i << " j " << j
128               << " iter " << iter;
129           }
130         }
131       }
132     }
133   }
134 }
135
136 }  // namespace caffe