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

时间:2017-03-16 13:43:23

标签: python tensorflow tensorflow-serving

是否有关于export_savedmodel的可用教程?

我在tensorflow.org和this article的github.com上经历了unittest code,但仍然不知道如何构建函数serving_input_fn的参数export_savedmodel

4 个答案:

答案 0 :(得分:6)

这样做:

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

答案 1 :(得分:2)

您有两个选择:

导出模型以使用JSON词典

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

    SET GLOBAL max_allowed_packet=1073741824;

导出模型以使用Tensorflow示例

This tutorial显示了如何使用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示例提供给导出的模型。重要的    部分:

export_savedmodel

答案 2 :(得分:1)

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

但我也无法让他们工作。

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

克里斯

答案 3 :(得分:0)

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