void HDF5DataLayer<Dtype>::LoadHDF5FileData(const char* filename, int n_blobs) {
  LOG(INFO) << "Loading HDF5 file" << filename;
  hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
  if (file_id < 0) {
    LOG(ERROR) << "Failed opening HDF5 file" << filename;
    return;
  }

  if (n_blobs >= 1) {
    const int MIN_DATA_DIM = 2;
    const int MAX_DATA_DIM = 4;
    hdf5_load_nd_dataset(
      file_id, "data",  MIN_DATA_DIM, MAX_DATA_DIM, &data_blob_);
  }

  if (n_blobs >= 2) {
    const int MIN_LABEL_DIM = 1;
    const int MAX_LABEL_DIM = 2;
    hdf5_load_nd_dataset(
      file_id, "label", MIN_LABEL_DIM, MAX_LABEL_DIM, &label_blob_);
    CHECK_EQ(data_blob_.num(), label_blob_.num());
  }

  if (n_blobs >= 3) {
    const int MIN_SAMPLE_WEIGHT_DIM = 1;
    const int MAX_SAMPLE_WEIGHT_DIM = 2;
    hdf5_load_nd_dataset(
      file_id, "sample_weight", MIN_SAMPLE_WEIGHT_DIM, MAX_SAMPLE_WEIGHT_DIM, &sample_weight_blob_);
    CHECK_EQ(data_blob_.num(), sample_weight_blob_.num());
  }

  herr_t status = H5Fclose(file_id);
  CHECK_GE(status, 0) << "Failed to close HDF5 file " << filename;
  LOG(INFO) << "Successully loaded " << data_blob_.num() << " rows";
}
예제 #2
0
TYPED_TEST(HDF5OutputLayerTest, TestForward) {
  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::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_;
  }
}
TYPED_TEST(HDF5OutputLayerTest, TestForward) {
  typedef typename TypeParam::Dtype Dtype;
  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_;
  // Allow reshape here as we are loading data not params
  bool reshape = true;
  hdf5_load_nd_dataset(file_id, HDF5_DATA_DATASET_NAME, 0, 5,
                       this->blob_data_, reshape);
  hdf5_load_nd_dataset(file_id, HDF5_DATA_LABEL_NAME, 0, 5,
                       this->blob_label_, reshape);
  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_);

  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<Dtype> layer(param);
    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,
                          H5P_DEFAULT);
  ASSERT_GE(
    file_id, 0)<< "Failed to open HDF5 file" <<
          this->input_file_name_;

  Blob<Dtype>* blob_data = new Blob<Dtype>();
  hdf5_load_nd_dataset(file_id, HDF5_DATA_DATASET_NAME, 0, 5,
                       blob_data, reshape);
  this->CheckBlobEqual(*(this->blob_data_), *blob_data);

  Blob<Dtype>* blob_label = new Blob<Dtype>();
  hdf5_load_nd_dataset(file_id, HDF5_DATA_LABEL_NAME, 0, 5,
                       blob_label, reshape);
  this->CheckBlobEqual(*(this->blob_label_), *blob_label);

  status = H5Fclose(file_id);
  EXPECT_GE(status, 0) << "Failed to close HDF5 file " <<
      this->output_file_name_;
}
예제 #4
0
void HDF5DataLayer<Dtype>::LoadHDF5FileData(const char* filename) {
  DLOG(INFO) << "Loading HDF5 file: " << filename;
  hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
  if (file_id < 0) {
    LOG(FATAL) << "Failed opening HDF5 file: " << filename;
  }

  int top_size = this->layer_param_.top_size();
  hdf_blobs_.resize(top_size);

  const int MIN_DATA_DIM = 1;
  const int MAX_DATA_DIM = INT_MAX;

  for (int i = 0; i < top_size; ++i) {
    hdf_blobs_[i] = shared_ptr<Blob<Dtype> >(new Blob<Dtype>());
    hdf5_load_nd_dataset(file_id, this->layer_param_.top(i).c_str(),
        MIN_DATA_DIM, MAX_DATA_DIM, hdf_blobs_[i].get());
  }

  herr_t status = H5Fclose(file_id);
  CHECK_GE(status, 0) << "Failed to close HDF5 file: " << filename;

  // MinTopBlobs==1 guarantees at least one top blob
  int num = hdf_blobs_[0]->num();
  for (int i = 1; i < top_size; ++i) {
    CHECK_EQ(hdf_blobs_[i]->num(), num);
  }
  DLOG(INFO) << "Successully loaded " << hdf_blobs_[0]->num() << " rows";
}
예제 #5
0
void HDF5DataLayer<Dtype>::load_hdf5_file_data(const char* filename) {
  LOG(INFO) << "Loading HDF5 file" << filename;
  hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
  if (file_id < 0) {
    LOG(ERROR) << "Failed opening HDF5 file" << filename;
    return;
  }

  const int MIN_DATA_DIM = 2;
  const int MAX_DATA_DIM = 4;
  hdf5_load_nd_dataset(
    file_id, "data",  MIN_DATA_DIM, MAX_DATA_DIM, &data_blob_);

  const int MIN_LABEL_DIM = 1;
  const int MAX_LABEL_DIM = 2;
  hdf5_load_nd_dataset(
    file_id, "label", MIN_LABEL_DIM, MAX_LABEL_DIM, &label_blob_);

  herr_t status = H5Fclose(file_id);
  CHECK_EQ(data_blob_.num(), label_blob_.num());
  LOG(INFO) << "Successully loaded " << data_blob_.num() << " rows";
}
예제 #6
0
void HDF5DataLayer<Dtype>::LoadHDF5FileData(const char* filename) {
  DLOG(INFO) << "Loading HDF5 file: " << filename;
  hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
  if (file_id < 0) {
    LOG(FATAL) << "Failed opening HDF5 file: " << filename;
  }

  int top_size = this->layer_param_.top_size();
  hdf_blobs_.resize(top_size);

  const int MIN_DATA_DIM = 1;
  const int MAX_DATA_DIM = INT_MAX;

  for (int i = 0; i < top_size; ++i) {
    hdf_blobs_[i] = shared_ptr<Blob<Dtype> >(new Blob<Dtype>());
    // Allow reshape here, as we are loading data not params
    hdf5_load_nd_dataset(file_id, this->layer_param_.top(i).c_str(),
        MIN_DATA_DIM, MAX_DATA_DIM, hdf_blobs_[i].get(), true);
  }

  herr_t status = H5Fclose(file_id);
  CHECK_GE(status, 0) << "Failed to close HDF5 file: " << filename;

  // MinTopBlobs==1 guarantees at least one top blob
  CHECK_GE(hdf_blobs_[0]->num_axes(), 1) << "Input must have at least 1 axis.";
  const int num = hdf_blobs_[0]->shape(0);
  for (int i = 1; i < top_size; ++i) {
    CHECK_EQ(hdf_blobs_[i]->shape(0), num);
  }
  // Default to identity permutation.
  data_permutation_.clear();
  data_permutation_.resize(hdf_blobs_[0]->shape(0));
  for (int i = 0; i < hdf_blobs_[0]->shape(0); i++)
    data_permutation_[i] = i;

  // Shuffle if needed.
  if (this->layer_param_.hdf5_data_param().shuffle()) {
    std::random_shuffle(data_permutation_.begin(), data_permutation_.end());
    DLOG(INFO) << "Successfully loaded " << hdf_blobs_[0]->shape(0)
               << " rows (shuffled)";
  } else {
    DLOG(INFO) << "Successfully loaded " << hdf_blobs_[0]->shape(0) << " rows";
  }
}
예제 #7
0
void HDF5DataLayer<Dtype>::LoadHDF5FileData(const char* filename) {
  DLOG(INFO) << "Loading HDF5 file: " << filename;
  hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
  if (file_id < 0) {
    LOG(FATAL) << "Failed opening HDF5 file: " << filename;
  }

  int top_size = this->layer_param_.top_size();
  hdf_blobs_.resize(top_size);

  const int MIN_DATA_DIM = 1;
  const int MAX_DATA_DIM = 4;

  for (int i = 0; i < top_size; ++i) {
    hdf_blobs_[i] = shared_ptr<Blob<Dtype> >(new Blob<Dtype>());
    hdf5_load_nd_dataset(file_id, this->layer_param_.top(i).c_str(),
        MIN_DATA_DIM, MAX_DATA_DIM, hdf_blobs_[i].get());
    // Applies data transform to the first blob if required.
    if (strcmp("data",  this->layer_param_.top(i).c_str()) == 0
        && this->layer_param_.has_transform_param()) {
      transform_data = shared_ptr<Blob<Dtype> >(new Blob<Dtype>());
      transform_data->Reshape(hdf_blobs_[i]->num(), hdf_blobs_[i]->channels(), 
          hdf_blobs_[i]->height(), hdf_blobs_[i]->width());
      data_transformer_->Transform(hdf_blobs_[i].get(), transform_data.get()); 
      data_top = i;
    }
  }

  herr_t status = H5Fclose(file_id);
  CHECK_GE(status, 0) << "Failed to close HDF5 file: " << filename;

  // MinTopBlobs==1 guarantees at least one top blob
  int num = hdf_blobs_[0]->num();
  for (int i = 1; i < top_size; ++i) {
    CHECK_EQ(hdf_blobs_[i]->num(), num);
  }
  DLOG(INFO) << "Successully loaded " << hdf_blobs_[0]->num() << " rows";
}
예제 #8
0
  void MILDataLayer<Dtype>::load_batch(Batch<Dtype>* batch) {
    CPUTimer timer;
    timer.Start();
    CHECK(batch->data_.count());

    //Dtype* top_data = this->prefetch_data_.mutable_cpu_data();
    //Dtype* top_label = this->prefetch_label_.mutable_cpu_data();
    Dtype* top_data = batch->data_.mutable_cpu_data();
    Dtype* top_label = batch->label_.mutable_cpu_data();
    const int img_size = this->transform_param_.crop_size();
    const int channels = this->layer_param_.mil_data_param().channels();
    const int scale = this->transform_param_.scale();
    const bool mirror = this->transform_param_.mirror();

    const int images_per_batch = this->layer_param_.mil_data_param().images_per_batch();
    const int n_classes = this->layer_param_.mil_data_param().n_classes();
    const int num_scales = this->layer_param_.mil_data_param().num_scales();
    const float scale_factor = this->layer_param_.mil_data_param().scale_factor();

    // zero out batch
    //caffe_set(this->prefetch_data_.count(), Dtype(0), top_data);
    caffe_set(batch->data_.count(), Dtype(0), top_data);
    int item_id;
    for(int i_image = 0; i_image < images_per_batch; i_image++){
      // Sample which image to read
      unsigned int index = counter_; counter_ = counter_ + 1;
      const unsigned int rand_index = this->PrefetchRand();
      if(this->layer_param_.mil_data_param().randomize())
        index = rand_index;

      // LOG(INFO) << index % this->num_images_ << ", " << this->num_images_;
      pair<string, string> p = this->image_database_[index % this->num_images_];
      string im_name = p.first;
      string full_im_name = p.second;
    
      cv::Mat cv_img = cv::imread(full_im_name, CV_LOAD_IMAGE_COLOR);
      if (!cv_img.data) {
        LOG(ERROR) << "Could not open or find file " << full_im_name;
        return;
      }
    
      //REVIEW ktran: do not hardcode dataset name (or its prefix "/labels-")
      //REVIEW ktran: also do not use deep dataset name so that we don't have to modify the core caffe code
      //(ref: https://github.com/BVLC/caffe/commit/a0787631a27ca6478f70341462aafdcf35dabb19)
      hdf5_load_nd_dataset(this->label_file_id_, string("/labels-"+im_name).c_str(), 4, 4, &this->label_blob_);
      const Dtype* label = label_blob_.mutable_cpu_data();
    
      CHECK_EQ(label_blob_.width(), 1)          << "Expected width of label to be 1." ;
      CHECK_EQ(label_blob_.height(), n_classes) << "Expected height of label to be " << n_classes;
      CHECK_EQ(label_blob_.channels(), 1)       << "Expected channels of label to be 1." ;
      CHECK_EQ(label_blob_.num(), 1)            << "Expected num of label to be 1." ;

      float img_size_i = img_size;
      for(int i_scales = 0; i_scales < num_scales; i_scales++){
        // Resize such that the image is of size img_size, img_size
        item_id = i_image*num_scales + i_scales;
        // LOG(INFO) << "MIL Data Layer: scale: " << (int) round(img_size_i);
        cv::Mat cv_cropped_img = Transform_IDL(cv_img, static_cast<int>(round(img_size_i)), mirror);
        for (int c = 0; c < channels; ++c) {
          for (int h = 0; h < cv_cropped_img.rows; ++h) {
            for (int w = 0; w < cv_cropped_img.cols; ++w) {
              Dtype pixel =
                  static_cast<Dtype>(cv_cropped_img.at<cv::Vec3b>(h, w)[c]);
              top_data[((item_id * channels + c) * img_size + h)
                       * img_size + w]
                  = (pixel - static_cast<Dtype>(mean_value_[c]))*scale;
            }
          }
        }
        img_size_i = std::max(static_cast<float>(1.), img_size_i*scale_factor);
      }
      
      for(int i_label = 0; i_label < n_classes; i_label++){
        top_label[i_image*n_classes + i_label] = 
          label[i_label];
      }
    }

    timer.Stop();
    DLOG(INFO) << "Prefetch batch: " << timer.MilliSeconds() << " ms.";
  }
void HDF5GeneralDataLayer<Dtype>::LoadGeneralHDF5FileData(const char* filename) {
  DLOG(INFO) << "Loading The general HDF5 file" << filename;
  hid_t file_id = H5Fopen(filename, H5F_ACC_RDONLY, H5P_DEFAULT);
  if (file_id < 0) {
    LOG(ERROR) << "Failed opening HDF5 file" << filename;
  }

  HDF5GeneralDataParameter data_param = this->layer_param_.hdf5_general_data_param();
  int fieldNum = data_param.field_size();
  hdf_blobs_.resize(fieldNum);

  const int MIN_DATA_DIM = 1;
  const int MAX_DATA_DIM = 4;
  for(int i = 0; i < fieldNum; ++i){
	  //LOG(INFO) << "Data type: " << data_param.datatype(i).data();
	  if(i < data_param.datatype_size() && 
      strcmp(data_param.datatype(i).data(), "int8") == 0){
		  
      // We take out the io functions here
      const char* dataset_name_ = data_param.field(i).data();
      hdf_blobs_[i] = shared_ptr<Blob<Dtype> >(new Blob<Dtype>());

		  CHECK(H5LTfind_dataset(file_id, dataset_name_))
        << "Failed to find HDF5 dataset " << dataset_name_;
      // Verify that the number of dimensions is in the accepted range.
      herr_t status;
      int ndims;
      status = H5LTget_dataset_ndims(file_id, dataset_name_, &ndims);
      CHECK_GE(status, 0) << "Failed to get dataset ndims for " << dataset_name_;
      CHECK_GE(ndims, MIN_DATA_DIM);
      CHECK_LE(ndims, MAX_DATA_DIM);
      
      // Verify that the data format is what we expect: int8
      std::vector<hsize_t> dims(ndims);
      H5T_class_t class_;
      status = H5LTget_dataset_info(file_id, dataset_name_, dims.data(), &class_, NULL);
      CHECK_GE(status, 0) << "Failed to get dataset info for " << dataset_name_;
      CHECK_EQ(class_, H5T_INTEGER) << "Expected integer data";

      vector<int> blob_dims(dims.size());
      for (int j = 0; j < dims.size(); ++j) {
        blob_dims[j] = dims[j];
      }
      hdf_blobs_[i]->Reshape(blob_dims);
      std::cout<<"Trying to allocate memories!\n";
		  int* buffer_data = new int[hdf_blobs_[i]->count()];
      std::cout<<"Memories loaded!!!\n";
		  status = H5LTread_dataset_int(file_id, dataset_name_, buffer_data);
		  CHECK_GE(status, 0) << "Failed to read int8 dataset " << dataset_name_;

		  Dtype* target_data = hdf_blobs_[i]->mutable_cpu_data();
		  for(int j = 0; j < hdf_blobs_[i]->count(); j++){
			  //LOG(INFO) << Dtype(buffer_data[j]);
			  target_data[j] = Dtype(buffer_data[j]);
		  }
		  delete buffer_data;

	  }else{
      // The dataset is still the float32 datatype
      hdf_blobs_[i] = shared_ptr<Blob<Dtype> >(new Blob<Dtype>());
		  hdf5_load_nd_dataset(file_id, data_param.field(i).data(),
        MIN_DATA_DIM, MAX_DATA_DIM, hdf_blobs_[i].get());
	  }
  }

  herr_t status = H5Fclose(file_id);
  CHECK_GE(status, 0) << "Failed to close HDF5 file " << filename;

  for(int i = 1; i < fieldNum; ++i){
	  CHECK_EQ(hdf_blobs_[0]->num(), hdf_blobs_[i]->num());
  }
  data_permutation_.clear();
  data_permutation_.resize(hdf_blobs_[0]->shape(0));
  for (int i = 0; i < hdf_blobs_[0]->shape(0); i++)
    data_permutation_[i] = i;
  //TODO: DATA SHUFFLE
  //LOG(INFO) << "Successully loaded " << data_blob_.num() << " rows";
}