【问题标题】:How should I store metadata in a TensorFlow SavedModel?我应该如何将元数据存储在 TensorFlow SavedModel 中?
【发布时间】:2019-01-19 20:28:11
【问题描述】:

我们使用不同的配置训练模型的许多变体,并且需要对输入进行不同的预处理(其中预处理在 TensorFlow 之外完成)。我想将我们的模型导出为 SavedModels,并且我认为我们将拥有一个 API 服务器,它将提供对模型的访问并处理预处理并使用配置与 TensorFlow 服务器对话,该配置将通过 TensorFlow 从模型元数据中检索服务器。模型元数据可能是结构化的 JSON,也可能使用协议缓冲区。我不清楚这方面的最佳实践是什么。特别是,MetaInfoDef 协议缓冲区具有三个不同的字段,它们似乎旨在保存元数据(meta_graph_versionany_infotags)。但除了tags 字段外,我找不到任何使用的例子。

// User specified Version string. Can be the name of the model and revision,
// steps this model has been trained to, etc.
string meta_graph_version = 1;

[...]

// A serialized protobuf. Can be the time this meta graph is created, or
// modified, or name of the model.
google.protobuf.Any any_info = 3;

// User supplied tag(s) on the meta_graph and included graph_def.
//
// MetaGraphDefs should be tagged with their capabilities or use-cases.
// Examples: "train", "serve", "gpu", "tpu", etc.
// These tags enable loaders to access the MetaGraph(s) appropriate for a
// specific use-case or runtime environment.
repeated string tags = 4;

(虽然我不确定这三个字段是否都可以使用客户端 API 以相同的方式检索到 TensorFlow 服务?)

【问题讨论】:

  • 我同意这个问题。查看signature_def_utils.build_signature_def documentation 仅显示inputoutputmethod 的选项。也就是说,看起来没有人必须使用input 选项进行输入。似乎任何东西都可以存储在那里,因为任何实际的键值对都必须单独提取以进行推理。这表明元数据可能会被存储和读取,而不用于推理。

标签: tensorflow tensorflow-serving


【解决方案1】:

@gmr, 通过 tf.add_to_collection 以及 builder.add_meta_graph_and_variables 将 proto 添加到集合中应该可以解决您的问题。

下面提到了相同的代码:

# Mention the path below where you want the model to be stored
export_dir = "/usr/local/google/home/abc/Jupyter_Notebooks/export"

tf.gfile.DeleteRecursively(export_dir)

tf.reset_default_graph()

# Check below for other ways of adding Proto to Collection
tf.add_to_collection("my_proto_collection", "my_proto_serialized")

builder = tf.saved_model.builder.SavedModelBuilder(export_dir)
with tf.Session() as session:
  builder.add_meta_graph_and_variables(
      session,
      tags=[tf.saved_model.tag_constants.SERVING])
  builder.save()

添加proto到collection的其他方式的代码如下:

tf.add_to_collection("your_collection_name", str(your_proto))

any_buf = any_pb2.Any()

tf.add_to_collection("your_collection_name",
         any_buf.Pack(your_proto))

保存在您提到的路径 (export_dir) 中的 .pb 文件 saved_model.pb 如下所示:

{   # (tensorflow.SavedModel) size=89B
  saved_model_schema_version: 1
  meta_graphs: {    # (tensorflow.MetaGraphDef) size=85B
    meta_info_def: {    # (tensorflow.MetaGraphDef.MetaInfoDef) size=29B
      stripped_op_list: {   # (tensorflow.OpList) size=0B
      } # meta_graphs[0].meta_info_def.stripped_op_list
      tags    : [ "serve" ] # size=5
      tensorflow_version    : "1.13.1"  # size=9
      tensorflow_git_version: "unknown" # size=7
    }   # meta_graphs[0].meta_info_def
    graph_def: {    # (tensorflow.GraphDef) size=4B
      versions: {   # (tensorflow.VersionDef) size=2B
        producer     : 23
      } # meta_graphs[0].graph_def.versions
    }   # meta_graphs[0].graph_def
    collection_def: {   # (tensorflow.MetaGraphDef.CollectionDefEntry) size=46B
      key  : "my_proto_collection"  # size=19
      value: {  # (tensorflow.CollectionDef) size=23B
        bytes_list: {   # (tensorflow.CollectionDef.BytesList) size=21B
          value: [ "my_proto_serialized" ]  # size=19
        }   # meta_graphs[0].collection_def[0].value.bytes_list
      } # meta_graphs[0].collection_def[0].value
    }   # meta_graphs[0].collection_def[0]
  } # meta_graphs[0]
}

【讨论】:

    【解决方案2】:

    使用客户端 API (REST) 提取元数据的命令如下所示

    GET http://host:port/v1/models/${MODEL_NAME}[/versions/${MODEL_VERSION}]/元数据

    /versions/${MODEL_VERSION} 是可选的。如果省略,则在响应中返回最新版本的模型元数据。

    您可以在链接中找到更多详细信息,https://www.tensorflow.org/tfx/serving/api_rest/ => 模型元数据 API

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-11
    • 1970-01-01
    • 2017-02-26
    • 2015-03-16
    • 2013-07-04
    • 2021-06-27
    • 2011-04-23
    • 1970-01-01
    相关资源
    最近更新 更多