

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

# 运行经过训练的 Amazon Rekognition Custom Labels 模型
<a name="running-model"></a>

当您对模型性能感到满意后，就可以开始使用它了。您可以使用控制台或 AWS SDK 启动和停止模型。控制台还包含您可以使用的示例 SDK 操作。

**Topics**
+ [推理单元](#running-model-inference-units)
+ [可用区](#running-model-availability-zones)
+ [启动 Amazon Rekognition Custom Labels 模型](rm-start.md)
+ [停止 Amazon Rekognition Custom Labels 模型](rm-stop.md)
+ [报告运行时长和使用的推理单元](rm-model-usage.md)

## 推理单元
<a name="running-model-inference-units"></a>

启动模型时，需要指定模型使用的计算资源（称为推理单元）的数量。

**重要**  
根据您配置模型运行的方式，您需要为模型运行的小时数和模型在运行时使用的推理单元数付费。例如，如果使用两个推理单元启动模型并使用模型 8 小时，则需要支付 16 个推理小时（8 小时运行时间 x 两个推理单元）的费用。有关更多信息，请参阅[推理小时](https://aws.amazon.com/rekognition/pricing/#Amazon_Rekognition_Custom_Labels_pricing)。如果没有明确[停止模型](rm-stop.md)，则即使没有主动使用模型来分析图像，也需要付费。

单个推理单元支持的每秒事务数 (TPS) 受以下因素影响。
+ 检测图像级标签（分类）的模型通常比使用边界框检测和定位物体（物体检测）的模型具有更高的 TPS。
+ 模型的复杂性。
+ 分辨率更高的图像需要的分析时间更多。
+ 图像中包含的物体越多，需要的分析时间也越多。
+ 小图像的分析速度比大图像更快。
+ 以图像字节形式传递的图像与先将图像上传到 Amazon S3 存储桶，然后再引用上传的图像相比，前者的分析速度更快。以图像字节形式传递的图像必须小于 4.0 MB。在近乎实时地处理大小小于 4.0 MB 的图像时，建议使用图像字节。例如，从 IP 摄像头捕获的图像。
+ 处理存储在 Amazon S3 存储桶中的图像，比下载图像、转换为图像字节，然后再传递图像字节进行分析要快。
+ 分析已存储在 Amazon S3 存储桶中的图像可能比分析作为图像字节传递的相同图像要快。当图像较大时，尤其如此。

如果调用 `DetectCustomLabels` 的次数超过了模型使用的推理单元总数所支持的最大 TPS，Amazon Rekognition Custom Labels 将返回 `ProvisionedThroughputExceededException` 异常。

### 使用推理单元管理吞吐量
<a name="running-model-manage-throughput"></a>

可以根据应用程序的需求提高或降低模型的吞吐量。可通过增加推理单元来提高吞吐量。每增加一个推理单元，您的处理速度就会增加一个推理单元。有关如何计算所需的推理单元数，请参阅[计算 Amazon Rekognition Custom Labels 和 Amazon Lookout for Vision 模型的推理单元数量](https://aws.amazon.com/blogs/machine-learning/calculate-inference-units-for-an-amazon-rekognition-custom-labels-model/)。如果要更改模型支持的吞吐量，您有两种选择：

#### 手动添加或移除推理单元
<a name="running-model-manual-inference-units"></a>

[停止](rm-stop.md)模型，然后使用所需数量的推理单元[重新启动](rm-start.md)模型。这种方法的缺点是，模型在重新启动时无法接收请求，也不能用于应对需求高峰。如果您的模型具有稳定的吞吐量，并且您的使用场景可以容忍 10-20 分钟的停机时间，请使用此方法。例如，您想使用每周计划对模型进行批量调用。

#### 自动扩缩推理单元数量
<a name="running-model-auto-scale-inference-units"></a>

如果模型必须应对需求高峰，Amazon Rekognition Custom Labels 可以自动扩缩模型使用的推理单元数量。随着需求的增加，Amazon Rekognition Custom Labels 会向模型添加额外的推理单元，并在需求减少时将其移除。

要让 Amazon Rekognition Custom Labels 自动扩缩模型的推理单元，请[启动](rm-start.md)模型并使用 `MaxInferenceUnits` 参数设置模型可使用的最大推理单元数。通过设置最大推理单元数，您可以通过限制模型可使用的推理单元数量来管理模型的运行成本。如果不指定最大单元数，Amazon Rekognition Custom Labels 将不会自动扩缩模型，而只会使用启动时所用的推理单元数。有关最大推理单元数的信息，请参阅[服务限额](https://docs.aws.amazon.com/general/latest/gr/rekognition.html#limits_rekognition)。

您也可使用 `MinInferenceUnits` 参数指定最小推理单元数。这可让您为模型指定最小吞吐量，这里的一个推理单元代表 1 小时的处理时间。

**注意**  
无法通过 Amazon Rekognition Custom Labels 控制台设置最大推理单元数，而应通过为 `StartProjectVersion` 操作指定 `MaxInferenceUnits` 输入参数来设置。

Amazon Rekognition 自定义标签提供了以下 CloudWatch 亚马逊日志指标，您可以使用这些指标来确定模型当前的自动扩展状态。


| 指标 | 说明 | 
| --- | --- | 
|  `DesiredInferenceUnits`  |  Amazon Rekognition Custom Labels 要增加或缩减到的推理单元数量。  | 
|  `InServiceInferenceUnits`  |  模型正在使用的推理单元数量。  | 

如果 `DesiredInferenceUnits` = `InServiceInferenceUnits`，Amazon Rekognition Custom Labels 目前不会增减推理单元的数量。

如果 `DesiredInferenceUnits` > `InServiceInferenceUnits`，Amazon Rekognition Custom Labels 会将数量增加到 `DesiredInferenceUnits` 的值。

如果 `DesiredInferenceUnits` < `InServiceInferenceUnits`，Amazon Rekognition Custom Labels 会将数量缩减到 `DesiredInferenceUnits` 的值。

 [有关亚马逊 Rekognition 自定义标签和筛选维度返回的指标的更多信息，请参阅 Rekognition 的指标。CloudWatch ](https://docs.aws.amazon.com/rekognition/latest/dg/cloudwatch-metricsdim.html)

要了解您为模型请求的最大推理单元数，请调用 `DescribeProjectsVersion` 并检查响应中的 `MaxInferenceUnits` 字段。有关示例代码，请参阅[描述模型 (SDK)](md-describing-model-sdk.md)。

## 可用区
<a name="running-model-availability-zones"></a>

Amazon Rekognition Custom Labels 会将推理单元分配到 AWS 区域内的多个可用区，以提高可用性。有关更多信息，请参阅[可用区](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/#Availability_Zones)。为帮助保护生产模型免受可用区中断和推理单元故障的影响，请使用至少两个推理单元启动生产模型。

如果可用区中断，可用区中的所有推理单元都将无法使用，模型容量也会减少。对的调[DetectCustomLabels](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectCustomLabels)用将重新分配到其余的推理单元中。如果此类调用不超过其余推理单元支持的每秒事务数 (TPS)，则会成功。AWS 修复可用区后，推理单元将重新启动，模型也将恢复其全部容量。

如果单个推理单元出现故障，Amazon Rekognition Custom Labels 会自动在同一可用区内启动新的推理单元。在新推理单元启动之前，模型容量会降低。

# 启动 Amazon Rekognition Custom Labels 模型
<a name="rm-start"></a>

您可以使用控制台或操作开始运行 Amazon Rekognition 自定义标签模型。[StartProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StartProjectVersion)

**重要**  
您需要为模型运行的小时数和模型在运行时使用的推理单元数付费。有关更多信息，请参阅 [运行经过训练的 Amazon Rekognition Custom Labels 模型](running-model.md)。

启动模型可能需要几分钟才能完成。要检查模型的当前就绪状态，请查看项目或用途的详细信息页面[DescribeProjectVersions](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DescribeProjectVersions)。

模型启动后 [DetectCustomLabels](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectCustomLabels)，您可以使用模型来分析图像。有关更多信息，请参阅 [使用经过训练的模型分析图像](detecting-custom-labels.md)。控制台还提供了调用 `DetectCustomLabels` 的示例代码。

**Topics**
+ [启动 Amazon Rekognition Custom Labels 模型（控制台）](#rm-start-console)
+ [启动 Amazon Rekognition Custom Labels 模型 (SDK)](#rm-start-sdk)

## 启动 Amazon Rekognition Custom Labels 模型（控制台）
<a name="rm-start-console"></a>

按照以下过程通过控制台启动运行 Amazon Rekognition Custom Labels 模型。您可以直接从控制台启动模型，也可以使用控制台提供的 AWS SDK 代码。

**启动模型（控制台）**

1. 打开亚马逊 Rekognition 控制台，网址为[https://console.aws.amazon.com/rekognition/](https://console.aws.amazon.com/rekognition/)。

1. 选择**使用自定义标签**。

1. 选择**开始**。

1. 在左侧导航窗格中，选择**项目**。

1. 在**项目**资源页面上，选择包含要启动的已训练模型的项目。

1. 在**模型**部分中，选择要启动的模型。

1. 选择**使用模型**选项卡。

1. 执行下列操作之一：

------
#### [ Start model using the console ]

   在**启动或停止模型**部分中，执行以下操作：

   1. 选择要使用的推理单元的数量。有关更多信息，请参阅 [运行经过训练的 Amazon Rekognition Custom Labels 模型](running-model.md)。

   1. 选择**启动**。

   1. 在**启动模型**对话框中，选择**启动**。

------
#### [ Start model using the AWS SDK ]

   在**使用模型**部分中，执行以下操作：

   1. 选择 **API 代码**。

   1. 选择 **AWS CLI** 或 **Python**。

   1. 在**启动模型**中，复制示例代码。

   1. 使用示例代码启动模型。有关更多信息，请参阅 [启动 Amazon Rekognition Custom Labels 模型 (SDK)](#rm-start-sdk)。

------

1. 要返回到项目概述页面，请在页面顶部选择项目名称。

1. 在**模型**部分中，检查模型的状态。当模型状态为 **RUNNING** 时，就可以使用模型来分析图像。有关更多信息，请参阅 [使用经过训练的模型分析图像](detecting-custom-labels.md)。

## 启动 Amazon Rekognition Custom Labels 模型 (SDK)
<a name="rm-start-sdk"></a>

您可以通过调用 [StartProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StartProjectVersion)API 并在`ProjectVersionArn`输入参数中传递模型的 Amazon 资源名称 (ARN) 来启动模型。此外，还需要指定要使用的推理单元数量。有关更多信息，请参阅 [运行经过训练的 Amazon Rekognition Custom Labels 模型](running-model.md)。

模型可能需要一段时间才能启动。本主题中的 Python 和 Java 示例使用 waiter 来等待模型启动。waiter 是一种实用程序方法，用于轮询是否发生了特定状态。或者，您可以通过致电来查看当前状态[DescribeProjectVersions](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DescribeProjectVersions)。

**启动模型 (SDK)**

1. 如果您尚未这样做，请安装并配置 AWS CLI 和 AWS SDKs。有关更多信息，请参阅 [步骤 4：设置 AWS CLI 和 AWS SDKs](su-awscli-sdk.md)。

1. 使用以下示例代码启动模型。

------
#### [ CLI ]

   将 `project-version-arn` 的值更改为要启动的模型的 ARN。将 `--min-inference-units` 的值更改为要使用的推理单元数。（可选）将 `--max-inference-units` 更改为 Amazon Rekognition Custom Labels 可用于自动扩缩模型的最大推理单元数。

   ```
   aws rekognition start-project-version  --project-version-arn model_arn \
      --min-inference-units minimum number of units \
      --max-inference-units maximum number of units \
      --profile custom-labels-access
   ```

------
#### [ Python ]

   提供以下命令行参数：
   + `project_arn`：包含要启动的模型的项目的 ARN。
   + `model_arn`：要启动的模型的 ARN。
   + `min_inference_units`：要使用的推理单元数。
   + （可选）`--max_inference_units`：Amazon Rekognition Custom Labels 可用于自动扩缩模型的最大推理单元数。

   ```
   # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   # SPDX-License-Identifier: Apache-2.0
   
   """
   Purpose
   Shows how to start running an Amazon Lookout for Vision model.
   """
   
   import argparse
   import logging
   import boto3
   from botocore.exceptions import ClientError
   
   logger = logging.getLogger(__name__)
   
   
   def get_model_status(rek_client, project_arn, model_arn):
       """
       Gets the current status of an Amazon Rekognition Custom Labels model
       :param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
       :param project_name:  The name of the project that you want to use.
       :param model_arn:  The name of the model that you want the status for.
       :return: The model status
       """
   
       logger.info("Getting status for %s.", model_arn)
   
       # Extract the model version from the model arn.
       version_name = (model_arn.split("version/", 1)[1]).rpartition('/')[0]
   
       models = rek_client.describe_project_versions(ProjectArn=project_arn,
                                                     VersionNames=[version_name])
   
       for model in models['ProjectVersionDescriptions']:
   
           logger.info("Status: %s", model['StatusMessage'])
           return model["Status"]
   
       error_message = f"Model {model_arn} not found."
       logger.exception(error_message)
       raise Exception(error_message)
   
   
   def start_model(rek_client, project_arn, model_arn, min_inference_units, max_inference_units=None):
       """
       Starts the hosting of an Amazon Rekognition Custom Labels model.
       :param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
       :param project_name:  The name of the project that contains the
       model that you want to start hosting.
       :param min_inference_units: The number of inference units to use for hosting.
       :param max_inference_units: The number of inference units to use for auto-scaling
       the model. If not supplied, auto-scaling does not happen.
       """
   
       try:
           # Start the model
           logger.info(f"Starting model: {model_arn}. Please wait....")
   
           if max_inference_units is None:
               rek_client.start_project_version(ProjectVersionArn=model_arn,
                                                MinInferenceUnits=int(min_inference_units))
           else:
               rek_client.start_project_version(ProjectVersionArn=model_arn,
                                                MinInferenceUnits=int(
                                                    min_inference_units),
                                                MaxInferenceUnits=int(max_inference_units))
   
           # Wait for the model to be in the running state
           version_name = (model_arn.split("version/", 1)[1]).rpartition('/')[0]
           project_version_running_waiter = rek_client.get_waiter(
               'project_version_running')
           project_version_running_waiter.wait(
               ProjectArn=project_arn, VersionNames=[version_name])
   
           # Get the running status
           return get_model_status(rek_client, project_arn, model_arn)
   
       except ClientError as err:
           logger.exception("Client error: Problem starting model: %s", err)
           raise
   
   
   def add_arguments(parser):
       """
       Adds command line arguments to the parser.
       :param parser: The command line parser.
       """
   
       parser.add_argument(
           "project_arn", help="The ARN of the project that contains that the model you want to start."
       )
       parser.add_argument(
           "model_arn", help="The ARN of the model that you want to start."
       )
       parser.add_argument(
           "min_inference_units", help="The minimum number of inference units to use."
       )
       parser.add_argument(
           "--max_inference_units",  help="The maximum number of inference units to use for auto-scaling the model.", required=False
       )
   
   
   def main():
   
       logging.basicConfig(level=logging.INFO,
                           format="%(levelname)s: %(message)s")
   
       try:
   
           # Get command line arguments.
           parser = argparse.ArgumentParser(usage=argparse.SUPPRESS)
           add_arguments(parser)
           args = parser.parse_args()
   
           # Start the model.
           session = boto3.Session(profile_name='custom-labels-access')
           rekognition_client = session.client("rekognition")
           
           status = start_model(rekognition_client,
                                args.project_arn, args.model_arn,
                                args.min_inference_units,
                                args.max_inference_units)
   
           print(f"Finished starting model: {args.model_arn}")
           print(f"Status: {status}")
   
       except ClientError as err:
           error_message = f"Client error: Problem starting model: {err}"
           logger.exception(error_message)
           print(error_message)
   
       except Exception as err:
           error_message = f"Problem starting model:{err}"
           logger.exception(error_message)
           print(error_message)
   
   
   if __name__ == "__main__":
       main()
   ```

------
#### [ Java V2 ]

   提供以下命令行参数：
   + `project_arn`：包含要启动的模型的项目的 ARN。
   + `model_arn`：要启动的模型的 ARN。
   + `min_inference_units`：要使用的推理单元数。
   + （可选）`max_inference_units`：Amazon Rekognition Custom Labels 可用于自动扩缩模型的最大推理单元数。如果不指定值，则不会进行自动扩缩。

   ```
   /*
      Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
      SPDX-License-Identifier: Apache-2.0
   */
   package com.example.rekognition;
   
   import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
   import software.amazon.awssdk.core.waiters.WaiterResponse;
   import software.amazon.awssdk.regions.Region;
   import software.amazon.awssdk.services.rekognition.RekognitionClient;
   import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsRequest;
   import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsResponse;
   import software.amazon.awssdk.services.rekognition.model.ProjectVersionDescription;
   import software.amazon.awssdk.services.rekognition.model.ProjectVersionStatus;
   import software.amazon.awssdk.services.rekognition.model.RekognitionException;
   import software.amazon.awssdk.services.rekognition.model.StartProjectVersionRequest;
   import software.amazon.awssdk.services.rekognition.model.StartProjectVersionResponse;
   import software.amazon.awssdk.services.rekognition.waiters.RekognitionWaiter;
   
   import java.util.Optional;
   import java.util.logging.Level;
   import java.util.logging.Logger;
   
   public class StartModel {
   
       public static final Logger logger = Logger.getLogger(StartModel.class.getName());
       
       
       
       public static int findForwardSlash(String modelArn, int n) {
   
           int start = modelArn.indexOf('/');
           while (start >= 0 && n > 1) {
               start = modelArn.indexOf('/', start + 1);
               n -= 1;
           }
           return start;
   
       }
   
       public static void startMyModel(RekognitionClient rekClient, String projectArn, String modelArn,
               Integer minInferenceUnits, Integer maxInferenceUnits
               ) throws Exception, RekognitionException {
   
           try {
               
               logger.log(Level.INFO, "Starting model: {0}", modelArn);
               
               StartProjectVersionRequest startProjectVersionRequest = null;
               
               if (maxInferenceUnits == null) {
                   startProjectVersionRequest = StartProjectVersionRequest.builder()
                       .projectVersionArn(modelArn)
                       .minInferenceUnits(minInferenceUnits)
                       .build();
               }
               else {
                   startProjectVersionRequest = StartProjectVersionRequest.builder()
                           .projectVersionArn(modelArn)
                           .minInferenceUnits(minInferenceUnits)
                           .maxInferenceUnits(maxInferenceUnits)
                           .build();
                   
               }
   
               StartProjectVersionResponse response = rekClient.startProjectVersion(startProjectVersionRequest);
   
               logger.log(Level.INFO, "Status: {0}", response.statusAsString() );
               
               
               // Get the model version
   
               int start = findForwardSlash(modelArn, 3) + 1;
               int end = findForwardSlash(modelArn, 4);
   
               String versionName = modelArn.substring(start, end);
   
   
               // wait until model starts
   
               DescribeProjectVersionsRequest describeProjectVersionsRequest = DescribeProjectVersionsRequest.builder()
                       .versionNames(versionName)
                       .projectArn(projectArn)
                       .build();
   
               RekognitionWaiter waiter = rekClient.waiter();
   
               WaiterResponse<DescribeProjectVersionsResponse> waiterResponse = waiter
                       .waitUntilProjectVersionRunning(describeProjectVersionsRequest);
   
               Optional<DescribeProjectVersionsResponse> optionalResponse = waiterResponse.matched().response();
   
               DescribeProjectVersionsResponse describeProjectVersionsResponse = optionalResponse.get();
   
               for (ProjectVersionDescription projectVersionDescription : describeProjectVersionsResponse
                       .projectVersionDescriptions()) {
                   if(projectVersionDescription.status() == ProjectVersionStatus.RUNNING) {
                       logger.log(Level.INFO, "Model is running" );
                    
                   }
                   else {
                       String error = "Model training failed: " + projectVersionDescription.statusAsString() + " "
                               + projectVersionDescription.statusMessage() + " " + modelArn;
                       logger.log(Level.SEVERE, error);
                       throw new Exception(error);
                   }
                   
               }
   
   
           } catch (RekognitionException e) {
               logger.log(Level.SEVERE, "Could not start model: {0}", e.getMessage());
               throw e;
           }
   
       }
   
       public static void main(String[] args) {
   
           String modelArn = null;
           String projectArn = null;
           Integer minInferenceUnits = null;
           Integer maxInferenceUnits = null;
           
   
   
   
           final String USAGE = "\n" + "Usage: " + "<project_name> <version_name> <min_inference_units> <max_inference_units>\n\n" + "Where:\n"
                   + "   project_arn - The ARN of the project that contains the model that you want to start. \n\n"
                   + "   model_arn - The ARN of the model version that you want to start.\n\n"
                   + "   min_inference_units - The number of inference units to start the model with.\n\n"
                   + "   max_inference_units - The maximum number of inference units that Custom Labels can use to "
                   + "   automatically scale the model. If the value is null, automatic scaling doesn't happen.\n\n";
   
           if (args.length < 3  || args.length >4) {
               System.out.println(USAGE);
               System.exit(1);
           }
   
           projectArn = args[0];
           modelArn = args[1];
           minInferenceUnits=Integer.parseInt(args[2]);
           
           if (args.length == 4) {
               maxInferenceUnits = Integer.parseInt(args[3]);
           }
     
           try {
   
   
               // Get the Rekognition client.
               RekognitionClient rekClient = RekognitionClient.builder()
               .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access"))
               .region(Region.US_WEST_2)
               .build();
   
               // Start the model.
               startMyModel(rekClient, projectArn, modelArn, minInferenceUnits, maxInferenceUnits);
               
   
               System.out.println(String.format("Model started: %s", modelArn));
   
               rekClient.close();
   
           } catch (RekognitionException rekError) {
               logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage());
               System.exit(1);
           } catch (Exception rekError) {
               logger.log(Level.SEVERE, "Error: {0}", rekError.getMessage());
               System.exit(1);
           }
   
           
   
       }
   
   }
   ```

------

# 停止 Amazon Rekognition Custom Labels 模型
<a name="rm-stop"></a>

您可以使用控制台或使用操作来停止运行 Amazon Rekognition 自定义标签模型。[StopProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StopProjectVersion)

**Topics**
+ [停止 Amazon Rekognition Custom Labels 模型（控制台）](#rm-stop-console)
+ [停止 Amazon Rekognition Custom Labels 模型 (SDK)](#rm-stop-sdk)

## 停止 Amazon Rekognition Custom Labels 模型（控制台）
<a name="rm-stop-console"></a>

按照以下过程通过控制台停止运行 Amazon Rekognition Custom Labels 模型。您可以直接从控制台停止模型，也可以使用控制台提供的 AWS SDK 代码。

**停止模型（控制台）**

1. 打开亚马逊 Rekognition 控制台，网址为[https://console.aws.amazon.com/rekognition/](https://console.aws.amazon.com/rekognition/)。

1. 选择**使用自定义标签**。

1. 选择**开始**。

1. 在左侧导航窗格中，选择**项目**。

1. 在**项目**资源页面上，选择包含要停止的已训练模型的项目。

1. 在**模型**部分中，选择要停止的模型。

1. 选择**使用模型**选项卡。

1. 

------
#### [ Stop model using the console ]

   1. 在**启动或停止模型**部分中，选择**停止**。

   1. 在**停止模型**对话框中，输入 **stop** 以确认要停止模型。

   1. 选择**停止**以停止模型。

------
#### [ Stop model using the AWS SDK ]

   在**使用模型**部分中，执行以下操作：

   1. 选择 **API 代码**。

   1. 选择 **AWS CLI** 或 **Python**。

   1. 在**停止模型**中，复制示例代码。

   1. 使用示例代码停止模型。有关更多信息，请参阅 [停止 Amazon Rekognition Custom Labels 模型 (SDK)](#rm-stop-sdk)。

------

1. 在页面顶部选择项目名称，返回项目概述页面。

1. 在**模型**部分中，检查模型的状态。当模型状态为 **STOPPED** 时，即表示模型已停止。

## 停止 Amazon Rekognition Custom Labels 模型 (SDK)
<a name="rm-stop-sdk"></a>

您可以通过调用 [StopProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StopProjectVersion)API 并在`ProjectVersionArn`输入参数中传递模型的 Amazon 资源名称 (ARN) 来停止模型。

模型可能需要一段时间才能停止。要检查当前状态，请使用 `DescribeProjectVersions`。

**停止模型 (SDK)**

1. 如果您尚未这样做，请安装并配置 AWS CLI 和 AWS SDKs。有关更多信息，请参阅 [步骤 4：设置 AWS CLI 和 AWS SDKs](su-awscli-sdk.md)。

1. 使用以下示例代码停止正在运行的模型。

------
#### [ CLI ]

   将 `project-version-arn` 的值更改为要停止的模型的 ARN。

   ```
   aws rekognition stop-project-version --project-version-arn "model arn" \
     --profile custom-labels-access
   ```

------
#### [ Python ]

   以下示例会停止已在运行的模型。

   提供以下命令行参数：
   + `project_arn`：包含要停止的模型的项目的 ARN。
   + `model_arn`：要停止的模型的 ARN。

   ```
   # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
   # SPDX-License-Identifier: Apache-2.0
   
   """
   Purpose
   Shows how to stop a running Amazon Lookout for Vision model.
   """
   
   import argparse
   import logging
   import time
   import boto3
   
   from botocore.exceptions import ClientError
   
   
   logger = logging.getLogger(__name__)
   
   
   def get_model_status(rek_client, project_arn, model_arn):
       """
       Gets the current status of an Amazon Rekognition Custom Labels model
       :param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
       :param project_name:  The name of the project that you want to use.
       :param model_arn:  The name of the model that you want the status for.
       """
   
       logger.info ("Getting status for %s.", model_arn)
   
       # Extract the model version from the model arn.
       version_name=(model_arn.split("version/",1)[1]).rpartition('/')[0]
   
       # Get the model status.
       models=rek_client.describe_project_versions(ProjectArn=project_arn,
       VersionNames=[version_name])
   
       for model in models['ProjectVersionDescriptions']: 
           logger.info("Status: %s",model['StatusMessage'])
           return model["Status"]
   
       # No model found.
       logger.exception("Model %s not found.", model_arn)
       raise Exception("Model %s not found.", model_arn)
   
   
   def stop_model(rek_client, project_arn, model_arn):
       """
       Stops a running Amazon Rekognition Custom Labels Model.
       :param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
       :param project_arn: The ARN of the project that you want to stop running.
       :param model_arn:  The ARN of the model (ProjectVersion) that you want to stop running.
       """
   
       logger.info("Stopping model: %s", model_arn)
   
       try:
           # Stop the model.
           response=rek_client.stop_project_version(ProjectVersionArn=model_arn)
   
           logger.info("Status: %s", response['Status'])
   
           # stops when hosting has stopped or failure.
           status = ""
           finished = False
   
           while finished is False:
   
               status=get_model_status(rek_client, project_arn, model_arn)
   
               if status == "STOPPING":
                   logger.info("Model stopping in progress...")
                   time.sleep(10)
                   continue
               if status == "STOPPED":
                   logger.info("Model is not running.")
                   finished = True
                   continue
   
               error_message = f"Error stopping model. Unexepected state: {status}"
               logger.exception(error_message)
               raise Exception(error_message)
   
           logger.info("finished. Status %s", status)
           return status
   
       except ClientError as err:
           logger.exception("Couldn't stop model - %s: %s",
              model_arn,err.response['Error']['Message'])
           raise
   
   
   def add_arguments(parser):
       """
       Adds command line arguments to the parser.
       :param parser: The command line parser.
       """
   
       parser.add_argument(
           "project_arn", help="The ARN of the project that contains the model that you want to stop."
       )
       parser.add_argument(
           "model_arn", help="The ARN of the model that you want to stop."
       )
   
   def main():
   
       logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s")
   
       try:
   
           # Get command line arguments.
           parser = argparse.ArgumentParser(usage=argparse.SUPPRESS)
           add_arguments(parser)
           args = parser.parse_args()
   
           # Stop the model.
           session = boto3.Session(profile_name='custom-labels-access')
           rekognition_client = session.client("rekognition")
   
           status=stop_model(rekognition_client, args.project_arn, args.model_arn)
   
           print(f"Finished stopping model: {args.model_arn}")
           print(f"Status: {status}")
   
       except ClientError as err:
           logger.exception("Problem stopping model:%s",err)
           print(f"Failed to stop model: {err}")
       
       except Exception as err:
           logger.exception("Problem stopping model:%s", err)
           print(f"Failed to stop model: {err}")
   
   if __name__ == "__main__":
       main()
   ```

------
#### [ Java V2 ]

   提供以下命令行参数：
   + `project_arn`：包含要停止的模型的项目的 ARN。
   + `model_arn`：要停止的模型的 ARN。

   ```
   /*
      Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
      SPDX-License-Identifier: Apache-2.0
   */
   
   package com.example.rekognition;
   
   import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
   import software.amazon.awssdk.regions.Region;
   import software.amazon.awssdk.services.rekognition.RekognitionClient;
   import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsRequest;
   import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsResponse;
   import software.amazon.awssdk.services.rekognition.model.ProjectVersionDescription;
   import software.amazon.awssdk.services.rekognition.model.ProjectVersionStatus;
   import software.amazon.awssdk.services.rekognition.model.RekognitionException;
   import software.amazon.awssdk.services.rekognition.model.StopProjectVersionRequest;
   import software.amazon.awssdk.services.rekognition.model.StopProjectVersionResponse;
   
   
   import java.util.logging.Level;
   import java.util.logging.Logger;
   
   public class StopModel {
   
       public static final Logger logger = Logger.getLogger(StopModel.class.getName());
   
       public static int findForwardSlash(String modelArn, int n) {
   
           int start = modelArn.indexOf('/');
           while (start >= 0 && n > 1) {
               start = modelArn.indexOf('/', start + 1);
               n -= 1;
           }
           return start;
   
       }
   
       public static void stopMyModel(RekognitionClient rekClient, String projectArn, String modelArn)
               throws Exception, RekognitionException {
   
           try {
   
               logger.log(Level.INFO, "Stopping {0}", modelArn);
   
               StopProjectVersionRequest stopProjectVersionRequest = StopProjectVersionRequest.builder()
                       .projectVersionArn(modelArn).build();
   
               StopProjectVersionResponse response = rekClient.stopProjectVersion(stopProjectVersionRequest);
   
               logger.log(Level.INFO, "Status: {0}", response.statusAsString());
   
               // Get the model version
   
               int start = findForwardSlash(modelArn, 3) + 1;
               int end = findForwardSlash(modelArn, 4);
   
               String versionName = modelArn.substring(start, end);
   
               // wait until model stops
   
               DescribeProjectVersionsRequest describeProjectVersionsRequest = DescribeProjectVersionsRequest.builder()
                       .projectArn(projectArn).versionNames(versionName).build();
   
               boolean stopped = false;
   
               // Wait until create finishes
   
               do {
   
                   DescribeProjectVersionsResponse describeProjectVersionsResponse = rekClient
                           .describeProjectVersions(describeProjectVersionsRequest);
   
                   for (ProjectVersionDescription projectVersionDescription : describeProjectVersionsResponse
                           .projectVersionDescriptions()) {
   
                       ProjectVersionStatus status = projectVersionDescription.status();
   
                       logger.log(Level.INFO, "stopping model: {0} ", modelArn);
   
                       switch (status) {
   
                       case STOPPED:
                           logger.log(Level.INFO, "Model stopped");
                           stopped = true;
                           break;
   
                       case STOPPING:
                           Thread.sleep(5000);
                           break;
   
                       case FAILED:
                           String error = "Model stopping failed: " + projectVersionDescription.statusAsString() + " "
                                   + projectVersionDescription.statusMessage() + " " + modelArn;
                           logger.log(Level.SEVERE, error);
                           throw new Exception(error);
   
                       default:
                           String unexpectedError = "Unexpected stopping state: "
                                   + projectVersionDescription.statusAsString() + " "
                                   + projectVersionDescription.statusMessage() + " " + modelArn;
                           logger.log(Level.SEVERE, unexpectedError);
                           throw new Exception(unexpectedError);
                       }
                   }
   
               } while (stopped == false);
   
           } catch (RekognitionException e) {
               logger.log(Level.SEVERE, "Could not stop model: {0}", e.getMessage());
               throw e;
           }
   
       }
   
       public static void main(String[] args) {
   
           String modelArn = null;
           String projectArn = null;
   
   
           final String USAGE = "\n" + "Usage: " + "<project_name> <version_name>\n\n" + "Where:\n"
                   + "   project_arn - The ARN of the project that contains the model that you want to stop. \n\n"
                   + "   model_arn - The ARN of the model version that you want to stop.\n\n";
   
           if (args.length != 2) {
               System.out.println(USAGE);
               System.exit(1);
           }
   
           projectArn = args[0];
           modelArn = args[1];
   
   
           try {
   
               // Get the Rekognition client.
               RekognitionClient rekClient = RekognitionClient.builder()
               .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access"))
               .region(Region.US_WEST_2)
               .build();
   
               // Stop model
               stopMyModel(rekClient, projectArn, modelArn);
   
               System.out.println(String.format("Model stopped: %s", modelArn));
   
               rekClient.close();
   
           } catch (RekognitionException rekError) {
               logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage());
               System.exit(1);
           } catch (Exception rekError) {
               logger.log(Level.SEVERE, "Error: {0}", rekError.getMessage());
               System.exit(1);
           }
   
       }
   
   }
   ```

------

# 报告运行时长和使用的推理单元
<a name="rm-model-usage"></a>

如果您在 2022 年 8 月之后训练并启动模型，则可以使用 `InServiceInferenceUnits` Amazon CloudWatch 指标来确定模型运行了多少小时以及这些时间[内使用的推理单位](running-model.md#running-model-inference-units)数量。

**注意**  
如果您在一个 AWS 区域中只有一个模型，则还可以通过跟踪成功调入`StartprojectVersion`和调入来获取模型`StopProjectVersion`的运行时间 CloudWatch。如果您 AWS 在该地区运行多个模型，则这种方法不起作用，因为这些指标不包含有关该模型的信息。  
或者，您可以使用跟踪 AWS CloudTrail 对`StartProjectVersion`和的调用`StopProjectVersion`（包括[事件历史记录`requestParameters`](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/view-cloudtrail-events.html?icmpid=docs_console_unmapped)字段中的模型 ARN）。 CloudTrail 活动限制在 90 天内，但您可以在[CloudTrail湖](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake.html)中存储长达 7 年的活动。

以下过程将创建以下内容的图表：
+ 模型运行的小时数。
+ 模型已使用的推理单元数。

您最多可以选择过去 15 个月的时间段。有关指标保留的更多信息，请参阅[指标保留](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#metrics-retention)。

**确定模型运行时间和所用推理单元**

1. 登录 AWS 管理控制台 并打开 CloudWatch 控制台，网址为[https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/)。

1. 在左侧导航窗格中，依次选择**指标**、**所有指标**。

1. 在内容窗格中，选择**来源**选项卡。

1. 确保选中**控制面板**按钮。

1. 在编辑框中，将现有 JSON 替换为以下 JSON。更改以下值：
   + `Project_Name`：包含要创建图表的模型的项目。
   + `Version_Name`：要创建图表的模型版本。
   + `AWS_Region`— 包含模型的 AWS 区域。通过选中页面顶部导航栏中的区域选择器，确保 CloudWatch 控制台位于同一 AWS 区域。根据需要更新。

   ```
   {
       "sparkline": true,
       "metrics": [
           [
               {
                   "expression": "SUM(m1)*m1",
                   "label": "Inference units used",
                   "id": "e1"
               }
           ],
           [
               {
                   "expression": "DATAPOINT_COUNT(m1)*m1/m1",
                   "label": "Hours running",
                   "id": "e2"
               }
           ],
           [
               "AWS/Rekognition",
               "InServiceInferenceUnits",
               "ProjectName",
               "Project_Name",
               "VersionName",
               "Version_Name",
               {
                   "id": "m1",
                   "visible": false
               }
           ]
       ],
       "view": "singleValue",
       "stacked": false,
       "region": "AWS_Region",
       "stat": "Average",
       "period": 3600,
       "title": "Hours run and inference units used"
   }
   ```

1. 选择**更新**。

1. 在页面顶部，选择一个时间线。您会看到在该时间线上使用的推理单元数和运行的小时数。图表中的间隙表示模型未运行的时间。下面的控制台屏幕截图显示一段时间内使用的推理单位数和运行时长，自定义时间设置为 2 周，最高值为 214 个推理单位，运行时长为 209 小时。  
![\[图表显示推理单位数。\]](http://docs.aws.amazon.com/zh_cn/rekognition/latest/customlabels-dg/images/model-duration.png)

1. （可选）依次选择**操作**、**添加到控制面板 - 已改进**，将图表添加到控制面板。