我们使用不同的配置训练我们的模型的许多变化,并且需要不同的输入预处理(其中预处理在TensorFlow之外完成)。我想将我们的模型导出为SavedModels,我想我们将有一个API服务器,它将提供对模型的访问并处理预处理并使用它将通过TensorFlow从模型元数据检索的配置与TensorFlow服务器交谈服务器。模型metatdata可能是一个结构化的JSON,或者可能使用协议缓冲区。我不清楚这方面的最佳做法是什么。特别是,MetaInfoDef协议缓冲区有三个不同的字段,似乎旨在保存元数据(meta_graph_version
,any_info
和tags
)。但除了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服务?)
使用客户端API(REST)提取MetaData的命令如下所示
获取http://host:port/v1/models/ $ {MODEL_NAME} [/ versions / $ {MODEL_VERSION}] /元数据
/ versions / $ {MODEL_VERSION}是可选的。如果省略,则在响应中返回最新版本的模型元数据。
您可以在链接中找到更多详细信息,https://www.tensorflow.org/tfx/serving/api_rest/ => Model Metadata API
@gmr,通过tf.add_to_collection将原型添加到集合中,以及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()
将proto添加到集合的其他方法的代码如下所示:
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]
}