gpt4 book ai didi

tensorflow - 我应该如何在 TensorFlow SavedModel 中存储元数据?

转载 作者:行者123 更新时间:2023-12-02 23:12:38 27 4
gpt4 key购买 nike

我们训练了模型的许多变体,这些变体具有不同的配置,并且需要不同的输入预处理(其中预处理是在 TensorFlow 之外完成的)。我想将我们的模型导出为 SavedModels,并且我认为我们将拥有一个 API 服务器,它将提供对模型的访问并处理预处理并使用配置与 TensorFlow 服务器进行通信,该配置将通过 TensorFlow 从模型元数据中检索服务器。模型元数据可能采用 JSON 结构,或者可能使用 Protocol Buffer 。我不清楚这方面的最佳实践是什么。特别是,MetaInfoDef Protocol Buffer 具有三个不同的字段,这些字段似乎旨在保存元数据(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;

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

最佳答案

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

下面提到了相同的代码:

# 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()

将原型(prototype)添加到集合的其他方式的代码如下所示:

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))

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

{   # (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]
}

关于tensorflow - 我应该如何在 TensorFlow SavedModel 中存储元数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54271098/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com