Separate TestForward into Test{CPU, GPU}Forward in HDF5OutputLayerTest
authorKai Li <kaili_kloud@163.com>
Mon, 2 Jun 2014 12:07:48 +0000 (20:07 +0800)
committerKai Li <kaili_kloud@163.com>
Fri, 20 Jun 2014 09:29:43 +0000 (17:29 +0800)
src/caffe/test/test_hdf5_output_layer.cpp

index 9f793f2..45cf11b 100644 (file)
@@ -19,7 +19,7 @@ namespace caffe {
 
 extern cudaDeviceProp CAFFE_TEST_CUDA_PROP;
 
-template <typename Dtype>
+template<typename Dtype>
 class HDF5OutputLayerTest : public ::testing::Test {
  protected:
   HDF5OutputLayerTest()
@@ -30,7 +30,8 @@ class HDF5OutputLayerTest : public ::testing::Test {
         num_(5),
         channels_(8),
         height_(5),
-        width_(5) {}
+        width_(5) {
+  }
 
   virtual ~HDF5OutputLayerTest() {
     delete blob_data_;
@@ -51,9 +52,9 @@ class HDF5OutputLayerTest : public ::testing::Test {
   int width_;
 };
 
-template <typename Dtype>
-void HDF5OutputLayerTest<Dtype>::CheckBlobEqual(
-    const Blob<Dtype>& b1, const Blob<Dtype>& b2) {
+template<typename Dtype>
+void HDF5OutputLayerTest<Dtype>::CheckBlobEqual(const Blob<Dtype>& b1,
+                                                const Blob<Dtype>& b2) {
   EXPECT_EQ(b1.num(), b2.num());
   EXPECT_EQ(b1.channels(), b2.channels());
   EXPECT_EQ(b1.height(), b2.height());
@@ -72,8 +73,61 @@ void HDF5OutputLayerTest<Dtype>::CheckBlobEqual(
 typedef ::testing::Types<float, double> Dtypes;
 TYPED_TEST_CASE(HDF5OutputLayerTest, Dtypes);
 
-TYPED_TEST(HDF5OutputLayerTest, TestForward) {
+/*
+ * TestCPUForward and TestGPUForward are almost identical except for the mode.
+ * They are separated to use with `test_all.testbin --gtest_filter="*CPU*"`.
+ */
+TYPED_TEST(HDF5OutputLayerTest, TestCPUForward) {
+  LOG(INFO) << "Loading HDF5 file " << this->input_file_name_;
+  hid_t file_id = H5Fopen(this->input_file_name_.c_str(), H5F_ACC_RDONLY,
+                          H5P_DEFAULT);
+  ASSERT_GE(file_id, 0)<< "Failed to open HDF5 file" <<
+      this->input_file_name_;
+  hdf5_load_nd_dataset(file_id, HDF5_DATA_DATASET_NAME, 0, 4,
+                       this->blob_data_);
+  hdf5_load_nd_dataset(file_id, HDF5_DATA_LABEL_NAME, 0, 4,
+                       this->blob_label_);
+  herr_t status = H5Fclose(file_id);
+  EXPECT_GE(status, 0)<< "Failed to close HDF5 file " <<
+      this->input_file_name_;
+  this->blob_bottom_vec_.push_back(this->blob_data_);
+  this->blob_bottom_vec_.push_back(this->blob_label_);
+
+  Caffe::set_mode(Caffe::CPU);
+  LayerParameter param;
+  param.mutable_hdf5_output_param()->set_file_name(this->output_file_name_);
+  // This code block ensures that the layer is deconstructed and
+  //   the output hdf5 file is closed.
+  {
+    HDF5OutputLayer<TypeParam> layer(param);
+    EXPECT_EQ(layer.file_name(), this->output_file_name_);
+    layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
+    layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
+  }
+  file_id = H5Fopen(this->output_file_name_.c_str(), H5F_ACC_RDONLY,
+                          H5P_DEFAULT);
+  ASSERT_GE(
+    file_id, 0)<< "Failed to open HDF5 file" <<
+          this->input_file_name_;
+
+  Blob<TypeParam>* blob_data = new Blob<TypeParam>();
+  hdf5_load_nd_dataset(file_id, HDF5_DATA_DATASET_NAME, 0, 4,
+                       blob_data);
+  this->CheckBlobEqual(*(this->blob_data_), *blob_data);
+
+  Blob<TypeParam>* blob_label = new Blob<TypeParam>();
+  hdf5_load_nd_dataset(file_id, HDF5_DATA_LABEL_NAME, 0, 4,
+                       blob_label);
+  this->CheckBlobEqual(*(this->blob_label_), *blob_label);
+
+  status = H5Fclose(file_id);
+  EXPECT_GE(status, 0) << "Failed to close HDF5 file " <<
+      this->output_file_name_;
+}
+
+TYPED_TEST(HDF5OutputLayerTest, TestGPUForward) {
   LOG(INFO) << "Loading HDF5 file " << this->input_file_name_;
+
   hid_t file_id = H5Fopen(this->input_file_name_.c_str(), H5F_ACC_RDONLY,
                           H5P_DEFAULT);
   ASSERT_GE(file_id, 0) << "Failed to open HDF5 file" <<
@@ -88,38 +142,37 @@ TYPED_TEST(HDF5OutputLayerTest, TestForward) {
   this->blob_bottom_vec_.push_back(this->blob_data_);
   this->blob_bottom_vec_.push_back(this->blob_label_);
 
-  Caffe::Brew modes[] = { Caffe::CPU, Caffe::GPU };
-  for (int m = 0; m < 2; ++m) {
-    Caffe::set_mode(modes[m]);
-    LayerParameter param;
-    param.mutable_hdf5_output_param()->set_file_name(this->output_file_name_);
-    // This code block ensures that the layer is deconstructed and
-    //   the output hdf5 file is closed.
-    {
-      HDF5OutputLayer<TypeParam> layer(param);
-      EXPECT_EQ(layer.file_name(), this->output_file_name_);
-      layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
-      layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
-    }
-    hid_t file_id = H5Fopen(this->output_file_name_.c_str(), H5F_ACC_RDONLY,
-                            H5P_DEFAULT);
-    ASSERT_GE(file_id, 0) << "Failed to open HDF5 file" <<
-        this->input_file_name_;
-
-    Blob<TypeParam>* blob_data = new Blob<TypeParam>();
-    hdf5_load_nd_dataset(file_id, HDF5_DATA_DATASET_NAME, 0, 4,
-                         blob_data);
-    this->CheckBlobEqual(*(this->blob_data_), *blob_data);
-
-    Blob<TypeParam>* blob_label = new Blob<TypeParam>();
-    hdf5_load_nd_dataset(file_id, HDF5_DATA_LABEL_NAME, 0, 4,
-                         blob_label);
-    this->CheckBlobEqual(*(this->blob_label_), *blob_label);
-
-    herr_t status = H5Fclose(file_id);
-    EXPECT_GE(status, 0) << "Failed to close HDF5 file " <<
-        this->output_file_name_;
+  Caffe::set_mode(Caffe::GPU);
+  LayerParameter param;
+  param.mutable_hdf5_output_param()->set_file_name(this->output_file_name_);
+  // This code block ensures that the layer is deconstructed and
+  //   the output hdf5 file is closed.
+  {
+    HDF5OutputLayer<TypeParam> layer(param);
+    EXPECT_EQ(layer.file_name(), this->output_file_name_);
+    layer.SetUp(this->blob_bottom_vec_, &this->blob_top_vec_);
+    layer.Forward(this->blob_bottom_vec_, &this->blob_top_vec_);
   }
+  file_id = H5Fopen(this->output_file_name_.c_str(), H5F_ACC_RDONLY,
+                          H5P_DEFAULT);
+  ASSERT_GE(
+    file_id, 0)<< "Failed to open HDF5 file" <<
+          this->input_file_name_;
+
+  Blob<TypeParam>* blob_data = new Blob<TypeParam>();
+  hdf5_load_nd_dataset(file_id, HDF5_DATA_DATASET_NAME, 0, 4,
+                       blob_data);
+  this->CheckBlobEqual(*(this->blob_data_), *blob_data);
+
+  Blob<TypeParam>* blob_label = new Blob<TypeParam>();
+  hdf5_load_nd_dataset(file_id, HDF5_DATA_LABEL_NAME, 0, 4,
+                       blob_label);
+  this->CheckBlobEqual(*(this->blob_label_), *blob_label);
+
+  status = H5Fclose(file_id);
+  EXPECT_GE(status, 0) << "Failed to close HDF5 file " <<
+      this->output_file_name_;
 }
 
-}  // namespace caffe
+}
+  // namespace caffe