Skip to content

Commit

Permalink
DataLayer and HDF5OutputLayer can be constructed and destroyed without
Browse files Browse the repository at this point in the history
errors
  • Loading branch information
jeffdonahue committed Jan 22, 2015
1 parent 4d9e17d commit ed04cb0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
6 changes: 4 additions & 2 deletions include/caffe/data_layers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
Expand All @@ -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_;
Expand Down
4 changes: 3 additions & 1 deletion src/caffe/layers/data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ template <typename Dtype>
DataLayer<Dtype>::~DataLayer<Dtype>() {
this->JoinPrefetchThread();
// clean up the dataset resources
dataset_->close();
if (dataset_) {
dataset_->close();
}
}

template <typename Dtype>
Expand Down
14 changes: 8 additions & 6 deletions src/caffe/layers/hdf5_output_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,21 @@
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>
Expand Down
2 changes: 1 addition & 1 deletion src/caffe/test/test_hdf5_output_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit ed04cb0

Please sign in to comment.