

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 部署 Autopilot 模型进行实时推理
<a name="text-classification-deploy-models"></a>

训练 Amazon A SageMaker utopilot 模型后，您可以设置终端节点并以交互方式获取预测结果。以下部分介绍将模型部署到 SageMaker AI 实时推理端点以从模型中获取预测的步骤。

## Real-time 推断
<a name="autopilot-deploy-models-text-image-classification-realtime"></a>

Real-time 推理非常适合需要实时、交互式、低延迟的推理工作负载。此部分演示如何使用实时推理，以交互方式从模型获取预测。

您可以使用 SageMaker API 手动部署在自动驾驶实验中生成最佳验证指标的模型，如下所示。

或者，当您创建 Autopilot 实验时，也可选择自动部署选项。有关设置模型自动部署的信息，请参阅请求参数 `[CreateAutoMLJobV2](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#API_CreateAutoMLJobV2_RequestParameters)` 中的 `[ModelDeployConfig](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateAutoMLJobV2.html#sagemaker-CreateAutoMLJobV2-request-ModelDeployConfig)`。这将自动创建一个端点。

**注意**  
为避免产生不必要的费用，您可以删除从模型部署中创建的不需要端点和资源。有关按地区划分的实例定价的信息，请参阅 [Amazon SageMaker 定价](https://www.amazonaws.cn/sagemaker/pricing/)。

1. **获取候选容器定义**

   从中获取候选容器定义[InferenceContainers](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_AutoMLCandidate.html#sagemaker-Type-AutoMLCandidate-InferenceContainers)。用于推理的容器定义是指专为部署和运行经过训练的 SageMaker AI 模型进行预测而设计的容器化环境。

   以下 Amazon CLI 命令示例使用 [DescribeAutoMLJobV2](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_DescribeAutoMLJobV2.html)API 获取最佳候选模型的候选定义。

   ```
   aws sagemaker describe-auto-ml-job-v2 --auto-ml-job-name {{job-name}} --region {{region}}
   ```

1. **列出候选项**

   以下 Amazon CLI 命令示例使用 [ListCandidatesForAutoMLJob](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_ListCandidatesForAutoMLJob.html)API 列出所有候选模型。

   ```
   aws sagemaker list-candidates-for-auto-ml-job --auto-ml-job-name {{<job-name>}} --region {{<region>}}
   ```

1. **创建 A SageMaker I 模型**

   使用前面步骤中的容器定义和您选择的候选容器通过[CreateModel](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateModel.html)该 API 创建 SageMaker AI 模型。以以下 Amazon CLI 命令为例。

   ```
   aws sagemaker create-model --model-name '{{<your-candidate-name>}}' \
                       --containers ['{{<container-definition1}}>, {{<container-definition2>}}, {{<container-definition3>}}]' \
                       --execution-role-arn '{{<execution-role-arn>}}' --region '{{<region>}}
   ```

1. **创建端点配置**

   以下 Amazon CLI 命令示例使用 [CreateEndpointConfig](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateEndpointConfig.html)API 创建终端节点配置。

   ```
   aws sagemaker create-endpoint-config --endpoint-config-name '{{<your-endpoint-config-name>}}' \
                       --production-variants '{{<list-of-production-variants>}}' \
                       --region '{{<region>}}'
   ```

1. **创建端点** 

   以下 Amazon CLI 示例使用 [CreateEndpoint](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_CreateEndpoint.html)API 创建终端节点。

   ```
   aws sagemaker create-endpoint --endpoint-name '{{<your-endpoint-name>}}' \
                       --endpoint-config-name '{{<endpoint-config-name-you-just-created>}}' \
                       --region '{{<region>}}'
   ```

   使用 [DescribeEndpoint](https://docs.amazonaws.cn/sagemaker/latest/APIReference/API_DescribeEndpoint.html)API 检查您的终端节点部署进度。以以下 Amazon CLI 命令为例。

   ```
   aws sagemaker describe-endpoint —endpoint-name '{{<endpoint-name>}}' —region {{<region>}}
   ```

   将 `EndpointStatus` 更改为 `InService` 后，端点即可用于实时推理。

1. **调用端点** 

   以下命令结构调用端点以进行实时推理。

   ```
   aws sagemaker invoke-endpoint --endpoint-name '{{<endpoint-name>}}' \ 
                     --region '{{<region>}}' --body '{{<your-data>}}' [--content-type] '{{<content-type>}}' {{<outfile>}}
   ```