batuman
batuman

Reputation: 7304

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

Normally trained caffe model are in .caffemodel extension and actually they are in binary protobuf format.

Any idea how to load a caffe model in hdf5 format to caffe net in c++?

I have a model trained with python caffe in hdf5 format.

My application is in c++ using caffe c++ version and I prefer to use c++ than python.

How to read the model in caffe trained model in hdf5 format to c++ caffe net?

I know that caffe has hdf5data layer inside. Is there a sample program for that?

EDIT:

I used CopyTrainedLayersFromHDF5() api and got following runtime errors.

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

EDIT 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}

Upvotes: 2

Views: 1476

Answers (1)

Shai
Shai

Reputation: 114786

Have you considered the net object method void CopyTrainedLayersFromHDF5(const string trained_filename);? It seems like it does what you are looking for.

As for "HDF5Data" layer: you are confusing two things here. The hdf5 file you have stores the trained parameters of the net. In contrast, "HDF5Data" layer stores the training examples used for training the net.

Upvotes: 1

Related Questions