如何使用 export_savedmodel 函数导出 Estimator 模型

Posted

技术标签:

【中文标题】如何使用 export_savedmodel 函数导出 Estimator 模型【英文标题】:How to export Estimator model with export_savedmodel function 【发布时间】:2017-08-07 17:30:34 【问题描述】:

有没有关于export_savedmodel 的教程?

我浏览过tensorflow.org上的this article和github.com上的unittest code,仍然不知道如何构造函数serving_input_fn的参数export_savedmodel

【问题讨论】:

Example of export_savedmodel function 检查此处提供的 sol ***.com/questions/58959582/… 【参考方案1】:

这样做:

your_feature_spec = 
    "some_feature": tf.FixedLenFeature([], dtype=tf.string, default_value=""),
    "some_feature": tf.VarLenFeature(dtype=tf.string),


def _serving_input_receiver_fn():
    serialized_tf_example = tf.placeholder(dtype=tf.string, shape=None, 
                                           name='input_example_tensor')
    # key (e.g. 'examples') should be same with the inputKey when you 
    # buid the request for prediction
    receiver_tensors = 'examples': serialized_tf_example
    features = tf.parse_example(serialized_tf_example, your_feature_spec)
    return tf.estimator.export.ServingInputReceiver(features, receiver_tensors)

estimator.export_savedmodel(export_dir, _serving_input_receiver_fn)

然后您可以批量请求具有“预测”签名名称的服务模型。

来源:https://www.tensorflow.org/guide/saved_model#prepare_serving_inputs

【讨论】:

使用 feature_spec = 'x': tf.FixedLenFeature([224, 224, 3], dtype=tf.float32) 我得到了错误:TypeError: Failed to convert object of type 到张量 签名是“预测”,有没有办法指定自定义签名?或者设置回“serving_default”。【参考方案2】:

你有两个选择:

导出模型以使用 JSON 字典

在我的mlengine-boilerplate repository 中,我使用它来将估算器模型导出到 Cloud ML Engine,以便轻松地将其用于在线预测 (sample code for the predictions)。重要部分:

def serving_input_fn():
    feature_placeholders = 
        'id': tf.placeholder(tf.string, [None], name="id_placeholder"),
        'feat': tf.placeholder(tf.float32, [None, FEAT_LEN], name="feat_placeholder"),
        #label is not required since serving is only used for inference
    
    return input_fn_utils.InputFnOps(
        feature_placeholders,
        None,
        feature_placeholders)

导出您的模型以使用 TensorFlow 示例

This tutorial 展示了如何使用export_savedmodel 服务 使用估计器实现的 Wide & Deep 模型以及如何将 Tensorflow 示例输入到导出的模型中。重要的 部分:

from tensorflow.contrib.learn.python.learn.utils import input_fn_utils      
serving_input_fn = input_fn_utils.build_parsing_serving_input_fn(feature_spec)

【讨论】:

我更新了tutorial 以支持r1.3。您可能应该更新您的答案以反映新的变化。 @MtDersvan ,在教程中:np.argmax(prediction) .. 我不相信它的功能与相信的一样。出于某种原因,对于返回的任何分数,它总是给出 0 作为响应。 type(prediction)tensorflow.core.framework.tensor_pb2.TensorProto 。我们如何读取这个对象? 对我来说,如果我将代码更新为np.argmax(prediction.float_val),它将完美运行。 这在github.com/MtDersvan/tf_playground/issues/5 的一个问题中进行了讨论。没错,你需要一个 float_val 的数组。【参考方案3】:

您需要拥有 tf.train.Example 和 tf.train.Feature 并将输入传递给输入接收器函数并调用模型。 你可以看看这个例子 https://github.com/tettusud/tensorflow-examples/tree/master/estimators

【讨论】:

【参考方案4】:

如果您直接从主分支使用 tensorflow,则有一个模块 tensorflow.python.estimator.export 提供了一个功能:

from tensorflow.python.estimator.export import export
feature_spec = 'MY_FEATURE': tf.constant(2.0, shape=[1, 1])
serving_input_fn = export.build_raw_serving_input_receiver_fn(feature_spec)

不幸的是,至少对我而言,它不会走得更远,但我不确定我的模型是否真的正确,所以也许你比我幸运。

另外,从 pypi 安装的当前版本有以下功能:

serving_input_fn = tf.contrib.learn.utils.build_parsing_serving_input_fn(feature_spec)
serving_input_fn = tf.contrib.learn.utils.build_default_serving_input_fn(feature_spec)

但我也无法让它们工作。

可能,我没有正确理解这一点,所以我希望你能有更多的运气。

克里斯

【讨论】:

以上是关于如何使用 export_savedmodel 函数导出 Estimator 模型的主要内容,如果未能解决你的问题,请参考以下文章

sap如何使用函数

c函数指针和指针函数如何使用何定义;如何调用使用

如何使用乘法函数来执行指数函数?

如何创建自定义 JQuery 函数以及如何使用它?

如何强制调用某些构造函数/函数以使用命名参数?

如何检测仅使用 cppcheck 从未使用的函数中调用的函数?