如何将经过训练的 h5 格式的 caffe 模型加载到 c++ caffe net?

How to load trained caffe model in h5 format to c++ caffe net?

通常训练的 caffe 模型是 .caffemodel 扩展,实际上它们是 binary protobuf 格式。

知道如何在 C++ 中将 hdf5 格式的 caffe 模型加载到 caffe net 吗?

我有一个用 python caffe 以 hdf5 格式训练的模型。

我的应用程序是在 c++ 中使用 caffe c++ 版本,我更喜欢使用 c++ 而不是 python。

如何将 caffe 训练模型中的 hdf5 格式的模型读取到 c++ caffe net?

我知道caffe里面有hdf5data层。 有相应的示例程序吗?

编辑:

我使用了 CopyTrainedLayersFromHDF5() api 并遇到了以下运行时错误。

HDF5-DIAG: Error detected in HDF5 (1.8.11) thread 140737353775552:
  #000: ../../../src/H5G.c line 463 in H5Gopen2(): unable to open group
    major: Symbol table
    minor: Can't open object
  #001: ../../../src/H5Gint.c line 320 in H5G__open_name(): group not found
    major: Symbol table
    minor: Object not found
  #002: ../../../src/H5Gloc.c line 430 in H5G_loc_find(): can't find object
    major: Symbol table
    minor: Object not found
  #003: ../../../src/H5Gtraverse.c line 861 in H5G_traverse(): internal path traversal failed
    major: Symbol table
    minor: Object not found
  #004: ../../../src/H5Gtraverse.c line 641 in H5G_traverse_real(): traversal operator failed
    major: Symbol table
    minor: Callback failed
  #005: ../../../src/H5Gloc.c line 385 in H5G_loc_find_cb(): object 'data' doesn't exist
    major: Symbol table
    minor: Object not found
F0220 15:32:14.272573 24576 net.cpp:811] Check failed: data_hid >= 0 (-1 vs. 0) Error reading weights from model_800000.h5
*** Check failure stack trace: ***
    @     0x7ffff64afdcd  google::LogMessage::Fail()
    @     0x7ffff64b1d08  google::LogMessage::SendToLog()
    @     0x7ffff64af963  google::LogMessage::Flush()
    @     0x7ffff64b263e  google::LogMessageFatal::~LogMessageFatal()
    @     0x7ffff691c3a3  caffe::Net<>::CopyTrainedLayersFromHDF5()
    @           0x40828d  ExtractFeature::ExtractFeature()
    @           0x40ce78  main
    @     0x7ffff5bf8f45  __libc_start_main
    @           0x4080c9  (unknown)

Program received signal SIGABRT, Aborted.
0x00007ffff5c0dc37 in __GI_raise (sig=sig@entry=6)
    at ../nptl/sysdeps/unix/sysv/linux/raise.c:56
56  ../nptl/sysdeps/unix/sysv/linux/raise.c: No such file or directory.
(gdb) cd 
[17]+  Stopped                 gdb ./endtoendlib

编辑 1:

>>h5ls model_800000.h5 command gave me

conv1                    Group
conv2                    Group
forget_gate              Dataset {1, 250, 1, 1274}
inception_3a             Group
inception_3b             Group
inception_4a             Group
inception_4b             Group
inception_4c             Group
inception_4d             Group
inception_4e             Group
inception_5a             Group
inception_5b             Group
input_gate               Dataset {1, 250, 1, 1274}
input_value              Dataset {1, 250, 1, 1274}
ip_bbox_unscaled0.p0     Dataset {4, 250}
ip_bbox_unscaled0.p1     Dataset {4}
ip_bbox_unscaled1.p0     Dataset {4, 250}
ip_bbox_unscaled1.p1     Dataset {4}
ip_bbox_unscaled2.p0     Dataset {4, 250}
ip_bbox_unscaled2.p1     Dataset {4}
ip_bbox_unscaled3.p0     Dataset {4, 250}
ip_bbox_unscaled3.p1     Dataset {4}
ip_bbox_unscaled4.p0     Dataset {4, 250}
ip_bbox_unscaled4.p1     Dataset {4}
ip_conf0.p0              Dataset {2, 250}
ip_conf0.p1              Dataset {2}
ip_conf1.p0              Dataset {2, 250}
ip_conf1.p1              Dataset {2}
ip_conf2.p0              Dataset {2, 250}
ip_conf2.p1              Dataset {2}
ip_conf3.p0              Dataset {2, 250}
ip_conf3.p1              Dataset {2}
ip_conf4.p0              Dataset {2, 250}
ip_conf4.p1              Dataset {2}
output_gate              Dataset {1, 250, 1, 1274}
post_fc7_conv.p0         Dataset {1024, 1024, 1, 1}
post_fc7_conv.p1         Dataset {1024}

您是否考虑过 net 对象方法 void CopyTrainedLayersFromHDF5(const string trained_filename);?它似乎满足了您的需求。

至于"HDF5Data"层:你在这里混淆了两件事。您拥有的 hdf5 文件存储了网络的 训练参数 。相比之下,"HDF5Data" 层存储用于训练网络的 训练示例