如何导出带导出savedmodel函数的估计器模型

2024-06-30 15:42:19 发布

您现在位置:Python中文网/ 问答频道 /正文

有关于export_savedmodel的教程吗?

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


Tags: orggithubcominput参数tensorflowarticlecode
4条回答

你有两个选择:

导出模型以使用JSON字典

在我的mlengine-boilerplate repository中,我使用它将估计器模型导出到Cloud ML引擎,以便轻松地将其用于在线预测(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服务 用估计器实现的宽深模型以及如何将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)

这样做:

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)

然后,您可以按批请求具有“predict”签名名称的服务模型。

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

如果直接从主分支使用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)

但我也不能让他们工作。

也许,我没有正确理解,所以我希望你会有更多的运气。

克里斯

如果直接从主分支使用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)

但我也不能让他们工作。

可能,我不太明白,所以我希望你会有更多的运气。

克里斯

相关问题 更多 >