[英]Why does the HDF5 Output Caffe layer write data of seemingly incorrect dimensions?
我有興趣將給定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_++;
}
代碼幾乎可以很好地工作,因為我能夠為每個實際上包含數據的圖像成功創建文件。 不幸的是,似乎寫入了錯誤的數據,因為日志和結果輸出文件中顯示的維度都不正確。 這是我指定輸出層的位置(在網絡prototext中):
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"
}
}
我相信保存錯誤數據的原因是因為當我觀察網絡設置時,top of conv5_3的尺寸如下所示:
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文件的數據的維度相匹配,因此日志准確描述了代碼的行為。 我的問題是為什么會出現這種情況? 似乎我已經正確地設置了一切,但必須有一些我不知道的東西......
正如@hbaderts幫助我發現的那樣,事實證明HDF5層是正確的並輸出正確尺寸的數據。 我對維度的困惑是由於網絡原型文本的測試版本中看似靜態的輸入維度定義。 事實證明,因為我通過使用pycaffe函數net.forward(** forward_kwargs)加載數據,卷積層正在自我縮放以滿足輸入圖像的不同輸入維度。 這解釋了大於預期的特征尺寸以及它們在圖像之間變化的事實。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.