当使用TensorFlow服务提供TF模型时,应该在哪里执行预处理和后处理步骤?

Maj*_*idL 36 tensorflow tensorflow-serving

通常,要使用TF图,必须将原始数据转换为数值.我将此过程称为预处理步骤.例如,如果原始数据是一个句子,那么一种方法是将句子标记化并将每个单词映射到唯一的数字.此预处理为每个句子创建一个数字序列,这将是模型的输入.

我们还需要对模型的输出进行后处理以解释它.例如,将模型生成的数字序列转换为单词,然后构建句子.

TF Serving是Google最近推出的一项新技术,用于服务TF模型.我的问题是:

当使用TensorFlow服务提供TF模型时,应该在哪里执行预处理和后处理?

我应该封装前处理和后处理步骤,在我的TF图(例如,使用py_funmap_fn),或有其它TensorFlow技术,我不知道的.

and*_*vog 5

我在这里遇到了同样的问题,即使我还不是 100% 确定如何使用 wordDict 变量(我猜你也使用一个来映射单词及其 id),主要的预处理和后处理- 流程函数定义如下:

https://www.tensorflow.org/programmers_guide/saved_model

作为export_outputsserving_input_receiver_fn.

  • exports_outputs

EstimatorSpec如果您使用估计器,则需要定义。这是分类算法的示例

  predicted_classes = tf.argmax(logits, 1)
  categories_tensor = tf.convert_to_tensor(CATEGORIES, tf.string)
  export_outputs = { "categories": export_output.ClassificationOutput(classes=categories_tensor) }
  if mode == tf.estimator.ModeKeys.PREDICT:
    return tf.estimator.EstimatorSpec(
        mode=mode,
        predictions={
            'class': predicted_classes,
            'prob': tf.nn.softmax(logits)
        },
        export_outputs=export_outputs)
Run Code Online (Sandbox Code Playgroud)
  • serving_input_receiver_fn

它需要在导出经过训练的估计器模型之前进行定义,它假设输入是原始字符串并从那里解析您的输入,您可以编写自己的函数,但我不确定何时可以使用外部变量。这是分类算法的一个简单示例:

def serving_input_receiver_fn():
    feature_spec = { "words": tf.FixedLenFeature(dtype=tf.int64, shape=[4]) }
    return tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)()

  export_dir = classifier.export_savedmodel(export_dir_base=args.job_dir,
                                            serving_input_receiver_fn=serving_input_receiver_fn)
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。