先决条件 - Amazon SageMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

先决条件

要使用 Amazon SageMaker Inference Recomperder,请首先确保您已满足以下列表中的先决条件。例如,我们展示了如何使用 PyTorch (v1.7.1) ResNet -18 预训练模型来处理这两种类型的 Amazon SageMaker Inference 推荐器推荐任务。所示的示例使用 Amazon SDK for Python (Boto3).

注意
  • 以下代码示例使用 Python。如果在终端或 Amazon CLI中运行以下任意代码示例,请删除 ! 前缀字符。

  • 你可以在亚马逊 SageMaker Studio 笔记本中使用 Python 3(TensorFlow 2.6 Python 3.8 CPU 优化)内核运行以下示例。有关 Studio 的更多信息,请参阅亚马逊 SageMaker Studio

  1. 为亚马逊创建 IAM 角色 SageMaker。

    为亚马逊 SageMaker 创建一个附加了 IA AmazonSageMakerFullAccess M 托管策略的 IAM 角色。

  2. 设置您的环境。

    导入依赖关系并为您的 Amazon Web Services 区域、您 SageMaker的 IAM 角色(从步骤 1 开始)和 SageMaker 客户端创建变量。

    !pip install --upgrade pip awscli botocore boto3 --quiet from sagemaker import get_execution_role, Session, image_uris import boto3 region = boto3.Session().region_name role = get_execution_role() sagemaker_client = boto3.client("sagemaker", region_name=region) sagemaker_session = Session()
  3. (可选)查看已由 Inference Recommender 进行基准测试的现有模型。

    来自常用模型库的 Inference Recommender 基准模型。Inference Recommender 为您的模型提供支持,即使它尚未进行基准测试也是如此。

    使用 ListModelMetaData 获取一个响应对象,其中列出了常见模型库中包含的机器学习模型的域、框架、任务和模型名称。

    在后面的步骤中,您可以使用域、框架、框架版本、任务和模型名称来选择推理 Docker 镜像并将模型注册到 Model Registry SageMaker 。下面演示了如何使用适用于 Python 的 SDK (Boto3) 列出模型元数据:

    list_model_metadata_response=sagemaker_client.list_model_metadata()

    输出包括模型摘要 (ModelMetadataSummaries) 和响应元数据 (ResponseMetadata),类似于以下示例:

    { 'ModelMetadataSummaries': [{ 'Domain': 'NATURAL_LANGUAGE_PROCESSING', 'Framework': 'PYTORCH:1.6.0', 'Model': 'bert-base-cased', 'Task': 'FILL_MASK' }, { 'Domain': 'NATURAL_LANGUAGE_PROCESSING', 'Framework': 'PYTORCH:1.6.0', 'Model': 'bert-base-uncased', 'Task': 'FILL_MASK' }, { 'Domain': 'COMPUTER_VISION', 'Framework': 'MXNET:1.8.0', 'Model': 'resnet18v2-gluon', 'Task': 'IMAGE_CLASSIFICATION' }, { 'Domain': 'COMPUTER_VISION', 'Framework': 'PYTORCH:1.6.0', 'Model': 'resnet152', 'Task': 'IMAGE_CLASSIFICATION' }], 'ResponseMetadata': { 'HTTPHeaders': { 'content-length': '2345', 'content-type': 'application/x-amz-json-1.1', 'date': 'Tue, 19 Oct 2021 20:52:03 GMT', 'x-amzn-requestid': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' }, 'HTTPStatusCode': 200, 'RequestId': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'RetryAttempts': 0 } }

    在本演示中,我们使用 PyTorch (v1.7.1) ResNet -18 模型来执行图像分类。以下 Python 代码示例将框架、框架版本、域和任务存储到变量中以便以后使用:

    # ML framework details framework = 'pytorch' framework_version = '1.7.1' # ML model details ml_domain = 'COMPUTER_VISION' ml_task = 'IMAGE_CLASSIFICATION'
  4. 将机器学习模型上传到 Amazon S3。

    如果您没有预先训练的机器学习模型,请使用此 PyTorch (v1.7.1) ResNet -18 模型:

    # Optional: Download a sample PyTorch model import torch from torchvision import models, transforms, datasets # Create an example input for tracing image = torch.zeros([1, 3, 256, 256], dtype=torch.float32) # Load a pretrained resnet18 model from TorchHub model = models.resnet18(pretrained=True) # Tell the model we are using it for evaluation (not training). Note this is required for Inferentia compilation. model.eval() model_trace = torch.jit.trace(model, image) # Save your traced model model_trace.save('model.pth')

    下载示例推理脚本 inference.py。创建 code 目录并将推理脚本移至 code 目录。

    # Download the inference script !wget https://aws-ml-blog-artifacts.s3.us-east-2.amazonaws.com/inference.py # move it into a code/ directory !mkdir code !mv inference.py code/

    Amazon SageMaker 要求将经过预训练的机器学习模型打包为压缩的 TAR 文件 (*.tar.gz)。压缩您的模型和推理脚本以满足以下要求:

    !tar -czf test.tar.gz model.pth code/inference.py

    预置端点时,会将存档中的文件提取到端点上的 /opt/ml/model/

    将模型和模型构件压缩为 .tar.gz 文件后,请将其上传到 Amazon S3 存储桶。以下示例演示如何使用以下方法将您的模型上传到 Amazon S3 Amazon CLI:

    !aws s3 cp test.tar.gz s3://{your-bucket}/models/
  5. 选择预构建的 Docker 推理映像或创建自己的推理 Docker 映像。

    SageMaker 为其内置算法提供了容器,并为一些最常见的机器学习框架(例如 Apache MX TensorFlow net、、和 Chainer)提供了预构建的 Docker 镜像。 PyTorch有关可用 SageMaker图像的完整列表,请参阅可用的 Deep Learning Containers 镜像

    如果现有 SageMaker 容器都不能满足您的需求,并且您没有自己的现有容器,请创建新的 Docker 镜像。请参阅使用您自己的推理代码以了解有关如何创建 Docker 映像的信息。

    以下内容演示如何使用 Pyth SageMaker on 软件开发工具包检索 1.7.1 PyTorch 版本的推理图像:

    from sagemaker import image_uris ## Uncomment and replace with your own values if you did not define ## these variables a previous step. #framework = 'pytorch' #framework_version = '1.7.1' # Note: you can use any CPU-based instance here, # this is just to set the arch as CPU for the Docker image instance_type = 'ml.m5.2xlarge' image_uri = image_uris.retrieve(framework, region, version=framework_version, py_version='py3', instance_type=instance_type, image_scope='inference')

    有关可用 SageMaker 实例的列表,请参阅 Amazon SageMaker 定价

  6. 创建示例负载存档。

    创建一个存档,其中包含负载测试工具可以发送到您的 SageMaker 端点的各个文件。您的推理代码必须能够从示例负载中读取文件格式。

    以下内容下载了一张.jpg 图像,此示例将在后面的步骤中用于 ResNet -18 模型。

    !wget https://cdn.pixabay.com/photo/2020/12/18/05/56/flowers-5841251_1280.jpg

    将示例有效载荷作为 tarball 压缩:

    !tar -cvzf payload.tar.gz flowers-5841251_1280.jpg

    将示例负载上传到 Amazon S3 并记下 Amazon S3 URI:

    !aws s3 cp payload.tar.gz s3://{bucket}/models/

    您需要在后面的步骤中使用 Amazon S3 URI,因此,请将它存储在变量中:

    bucket_prefix='models' bucket = '<your-bucket-name>' # Provide the name of your S3 bucket payload_s3_key = f"{bucket_prefix}/payload.tar.gz" sample_payload_url= f"s3://{bucket}/{payload_s3_key}"
  7. 为推荐作业准备模型输入

    对于最后一个先决条件,为您提供了两个选项来准备模型输入。您可以在 Model Regi SageMaker stry 中注册您的模型,该注册表可用于对模型进行编目以供生产,也可以创建 SageMaker 模型并在创建推荐任务时在ContainerConfig字段中进行指定。如果您想利用模型注册表提供的功能,例如管理模型版本和自动部署模型,那么最好是选择第一个选项。如果您想快速开始操作,那么第二个选项是理想之选。对于第一个选项,请转到步骤 7。对于第二个选项,请跳过步骤 7 并转到步骤 8。

  8. 选项 1:将模型注册到模型注册表中

    借助 SageMaker Model Registry,您可以对生产模型进行编目、管理模型版本、将元数据(例如训练指标)与模型关联起来、管理模型的批准状态、将模型部署到生产环境以及使用 CI/CD 自动部署模型。

    当您使用 SageMaker Model Registry 跟踪和管理模型时,它们在模型包组中以版本化模型包的形式表示。未版本化的模型包不属于模型组。模型包组包含模型的多个版本或迭代。尽管不需要为注册表中的每个模型创建它们,但它们有助于组织各种具有相同用途的模型并提供自动版本控制。

    要使用 Amazon SageMaker Inference 推荐器,您必须拥有版本控制的模型包。您可以使用 Amazon SDK for Python (Boto3) 或使用 Amazon SageMaker Studio Classic 以编程方式创建版本控制模型包。要以编程方式创建版本控制模型包,请首先使用 CreateModelPackageGroup API 创建模型包组。接下来,使用 CreateModelPackage API 创建模型包。调用此方法将生成版本控制模型包。

    有关如何以编程方式创建模型组注册模型版本交互方式创建模型包组以及如何使用和 Amazon SDK for Python (Boto3) Ama SageMaker zon Studio Classic 分别创建版本控制模型包组的详细说明,请参阅和。

    以下代码示例演示了如何使用 Amazon SDK for Python (Boto3)创建版本控制模型包。

    注意

    您无需批准模型包即可创建 Inference Recommender 作业。

    1. 创建模型包组

      使用 CreateModelPackageGroup API 创建模型包组。为 ModelPackageGroupName 的模型包组提供名称,并可以选择在 ModelPackageGroupDescription 字段中提供模型包的描述。

      model_package_group_name = '<INSERT>' model_package_group_description = '<INSERT>' model_package_group_input_dict = { "ModelPackageGroupName" : model_package_group_name, "ModelPackageGroupDescription" : model_package_group_description, } model_package_group_response = sagemaker_client.create_model_package_group(**model_package_group_input_dict)

      有关您可以传递的可选参数和必填参数的完整列表,请参阅 Amazon SageMaker API 参考指南CreateModelPackageGroup

      通过指定运行推理代码的 Docker 映像和模型工件的 Amazon S3 位置来创建模型包,并为其提供值。InferenceSpecification InferenceSpecification应包含有关可使用基于此模型包的模型运行的推理作业的信息,包括以下内容:

      • 运行您的推理代码的映像的 Amazon ECR 路径。

      • (可选)模型包支持的变换任务的实例类型和用于推理的实时终端节点。

      • 模型包支持用于推理的输入和输出内容格式。

      此外,在创建模型包时,必须指定以下参数:

      • :模型包及其组件的机器学习域。常见的机器学习域包括计算机视觉和自然语言处理。

      • 任务:模型包完成的机器学习任务。常见的机器学习任务包括对象检测和图像分类。如果 API 参考指南中列出的任务均无法满足您的使用案例,请指定“其他”。有关支持的机器学习任务的列表,请参阅任务 API 字段描述。

      • SamplePayloadUrl:存储示例负载的亚马逊简单存储服务 (Amazon S3) Service 路径。此路径必须指向单个 GZIP 压缩的 TAR 存档(.tar.gz 后缀)。

      • 框架:模型包容器映像的机器学习框架。

      • FrameworkVersion:模型包容器镜像的框架版本。

      如果您提供实例类型的允许列表以用于实时生成推断 SupportedRealtimeInferenceInstanceTypes,则推断推荐器会在作业期间限制实例类型的搜索空间。Default如果您有预算限制,或知道有一组特定的实例类型可以支持您的模型和容器映像,请使用此参数。

      在前面的步骤中,我们下载了一个预训练的 ResNet 18 模型,并将其存储在 Amazon S3 存储桶中的一个名models为的目录中。我们检索了一张 PyTorch (v1.7.1) 深度学习容器推理图像,并将 URI 存储在一个名为的变量中。image_uri在以下代码示例中使用这些变量来定义用作 CreateModelPackageAPI 输入的字典。

      # Provide the Amazon S3 URI of your compressed tarfile # so that Model Registry knows where to find your model artifacts bucket_prefix='models' bucket = '<your-bucket-name>' # Provide the name of your S3 bucket model_s3_key = f"{bucket_prefix}/test.tar.gz" model_url= f"s3://{bucket}/{model_s3_key}" # Similar open source model to the packaged model # The name of the ML model as standardized by common model zoos nearest_model_name = 'resnet18' # The supported MIME types for input and output data. In this example, # we are using images as input. input_content_type='image/jpeg' # Optional - provide a description of your model. model_package_description = '<INSERT>' ## Uncomment if you did not store the domain and task in an earlier ## step #ml_domain = 'COMPUTER_VISION' #ml_task = 'IMAGE_CLASSIFICATION' ## Uncomment if you did not store the framework and framework version ## in a previous step. #framework = 'PYTORCH' #framework_version = '1.7.1' # Optional: Used for optimizing your model using SageMaker Neo # PyTorch uses NCHW format for images data_input_configuration = "[[1,3,256,256]]" # Create a dictionary to use as input for creating a model pacakge group model_package_input_dict = { "ModelPackageGroupName" : model_package_group_name, "ModelPackageDescription" : model_package_description, "Domain": ml_domain, "Task": ml_task, "SamplePayloadUrl": sample_payload_url, "InferenceSpecification": { "Containers": [ { "Image": image_uri, "ModelDataUrl": model_url, "Framework": framework.upper(), "FrameworkVersion": framework_version, "NearestModelName": nearest_model_name, "ModelInput": {"DataInputConfig": data_input_configuration} } ], "SupportedContentTypes": [input_content_type] } }
    2. 创建模型包

      使用 CreateModelPackage API 创建模型包。传递上一步中定义的输入字典:

      model_package_response = sagemaker_client.create_model_package(**model_package_input_dict)

      您需要模型包 ARN 才能使用 Amazon SageMaker 推理推荐器。记下模型包的 ARN 或将它存储在变量中:

      model_package_arn = model_package_response["ModelPackageArn"] print('ModelPackage Version ARN : {}'.format(model_package_arn))
  9. 选项 2:创建模型并配置 ContainerConfig 字段

    如果您需要开始推理推荐作业,并且不需要在模型注册表中注册模型,请使用此选项。在以下步骤中,您将在中创建模型 SageMaker 并将该ContainerConfig字段配置为推荐作业的输入。

    1. 创建模型

      使用 CreateModel API 创建模型。有关在将模型部署到 SageMaker Hosting 时调用此方法的示例,请参阅创建模型 (Amazon SDK for Python (Boto3))

      在前面的步骤中,我们下载了一个预训练的 ResNet 18 模型,并将其存储在 Amazon S3 存储桶中的一个名models为的目录中。我们检索了一张 PyTorch (v1.7.1) 深度学习容器推理图像,并将 URI 存储在一个名为的变量中。image_uri我们在下面的代码示例中使用这些变量,其中我们定义了用作 CreateModel API 的输入的字典。

      model_name = '<name_of_the_model>' # Role to give SageMaker permission to access AWS services. sagemaker_role= "arn:aws:iam::<region>:<account>:role/*" # Provide the Amazon S3 URI of your compressed tarfile # so that Model Registry knows where to find your model artifacts bucket_prefix='models' bucket = '<your-bucket-name>' # Provide the name of your S3 bucket model_s3_key = f"{bucket_prefix}/test.tar.gz" model_url= f"s3://{bucket}/{model_s3_key}" #Create model create_model_response = sagemaker_client.create_model( ModelName = model_name, ExecutionRoleArn = sagemaker_role, PrimaryContainer = { 'Image': image_uri, 'ModelDataUrl': model_url, })
    2. 配置 ContainerConfig 字段

      接下来,您必须使用刚刚创建的模型配置该ContainerConfig字段,并在其中指定以下参数:

      • Domain:模型及其组件的机器学习域,例如计算机视觉或自然语言处理。

      • Task:模型完成的机器学习任务,例如图像分类或对象检测。

      • PayloadConfig:推荐作业的负载的配置。有关子字段的更多信息,请参阅 RecommendationJobPayloadConfig

      • Framework: 容器镜像的机器学习框架,例如 PyTorch。

      • FrameworkVersion:容器映像的框架版本。

      • (可选)SupportedInstanceTypes:用于实时生成推理的实例类型的列表。

      如果您使用 SupportedInstanceTypes 参数,则 Inference Recommender 会限制 Default 作业期间实例类型的搜索空间。如果您有预算限制,或知道有一组特定的实例类型可以支持您的模型和容器映像,请使用此参数。

      在下面的代码示例中,我们使用之前定义的参数以及 NearestModelName 来定义用作 CreateInferenceRecommendationsJob API 的输入的字典。

      ## Uncomment if you did not store the domain and task in a previous step #ml_domain = 'COMPUTER_VISION' #ml_task = 'IMAGE_CLASSIFICATION' ## Uncomment if you did not store the framework and framework version in a previous step #framework = 'PYTORCH' #framework_version = '1.7.1' # The name of the ML model as standardized by common model zoos nearest_model_name = 'resnet18' # The supported MIME types for input and output data. In this example, # we are using images as input input_content_type='image/jpeg' # Optional: Used for optimizing your model using SageMaker Neo # PyTorch uses NCHW format for images data_input_configuration = "[[1,3,256,256]]" # Create a dictionary to use as input for creating an inference recommendation job container_config = { "Domain": ml_domain, "Framework": framework.upper(), "FrameworkVersion": framework_version, "NearestModelName": nearest_model_name, "PayloadConfig": { "SamplePayloadUrl": sample_payload_url, "SupportedContentTypes": [ input_content_type ] }, "DataInputConfig": data_input_configuration "Task": ml_task, }