【问题标题】:Serving the inception model v3 in Java using SavedModelBundle使用 SavedModelBundle 在 Java 中提供初始模型 v3
【发布时间】:2017-07-25 21:20:26
【问题描述】:

使用 org.tensorflow:tensorflow:1.3.0-rc0。

我已经按照教程https://tensorflow.github.io/serving/serving_inception从检查点生成了初始模型:

inception_saved_model --checkpoint_dir=/root/xmod/inception-v3

一切顺利,生成了一个 saved_model.pb 和一个带有数据的 variables/ 子目录,我将所有这些内容移到了 /tmp/inception-model 目录。 现在我试图通过基本上转换https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java来使用这个模型 我正在加载这样的模型,没有错误:

SavedModelBundle modelBundle = SavedModelBundle.load("/tmp/inception-model", "serve");

现在我正在尝试制定查询(类似于 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/java/src/main/java/org/tensorflow/examples/LabelImage.java#L112),但我一直在试图弄清楚如何使用 feed 和 fetch 方法:

    private static float[] executeInceptionGraph(SavedModelBundle modelBundle, Tensor image) throws Exception {
     Tensor result = modelBundle.session().runner().feed(???).fetch(???).run().get(0);

非常感谢任何关于如何编写此查询的帮助。

【问题讨论】:

标签: java tensorflow tensorflow-serving


【解决方案1】:

您需要提供与图中节点名称相关联的输入(此处为您的张量图像),从您发布的链接看来,本教程使用“图像”(参见此处https://github.com/tensorflow/serving/blob/master/tensorflow_serving/example/inception_client.py#L49,代码与python查询到教程中内置的服务器https://tensorflow.github.io/serving/serving_inception)。 然后,您也获取输出节点及其名称,查看服务器响应示例https://tensorflow.github.io/serving/serving_inception,您可以获得“类”或“分数”,具体取决于您想要的。

所以两个命令之一应该可以工作:

Tensor result = modelBundle.session().runner().feed("images", image).fetch("classes").run().get(0);

或

 Tensor result = modelBundle.session().runner().feed("images", image).fetch("scores").run().get(0);

【讨论】:

    【解决方案2】:

    我发现它仅适用于冻结模型。 fetch 方法的参数是 freeze_graph 参数 output_node_names 中使用的参数。见https://github.com/tensorflow/models/blob/master/slim/export_inference_graph.py#L32

    【讨论】:

      猜你喜欢
      • 2013-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-06
      • 1970-01-01
      相关资源
      最近更新 更多