

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# Autopilot モデルのデプロイと予測
<a name="autopilot-deploy-models"></a>

この Amazon SageMaker Autopilot ガイドには、モデルのデプロイ、リアルタイム推論の設定、バッチジョブによる推論の実行の手順が記載されています。

Autopilot モデルをトレーニングしたら、以下の 2 つのいずれかの方法でそれらのモデルをデプロイして、予測を取得することができます。

1. [リアルタイム推論用のモデルをデプロイする](autopilot-deploy-models-realtime.md) を使用して、エンドポイントを設定し、インタラクティブに予測を取得します。リアルタイム推論は、リアルタイム、インタラクティブ、低レイテンシーの要件がある推論ワークロードに最適です。

1. [バッチ推論ジョブを実行する](autopilot-deploy-models-batch.md) を使用して、データセット全体の観測値のバッチを並列的に予測します。バッチ推論は、データセットが大きい場合や、モデル予測のリクエストにすぐに応答する必要がない場合に適しています。

**注記**  
不要な料金が発生しないようにするには: モデルのデプロイで作成されたエンドポイントとリソースが不要になったら、それらを削除できます。リージョン別のインスタンスの料金については、「[Amazon SageMaker の料金](https://aws.amazon.com/sagemaker/pricing/)」を参照してください。

# リアルタイム推論用のモデルをデプロイする
<a name="autopilot-deploy-models-realtime"></a>

リアルタイム推論は、リアルタイム、インタラクティブ、低レイテンシーの要件がある推論ワークロードに最適です。このセクションでは、リアルタイム推論を使用してモデルからインタラクティブに予測を取得する方法を説明します。

Autopilot 実験で最良の検証メトリクスを出したモデルをデプロイするには、オプションがいくつかあります。例えば、SageMaker Studio Classic で Autopilot を使用する場合、モデルを自動または手動でデプロイできます。SageMaker API を使用して Autopilot モデルを手動でデプロイすることもできます。

次のタブは、モデルをデプロイするための 3 つのオプションを示しています。これらの手順は、Autopilot で既にモデルを作成していることを前提としています。モデルがない場合は、「[AutoML API を使用して表形式データのリグレッションジョブまたは分類ジョブを作成する](autopilot-automate-model-development-create-experiment.md)」を参照してください。各オプションの例を見るには、各タブを開いてください。

## Autopilot ユーザーインターフェイス (UI) を使用してデプロイする
<a name="autopilot-deploy-models-realtime-ui"></a>

Autopilot UI には、モデルのデプロイをナビゲートするのに役立つドロップダウンメニュー、トグル、ツールチップなどが含まれています。自動または手動の手順のいずれかを使用してデプロイできます。
+ **自動デプロイ**: Autopilot の実験から得た最適なモデルをエンドポイントに自動的にデプロイします

  1. SageMaker Studio Classic で[実験を作成](https://docs.aws.amazon.com/sagemaker/latest/dg/autopilot-automate-model-development-create-experiment.html)します。

  1. **[自動デプロイ]** の値を **[はい]** に切り替えます。
**注記**  
**リージョン内のエンドポイントインスタンスに対するデフォルトのリソースクォータまたはカスタマークォータのいずれかが過度に制限されていると、自動デプロイは失敗します。**ハイパーパラメータ最適化 (HPO) モードでは、少なくとも 2 つの ml.m5.2xlarge インスタンスが必要です。アンサンブルモードでは、少なくとも 1 つの ml.m5.12xlarge インスタンスが必要です。クォータに関連する障害が発生した場合は、SageMaker AI エンドポイントインスタンスの[サービス制限の引き上げをリクエスト](https://docs.aws.amazon.com/servicequotas/latest/userguide/request-quota-increase.html)できます。
+ **手動デプロイ**: Autopilot テストから得た最適なモデルをエンドポイントに手動でデプロイします

  1. SageMaker Studio Classic で[実験を作成](https://docs.aws.amazon.com/sagemaker/latest/dg/autopilot-automate-model-development-create-experiment.html)します。

  1. **[自動デプロイ]** の値を **[いいえ]** に切り替えます。

  1. **[モデル名]** には、デプロイするモデルを選択します。

  1. リーダーボードの右側にあるオレンジ色の **[デプロイと詳細設定]** ボタンを選択します。これにより、新しいタブが開きます。

  1. エンドポイント名、インスタンスタイプ、その他のオプション情報を設定します。

  1.  オレンジ色の **[デプロイモデル]** を選択してエンドポイントにデプロイします。

  1. [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/) の「エンドポイント」セクションに移動して、エンドポイント作成プロセスの進行状況を確認します。このセクションはナビゲーションパネルの **[推論]** ドロップダウンメニューにあります。

  1. 以下に示すように、エンドポイントのステータスが **[作成中]** から **[InService]** に変わったら、Studio Classic に戻り、エンドポイントを呼び出します。  
![\[SageMaker AI コンソール: エンドポイントを作成したり、エンドポイントのステータスを確認したりするためのエンドポイントページ。\]](http://docs.aws.amazon.com/ja_jp/sagemaker/latest/dg/images/autopilot/autopilot-check-progress.PNG)

## SageMaker API を使用してデプロイする
<a name="autopilot-deploy-models-api"></a>

**API コール**を使用してモデルをデプロイすることで、リアルタイムの推論を取得することもできます。このセクションでは、 AWS Command Line Interface (AWS CLI) コードスニペットを使用したこのプロセスの 5 つのステップを示します。

 AWS CLI コマンドと AWS SDK for Python (boto3) の両方の完全なコード例については、以下の手順に従ってタブを直接開きます。

1. **候補定義を取得する**

   [InferenceContainers](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLCandidate.html#sagemaker-Type-AutoMLCandidate-InferenceContainers) から候補コンテナ定義を取得します。これらの候補定義は、SageMaker AI モデルを作成するために使用されます。

   以下の例では [DescribeAutoMLJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJob.html) API を使用して、最適なモデル候補の候補定義を取得しています。例として、次の AWS CLI コマンドを参照してください。

   ```
   aws sagemaker describe-auto-ml-job --auto-ml-job-name <job-name> --region <region>
   ```

1. **候補を一覧表示する**

   次の例では、[ListCandidatesForAutoMLJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ListCandidatesForAutoMLJob.html) API を使用してすべての候補を一覧表示しています。例として、次の AWS CLI コマンドを参照してください。

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

1. **SageMaker AI モデルを作成する**

   前のステップのコンテナ定義を使用して、[CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API を使用して SageMaker AI モデルを作成します。例として、次の AWS CLI コマンドを参照してください。

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

1. **エンドポイント設定を作成する** 

   次の例では、[CreateEndpointConfig](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpointConfig.html) API を使用してエンドポイント設定を作成します。例として、次の AWS CLI コマンドを参照してください。

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

1. **エンドポイントの作成** 

   次の AWS CLI 例では、[CreateEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateEndpoint.html) API を使用してエンドポイントを作成します。

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

   [DescribeEndpoint](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeEndpoint.html) API を使用して、エンドポイントのデプロイの進行状況を確認します。例として、次の AWS CLI コマンドを参照してください。

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

   `EndpointStatus` が `InService` に変更されると、エンドポイントはリアルタイム推論に使用できるようになります。

1. **エンドポイントの呼び出し** 

   以下のコマンド構造は、エンドポイントを呼び出してリアルタイム推論を行います。

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

次のタブには、 AWS SDK for Python (boto3) または AWS CLIを使用してモデルをデプロイするための完全なコード例が含まれています。

------
#### [ AWS SDK for Python (boto3) ]

1. 次のコード例を使用して**候補定義を入手**します。

   ```
   import sagemaker 
   import boto3
   
   session = sagemaker.session.Session()
   
   sagemaker_client = boto3.client('sagemaker', region_name='us-west-2')
   job_name = 'test-auto-ml-job'
   
   describe_response = sm_client.describe_auto_ml_job(AutoMLJobName=job_name)
   # extract the best candidate definition from DescribeAutoMLJob response
   best_candidate = describe_response['BestCandidate']
   # extract the InferenceContainers definition from the caandidate definition
   inference_containers = best_candidate['InferenceContainers']
   ```

1. 次のコード例を使用して**モデルを作成**します。

   ```
   # Create Model
   model_name = 'test-model' 
   sagemaker_role = 'arn:aws:iam:444455556666:role/sagemaker-execution-role'
   create_model_response = sagemaker_client.create_model(
      ModelName = model_name,
      ExecutionRoleArn = sagemaker_role,
      Containers = inference_containers 
   )
   ```

1. 次のコード例を使用して**エンドポイント設定を作成**します。

   ```
   endpoint_config_name = 'test-endpoint-config'
                                                           
   instance_type = 'ml.m5.2xlarge' 
   # for all supported instance types, see 
   # https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ProductionVariant.html#sagemaker-Type-ProductionVariant-InstanceType    # Create endpoint config
   
   endpoint_config_response = sagemaker_client.create_endpoint_config(
      EndpointConfigName=endpoint_config_name, 
      ProductionVariants=[
          {
              "VariantName": "variant1",
              "ModelName": model_name, 
              "InstanceType": instance_type,
              "InitialInstanceCount": 1
          }
      ]
   )
   
   print(f"Created EndpointConfig: {endpoint_config_response['EndpointConfigArn']}")
   ```

1. **エンドポイントを作成**し、次のコード例を使用してモデルをデプロイします。

   ```
   # create endpoint and deploy the model
   endpoint_name = 'test-endpoint'
   create_endpoint_response = sagemaker_client.create_endpoint(
                                               EndpointName=endpoint_name, 
                                               EndpointConfigName=endpoint_config_name)
   print(create_endpoint_response)
   ```

   次のコード例を使用して、**エンドポイントの作成状況を確認**します。

   ```
   # describe endpoint creation status
   status = sagemaker_client.describe_endpoint(EndpointName=endpoint_name)["EndpointStatus"]
   ```

1. 次のコマンド構造を使用して、**エンドポイントを呼び出し**てリアルタイム推論を行います。

   ```
   # once endpoint status is InService, you can invoke the endpoint for inferencing
   if status == "InService":
     sm_runtime = boto3.Session().client('sagemaker-runtime')
     inference_result = sm_runtime.invoke_endpoint(EndpointName='test-endpoint', ContentType='text/csv', Body='1,2,3,4,class')
   ```

------
#### [ AWS Command Line Interface (AWS CLI) ]

1. 次のコード例を使用して**候補定義を取得**します。

   ```
   aws sagemaker describe-auto-ml-job --auto-ml-job-name 'test-automl-job' --region us-west-2
   ```

1. 次のコード例を使用して、**モデルを作成**します。

   ```
   aws sagemaker create-model --model-name 'test-sagemaker-model'
   --containers '[{
       "Image": "348316444620.dkr.ecr.us-west-2.amazonaws.com/sagemaker-sklearn-automl:2.5-1-cpu-py3", amzn-s3-demo-bucket1
       "ModelDataUrl": "s3://amzn-s3-demo-bucket/output/model.tar.gz",
       "Environment": {
           "AUTOML_SPARSE_ENCODE_RECORDIO_PROTOBUF": "1",
           "AUTOML_TRANSFORM_MODE": "feature-transform",
           "SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT": "application/x-recordio-protobuf",
           "SAGEMAKER_PROGRAM": "sagemaker_serve",
           "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code"
       }
   }, {
       "Image": "348316444620.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xgboost:1.3-1-cpu-py3",
       "ModelDataUrl": "s3://amzn-s3-demo-bucket/output/model.tar.gz",
       "Environment": {
           "MAX_CONTENT_LENGTH": "20971520",
           "SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT": "text/csv",
           "SAGEMAKER_INFERENCE_OUTPUT": "predicted_label", 
           "SAGEMAKER_INFERENCE_SUPPORTED": "predicted_label,probability,probabilities" 
       }
   }, {
       "Image": "348316444620.dkr.ecr.us-west-2.amazonaws.com/sagemaker-sklearn-automl:2.5-1-cpu-py3", aws-region
       "ModelDataUrl": "s3://amzn-s3-demo-bucket/output/model.tar.gz", 
       "Environment": { 
           "AUTOML_TRANSFORM_MODE": "inverse-label-transform", 
           "SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT": "text/csv", 
           "SAGEMAKER_INFERENCE_INPUT": "predicted_label", 
           "SAGEMAKER_INFERENCE_OUTPUT": "predicted_label", 
           "SAGEMAKER_INFERENCE_SUPPORTED": "predicted_label,probability,labels,probabilities", 
           "SAGEMAKER_PROGRAM": "sagemaker_serve", 
           "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code"
       } 
   }]' \
   --execution-role-arn 'arn:aws:iam::1234567890:role/sagemaker-execution-role' \ 
   --region 'us-west-2'
   ```

   詳細については、「[creating a model](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker/create-model.html)」を参照してください。

   `create model` コマンドでは、次の形式のレスポンスが返されます。

   ```
   {
       "ModelArn": "arn:aws:sagemaker:us-west-2:1234567890:model/test-sagemaker-model"
   }
   ```

1. 次のコード例を使用して、**エンドポイント設定を作成**します。

   ```
   aws sagemaker create-endpoint-config --endpoint-config-name 'test-endpoint-config' \
   --production-variants '[{"VariantName": "variant1", 
                           "ModelName": "test-sagemaker-model",
                           "InitialInstanceCount": 1,
                           "InstanceType": "ml.m5.2xlarge"
                          }]' \
   --region us-west-2
   ```

   `create endpoint` 設定コマンドでは、次の形式のレスポンスが返されます。

   ```
   {
       "EndpointConfigArn": "arn:aws:sagemaker:us-west-2:1234567890:endpoint-config/test-endpoint-config"
   }
   ```

1. 次のコード例を使用して、**エンドポイントを作成**します。

   ```
   aws sagemaker create-endpoint --endpoint-name 'test-endpoint' \    
   --endpoint-config-name 'test-endpoint-config' \                 
   --region us-west-2
   ```

   `create endpoint` コマンドでは、次の形式のレスポンスが返されます。

   ```
   {
       "EndpointArn": "arn:aws:sagemaker:us-west-2:1234567890:endpoint/test-endpoint"
   }
   ```

   次の [describe-endpoint](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/describe-endpoint.html) CLI コード例を使用して、エンドポイントのデプロイの進行状況を確認します。

   ```
   aws sagemaker describe-endpoint --endpoint-name 'test-endpoint' --region us-west-2
   ```

   前回の進捗確認では、次の形式でレスポンスが返されます。

   ```
   {
       "EndpointName": "test-endpoint",
       "EndpointArn": "arn:aws:sagemaker:us-west-2:1234567890:endpoint/test-endpoint",
       "EndpointConfigName": "test-endpoint-config",
       "EndpointStatus": "Creating",
       "CreationTime": 1660251167.595,
       "LastModifiedTime": 1660251167.595
   }
   ```

   `EndpointStatus` が `InService` に変わると、エンドポイントはリアルタイム推論に使用できるようになります。

1. 次のコマンド構造を使用して、**エンドポイントを呼び出し**てリアルタイム推論を行います。

   ```
   aws sagemaker-runtime invoke-endpoint --endpoint-name 'test-endpoint' \
   --region 'us-west-2' \
   --body '1,51,3.5,1.4,0.2' \
   --content-type 'text/csv' \
   '/tmp/inference_output'
   ```

   その他のオプションについては、「[invoking an endpoint](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sagemaker-runtime/invoke-endpoint.html)」を参照してください。

------

## さまざまなアカウントからモデルをデプロイできます。
<a name="autopilot-deploy-models-realtime-across-accounts"></a>

Autopilot モデルは、モデルを生成した元のアカウントとは別のアカウントからデプロイできます。クロスアカウントモデルのデプロイを実装するために、このセクションでは以下の方法を説明します。   デプロイ元のアカウント (生成アカウント) にロールを引き受けるアクセス許可を付与します。   デプロイするアカウントから `DescribeAutoMLJob` を呼び出して、モデル情報を取得します。   生成アカウントからモデルアーティファクトへのアクセス権を付与します。   

1. **デプロイするアカウントにアクセス許可を付与する** 

   生成アカウントでロールを引き受けるには、デプロイするアカウントにアクセス許可を付与する必要があります。これにより、デプロイするアカウントは、生成アカウントの Autopilot ジョブを記述できるようになります。

   次の例では、信頼できる `sagemaker-role` エンティティの生成アカウントを使用しています。この例は、ID 111122223333 のデプロイするアカウントに、生成アカウントのロールを引き継ぐアクセス許可を付与する方法を示しています。

   ```
   "Statement": [
           {
               "Effect": "Allow",
               "Principal": {
                   "Service": [
                       "sagemaker.amazonaws.com"
                   ],
                   "AWS": [ "111122223333"]
               },
               "Action": "sts:AssumeRole"
           }
   ```

   これで、ID 111122223333 の新しいアカウントが、生成アカウントのロールを引き継ぐことができます。

   次に、デプロイするアカウントから `DescribeAutoMLJob` API を呼び出して、生成アカウントによって作成されたジョブの説明を取得します。

   次のコード例では、デプロイするアカウントからモデルを説明しています。

   ```
   import sagemaker 
   import boto3
   session = sagemaker.session.Session()
   
   sts_client = boto3.client('sts')
   sts_client.assume_role
   
   role = 'arn:aws:iam::111122223333:role/sagemaker-role'
   role_session_name = "role-session-name"
   _assumed_role = sts_client.assume_role(RoleArn=role, RoleSessionName=role_session_name)
   
   credentials = _assumed_role["Credentials"]
   access_key = credentials["AccessKeyId"]
   secret_key = credentials["SecretAccessKey"]
   session_token = credentials["SessionToken"]
   
   session = boto3.session.Session()
           
   sm_client = session.client('sagemaker', region_name='us-west-2', 
                              aws_access_key_id=access_key,
                               aws_secret_access_key=secret_key,
                               aws_session_token=session_token)
   
   # now you can call describe automl job created in account A 
   
   job_name = "test-job"
   response= sm_client.describe_auto_ml_job(AutoMLJobName=job_name)
   ```

1. 生成アカウントのモデルアーティファクトに**デプロイするアカウントへのアクセス許可を付与**します。

   デプロイするアカウントには、デプロイするために生成アカウントのモデルアーティファクトへのアクセスのみが必要です。これらは、モデル生成時に元の `CreateAutoMLJob` API 呼び出しで指定された [S3OutputPath](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLOutputDataConfig.html#sagemaker-Type-AutoMLOutputDataConfig-S3OutputPath) にあります。

   デプロイするアカウントにモデルアーティファクトへのアクセス許可を付与するには、以下のいずれかのオプションを選択します。

   1. 生成アカウントからデプロイするアカウントに `ModelDataUrl` への[アクセスを許可](https://aws.amazon.com/premiumsupport/knowledge-center/cross-account-access-s3/)します。

      次に、デプロイするアカウントにロールを引き受けるアクセス許可を付与する必要があります。[リアルタイム推論の手順](https://docs.aws.amazon.com/sagemaker/latest/dg/autopilot-deploy-models.html#autopilot-deploy-models-realtime)に従ってデプロイします。

   1. 生成アカウントの元の [S3OutputPath](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLOutputDataConfig.html#sagemaker-Type-AutoMLOutputDataConfig-S3OutputPath) から生成アカウントに[モデルアーティファクトをコピー](https://aws.amazon.com/premiumsupport/knowledge-center/copy-s3-objects-account/)します。

      モデルアーティファクトへのアクセスを許可するには、`best_candidate` モデルを定義し、モデルコンテナを新しいアカウントに再割り当てする必要があります。

      次の例は、`best_candidate` モデルを定義して `ModelDataUrl` を再割り当てする方法を示しています。

      ```
      best_candidate = automl.describe_auto_ml_job()['BestCandidate']
      
      # reassigning ModelDataUrl for best_candidate containers below
      new_model_locations = ['new-container-1-ModelDataUrl', 'new-container-2-ModelDataUrl', 'new-container-3-ModelDataUrl']
      new_model_locations_index = 0
      for container in best_candidate['InferenceContainers']:
          container['ModelDataUrl'] = new_model_locations[new_model_locations_index++]
      ```

      このコンテナの割り当てが終わったら、[SageMaker API を使用してデプロイする](#autopilot-deploy-models-api) の手順に従ってデプロイします。

リアルタイム推論でペイロードを構築するには、ノートブックの例を参照して[テストペイロードを定義](https://aws.amazon.com/getting-started/hands-on/machine-learning-tutorial-automatically-create-models)してください。CSV ファイルからペイロードを作成してエンドポイントを呼び出すには、「[機械学習モデルを自動的に作成する](https://aws.amazon.com/getting-started/hands-on/create-machine-learning-model-automatically-sagemaker-autopilot/#autopilot-cr-room)」の「**モデルを使用して予測を行う**」セクションを参照してください。

# バッチ推論ジョブを実行する
<a name="autopilot-deploy-models-batch"></a>

バッチ推論はオフライン推論とも呼ばれ、複数の観測値に基づいてモデル予測を生成します。バッチ推論は、データセットが大きい場合や、モデル予測のリクエストにすぐに応答する必要がない場合に適しています。これとは対照的に、オンライン推論 ([リアルタイム推論](https://docs.aws.amazon.com/sagemaker/latest/dg/autopilot-deploy-models.html#autopilot-deploy-models-realtime)) はリアルタイムで予測を生成します。[SageMaker Python SDK、Autopilot ユーザーインターフェイス (UI](https://sagemaker.readthedocs.io/en/stable/))、[AWS SDK for Python (boto3)、または () を使用して](https://aws.amazon.com/sdk-for-python/)、Autopilot モデルからバッチ推論を行うことができます[AWS CLI](https://docs.aws.amazon.com/cli/)。 AWS Command Line Interface 

以下のタブでは、モデルをデプロイするための 3 つのオプションを示しています。「API を使用する」、「Autopilot UI」、「API を使用して異なるアカウントからデプロイする」です。これらの手順は、Autopilot で既にモデルを作成していることを前提としています。モデルがない場合は、「[AutoML API を使用して表形式データのリグレッションジョブまたは分類ジョブを作成する](autopilot-automate-model-development-create-experiment.md)」を参照してください。各オプションの例を見るには、各タブを開いてください。

## Autopilot UI を使用してモデルをデプロイする
<a name="autopilot-deploy-models-batch-ui"></a>

Autopilot UI には、モデルのデプロイをナビゲートするのに役立つドロップダウンメニュー、トグル、ツールチップなどが含まれています。

以下の手順は、Autopilot 実験からバッチ予測用のモデルをデプロイする方法を示しています。

1. [https://console.aws.amazon.com/sagemaker/](https://console.aws.amazon.com/sagemaker/) にサインインし、ナビゲーションペインから **[Studio]** を選択します。

1. 左側のナビゲーションペインで、**[Studio]** を選択します。

1. **[使用を開始]** で、Studio アプリケーションを起動するドメインを選択します。ユーザープロファイルが 1 つのドメインにのみ属している場合、ドメインの選択オプションは表示されません。

1. Studio Classic アプリケーションを起動するユーザープロファイルを選択します。ドメインにユーザープロファイルがない場合は、**[ユーザープロファイルを作成]** を選択します。詳細については、「[Add user profiles](https://docs.aws.amazon.com/sagemaker/latest/dg/domain-user-profile-add.html)」を参照してください。

1. **[Studio を起動]** を選択します。ユーザープロファイルが共有スペースに属している場合は、**[オープンスペース]** を選択します。

1. SageMaker Studio Classic コンソールが開いたら、**[SageMaker Studio を起動]** ボタンを選択します。

1. 左のナビゲーションペインで **[AutoML]** を選択します。

1. **[名前]** で、デプロイするモデルに対応する Autopilot 実験を選択します。これにより、新しい **[Autopilot ジョブ]** タブが開きます。

1. **[モデル名]** セクションで、デプロイするモデルを選択します。

1. [**Deploy model (モデルのデプロイ)**] を選択します。これにより、新しいタブが開きます。

1. ページ上部の **[バッチ予測を行う]** を選択します。

1. **[バッチ変換ジョブの設定]** に、**[インスタンスタイプ]**、**[インスタンス数]**、その他のオプション情報を入力します。

1. **[入力データ設定]** セクションで、ドロップダウンメニューを開きます。

   1. **[S3 データタイプ]** には、**[マニフェストファイル]** または **[S3 プレフィックス]** を選択します。

   1. **[分割タイプ]** には、**[ライン]**、**[RecordIO]**、**[TFRecord]**、または **[なし]** を選択します。

   1. **[圧縮]** には、**[Gzip]** または **[なし]** を選択します。

1. **[S3 の場所]** には、入力データの Amazon S3 バケットの場所とそれ以外のオプション情報を入力します。

1. **[出力データ設定]** で、出力データの S3 バケットを入力し、ジョブの[出力をアセンブルする](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_TransformOutput.html#sagemaker-Type-TransformOutput-AssembleWith)方法を選択します。

   1. **[追加設定 (オプション)]** では、MIME タイプと **[S3 暗号化キー]** を入力できます。

1. **[入力/出力フィルタリングとデータ結合 (オプション)]** では、JSONpath 式を入力して入力データをフィルタリングし、入力ソースデータを出力データと結合し、JSONpath 式を入力して出力データをフィルタリングします。

   1. 各タイプのフィルターの例については、「[DataProcessing API](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DataProcessing.html#sagemaker-Type-DataProcessing-InputFilter)」を参照してください。

1. 入力データセットのバッチ予測を実行するには、**[バッチ変換ジョブの作成]** を選択します。新しい **[バッチ変換ジョブ]** タブが表示されます。

1. **[バッチ変換ジョブ]** タブの、**[ステータス]** セクションでジョブの名前を見つけます。次に、ジョブの進行状況を確認します。

## SageMaker API を使用してデプロイする
<a name="autopilot-deploy-models-batch-steps"></a>

SageMaker API をバッチ推論に使用するには、次の 3 つのステップがあります。

1. **候補定義を入手する** 

   SageMaker AI モデルの作成には、[InferenceContainers](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_AutoMLCandidate.html#sagemaker-Type-AutoMLCandidate-InferenceContainers) からの候補定義が使用されます。

   次の例は、[DescribeAutoMLJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeAutoMLJob.html) API を使用して最適なモデル候補の候補定義を取得する方法を示しています。例として、次の AWS CLI コマンドを参照してください。

   ```
   aws sagemaker describe-auto-ml-job --auto-ml-job-name <job-name> --region <region>
   ```

   [ListCandidatesForAutoMLJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ListCandidatesForAutoMLJob.html) API を使用して、すべての候補を一覧表示します。次の AWS CLI コマンドを参照してください。

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

1. **SageMaker AI モデルを作成する**

   [CreateModel](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateModel.html) API を使用して SageMaker AI モデルを作成するには、前のステップのコンテナ定義を使用します。例として、次の AWS CLI コマンドを参照してください。

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

1. **SageMaker AI 変換ジョブを作成する** 

   次の例では、[CreateTransformJob](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-transform-job.html) API を使用して SageMaker AI 変換ジョブを作成します。例として、次の AWS CLI コマンドを参照してください。

   ```
   aws sagemaker create-transform-job --transform-job-name '<your-custom-transform-job-name>' --model-name '<your-custom-model-name-from-last-step>'\
   --transform-input '{
           "DataSource": {
               "S3DataSource": {
                   "S3DataType": "S3Prefix", 
                   "S3Uri": "<your-input-data>" 
               }
           },
           "ContentType": "text/csv",
           "SplitType": "Line"
       }'\
   --transform-output '{
           "S3OutputPath": "<your-output-path>",
           "AssembleWith": "Line" 
       }'\
   --transform-resources '{
           "InstanceType": "<instance-type>", 
           "InstanceCount": 1
       }' --region '<region>'
   ```

[DescribeTransformJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeTransformJob.html) API を使用して変換ジョブの進行状況を確認します。例として、次の AWS CLI コマンドを参照してください。

```
aws sagemaker describe-transform-job --transform-job-name '<your-custom-transform-job-name>' --region <region>
```

ジョブが終了すると、予測された結果が `<your-output-path>` で利用可能になります。

出力ファイル名は次の形式です。`<input_data_file_name>.out`。例えば、入力ファイルが `text_x.csv` の場合、出力名は `text_x.csv.out` になります。

次のタブは、SageMaker Python SDK、 AWS SDK for Python (boto3)、および のコード例を示しています AWS CLI。

------
#### [ SageMaker Python SDK ]

次の例では、**[SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable/overview.html)** を使用してバッチで予測を実行します。

```
from sagemaker import AutoML

sagemaker_session= sagemaker.session.Session()

job_name = 'test-auto-ml-job' # your autopilot job name
automl = AutoML.attach(auto_ml_job_name=job_name)
output_path = 's3://test-auto-ml-job/output'
input_data = 's3://test-auto-ml-job/test_X.csv'

# call DescribeAutoMLJob API to get the best candidate definition
best_candidate = automl.describe_auto_ml_job()['BestCandidate']
best_candidate_name = best_candidate['CandidateName']

# create model
model = automl.create_model(name=best_candidate_name, 
               candidate=best_candidate)

# create transformer
transformer = model.transformer(instance_count=1, 
    instance_type='ml.m5.2xlarge',
    assemble_with='Line',
    output_path=output_path)

# do batch transform
transformer.transform(data=input_data,
                      split_type='Line',
                       content_type='text/csv',
                       wait=True)
```

------
#### [ AWS SDK for Python (boto3) ]

 次の例では、**AWS SDK for Python (boto3)** を使用してバッチで予測を実行します。

```
import sagemaker 
import boto3

session = sagemaker.session.Session()

sm_client = boto3.client('sagemaker', region_name='us-west-2')
role = 'arn:aws:iam::1234567890:role/sagemaker-execution-role'
output_path = 's3://test-auto-ml-job/output'
input_data = 's3://test-auto-ml-job/test_X.csv'

best_candidate = sm_client.describe_auto_ml_job(AutoMLJobName=job_name)['BestCandidate']
best_candidate_containers = best_candidate['InferenceContainers']
best_candidate_name = best_candidate['CandidateName']

# create model
reponse = sm_client.create_model(
    ModelName = best_candidate_name,
    ExecutionRoleArn = role,
    Containers = best_candidate_containers 
)

# Lauch Transform Job
response = sm_client.create_transform_job(
    TransformJobName=f'{best_candidate_name}-transform-job',
    ModelName=model_name,
    TransformInput={
        'DataSource': {
            'S3DataSource': {
                'S3DataType': 'S3Prefix',
                'S3Uri': input_data
            }
        },
        'ContentType': "text/csv",
        'SplitType': 'Line'
    },
    TransformOutput={
        'S3OutputPath': output_path,
        'AssembleWith': 'Line',
    },
    TransformResources={
        'InstanceType': 'ml.m5.2xlarge',
        'InstanceCount': 1,
    },
)
```

バッチ推論ジョブは、以下の形式でレスポンスを返します。

```
{'TransformJobArn': 'arn:aws:sagemaker:us-west-2:1234567890:transform-job/test-transform-job',
 'ResponseMetadata': {'RequestId': '659f97fc-28c4-440b-b957-a49733f7c2f2',
  'HTTPStatusCode': 200,
  'HTTPHeaders': {'x-amzn-requestid': '659f97fc-28c4-440b-b957-a49733f7c2f2',
   'content-type': 'application/x-amz-json-1.1',
   'content-length': '96',
   'date': 'Thu, 11 Aug 2022 22:23:49 GMT'},
  'RetryAttempts': 0}}
```

------
#### [ AWS Command Line Interface (AWS CLI) ]

1. 次のコード例を使用して**候補定義を取得**します。

   ```
   aws sagemaker describe-auto-ml-job --auto-ml-job-name 'test-automl-job' --region us-west-2
   ```

1. 次のコード例を使用して**モデルを作成**します。

   ```
   aws sagemaker create-model --model-name 'test-sagemaker-model'
   --containers '[{
       "Image": "348316444620.dkr.ecr.us-west-2.amazonaws.com/sagemaker-sklearn-automl:2.5-1-cpu-py3",
       "ModelDataUrl": "s3://amzn-s3-demo-bucket/out/test-job1/data-processor-models/test-job1-dpp0-1-e569ff7ad77f4e55a7e549a/output/model.tar.gz",
       "Environment": {
           "AUTOML_SPARSE_ENCODE_RECORDIO_PROTOBUF": "1",
           "AUTOML_TRANSFORM_MODE": "feature-transform",
           "SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT": "application/x-recordio-protobuf",
           "SAGEMAKER_PROGRAM": "sagemaker_serve",
           "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code"
       }
   }, {
       "Image": "348316444620.dkr.ecr.us-west-2.amazonaws.com/sagemaker-xgboost:1.3-1-cpu-py3",
       "ModelDataUrl": "s3://amzn-s3-demo-bucket/out/test-job1/tuning/flicdf10v2-dpp0-xgb/test-job1E9-244-7490a1c0/output/model.tar.gz",
       "Environment": {
           "MAX_CONTENT_LENGTH": "20971520",
           "SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT": "text/csv",
           "SAGEMAKER_INFERENCE_OUTPUT": "predicted_label", 
           "SAGEMAKER_INFERENCE_SUPPORTED": "predicted_label,probability,probabilities" 
       }
   }, {
       "Image": "348316444620.dkr.ecr.us-west-2.amazonaws.com/sagemaker-sklearn-automl:2.5-1-cpu-py3", 
       "ModelDataUrl": "s3://amzn-s3-demo-bucket/out/test-job1/data-processor-models/test-job1-dpp0-1-e569ff7ad77f4e55a7e549a/output/model.tar.gz", 
       "Environment": { 
           "AUTOML_TRANSFORM_MODE": "inverse-label-transform", 
           "SAGEMAKER_DEFAULT_INVOCATIONS_ACCEPT": "text/csv", 
           "SAGEMAKER_INFERENCE_INPUT": "predicted_label", 
           "SAGEMAKER_INFERENCE_OUTPUT": "predicted_label", 
           "SAGEMAKER_INFERENCE_SUPPORTED": "predicted_label,probability,labels,probabilities", 
           "SAGEMAKER_PROGRAM": "sagemaker_serve", 
           "SAGEMAKER_SUBMIT_DIRECTORY": "/opt/ml/model/code" 
       } 
   }]' \
   --execution-role-arn 'arn:aws:iam::1234567890:role/sagemaker-execution-role' \
   --region 'us-west-2'
   ```

1. 以下のコード例を使用して**変換ジョブを作成**します。

   ```
   aws sagemaker create-transform-job --transform-job-name 'test-tranform-job'\
    --model-name 'test-sagemaker-model'\
   --transform-input '{
           "DataSource": {
               "S3DataSource": {
                   "S3DataType": "S3Prefix",
                   "S3Uri": "s3://amzn-s3-demo-bucket/data.csv"
               }
           },
           "ContentType": "text/csv",
           "SplitType": "Line"
       }'\
   --transform-output '{
           "S3OutputPath": "s3://amzn-s3-demo-bucket/output/",
           "AssembleWith": "Line"
       }'\
   --transform-resources '{
           "InstanceType": "ml.m5.2xlarge",
           "InstanceCount": 1
       }'\
   --region 'us-west-2'
   ```

1. 次のコード例を使用して、**変換ジョブの進行状況を確認**します。

   ```
   aws sagemaker describe-transform-job --transform-job-name  'test-tranform-job' --region us-west-2
   ```

   以下は、変換ジョブからのレスポンスです。

   ```
   {
       "TransformJobName": "test-tranform-job",
       "TransformJobArn": "arn:aws:sagemaker:us-west-2:1234567890:transform-job/test-tranform-job",
       "TransformJobStatus": "InProgress",
       "ModelName": "test-model",
       "TransformInput": {
           "DataSource": {
               "S3DataSource": {
                   "S3DataType": "S3Prefix",
                   "S3Uri": "s3://amzn-s3-demo-bucket/data.csv"
               }
           },
           "ContentType": "text/csv",
           "CompressionType": "None",
           "SplitType": "Line"
       },
       "TransformOutput": {
           "S3OutputPath": "s3://amzn-s3-demo-bucket/output/",
           "AssembleWith": "Line",
           "KmsKeyId": ""
       },
       "TransformResources": {
           "InstanceType": "ml.m5.2xlarge",
           "InstanceCount": 1
       },
       "CreationTime": 1662495635.679,
       "TransformStartTime": 1662495847.496,
       "DataProcessing": {
           "InputFilter": "$",
           "OutputFilter": "$",
           "JoinSource": "None"
       }
   }
   ```

   `TransformJobStatus` が `Completed` に変わったら、`S3OutputPath` で推論結果を確認できます。

------

## 異なるアカウントからモデルをデプロイする
<a name="autopilot-deploy-models-batch-across-accounts"></a>

モデルを生成したアカウントとは別のアカウントでバッチ推論ジョブを作成するには、「[さまざまなアカウントからモデルをデプロイできます。](autopilot-deploy-models-realtime.md#autopilot-deploy-models-realtime-across-accounts)」の手順に従います。その後、「[SageMaker API を使用してデプロイする](#autopilot-deploy-models-batch-steps)」に従ってモデルを作成し、ジョブを変換できます。