SageMaker 脚本模式服务

SageMaker Script Mode Serving

我已经使用 SageMaker 脚本模式训练了一个 tensorflow.keras 模型,如下所示:

import os

import sagemaker
from sagemaker.tensorflow import TensorFlow

estimator = TensorFlow(entry_point='train.py',
                       source_dir='src',
                       train_instance_type=train_instance_type,
                       train_instance_count=1,
                       hyperparameters=hyperparameters,
                       role=sagemaker.get_execution_role(),
                       framework_version='1.12.0',
                       py_version='py3', 
                       script_mode=True)

但是,调用 estimator.deploy() 时如何指定服务代码?默认情况下是什么?还有什么方法可以使用脚本模式修改 nginx.conf?

Tensorflow 容器是开源的:https://github.com/aws/sagemaker-tensorflow-container您可以查看它的具体工作原理。当然,您可以调整它,在本地构建它,将它推送到 ECR 并在 SageMaker 上使用它:)

一般有两种部署方式:

我还建议您查看此处的 TensorFlow 示例:https://github.com/awslabs/amazon-sagemaker-examples/tree/master/sagemaker-python-sdk

对于脚本模式,默认服务方法是基于 TensorFlow 服务的方法: https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/estimator.py#L393 基于 TFS 的容器不允许自定义脚本。您可以使用 serving_input_receiver_fn 指定如何处理输入数据,如下所述:https://www.tensorflow.org/guide/saved_model

至于修改 ngnix.conf,没有支持的方法。取决于你想在配置文件中更改什么,你可以破解 sagemaker-python-sdk 为这些环境变量传递不同的值:https://github.com/aws/sagemaker-tensorflow-serving-container/blob/3fd736aac4b0d97df5edaea48d37c49a1688ad6e/container/sagemaker/serve.py#L29

您可以在此处覆盖环境变量:https://github.com/aws/sagemaker-python-sdk/blob/master/src/sagemaker/tensorflow/serving.py#L130