【问题标题】:Convert a graph proto (pb/pbtxt) to a SavedModel for use in TensorFlow Serving or Cloud ML Engine将图形原型 (pb/pbtxt) 转换为 SavedModel 以在 TensorFlow Serving 或 Cloud ML Engine 中使用
【发布时间】:2017-11-03 21:07:31
【问题描述】:

我一直在关注我训练过的模型上的 TensorFlow for Poets 2 代码实验室,并创建了一个带有嵌入权重的冻结量化图。它被捕获在一个文件中 - 比如my_quant_graph.pb

由于我可以使用该图进行推理 TensorFlow Android inference library 就好了,我认为我可以使用 Cloud ML Engine 做同样的事情,但它似乎只适用于 SavedModel 模型。

如何简单地将单个 pb 文件中的冻结/量化图转换为在 ML 引擎上使用?

【问题讨论】:

标签: tensorflow tensorflow-serving google-cloud-ml


【解决方案1】:

事实证明,SavedModel 提供了一些关于已保存图形的额外信息。假设冻结图不需要资产,那么它只需要指定的服务签名。

这是我运行的 Python 代码,用于将我的图表转换为 Cloud ML 引擎接受的格式。注意我只有一对输入/输出张量。

import tensorflow as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants

export_dir = './saved'
graph_pb = 'my_quant_graph.pb'

builder = tf.saved_model.builder.SavedModelBuilder(export_dir)

with tf.gfile.GFile(graph_pb, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

sigs = {}

with tf.Session(graph=tf.Graph()) as sess:
    # name="" is important to ensure we don't get spurious prefixing
    tf.import_graph_def(graph_def, name="")
    g = tf.get_default_graph()
    inp = g.get_tensor_by_name("real_A_and_B_images:0")
    out = g.get_tensor_by_name("generator/Tanh:0")

    sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
        tf.saved_model.signature_def_utils.predict_signature_def(
            {"in": inp}, {"out": out})

    builder.add_meta_graph_and_variables(sess,
                                         [tag_constants.SERVING],
                                         signature_def_map=sigs)

builder.save()

【讨论】:

  • 我正在尝试这样做,但是有人给了我没有代码的检查点目录。似乎我需要输入和输出节点的名称。有没有办法从检查点目录中的信息中获取输入和输出节点?
  • 感谢您的快速回复。当我运行它时,我得到:python inspect_checkpoint.py --file_name=checkpoint 2017-07-14 07:38:02.585722: W tensorflow/core/util/tensor_slice_reader.cc:95] Could not open ./checkpoint: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator? Unable to open table file ./checkpoint: Data loss: not an sstable (bad magic number): perhaps your file is in a different file format and you need to use a different restore operator?
  • 我试用了你的代码!但变量文件夹是空的。我正在使用 tensorflow 集线器在 this 之后重新训练图像分类器,变量文件夹应该是空的吗? (在某些情况下)
  • @MarkMcDonald 如果我有多个输入和输出节点怎么办?
【解决方案2】:

有多个输出节点的样本:

# Convert PtotoBuf model to saved_model, format for TF Serving
# https://cloud.google.com/ai-platform/prediction/docs/exporting-savedmodel-for-prediction
import shutil
import tensorflow.compat.v1 as tf
from tensorflow.python.saved_model import signature_constants
from tensorflow.python.saved_model import tag_constants

export_dir = './1' # TF Serving supports run different versions of same model. So we put current model to '1' folder.
graph_pb = 'frozen_inference_graph.pb'

# Clear out folder
shutil.rmtree(export_dir, ignore_errors=True)

builder = tf.saved_model.builder.SavedModelBuilder(export_dir)

with tf.io.gfile.GFile(graph_pb, "rb") as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())

sigs = {}

with tf.Session(graph=tf.Graph()) as sess:
    # Prepare input and outputs of model
    tf.import_graph_def(graph_def, name="")
    g = tf.get_default_graph()
    image_tensor = g.get_tensor_by_name("image_tensor:0")
    num_detections = g.get_tensor_by_name("num_detections:0")
    detection_scores = g.get_tensor_by_name("detection_scores:0")
    detection_boxes = g.get_tensor_by_name("detection_boxes:0")
    detection_classes = g.get_tensor_by_name("detection_classes:0")

    sigs[signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY] = \
        tf.saved_model.signature_def_utils.predict_signature_def(
            {"input_image": image_tensor}, 
            {   "num_detections": num_detections,
                "detection_scores": detection_scores, 
                "detection_boxes": detection_boxes, 
                "detection_classes": detection_classes})

    builder.add_meta_graph_and_variables(sess,
                                         [tag_constants.SERVING],
                                         signature_def_map=sigs)

builder.save()

【讨论】:

  • 我的冻结 TF1 图(我想转换为保存的模型)具有以下张量:ghostbin.com/MlDHG - 我需要使用您的脚本指定哪些参数?
猜你喜欢
  • 2017-11-22
  • 2019-07-27
  • 2019-11-28
  • 2019-06-04
  • 1970-01-01
  • 2019-07-22
  • 2018-08-17
  • 2018-03-24
  • 2018-09-12
相关资源
最近更新 更多