DataLayer and HDF5OutputLayer can be constructed and destroyed without
authorJeff Donahue <jeff.donahue@gmail.com>
Mon, 12 Jan 2015 22:27:06 +0000 (14:27 -0800)
committerJeff Donahue <jeff.donahue@gmail.com>
Thu, 5 Feb 2015 22:49:21 +0000 (14:49 -0800)
errors

include/caffe/data_layers.hpp
src/caffe/layers/hdf5_output_layer.cpp
src/caffe/test/test_hdf5_output_layer.cpp

index e55a0bb..cdf2d18 100644 (file)
@@ -181,10 +181,11 @@ class HDF5DataLayer : public Layer<Dtype> {
 template <typename Dtype>
 class HDF5OutputLayer : public Layer<Dtype> {
  public:
-  explicit HDF5OutputLayer(const LayerParameter& param);
+  explicit HDF5OutputLayer(const LayerParameter& param)
+      : Layer<Dtype>(param), file_opened_(false) {}
   virtual ~HDF5OutputLayer();
   virtual void LayerSetUp(const vector<Blob<Dtype>*>& bottom,
-      const vector<Blob<Dtype>*>& top) {}
+      const vector<Blob<Dtype>*>& top);
   // Data layers have no bottoms, so reshaping is trivial.
   virtual void Reshape(const vector<Blob<Dtype>*>& bottom,
       const vector<Blob<Dtype>*>& top) {}
@@ -207,6 +208,7 @@ class HDF5OutputLayer : public Layer<Dtype> {
       const vector<bool>& propagate_down, const vector<Blob<Dtype>*>& bottom);
   virtual void SaveBlobs();
 
+  bool file_opened_;
   std::string file_name_;
   hid_t file_id_;
   Blob<Dtype> data_blob_;
index d2fdeff..f63375c 100644 (file)
 namespace caffe {
 
 template <typename Dtype>
-HDF5OutputLayer<Dtype>::HDF5OutputLayer(const LayerParameter& param)
-    : Layer<Dtype>(param),
-      file_name_(param.hdf5_output_param().file_name()) {
-  /* create a HDF5 file */
+void HDF5OutputLayer<Dtype>::LayerSetUp(const vector<Blob<Dtype>*>& bottom,
+    const vector<Blob<Dtype>*>& top) {
+  file_name_ = this->layer_param_.hdf5_output_param().file_name();
   file_id_ = H5Fcreate(file_name_.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
                        H5P_DEFAULT);
   CHECK_GE(file_id_, 0) << "Failed to open HDF5 file" << file_name_;
+  file_opened_ = true;
 }
 
 template <typename Dtype>
 HDF5OutputLayer<Dtype>::~HDF5OutputLayer<Dtype>() {
-  herr_t status = H5Fclose(file_id_);
-  CHECK_GE(status, 0) << "Failed to close HDF5 file " << file_name_;
+  if (file_opened_) {
+    herr_t status = H5Fclose(file_id_);
+    CHECK_GE(status, 0) << "Failed to close HDF5 file " << file_name_;
+  }
 }
 
 template <typename Dtype>
index 2e8f096..a23034f 100644 (file)
@@ -92,8 +92,8 @@ TYPED_TEST(HDF5OutputLayerTest, TestForward) {
   //   the output hdf5 file is closed.
   {
     HDF5OutputLayer<Dtype> layer(param);
-    EXPECT_EQ(layer.file_name(), this->output_file_name_);
     layer.SetUp(this->blob_bottom_vec_, this->blob_top_vec_);
+    EXPECT_EQ(layer.file_name(), this->output_file_name_);
     layer.Forward(this->blob_bottom_vec_, this->blob_top_vec_);
   }
   file_id = H5Fopen(this->output_file_name_.c_str(), H5F_ACC_RDONLY,