【问题标题】:TensorFlow 2.0 C++ - Load pre-trained modelTensorFlow 2.0 C++ - 加载预训练模型
【发布时间】:2020-08-16 12:42:30
【问题描述】:

有人可以告诉我如何使用 tensorflow 2.0 的 C++ API 加载模型、使用 keras 在 python 中训练和导出吗?

我找不到这方面的信息,只有 tensorflow 版本

亲切的问候

【问题讨论】:

    标签: python c++ keras load tensorflow2.0


    【解决方案1】:

    好的,我找到了解决其他问题的方法:

    在 Python 中,您必须使用以下命令导出它:

    tf.keras.models.save_model(model, 'model')
    

    在 C++ 中你必须加载它:

    tensorflow::SavedModelBundle model;
    tensorflow::Status status = tensorflow::LoadSavedModel(
      tensorflow::SessionOptions(), 
      tensorflow::RunOptions(), 
      "path/to/model/folder", 
      {tensorflow::kSavedModelTagServe}, 
      &model);
    

    根据这篇文章:Using Tensorflow checkpoint to restore model in C++

    如果我现在尝试设置输入和输出,则会引发错误:“找不到名称为 'outputlayer' 的节点”和“无效参数:张量输入:0,在 feed_devices 或 fetch_devices 中指定的不在图表中” .

    有人知道这里出了什么问题吗?

    【讨论】:

      【解决方案2】:

      您必须检查输入和输出名称。使用 tensorboard 选项显示模型结构。它在图表选项卡中。或者像 Netron 等一些网络浏览器。

      【讨论】: