Python 如何使用export_saved模型函数导出估计模型

Python 如何使用export_saved模型函数导出估计模型,python,tensorflow,tensorflow-serving,Python,Tensorflow,Tensorflow Serving,有关于导出保存的模型的教程吗 我在tensorflow.org和github.com上浏览过,仍然不知道如何构造函数export\u savedmodel的参数为输入提供服务如果直接从主分支使用tensorflow,则有一个模块tensorflow.python.estimator.export为其提供函数: from tensorflow.python.estimator.export import export feature_spec = {'MY_FEATURE': tf.constan

有关于导出保存的模型的教程吗


我在tensorflow.org和github.com上浏览过,仍然不知道如何构造函数
export\u savedmodel的参数
为输入提供服务
如果直接从主分支使用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)
但是我也不能让他们工作

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

克里斯

您有两种选择:

导出模型以使用JSON字典 在我的示例中,我使用它将估计器模型导出到Cloud ML引擎,以便轻松地将其用于在线预测()。基本部分:

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示例 显示如何使用
导出\u saved模型
使用估计器实现的宽深模型,以及如何将Tensorflow示例输入导出的模型。本质
第部分:


您需要有tf.train.Example和tf.train.Feature,并将输入传递给输入接收器函数并调用模型。 你可以看看这个例子

这样做:

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”签名名的服务模型


来源:

我更新了以支持r1.3。您可能应该更新答案以反映新的更改。@MtDersvan,在教程中:
np.argmax(预测)
。。我不相信它能像人们所相信的那样发挥作用。出于某种原因,对于返回的任何分数,它总是给出0作为响应<代码>类型(预测)
给出了
tensorflow.core.framework.tensor\u pb2.TensorProto
。我们如何读取这个对象?对我来说,如果我将代码更新为
np.argmax(prediction.float\u val)
,它就工作得很好。这在这里的一个问题中讨论过。确切地说,您需要一个
float\u val
数组。检查这里提供的sol,使用feature\u spec={'x':tf.FixedLenFeature([224,224,3],dtype=tf.float32])我得到了一个错误:TypeError:无法将类型的对象转换为tensort签名是“predict”,有没有办法指定自定义签名?或设置回“默认值”。
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)