如何在调用体中使用csv文件调用使用TensorFlow训练的SageMaker模型?

时间:2018-04-11 12:46:19

标签: tensorflow amazon-sagemaker

我在AWS SageMaker上部署了TensorFlow模型,我希望能够使用csv文件作为调用主体来调用它。文档说明了如下所示创建serving_input_function

def serving_input_fn(hyperparameters):
  # Logic to the following:
  # 1. Defines placeholders that TensorFlow serving will feed with inference requests
  # 2. Preprocess input data
  # 3. Returns a tf.estimator.export.ServingInputReceiver or tf.estimator.export.TensorServingInputReceiver,
  # which packages the placeholders and the resulting feature Tensors together.

在步骤2中,它说预处理输入数据,如何处理输入数据以处理它们?

2 个答案:

答案 0 :(得分:1)

我遇到了同样的问题,但是我想处理jpeg请求。

准备好model_data后,可以使用以下几行进行部署。

from sagemaker.tensorflow.model import TensorFlowModel
sagemaker_model = TensorFlowModel(
            model_data = 's3://path/to/model/model.tar.gz',
            role = role,
            framework_version = '1.12',
            entry_point = 'train.py',
            source_dir='my_src',
            env={'SAGEMAKER_REQUIREMENTS': 'requirements.txt'}
)

predictor = sagemaker_model.deploy(
    initial_instance_count=1,
    instance_type='ml.m4.xlarge', 
    endpoint_name='resnet-tensorflow-classifier'
)

您的笔记本应该具有一个my_src目录,其中包含文件train.py和一个requirements.txt文件。 train.py文件应具有定义的功能input_fn。对我来说,该函数处理图片/ jpeg内容:

CSV_CONTENT_TYPE = 'text/csv'

# Deserialize the Invoke request body into an object we can perform prediction on
def input_fn(request_body, content_type=CSV_CONTENT_TYPE):
    # process an image uploaded to the endpoint
    if content_type == CSV_CONTENT_TYPE:
        ##handle input 
        return handled_input

    else: 
        raise errors.UnsupportedFormatError(content_type)

如果您的train.py代码导入了某些模块,则必须提供requirements.txt来定义那些依赖项(那是我在文档中找不到的部分)。

希望这对以后的人有帮助。

答案 1 :(得分:0)

您可以通过添加input_fn来预处理输入数据,每次调用和端点时都会调用该输入数据。它接收输入数据和数据的内容类型。

def input_fn(data, content_type):
    // do some data preprocessing.
    return preprocessed_data

本文更深入地解释了它: https://docs.aws.amazon.com/sagemaker/latest/dg/tf-training-inference-code-template.html