如何确定图中输入和输出节点的形状。(张量流 C++)

数据挖掘 张量流
2022-03-10 21:41:07

起初,我尝试实现以下管道:

  • 1a) 我将模型从 keras (.hdf5) 转换为 tensorflow (.pb) 并使用tf.saved_model.simple_savePython 中的函数保存。
  • 2a)我使用加载模型tensorflow::LoadSavedModel
  • 3a)我使用以下代码确定了一个形状:
const string export_dir = "/home/user/MyBuild/build_tftest2/models";
 SavedModelBundle bundle; RunOptions run_options;
status = LoadSavedModel(sopt, run_options, export_dir,
               {kSavedModelTagServe}, &bundle);
if (status.ok())
{
    GraphDef graph = bundle.meta_graph_def.graph_def();
    auto shape = graph.node().Get(0).attr().at("shape").shape();
    for (int i = 0; i < shape.dim_size(); i++) {
        std::cout << shape.dim(i).size()<<std::endl;
    }
}

后来我希望使用最佳模型,因此我使用了另一个管道:

  • 1b) 我将模型从 keras (.hdf5) 转换为 tensorflow (.pb) 使用
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)

并使用以下方法保存在 Python 中:

tf.train.write_graph(frozen_graph, "model", "tf_model.pb", as_text=False)
  • 2b)我使用transform_graphtensorflow 包中的实用程序优化了模型。
  • 3b)我使用以下代码(C++)加载了我的图表:
status = ReadBinaryProto(Env::Default(), "/home/user/MySoftware/foreign code/netology_JN/Diplom/Models/optimized/optim_model.pb", &graph_def);
  • 4b) 我尝试使用 3a) 确定形状并引发异常:
[libprotobuf FATAL /usr/local/include/google/protobuf/map.h:1064]
CHECK failed: it != end(): key not found: shape terminate called after
throwing an instance of 'google::protobuf::FatalException' what():
CHECK failed: it != end(): key not found: shape 14:21:36: The program
has unexpectedly finished.
0个回答
没有发现任何回复~