SageMaker创建PyTorchModel而不部署

时间:2019-06-07 20:12:23

标签: python amazon-sagemaker

如果我像这样实例化SageMaker PyTorchModel对象:

from sagemaker.pytorch import PyTorchModel

model = PyTorchModel(name=name_from_base('model-name'),
                     model_data=model_data,
                     role=role,
                     framework_version='1.0.0',
                     entry_point='serve.py',
                     source_dir='src',
                     sagemaker_session=sagemaker_session,
                     predictor_cls=ImagePredictor)

#model.create_without_deploying??

是否可以使用sagemaker python SDK创建此模型,以使该模型显示在SageMaker控制台中,但没有实际将其部署到端点?

1 个答案:

答案 0 :(得分:1)

我认为无法使用高级SageMaker Pyhton SDK来做到这一点。但是,您应该可以通过使用低级boto3 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_model调用CreateModel API来做到这一点。供您参考,下面是有关如何执行此操作的示例代码段。

%%time
import boto3
import time

sage = boto3.Session().client(service_name='sagemaker')

image_uri = '520713654638.dkr.ecr.us-east-1.amazonaws.com/sagemaker-pytorch:1.0.0-cpu-py3'
model_data ='s3://<bucket>/<prefix>/output/model.tar.gz'
source = 's3://<bucket>/<prefix>/sourcedir.tar.gz'
role = 'arn:aws:iam::xxxxxxxx:role/service-role/AmazonSageMaker-ExecutionRole-xxxxxx'

timestamp = time.strftime('-%Y-%m-%d-%H-%M-%S', time.gmtime())
model_name = 'my-pytorch-model' + timestamp

response = sage.create_model(
    ModelName=model_name,
    PrimaryContainer={
        'Image': image_uri,
        'ModelDataUrl': model_data,
        'Environment': { 'SAGEMAKER_CONTAINER_LOG_LEVEL':'20', 'SAGEMAKER_ENABLE_CLOUDWATCH_METRICS': 'False', 
                   'SAGEMAKER_PROGRAM': 'generate.py','SAGEMAKER_REGION': 'us-east-1','SAGEMAKER_SUBMIT_DIRECTORY': source}
         },
         ExecutionRoleArn=role
}
print(response)

如果未收到错误消息,则模型将显示在SageMaker控制台中