

# Running a trained Amazon Rekognition Custom Labels model
Running a trained model

When you're satisfied with the performance of the model, you can start to use it. You can start and stop a model by using the console or the AWS SDK. The console also includes example SDK operations that you can use.

**Topics**
+ [

## Inference units
](#running-model-inference-units)
+ [

## Availability Zones
](#running-model-availability-zones)
+ [

# Starting an Amazon Rekognition Custom Labels model
](rm-start.md)
+ [

# Stopping an Amazon Rekognition Custom Labels model
](rm-stop.md)
+ [

# Reporting running duration and inference units used
](rm-model-usage.md)

## Inference units


When you start your model, you specify the number of compute resources, known as an inference unit, that the model uses. 

**Important**  
You are charged for the number of hours that your model is running and for the number of inference units that your model uses while it's running, based on how you configure the running of your model. For example, if you start the model with two inference units and use the model for 8 hours, you are charged for 16 inference hours (8 hours running time \$1 two inference units). For more information, see [Inference hours](https://aws.amazon.com/rekognition/pricing/#Amazon_Rekognition_Custom_Labels_pricing). If you don't explicitly [stop your model](rm-stop.md), you are charged even if you are not actively analyzing images with your model. 

The transactions per second (TPS) that a single inference unit supports is affected by the following.
+ A model that detects image-level labels (classification) generally has a higher TPS than a model that detects and localizes objects with bounding boxes (object detection).
+ The complexity of the model.
+ A higher resolution image requires more time for analysis.
+ More objects in an image requires more time for analysis.
+ Smaller images are analyzed faster than larger images. 
+ An image passed as image bytes is analyzed faster than first uploading the image to an Amazon S3 bucket and then referencing the uploaded image. Images passed as image bytes must be smaller than 4.0 MB. We recommend that you use image bytes for near real time processing of images and when the image size is less that 4.0 MB. For example, images captured from an IP camera.
+ Processing images stored in an Amazon S3 bucket is faster than downloading the images, converting to image bytes, and then passing the image bytes for analysis.
+ Analyzing an image already stored in an Amazon S3 bucket is probably faster than analyzing the same image passed as image bytes. That's especially true if the image size is larger.

If the number of calls to `DetectCustomLabels` exceeds the maximum TPS supported by the sum of inference units that a model uses, Amazon Rekognition Custom Labels returns an `ProvisionedThroughputExceededException` exception. 

### Managing throughput with inference units


You can increase or decrease the throughput of your model depending on the demands on your application. To increase throughput, use additional inference units. Each additional inference unit increases your processing speed by one inference unit. For information about calculating the number of inference units that you need, see [Calculate inference units for Amazon Rekognition Custom Labels and Amazon Lookout for Vision models](https://aws.amazon.com/blogs/machine-learning/calculate-inference-units-for-an-amazon-rekognition-custom-labels-model/). If you want to change the supported throughput of your model, you have two options: 

#### Manually add or remove inference units


[Stop](rm-stop.md) the model and then [restart](rm-start.md) with the required number of inference units. The disadvantage with this approach is that the model can't receive requests while it's restarting and can't be used to handle spikes in demand. Use this approach if your model has steady throughput and your use case can tolerate 10–20 minutes of downtime. An example would be if you want to batch calls to your model using a weekly schedule. 

#### Auto-scale inference units


If your model has to accommodate spikes in demand, Amazon Rekognition Custom Labels can automatically scale the number of inference units your model uses. As demand increases, Amazon Rekognition Custom Labels adds additional inference units to the model and removes them when demand decreases. 

To let Amazon Rekognition Custom Labels automatically scale inference units for a model, [start](rm-start.md) the model and set the maximum number of inference units that it can use by using the `MaxInferenceUnits` parameter. Setting a maximum number of inference units lets you manage the cost of running the model by limiting the number of inference units available to it. If you don't specify a maximum number of units, Amazon Rekognition Custom Labels won't automatically scale your model, only using the number of inference units that you started with. For information regarding the maximum number of inference units, see [Service Quotas](https://docs.aws.amazon.com/general/latest/gr/rekognition.html#limits_rekognition). 

You can also specify a minimum number of inference units by using the `MinInferenceUnits` parameter. This lets you specify the minimum throughput for your model, where a single inference unit represents 1 hour of processing time.

**Note**  
You can't set the maximum number of inference units with the Amazon Rekognition Custom Labels console. Instead, specify the `MaxInferenceUnits` input parameter to the `StartProjectVersion` operation.

Amazon Rekognition Custom Labels provides the following Amazon CloudWatch Logs metrics that you can use to determine the current automatic scaling status for a model.


| Metric | Description | 
| --- | --- | 
|  `DesiredInferenceUnits`  |  The number of inference units to which Amazon Rekognition Custom Labels is scaling up or down.   | 
|  `InServiceInferenceUnits`  |  The number of inference units that the model is using.  | 

If `DesiredInferenceUnits` = `InServiceInferenceUnits`, Amazon Rekognition Custom Labels is not currently scaling the number of inference units. 

If `DesiredInferenceUnits` > `InServiceInferenceUnits`, Amazon Rekognition Custom Labels is scaling up to the value of `DesiredInferenceUnits`. 

If `DesiredInferenceUnits` < `InServiceInferenceUnits`, Amazon Rekognition Custom Labels is scaling down to the value of `DesiredInferenceUnits`. 

 For more information regarding the metrics returned by Amazon Rekognition Custom Labels and filtering dimensions, see [CloudWatch metrics for Rekognition](https://docs.aws.amazon.com/rekognition/latest/dg/cloudwatch-metricsdim.html).

To find out the maximum number of inference units that you requested for a model, call `DescribeProjectsVersion` and check the `MaxInferenceUnits` field in the response. For example code, see [Describing a model (SDK)](md-describing-model-sdk.md). 

## Availability Zones


Amazon Rekognition Custom Labels distributes inference units across multiple Availability Zones within an AWS Region to provide increased availability. For more information, see [Availability Zones](https://aws.amazon.com/about-aws/global-infrastructure/regions_az/#Availability_Zones). To help protect your production models from Availability Zone outages and inference unit failures, start your production models with at least two inference units. 

If an Availability Zone outage occurs, all inference units in the Availability Zone are unavailable and model capacity is reduced. Calls to [DetectCustomLabels](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectCustomLabels) are redistributed across the remaining inference units. Such calls succeed if they don’t exceed the supported Transactions Per Seconds (TPS) of the remaining inference units. After AWS repairs the Availability Zone, the inference units are restarted, and full capacity is restored. 

If a single inference unit fails, Amazon Rekognition Custom Labels automatically starts a new inference unit in the same Availability Zone. Model capacity is reduced until the new inference unit starts.

# Starting an Amazon Rekognition Custom Labels model
Starting a model

You can start running an Amazon Rekognition Custom Labels model by using the console or by using the [StartProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StartProjectVersion) operation.

**Important**  
You are charged for the number of hours that your model is running and for the number of inference units that your model uses while it is running. For more information, see [Running a trained Amazon Rekognition Custom Labels model](running-model.md).

Starting a model might take a few minutes to complete. To check the current status of the model readiness, check the details page for the project or use [DescribeProjectVersions](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DescribeProjectVersions).

After the model is started you use [DetectCustomLabels](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DetectCustomLabels), to analyze images using the model. For more information, see [Analyzing an image with a trained model](detecting-custom-labels.md). The console also provides example code to call `DetectCustomLabels`. 

**Topics**
+ [

## Starting an Amazon Rekognition Custom Labels model (Console)
](#rm-start-console)
+ [

## Starting an Amazon Rekognition Custom Labels model (SDK)
](#rm-start-sdk)

## Starting an Amazon Rekognition Custom Labels model (Console)
Starting or stopping a model (Console)

Use the following procedure to start running an Amazon Rekognition Custom Labels model with the console. You can start the model directly from the console or use the AWS SDK code provided by the console. 

**To start a model (console)**

1. Open the Amazon Rekognition console at [https://console.aws.amazon.com/rekognition/](https://console.aws.amazon.com/rekognition/).

1. Choose **Use Custom Labels**.

1. Choose **Get started**. 

1. In the left navigation pane, choose **Projects**.

1. On the **Projects** resources page, choose the project that contains the trained model that you want to start.

1. In the **Models** section, choose the model that you want to start. 

1. Choose the **Use model** tab.

1. Do one of the following:

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

   In the **Start or stop model** section do the following:

   1. Select the number of inference units that you want to use. For more information, see [Running a trained Amazon Rekognition Custom Labels model](running-model.md).

   1. Choose **Start**.

   1. In the **Start model** dialog box, choose **Start**. 

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

   In the **Use your model** section do the following:

   1. Choose **API Code.**

   1. Choose either **AWS CLI** or **Python**.

   1. In **Start model** copy the example code.

   1. Use the example code to start your model. For more information, see [Starting an Amazon Rekognition Custom Labels model (SDK)](#rm-start-sdk).

------

1. To go back to the project overview page, choose your project name at the top of the page .

1. In the **Model** section, check the status of the model. When the model status is **RUNNING**, you can use the model to analyze images. For more information, see [Analyzing an image with a trained model](detecting-custom-labels.md).

## Starting an Amazon Rekognition Custom Labels model (SDK)
Starting a model (SDK)

You start a model by calling the [StartProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StartProjectVersion) API and passing the Amazon Resource Name (ARN) of the model in the `ProjectVersionArn` input parameter. You also specify the number of inference units that you want to use. For more information, see [Running a trained Amazon Rekognition Custom Labels model](running-model.md).

A model might take a while to start. The Python and Java examples in this topic use waiters to wait for the model to start. A waiter is a utility method that polls for a particular state to occur. Alternatively, you can check the current status by calling [DescribeProjectVersions](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_DescribeProjectVersions). 

**To start a model (SDK)**

1. If you haven't already done so, install and configure the AWS CLI and the AWS SDKs. For more information, see [Step 4: Set up the AWS CLI and AWS SDKs](su-awscli-sdk.md).

1. Use the following example code to start a model.

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

   Change the value of `project-version-arn` to the ARN of the model that you want to start. Change the value of `--min-inference-units` to the number of inference units that you want to use. Optionally, change `--max-inference-units` to the maximum number of inference units that Amazon Rekognition Custom Labels can use to automatically scale the model.

   ```
   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 ]

   Supply the following command line parameters:
   + `project_arn` – the ARN of the project that contains the model that you want to start.
   + `model_arn` – the ARN of the model that you want to start.
   + `min_inference_units` – the number of inference units that you want to use.
   + (Optional) `--max_inference_units` The maximum number of inference units that Amazon Rekognition Custom Labels can use to auto-scale the model. 

   ```
   # 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 ]

   Supply the following command line parameters:
   + `project_arn` – the ARN of the project that contains the model that you want to start.
   + `model_arn` – the ARN of the model that you want to start.
   + `min_inference_units` – the number of inference units that you want to use.
   + (Optional)`max_inference_units` – the maximum number of inference units that Amazon Rekognition Custom Labels can use to automatically scale the model. If you don't specify a value, automatic scaling doesn't happen.

   ```
   /*
      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);
           }
   
           
   
       }
   
   }
   ```

------

# Stopping an Amazon Rekognition Custom Labels model
Stopping a model

You can stop running an Amazon Rekognition Custom Labels model by using the console or by using the [StopProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StopProjectVersion) operation.

**Topics**
+ [

## Stopping an Amazon Rekognition Custom Labels model (Console)
](#rm-stop-console)
+ [

## Stopping an Amazon Rekognition Custom Labels model (SDK)
](#rm-stop-sdk)

## Stopping an Amazon Rekognition Custom Labels model (Console)
Stopping a model (Console)

Use the following procedure to stop a running Amazon Rekognition Custom Labels model with the console. You can stop the model directly from the console or use the AWS SDK code provided by the console. 

**To stop a model (console)**

1. Open the Amazon Rekognition console at [https://console.aws.amazon.com/rekognition/](https://console.aws.amazon.com/rekognition/).

1. Choose **Use Custom Labels**.

1. Choose **Get started**. 

1. In the left navigation pane, choose **Projects**.

1. On the **Projects** resources page, choose the project that contains the trained model that you want to stop.

1. In the **Models** section, choose the model that you want to stop. 

1. Choose the **Use model** tab.

1. 

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

   1. In the **Start or stop model** section, choose **Stop**.

   1. In the **Stop model** dialog box, enter **stop** to confirm that you want to stop the model.

   1. Choose **Stop** to stop your model.

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

   In the **Use your model** section do the following:

   1. Choose **API Code.**

   1. Choose either **AWS CLI** or **Python**.

   1. In **Stop model** copy the example code.

   1. Use the example code to stop your model. For more information, see [Stopping an Amazon Rekognition Custom Labels model (SDK)](#rm-stop-sdk).

------

1. Choose your project name at the top of the page to go back to the project overview page.

1. In the **Model** section, check the status of the model. The model has stopped when the model status is **STOPPED**.

## Stopping an Amazon Rekognition Custom Labels model (SDK)
Stopping a model (SDK)

You stop a model by calling the [StopProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_StopProjectVersion) API and passing the Amazon Resource Name (ARN) of the model in the `ProjectVersionArn` input parameter. 

A model might take a while to stop. To check the current status, use `DescribeProjectVersions`. 

**To stop a model (SDK)**

1. If you haven't already done so, install and configure the AWS CLI and the AWS SDKs. For more information, see [Step 4: Set up the AWS CLI and AWS SDKs](su-awscli-sdk.md).

1. Use the following example code to stop a running model.

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

   Change the value of `project-version-arn` to the ARN of the model version that you want to stop.

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

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

   The following example stops a model that is already running.

   Supply the following command line parameters:
   + `project_arn` – the ARN of the project that contains the model that you want to stop.
   + `model_arn` – the ARN of the model that you want to stop.

   ```
   # 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 ]

   Supply the following command line parameters:
   + `project_arn` – the ARN of the project that contains the model that you want to stop.
   + `model_arn` – the ARN of the model that you want to stop.

   ```
   /*
      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);
           }
   
       }
   
   }
   ```

------

# Reporting running duration and inference units used
Reporting duration and inference units

If you trained and started your model after August 2022, you can use the `InServiceInferenceUnits` Amazon CloudWatch metric to determine how many hours a model has run for and the number of [inference units](running-model.md#running-model-inference-units) used during those hours.

**Note**  
If you only have one model in an AWS region, you can also get the running time for the model by tracking successful calls to `StartprojectVersion` and `StopProjectVersion` in CloudWatch. This approach doesn't work if you run more that one model in the AWS Region as the metrics don't include information about the model.  
Alternatively, you can use AWS CloudTrail to track calls to `StartProjectVersion` and `StopProjectVersion` (which includes the model ARN in the `requestParameters` field of the [event history](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/view-cloudtrail-events.html?icmpid=docs_console_unmapped)). CloudTrail events are limited to 90 days, but you can store events for up to 7 years in a [CloudTrail Lake](https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-lake.html).

The following procedure creates graphs for the following:
+ The number of hours that a model has run for.
+ The number of inference units that a model has used.

You can choose a time period up to 15 months in the past. For more information about metric retention, see [Metrics retention](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html#metrics-retention). 

**To determine model duration and inference units used for a model**

1. Sign in to the AWS Management Console and open the CloudWatch console at [https://console.aws.amazon.com/cloudwatch/](https://console.aws.amazon.com/cloudwatch/).

1. In the left navigation pane, choose **All metrics** under **Metrics**.

1. In the content pane, choose the **Source** tab.

1. Make sure that the **Dashboard** button is selected.

1. In the edit box, replace the existing JSON with the following JSON. Change the following values:
   + `Project_Name` — The project that contains the model that you want to graph.
   + `Version_Name` — The version of the model that you want to graph.
   + `AWS_Region` — The AWS Region that contains the model. Make sure that the CloudWatch console is in the same AWS Region, by checking the Region selector in the navigation bar at the top of the page. Update as necessary.

   ```
   {
       "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. Choose **Update**.

1. At the top of the page, choose a timeline. You should see numbers for inference units used and hours running during the timeline. Gaps in the graph indicate times when the model wasn't running. The screenshot of the console below showing inference units used and hours running over time periods, with a custom time of 2 weeks set, with the highest values of 214 inference units and 209 hours running.  
![\[Graph showing inference units.\]](http://docs.aws.amazon.com/rekognition/latest/customlabels-dg/images/model-duration.png)

1. (Optional) Add the graph to a dashboard by choosing **Actions** and then **Add to dashboard - improved**.