如何使用tf.saveModel加载模型并调用预测函数[TensorFlow 2.0 API] [英] How to load a model with tf.saved_model and call the predict function [TENSORFLOW 2.0 API]

查看:0
本文介绍了如何使用tf.saveModel加载模型并调用预测函数[TensorFlow 2.0 API]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对TensorFlow,尤其是2.0非常陌生,因为关于该API的示例还不够多,但它似乎比1.x方便得多 到目前为止,我成功地使用tf.stistiator API训练了一个线性模型,然后使用tf.stistiator.exporter成功地保存了它。

之后,我想使用tf.saveModel API加载此模型,我认为我成功地完成了该操作,但我在过程中有一些疑问,因此下面快速查看一下我的代码:

我有一个使用tf.Feature_Column API创建的功能数组,如下所示:

feature_columns = 
[NumericColumn(key='geoaccuracy', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='longitude', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='latitude', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 NumericColumn(key='bidfloor', shape=(1,), default_value=None, dtype=tf.float32, normalizer_fn=None),
 VocabularyListCategoricalColumn(key='adid', vocabulary_list=('115', '124', '139', '122', '121', '146', '113', '103', '123', '104', '147', '114', '149', '148'), dtype=tf.string, default_value=-1, num_oov_buckets=0),
 VocabularyListCategoricalColumn(key='campaignid', vocabulary_list=('36', '31', '33', '28'), dtype=tf.string, default_value=-1, num_oov_buckets=0),
 VocabularyListCategoricalColumn(key='exchangeid', vocabulary_list=('1241', '823', '1240', '1238'), dtype=tf.string, default_value=-1, num_oov_buckets=0),
...]
之后,我使用我的特征列数组以这种方式定义估计器,并对其进行训练。在此之前,没有问题。

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)

训练完我的模型后,我想要保存它,所以从这里开始怀疑,以下是我如何操作但不确定它是否正确的方法:

serving_input_parse = tf.feature_column.make_parse_example_spec(feature_columns=feature_columns)

""" view of the variable : serving_input_parse = 
 {'adid': VarLenFeature(dtype=tf.string),
 'at': VarLenFeature(dtype=tf.string),
 'basegenres': VarLenFeature(dtype=tf.string),
 'bestkw': VarLenFeature(dtype=tf.string),
 'besttopic': VarLenFeature(dtype=tf.string),
 'bidfloor': FixedLenFeature(shape=(1,), dtype=tf.float32, default_value=None),
 'browserid': VarLenFeature(dtype=tf.string),
 'browserlanguage': VarLenFeature(dtype=tf.string)
 ...} """

# exporting the model :
linear_est.export_saved_model(export_dir_base='./saved',
 serving_input_receiver_fn=tf.estimator.export.build_parsing_serving_input_receiver_fn(serving_input_receiver_fn),
 as_text=True)

现在我尝试加载它,但我不知道如何使用加载的模型来调用预测,例如使用来自 pandas 数据帧的原始数据

loaded = tf.saved_model.load('saved/1573144361/')

还有一件事,我试图查看模型的签名,但我真的不能理解我的输入形状发生了什么

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['classification']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: input_example_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 2)
        name: head/Tile:0
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: head/predictions/probabilities:0
  Method name is: tensorflow/serving/classify

signature_def['predict']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['examples'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: input_example_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['all_class_ids'] tensor_info:
        dtype: DT_INT32
        shape: (-1, 2)
        name: head/predictions/Tile:0
    outputs['all_classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 2)
        name: head/predictions/Tile_1:0
    outputs['class_ids'] tensor_info:
        dtype: DT_INT64
        shape: (-1, 1)
        name: head/predictions/ExpandDims:0
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: head/predictions/str_classes:0
    outputs['logistic'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: head/predictions/logistic:0
    outputs['logits'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: linear/linear_model/linear/linear_model/linear/linear_model/weighted_sum:0
    outputs['probabilities'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: head/predictions/probabilities:0
  Method name is: tensorflow/serving/predict

signature_def['regression']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: input_example_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['outputs'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: head/predictions/logistic:0
  Method name is: tensorflow/serving/regress

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['inputs'] tensor_info:
        dtype: DT_STRING
        shape: (-1)
        name: input_example_tensor:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['classes'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 2)
        name: head/Tile:0
    outputs['scores'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: head/predictions/probabilities:0
  Method name is: tensorflow/serving/classify

推荐答案

saved_model.load(...)documentation演示基本机制如下:

imported = tf.saved_model.load(path)
f = imported.signatures["serving_default"]
print(f(x=tf.constant([[1.]])))

我自己还是个新手,但serving_default似乎是使用saved_model.save(...)时的默认签名。

(我的理解是saved_model.save(...)不保存模型,它保存图形。为了解释图形,您需要显式地存储图形上的定义操作的"签名"。如果您没有明确地这样做,那么"SERVE_DEFAULT"将是您唯一的签名。)

我在下面提供了一个实现。有几个细节值得注意:

  1. 输入需要为张量;于是我需要手动进行转换。
  2. 输出是一个词典。文档将其描述为"具有从签名键到函数的签名属性映射的可跟踪对象。"

在我的例子中,词典的关键字是一个相对随意的"Dense_83"。这看起来有点..。具体的。因此,我将解决方案概括为使用迭代器忽略键:

import tensorflow as tf
input_data = tf.constant(input_data, dtype=tf.float32)
prediction_tensors = signature_collection.signatures["serving_default"](input_data)
for _, values in prediction_tensors.items():
    predictions = values.numpy()[0]
    return predictions
raise Exception("Expected a response from predict(...).")

这篇关于如何使用tf.saveModel加载模型并调用预测函数[TensorFlow 2.0 API]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆