gpt4 book ai didi

c++ - 为什么 HDF5 Output Caffe 层会写入看似不正确维度的数据?

转载 作者:太空宇宙 更新时间:2023-11-04 13:06:28 24 4
gpt4 key购买 nike

我有兴趣将给定 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_++;
}

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

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 顶部的维度如下所示:

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 层是正确的并且输出正确维度的数据。我对维度的混淆是由于在网络原型(prototype)文本的测试版本中输入维度的看似静态的定义。事实证明,因为我使用 pycaffe 函数 net.forward(**forward_kwargs) 加载数据,所以卷积层会自行缩放以满足输入图像的不同输入尺寸。这解释了大于预期的特征尺寸以及它们在图像之间存在差异的事实。

关于c++ - 为什么 HDF5 Output Caffe 层会写入看似不正确维度的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com