使用 Boto3 部署編譯的模型 - Amazon SageMaker

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

使用 Boto3 部署編譯的模型

如果模型是使用 AWS SDK for Python (Boto3)、或 Amazon SageMaker 主控台編譯 AWS CLI,則必須符先決條件部分。請遵循下列步驟,使用適用於 Python 的 Amazon Web Services 開發套件 (Bo to3) 建立和部署 SageMaker 新編譯的模型。

部署模型

在您符合先決條件之後,請使用 create_modelcreate_enpoint_configcreate_endpoint API。

下列範例示範如何使用這些 API 部署使用 Neo 編譯的模型:

import boto3 client = boto3.client('sagemaker') # create sagemaker model create_model_api_response = client.create_model( ModelName='my-sagemaker-model', PrimaryContainer={ 'Image': <insert the ECR Image URI>, 'ModelDataUrl': 's3://path/to/model/artifact/model.tar.gz', 'Environment': {} }, ExecutionRoleArn='ARN for AmazonSageMaker-ExecutionRole' ) print ("create_model API response", create_model_api_response) # create sagemaker endpoint config create_endpoint_config_api_response = client.create_endpoint_config( EndpointConfigName='sagemaker-neomxnet-endpoint-configuration', ProductionVariants=[ { 'VariantName': <provide your variant name>, 'ModelName': 'my-sagemaker-model', 'InitialInstanceCount': 1, 'InstanceType': <provide your instance type here> }, ] ) print ("create_endpoint_config API response", create_endpoint_config_api_response) # create sagemaker endpoint create_endpoint_api_response = client.create_endpoint( EndpointName='provide your endpoint name', EndpointConfigName=<insert your endpoint config name>, ) print ("create_endpoint API response", create_endpoint_api_response)
注意

AmazonSageMakerFullAccessAmazonS3ReadOnlyAccess 政策必須連接到 AmazonSageMaker-ExecutionRole IAM 角色。

如需 create_modelcreate_endpoint_configcreate_endpoint API 的完整語法,請分別參閱 create_modelcreate_endpoint_config 以及 create_endpoint

如果您沒有使用訓練模型 SageMaker,請指定下列環境變數:

MXNet and PyTorch
"Environment": { "SAGEMAKER_PROGRAM": "inference.py", "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code", "SAGEMAKER_CONTAINER_LOG_LEVEL": "20", "SAGEMAKER_REGION": "insert your region", "MMS_DEFAULT_RESPONSE_TIMEOUT": "500" }
TensorFlow
"Environment": { "SAGEMAKER_PROGRAM": "inference.py", "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code", "SAGEMAKER_CONTAINER_LOG_LEVEL": "20", "SAGEMAKER_REGION": "insert your region" }

如果您使用訓練模型 SageMaker,請將環境變數指定SAGEMAKER_SUBMIT_DIRECTORY為包含訓練指令碼的完整 Amazon S3 儲存貯體 URI。