

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

# Amazon Rekognition Custom Labels モデルをトレーニングする
<a name="training-model"></a>

Amazon Rekognition Custom Labels コンソールを使用するか、Amazon Rekognition Custom Labels API を使用してモデルをトレーニングできます。モデルトレーニングが失敗した場合は、[失敗したモデルトレーニングのデバッグ](tm-debugging.md) の情報を使用して失敗の原因を突き止めてください。

**注記**  
モデルのトレーニングに成功するまでの時間に対して課金されます。通常、トレーニングが完了するまでに 30 分から 24 時間かかります。詳細については、「[トレーニング時間](https://aws.amazon.com/rekognition/pricing/#Amazon_Rekognition_Custom_Labels_pricing)」を参照してください。

モデルをトレーニングするたびに、モデルの新しいバージョンが作成されます。Amazon Rekognition Custom Labels は、プロジェクト名とモデル作成時のタイムスタンプを組み合わせたモデル名を作成します。

モデルをトレーニングするために、Amazon Rekognition Custom Labels は出典トレーニング画像とテスト画像のコピーを作成します。デフォルトでは、コピーされたイメージは、AWS が所有および管理するキーで保管時に暗号化されます。独自の AWS KMS keyの使用を選択することもできます。独自の KMS キーを使用する場合は、KMS キーに対する次のアクセス許可が必要です。
+ kms:CreateGrant
+ kms:DescribeKey

詳細については、「[AWS Key Management Service の概念](https://docs.aws.amazon.com/kms/latest/developerguide/concepts.html#master_keys)」を参照してください。ソース画像には影響がありません。

KMS サーバー側暗号化 (SSE-KMS) を使用して、Amazon S3 バケット内のトレーニングイメージとテストイメージを Amazon Rekognition Custom Labels にコピーする前に暗号化できます。Amazon Rekognition Custom Labels がイメージにアクセスできるようにするには、 AWS アカウントに KMS キーに対する次のアクセス許可が必要です。
+ kms:GenerateDataKey
+ kms:Decrypt

詳細については、「[AWS Key Management Service (SSE-KMS) に保存された KMS キーを使用したサーバー側暗号化を使用したデータの保護](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingKMSEncryption.html)」を参照してください。

モデルにトレーニングを行うと、そのモデルのパフォーマンスを評価し、モデルを改善できます。詳細については、「[トレーニング済み Amazon Rekognition Custom Labels の改善](improving-model.md)」を参照してください。

モデルのタグ付けなど、そのほかのモデルタスクについては、「[Amazon Rekognition Custom Labels モデルの管理](managing-model.md)」を参照してください。

**Topics**
+ [モデルのトレーニング (コンソール)](#tm-console)
+ [モデルのトレーニング (SDK)](#tm-sdk)

## モデルのトレーニング (コンソール)
<a name="tm-console"></a>

Amazon Rekognition Custom Labels コンソールを使用して、モデルをトレーニングできます。

トレーニングには、トレーニングデータセットとテストデータセットを含むプロジェクトが必要です。プロジェクトにテストデータセットがない場合、Amazon Rekognition Custom Labels コンソールはトレーニング中にトレーニングデータセットを分割してプロジェクト用のデータセットを作成します。選択したイメージは代表的なサンプルであり、トレーニングデータセットには使用されません。使用できる代替テストデータセットがない場合にのみ、トレーニングデータセットを分割することをお勧めします。トレーニングデータセットを分割すると、トレーニングに使用できるイメージの数が減ります。

**注記**  
モデルのトレーニングにかかる時間に対して課金されます。詳細については、「[トレーニング時間](https://aws.amazon.com/rekognition/pricing/#Amazon_Rekognition_Custom_Labels_pricing)」を参照してください。

**モデルをトレーニングするには (コンソール)**

1. Amazon Rekognition コンソールを [https://console.aws.amazon.com/rekognition/](https://console.aws.amazon.com/rekognition/) で開きます。

1. **[カスタムラベルを使用]** を選択します。

1. 左側のナビゲーションペインで、**[プロジェクト]** を選択します。

1. **[プロジェクト]** ページで、トレーニングしたいモデルを含むプロジェクトを選択します。

1. **[プロジェクト]** ページで、**[モデルをトレーニング]** を選択します。  
![\[現在のプロジェクトのデータセットで機械学習モデルをトレーニングするための [モデルをトレーニング] ボタン。\]](http://docs.aws.amazon.com/ja_jp/rekognition/latest/customlabels-dg/images/tutorial-train-model.jpg)

1. (オプション) 独自の AWS KMS 暗号化キーを使用する場合は、次の手順を実行します。

   1. In **Image data encryption** choose **Customize encryption settings (advanced)**.

   1. **encryption.aws\$1kms\$1key** に、キーの Amazon リソースネーム (ARN) を入力するか、既存の AWS KMS キーを選択します。To create a new key, choose **Create an AWS IMS key**.

1. (オプション) モデルにタグを追加する場合は、次の操作を行います。

   1. In the **Tags** section, choose **Add new tag**.

   1. 次のように入力します。

      1. The name of the key in **Key**.

      1. The value of the key in **Value**.

   1. タグをさらに追加するには、手順 6a と 6b を繰り返します。

   1. (Optional) If you want to remove a tag, choose **Remove** next to the tag that you want to remove. 以前に保存したタグを削除する場合、変更を保存するとそのタグが削除されます。

1. **[モデルをトレーニング]** ページで、**[モデルをトレーニング]** を選択します。プロジェクトの Amazon リソースネーム (ARN) が **[プロジェクトを選択]** 編集ボックスに入力されているはずです。そうでない場合は、プロジェクトの ARN を入力します。  
![\[Amazon Rekognition Custom Labels サービスで AI モデルのトレーニングを開始するための [モデルをトレーニング] ボタン。\]](http://docs.aws.amazon.com/ja_jp/rekognition/latest/customlabels-dg/images/tutorial-train-model-page-train-model.jpg)

1. In the **Do you want to train your model?** dialog box, choose **Train model**.  
![\[[モデルをトレーニング] ボタンが表示された [モデルをトレーニング] 設定ページ。\]](http://docs.aws.amazon.com/ja_jp/rekognition/latest/customlabels-dg/images/tutorial-dialog-train-model.jpg)

1. プロジェクトページの **[モデル]** セクションでは、トレーニングが進行中の `Model Status` 列で現在のステータスを確認できます。モデルのトレーニングは、完了までに時間がかかります。  
![\[モデルが現在トレーニング中であることを示す TRAINING_IN_PROGRESS ステータス。\]](http://docs.aws.amazon.com/ja_jp/rekognition/latest/customlabels-dg/images/tutorial-training-progress.jpg)

1. トレーニングが完了したら、モデル名を選択します。モデルのステータスが **[TRAINING\$1COMPLETED]** になったらトレーニングは終了します。トレーニングが失敗した場合は、「[失敗したモデルトレーニングのデバッグ](tm-debugging.md)」を読んでください。  
![\[トレーニング済みのモデルが表示されたインターフェイス。モデルが実行準備完了になったことを示す TRAINING_COMPLETED ステータスが表示されている。\]](http://docs.aws.amazon.com/ja_jp/rekognition/latest/customlabels-dg/images/get-started-choose-model.jpg)

1. 次のステップ: モデルを評価する。詳細については、「[トレーニング済み Amazon Rekognition Custom Labels の改善](improving-model.md)」を参照してください。

## モデルのトレーニング (SDK)
<a name="tm-sdk"></a>

モデルをトレーニングするには、[CreateProjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_CreateProjectVersion) を呼び出します。モデルをトレーニングするには、以下の情報が必要です。
+ 名前 - モデルバージョンの一意の名前。
+ プロジェクト ARN - モデルを管理するプロジェクトの Amazon リソースネーム (ARN)。
+ トレーニング結果の場所 - 結果が置かれている Amazon S3 の場所。コンソールの Amazon S3 バケットと同じ場所を使用することも、別の場所を選択することもできます。別の場所を選択することをお勧めします。これにより、アクセス許可を設定でき、Amazon Rekognition Custom Labels コンソールを使用した場合のトレーニング出力と名前が競合する可能性を回避できます。

トレーニングは、プロジェクトに関連付けられたトレーニングデータセットとテストデータセットを使用します。詳細については、「[データセットの管理](managing-dataset.md)」を参照してください。

**注記**  
オプションで、プロジェクトの外部にあるトレーニングデータセットとテストデータセットのマニフェストファイルを指定できます。外部のマニフェストファイルを使用してモデルをトレーニングした後にコンソールを開くと、Amazon Rekognition Custom Labels は、トレーニングに使用したマニフェストファイルの最後のセットを使用してデータセットを作成します。外部のマニフェストファイルを指定して、プロジェクトのモデルバージョンをトレーニングすることはできなくなりました。詳細については、「[CreatePrjectVersion](https://docs.aws.amazon.com/rekognition/latest/APIReference/API_CreateProjectVersion)」を参照してください。

`CreateProjectVersion` からの応答は、後続のリクエストでモデルバージョンを識別するために使用する ARN です。ARN を使用してモデルバージョンを保護することもできます。詳細については、「[Amazon Rekognition Custom Labels プロジェクトの保護](sc-introduction.md#sc-resources)」を参照してください。

モデルバージョンのトレーニングは、完了までに時間がかかります。このトピックの Python と Java の例では、ウェーターを使用してトレーニングが完了するのを待ちます。ウェーターは、発生する特定の状態をポーリングするユーティリティメソッドです。または、`DescribeProjectVersions` を呼び出すことにより、トレーニングの現在のステータスを取得できます。`Status` フィールドの値が `TRAINING_COMPLETED` になると、トレーニングは完了です。トレーニングが完了したら、評価結果を確認してモデルの品質を評価できます。

### モデルのトレーニング (SDK)
<a name="tm-sdk-datasets"></a>

次の例は、プロジェクトに関連するトレーニングデータセットとテストデータセットを使用してモデルをトレーニングする方法を示しています。

**モデル (SDK) をトレーニングするには**

1. まだインストールしていない場合は、 と AWS SDKs をインストール AWS CLI して設定します。詳細については、「[ステップ 4: AWS CLI と AWS SDKsを設定する](su-awscli-sdk.md)」を参照してください。

1. 次のサンプルコードを使用して、プロジェクトをトレーニングします。

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

   次の例では、モデルを作成しています。トレーニングデータセットを分割してテストデータセットを作成します。以下に置き換えます:
   + プロジェクトの Amazon リソースネーム (ARN) を持つ `my_project_arn`。
   + 選択した固有のバージョン名を持つ `version_name`。
   + Amazon Rekognition Custom Labels がトレーニング結果を保存する Amazon S3 バケットの名前を持つ `output_bucket`。
   + トレーニング結果を保存するフォルダの名前を持つ `output_folder`。
   + (オプションパラメータ) AWS Key Management Service カスタマーマスターキーの識別子を持つ `--kms-key-id`。

   ```
   aws rekognition create-project-version \
     --project-arn project_arn \
     --version-name version_name \
     --output-config '{"S3Bucket":"output_bucket", "S3KeyPrefix":"output_folder"}' \
     --profile custom-labels-access
   ```

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

   次の例では、モデルを作成しています。次のコマンドライン引数を指定します。
   + `project_arn` - プロジェクトの Amazon リソースネーム (ARN)。
   + `version_name` - 選択したモデルに固有のバージョン名。
   + `output_bucket` - Amazon Rekognition Custom Labels がトレーニング結果を保存する Amazon S3 バケットの名前。
   + `output_folder` - トレーニング結果を保存するフォルダの名前。

   オプションで、以下のコマンドラインパラメータを指定してモデルにタグをアタッチします。
   + `tag` - モデルにアタッチする選択したタグ名。
   + `tag_value` タグ値。

   ```
   #Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   #PDX-License-Identifier: MIT-0 (For details, see https://github.com/awsdocs/amazon-rekognition-custom-labels-developer-guide/blob/master/LICENSE-SAMPLECODE.)
   
   
   import argparse
   import logging
   import json
   import boto3
   
   from botocore.exceptions import ClientError
   
   logger = logging.getLogger(__name__)
   
   def train_model(rek_client, project_arn, version_name, output_bucket, output_folder, tag_key, tag_key_value):
       """
       Trains an Amazon Rekognition Custom Labels model.
       :param rek_client: The Amazon Rekognition Custom Labels Boto3 client.
       :param project_arn: The ARN of the project in which you want to train a model.
       :param version_name: A version for the model.
       :param output_bucket: The S3 bucket that hosts training output.
       :param output_folder: The path for the training output within output_bucket
       :param tag_key: The name of a tag to attach to the model. Pass None to exclude
       :param tag_key_value: The value of the tag. Pass None to exclude
   
       """
   
       try:
           #Train the model
   
           status=""
           logger.info("training model version %s for project %s",
               version_name, project_arn)
   
   
           output_config = json.loads(
               '{"S3Bucket": "'
               + output_bucket
               + '", "S3KeyPrefix": "'
               + output_folder
               + '" }  '
           )
   
           tags={}
   
           if tag_key is not None and tag_key_value is not None:
               tags = json.loads(
                   '{"' + tag_key + '":"' + tag_key_value + '"}'
               )
   
   
           response=rek_client.create_project_version(
               ProjectArn=project_arn, 
               VersionName=version_name,
               OutputConfig=output_config,
               Tags=tags
           )
   
           logger.info("Started training: %s", response['ProjectVersionArn'])
   
           # Wait for the project version training to complete.
   
           project_version_training_completed_waiter = rek_client.get_waiter('project_version_training_completed')
           project_version_training_completed_waiter.wait(ProjectArn=project_arn,
           VersionNames=[version_name])
       
   
           # Get the completion status.
           describe_response=rek_client.describe_project_versions(ProjectArn=project_arn,
               VersionNames=[version_name])
           for model in describe_response['ProjectVersionDescriptions']:
               logger.info("Status: %s", model['Status'])
               logger.info("Message: %s", model['StatusMessage'])
               status=model['Status']
   
   
           logger.info("finished training")
   
           return response['ProjectVersionArn'], status
       
       except ClientError as err:
           logger.exception("Couldn't create model: %s", 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 in which you want to train a model"
       )
   
       parser.add_argument(
           "version_name", help="A version name of your choosing."
       )
   
       parser.add_argument(
           "output_bucket", help="The S3 bucket that receives the training results."
       )
   
       parser.add_argument(
           "output_folder", help="The folder in the S3 bucket where training results are stored."
       )
   
       parser.add_argument(
           "--tag_name",  help="The name of a tag to attach to the model", required=False
       )
   
       parser.add_argument(
           "--tag_value",  help="The value for the tag.", 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()
   
           print(f"Training model version {args.version_name} for project {args.project_arn}")
   
           # Train the model.
           session = boto3.Session(profile_name='custom-labels-access')
           rekognition_client = session.client("rekognition")
   
           model_arn, status=train_model(rekognition_client, 
               args.project_arn,
               args.version_name,
               args.output_bucket,
               args.output_folder,
               args.tag_name,
               args.tag_value)
   
   
           print(f"Finished training model: {model_arn}")
           print(f"Status: {status}")
   
   
       except ClientError as err:
           logger.exception("Problem training model: %s", err)
           print(f"Problem training model: {err}")
       except Exception as err:
           logger.exception("Problem training model: %s", err)
           print(f"Problem training model: {err}")
   
   
   if __name__ == "__main__":
       main()
   ```

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

   次の例では、モデルをトレーニングしています。次のコマンドライン引数を指定します。
   + `project_arn` - プロジェクトの Amazon リソースネーム (ARN)。
   + `version_name` - 選択したモデルに固有のバージョン名。
   + `output_bucket` - Amazon Rekognition Custom Labels がトレーニング結果を保存する Amazon S3 バケットの名前。
   + `output_folder` - トレーニング結果を保存するフォルダの名前。

   ```
   /*
      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.CreateProjectVersionRequest;
   import software.amazon.awssdk.services.rekognition.model.CreateProjectVersionResponse;
   import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsRequest;
   import software.amazon.awssdk.services.rekognition.model.DescribeProjectVersionsResponse;
   import software.amazon.awssdk.services.rekognition.model.OutputConfig;
   import software.amazon.awssdk.services.rekognition.model.ProjectVersionDescription;
   import software.amazon.awssdk.services.rekognition.model.RekognitionException;
   import software.amazon.awssdk.services.rekognition.waiters.RekognitionWaiter;
   
   import java.util.Optional;
   import java.util.logging.Level;
   import java.util.logging.Logger;
   
   public class TrainModel {
   
       public static final Logger logger = Logger.getLogger(TrainModel.class.getName());
   
       public static String trainMyModel(RekognitionClient rekClient, String projectArn, String versionName,
               String outputBucket, String outputFolder) {
   
           try {
   
               OutputConfig outputConfig = OutputConfig.builder().s3Bucket(outputBucket).s3KeyPrefix(outputFolder).build();
   
               logger.log(Level.INFO, "Training Model for project {0}", projectArn);
               CreateProjectVersionRequest createProjectVersionRequest = CreateProjectVersionRequest.builder()
                       .projectArn(projectArn).versionName(versionName).outputConfig(outputConfig).build();
   
               CreateProjectVersionResponse response = rekClient.createProjectVersion(createProjectVersionRequest);
   
               logger.log(Level.INFO, "Model ARN: {0}", response.projectVersionArn());
               logger.log(Level.INFO, "Training model...");
   
               // wait until training completes
   
               DescribeProjectVersionsRequest describeProjectVersionsRequest = DescribeProjectVersionsRequest.builder()
                       .versionNames(versionName)
                       .projectArn(projectArn)
                       .build();
   
               RekognitionWaiter waiter = rekClient.waiter();
   
               WaiterResponse<DescribeProjectVersionsResponse> waiterResponse = waiter
                       .waitUntilProjectVersionTrainingCompleted(describeProjectVersionsRequest);
   
               Optional<DescribeProjectVersionsResponse> optionalResponse = waiterResponse.matched().response();
   
               DescribeProjectVersionsResponse describeProjectVersionsResponse = optionalResponse.get();
   
               for (ProjectVersionDescription projectVersionDescription : describeProjectVersionsResponse
                       .projectVersionDescriptions()) {
                   System.out.println("ARN: " + projectVersionDescription.projectVersionArn());
                   System.out.println("Status: " + projectVersionDescription.statusAsString());
                   System.out.println("Message: " + projectVersionDescription.statusMessage());
               }
   
               return response.projectVersionArn();
   
           } catch (RekognitionException e) {
               logger.log(Level.SEVERE, "Could not train model: {0}", e.getMessage());
               throw e;
           }
   
       }
   
       public static void main(String args[]) {
   
           String versionName = null;
           String projectArn = null;
           String projectVersionArn = null;
           String bucket = null;
           String location = null;
   
           final String USAGE = "\n" + "Usage: " + "<project_name> <version_name> <output_bucket> <output_folder>\n\n" + "Where:\n"
                   + "   project_arn - The ARN of the project that you want to use. \n\n"
                   + "   version_name - A version name for the model.\n\n"
                   + "   output_bucket - The S3 bucket in which to place the training output. \n\n"
                   + "   output_folder - The folder within the bucket that the training output is stored in. \n\n";
   
           if (args.length != 4) {
               System.out.println(USAGE);
               System.exit(1);
           }
   
           projectArn = args[0];
           versionName = args[1];
           bucket = args[2];
           location = args[3];
   
           try {
   
               // Get the Rekognition client.
               RekognitionClient rekClient = RekognitionClient.builder()
               .credentialsProvider(ProfileCredentialsProvider.create("custom-labels-access"))
               .region(Region.US_WEST_2)
               .build();
   
   
               // Train model
               projectVersionArn = trainMyModel(rekClient, projectArn, versionName, bucket, location);
   
               System.out.println(String.format("Created model: %s for Project ARN: %s", projectVersionArn, projectArn));
   
               rekClient.close();
   
           } catch (RekognitionException rekError) {
               logger.log(Level.SEVERE, "Rekognition client error: {0}", rekError.getMessage());
               System.exit(1);
           }
   
       }
   
   }
   ```

------

1. トレーニングが失敗した場合は、「[失敗したモデルトレーニングのデバッグ](tm-debugging.md)」を読んでください。