2017-02-20 101 views
1

正常訓練的咖啡模型在.caffemodel擴展名中,實際上它們的格式爲binary protobuf如何將訓練過的caffe模型以h5格式加載到C++ caffe網?

任何想法如何將hdf5格式的caffe模型加載到C++中的caffe net?

我有一個用python caffe訓練的模型,格式爲hdf5

我的應用程序是使用caffe C++版本的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 ([email protected]=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} 
+0

是的,我檢查了.h5文件,它是二進制格式。 – batuman

+0

但是二進制格式是否正確?如果你'h5ls model_80000.h5'你會得到什麼?它看起來好像caffe希望文件有一個不存在的「數據」數據集...... – Shai

+0

h5ls命令給了我和EDIT1一樣。我看到數據集。 – batuman

回答

1

您曾經考慮過net對象方法void CopyTrainedLayersFromHDF5(const string trained_filename);?它看起來像它在做你正在尋找的東西。

至於"HDF5Data"層:你在這裏混淆了兩件事。你有hdf5文件存儲訓練參數的網絡。相比之下,"HDF5Data"層存儲用於訓練網絡的訓練實例

+1

是的,您的解決方案是正確的。我需要處理我的h5文件,以便它可以加載到C++ caffe。 – batuman

相關問題