【问题标题】:Why does the HDF5 Output Caffe layer write data of seemingly incorrect dimensions?为什么 HDF5 Output Caffe 层会写入看似不正确维度的数据?
【发布时间】:2017-06-24 01:47:23
【问题描述】:

我有兴趣将给定 Caffe 层的输出写入文件。我想为多个图像执行此操作,因此我对 HDF5 输出层代码进行了一些修改,以便为每个图像创建一个文件,其中包含每个图像的特征。这是修改后的 SaveBlobs 函数:

template <typename Dtype> void HDF5OutputLayer<Dtype>::SaveBlobs() {                               
  LOG(INFO) << "Saving HDF5 file " << file_name_ << "ds: " << ds_iter_;  
  CHECK_EQ(data_blob_.num(), label_blob_.num()) <<                       
      "data blob and label blob must have the same batch size";          

  // Open hdf5 file to write this blob                                   
  file_name_ = this->layer_param_.hdf5_output_param().file_name();          
  ostringstream appender;                                                
  appender << "_" << ds_iter_ << ".h5";                                  
  file_name_.append(appender.str());                                     
  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_;        

  // Write the data and label                                            
  hdf5_save_nd_dataset(file_id_, HDF5_DATA_DATASET_NAME, data_blob_);       
  hdf5_save_nd_dataset(file_id_, HDF5_DATA_LABEL_NAME, label_blob_);        
  LOG(INFO) << "Successfully saved " << data_blob_.num() << " rows";        
  LOG(INFO) << "SAVEBLOB - Data size is: " << data_blob_.shape_string(); 
  LOG(INFO) << "SAVEBLOB - Label size is: " << label_blob_.shape_string();

  // Close the file                                                      
  herr_t status = H5Fclose(file_id_);                                    
  CHECK_GE(status, 0) << "Failed to close HDF5 file " << file_name_;        

  // Update iterator for next image                                      
  ds_iter_++;                                                            
}   

代码几乎运行良好,因为我能够成功地为每个实际上包含数据的图像创建文件。不幸的是,似乎正在写入错误的数据,因为日志中显示的维度和生成的输出文件中的维度都不正确。这是我指定输出层的地方(在网络原型中):

layer {                                                                  
  name: "conv5_3"                                                        
  type: "Convolution"                                                    
  bottom: "conv5_2"                                                      
  top: "conv5_3"                                                         
  param {                                                                
    lr_mult: 1                                                           
    decay_mult: 1                                                        
  }                                                                      
  param {                                                                
    lr_mult: 2                                                           
    decay_mult: 0                                                        
  }                                                                      
  convolution_param {                                                    
    num_output: 512                                                      
    pad: 1                                                               
    kernel_size: 3                                                       
  }                                                                      
}                                                                        
layer {                                                                  
  name: "relu5_3"                                                        
  type: "ReLU"                                                           
  bottom: "conv5_3"                                                      
  top: "conv5_3"                                                         
}                                                                        

#===== Data Logging =======                                              

layer {                                                                  
   type: "HDF5Output"                                                    
   name: "hdf5output"                                                    
   bottom: "conv5_3"   #                                                 
   bottom: "conv5_3"   #                                                 
   hdf5_output_param {                                                   
     # File name is only a base                                          
     file_name: "./test_features/image"                                  
   }                                                                     
 }   

我之所以认为保存了不正确的数据是因为当我观察网络​​设置时,conv5_3的Top尺寸如下所示:

I0206 23:07:44.815330  7630 layer_factory.hpp:77] Creating layer conv5_3_relu5_3_0_split
I0206 23:07:44.815343  7630 net.cpp:106] Creating Layer conv5_3_relu5_3_0_split
I0206 23:07:44.815348  7630 net.cpp:454] conv5_3_relu5_3_0_split <- conv5_3
I0206 23:07:44.815356  7630 net.cpp:411] conv5_3_relu5_3_0_split -> conv5_3_relu5_3_0_split_0
I0206 23:07:44.815366  7630 net.cpp:411] conv5_3_relu5_3_0_split -> conv5_3_relu5_3_0_split_1
I0206 23:07:44.815372  7630 net.cpp:411] conv5_3_relu5_3_0_split -> conv5_3_relu5_3_0_split_2
I0206 23:07:44.815382  7630 net.cpp:411] conv5_3_relu5_3_0_split -> conv5_3_relu5_3_0_split_3
I0206 23:07:44.815459  7630 net.cpp:150] Setting up conv5_3_relu5_3_0_split
I0206 23:07:44.815467  7630 net.cpp:157] Top shape: 1 512 14 14 (100352)
I0206 23:07:44.815474  7630 net.cpp:157] Top shape: 1 512 14 14 (100352)
I0206 23:07:44.815479  7630 net.cpp:157] Top shape: 1 512 14 14 (100352)
I0206 23:07:44.815484  7630 net.cpp:157] Top shape: 1 512 14 14 (100352)
I0206 23:07:44.815495  7630 net.cpp:165] Memory required for data: 116006912
I0206 23:07:44.815500  7630 layer_factory.hpp:77] Creating layer hdf5output
I0206 23:07:44.815511  7630 net.cpp:106] Creating Layer hdf5output
I0206 23:07:44.815515  7630 net.cpp:454] hdf5output <- conv5_3_relu5_3_0_split_0
I0206 23:07:44.815521  7630 net.cpp:454] hdf5output <- conv5_3_relu5_3_0_split_1
I0206 23:07:44.815527  7630 net.cpp:150] Setting up hdf5output
I0206 23:07:44.815531  7630 net.cpp:165] Memory required for data: 116006912

太好了,我希望得到维度为 1 512 14 14 的数据。不幸的是,当我对模型进行推理时,我在日志中看到显示了错误的维度:

I0206 23:07:46.108660  7630 hdf5_output_layer.cpp:31] Saving HDF5 file ds: 0
I0206 23:07:46.115536  7630 hdf5_output_layer.cpp:48] Successfully saved 1 rows
I0206 23:07:46.115557  7630 hdf5_output_layer.cpp:49] SAVEBLOB - Data size is: 1 512 54 38 (1050624)
I0206 23:07:46.115566  7630 hdf5_output_layer.cpp:50] SAVEBLOB - Label size is: 1 512 54 38 (1050624)
I0206 23:07:46.316557  7630 hdf5_output_layer.cpp:31] Saving HDF5 file ./test_features/image_0.h5ds: 1
I0206 23:07:46.322437  7630 hdf5_output_layer.cpp:48] Successfully saved 1 rows
I0206 23:07:46.322456  7630 hdf5_output_layer.cpp:49] SAVEBLOB - Data size is: 1 512 56 38 (1089536)
I0206 23:07:46.322463  7630 hdf5_output_layer.cpp:50] SAVEBLOB - Label size is: 1 512 56 38 (1089536)
I0206 23:07:46.457828  7630 hdf5_output_layer.cpp:31] Saving HDF5 file ./test_features/image_1.h5ds: 2
I0206 23:07:46.463618  7630 hdf5_output_layer.cpp:48] Successfully saved 1 rows
I0206 23:07:46.463636  7630 hdf5_output_layer.cpp:49] SAVEBLOB - Data size is: 1 512 38 50 (972800)
I0206 23:07:46.463644  7630 hdf5_output_layer.cpp:50] SAVEBLOB - Label size is: 1 512 38 50 (972800)
I0206 23:07:46.594746  7630 hdf5_output_layer.cpp:31] Saving HDF5 file ./test_features/image_2.h5ds: 3

这表明不仅输出的尺寸不正确,而且它们在迭代(图像)之间也有所不同!日志中显示的维度与写入 h5 文件的数据的维度相匹配,因此日志准确地描述了代码的行为。我的问题是为什么会这样?看起来我已经正确设置了所有内容,但一定有一些我遗漏的东西......

【问题讨论】:

  • 您确定输入到网络中的所有输入图像都具有相同的大小吗?
  • 感谢您的回复! JPEG 图像的大小不同,但是输入数据层被指定为 1 x 3 x 224 x 224。所以就网络而言,看起来所有图像的大小都相同,因为它们被缩放到那些输入尺寸.
  • 让我猜猜:你的第一张图片是 864x608 像素,第二张是 896x608 像素,第三张是 608x800 像素?你能展示你如何将图像加载到网络中吗? IE。你有什么数据层,你如何运行caffe?我认为你修改的 HDF5 层很好,加载数据是问题。

标签: c++ machine-learning neural-network deep-learning caffe


【解决方案1】:

正如@hbaderts 帮助我发现的那样,事实证明 HDF5 层是正确的,并且可以输出正确尺寸的数据。我对维度的困惑是由于网络原型的测试版本中输入维度的看似静态的定义。事实证明,因为我使用 pycaffe 函数 net.forward(**forward_kwargs) 加载数据,所以卷积层正在缩放自己以满足输入图像的不同输入尺寸。这解释了大于预期的特征尺寸以及它们在图像之间变化的事实。

【讨论】:

    猜你喜欢
    • 2021-05-03
    • 2017-05-26
    • 2017-02-24
    • 2011-11-08
    • 1970-01-01
    • 2017-02-22
    • 2013-03-25
    • 1970-01-01
    • 2018-10-28
    相关资源
    最近更新 更多