

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立標籤工作 (API)
<a name="sms-create-labeling-job-api"></a>

若要使用 Amazon SageMaker API 來建立標籤工作，請使用 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html) 操作。如需為內建任務類型建立標籤工作的特定指示，請參閱該[任務類型頁面](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-task-types.html)。若要了解如何建立串流標籤工作 (永久執行的標籤工作)，請參閱[建立串流標籤工作](sms-streaming-create-job.md)。

若要使用 `CreateLabelingJob` 操作，您需要下列項目：
+ Amazon S3 中的工作者任務範本 (`UiTemplateS3Uri`) 或人工任務使用者介面 ARN (`[HumanTaskUiArn](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_UiConfig.html#sagemaker-Type-UiConfig-HumanTaskUiArn)`)。
  + 對於 3D 點雲工作、影片物件偵測和追蹤工作，以及 NER 工作，請針對您的任務類型使用 `HumanTaskUiArn` 中列出的 ARN。
  + 如果使用 3D 點雲模式任務以外的內建任務類型，您可以將工作者指示新增至其中一個預先建置的範本，並將範本儲存在 S3 儲存貯體中 (使用 .html 或 .liquid 副檔名)。在您的[任務類型頁面](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-task-types.html)上尋找預先建置的範本。
  + 如果您使用自訂標籤工作流程，則可以建立自訂範本，並將範本儲存在 S3 儲存貯體中。若要了解如何建立自訂工作者範本，請參閱[建立自訂工作者任務範本](sms-custom-templates-step2.md)。如需可用來自訂範本的自訂 HTML 元素，請參閱[Crowd HTML 元素參考](sms-ui-template-reference.md)。如需各種標籤任務的示範範本儲存庫，請參閱 [Amazon SageMaker Ground Truth 範例任務使用者介面](https://github.com/aws-samples/amazon-sagemaker-ground-truth-task-uis)。
+ 指定您在 Amazon S3 中的輸入資料的輸入資訊清單檔案。在 `ManifestS3Uri` 指定輸入資訊清單檔案的位置。如需建立輸入資訊清單的相關資訊，請參閱[輸入資料](sms-data-input.md)。如果您建立串流標籤工作，此為選用。若要了解如何建立串流標籤工作，請參閱[建立串流標籤工作](sms-streaming-create-job.md)。
+ 用來儲存輸出資料的 Amazon S3 儲存貯體。您可以在 `S3OutputPath` 中指定此儲存貯體及選擇性指定字首。
+ 標籤類別組態檔案。每個標籤類別名稱必須是唯一的。使用 `LabelCategoryConfigS3Uri` 參數，指定此檔案在 Amazon S3 中的位置。此檔案的格式和標籤類別取決於您使用的任務類型：
  + 對於影像分類和文字分類 (單一標籤和多標籤)，您必須至少指定兩個標籤類別。對於所有其他任務類型，最少需要一個標籤類別。
  + 對於具名實體辨識任務，您必須在此檔案中提供工作者指示。如需詳細資訊和範例，請參閱[在標籤類別組態檔案中提供工作者指示](sms-named-entity-recg.md#worker-instructions-ner)。
  + 對於 3D 點雲和影片影格任務類型，請使用 [有標籤類別和影格屬性參考的標籤類別組態檔案](sms-label-cat-config-attributes.md) 中的格式。
  + 對於所有其他內建任務類型和自訂任務，您的標籤類別組態檔案必須是下列格式的 JSON 檔案。使用標籤類別取代 `label_1`、`label_2`、`...`、`label_n`，藉此來識別您要使用的標籤。

    ```
    {
        "document-version": "2018-11-28",
        "labels": [
            {"label": "label_1"},
            {"label": "label_2"},
            ...
            {"label": "label_n"}
        ]
    }
    ```
+ 連接 [AmazonSageMakerGroundTruthExecution](https://console.aws.amazon.com/iam/home?#/policies/arn:aws:iam::aws:policy/AmazonSageMakerGroundTruthExecution) 受管 IAM 政策並具有存取 S3 儲存貯體許可的 AWS Identity and Access Management (IAM) 角色。在 `RoleArn` 中指定此角色。若要進一步了解此政策，請參閱[在 Ground Truth 使用 IAM 受管政策](sms-security-permissions-get-started.md)。如果您需要更精細的許可，請參閱[指派 IAM 許可以使用 Ground Truth](sms-security-permission.md)。

  如果您的輸入或輸出儲存貯體名稱不包含 `sagemaker`，您可以將類似下列內容的政策連接至傳遞到 `CreateLabelingJob` 操作的角色。

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "s3:GetObject"
              ],
              "Resource": [
                  "arn:aws:s3:::my_input_bucket/*"
              ]
          },
          {
              "Effect": "Allow",
              "Action": [
                  "s3:PutObject"
              ],
              "Resource": [
                  "arn:aws:s3:::my_output_bucket/*"
              ]
          }
      ]
  }
  ```

------
+ 可處理您的輸入和輸出資料的註釋前和註釋後 (或註釋合併) AWS Lambda 函式 Amazon Resource Name (ARN)。
  + Lambda 函數會在每個 AWS 區域中針對內建任務類型預先定義。若要尋找您區域的註釋前 Lambda ARN，請參閱 [PreHumanTaskLambdaArn](https://docs.aws.amazon.com/sagemaker/latest/dg/API_HumanTaskConfig.html#SageMaker-Type-HumanTaskConfig-PreHumanTaskLambdaArn)。若要尋找您區域的註釋合併 Lambda ARN，請參閱[AnnotationConsolidationLambdaArn](https://docs.aws.amazon.com/sagemaker/latest/dg/API_AnnotationConsolidationConfig.html#SageMaker-Type-AnnotationConsolidationConfig-AnnotationConsolidationLambdaArn)。
  + 對於自訂標籤工作流程，您必須提供自訂的註釋前和註釋後 Lambda ARN。若要了解如何建立這些 Lambda 函式，請參閱[使用 在自訂標記工作流程中處理資料 AWS Lambda](sms-custom-templates-step3.md)。
+ 您在 `WorkteamArn` 中指定的工作團隊 ARN。訂閱廠商人力資源或建立私人工作團隊時，您會收到工作團隊 ARN。如果您要為影片影格或點雲任務類型建立標記任務，則無法使用 Amazon Mechanical Turk 人力資源。對於所有其他任務類型，若要使用 Mechanical Turk 人力資源，請使用以下 ARN。*`region`* 將 取代為您用來建立標籤工作的 AWS 區域。

  ` arn:aws:sagemaker:region:394669845002:workteam/public-crowd/default`

  如果您使用 [Amazon Mechanical Turk 人力資源](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-workforce-management-public.html)，請在 `InputConfig` 的 `DataAttributes` 中使用 `ContentClassifiers` 參數，以宣告您的內容不含個人識別資訊和成人內容。

  如果您使用 Mechanical Turk 人力資源，Ground Truth *要求*您的輸入資料不含個人身分識別資訊 (PII)。如果您使用 Mechanical Turk，並且沒有使用 `FreeOfPersonallyIdentifiableInformation` 標記註明輸入資料不含 PII，則標籤工作將會失敗。使用 `FreeOfAdultContent` 旗標來宣告您的輸入資料不含成人內容。 如果包含成人內容，SageMaker AI 可能會限制可以查看您任務的 Amazon Mechanical Turk 工作者。

  若要進一步了解工作團隊和人力資源，請參閱[人力資源](sms-workforce-management.md)。
+ 如果您使用 Mechanical Turk 人力資源，則必須在 `PublicWorkforceTaskPrice` 指定將支付給執行單一任務工作者的價格。
+ 若要配置工作，您必須分別使用 `TaskDescription` 和 `TaskTitle` 提供任務說明和標題。或者，您可以提供時間限制，以控制工作者處理個別任務的時間長度 (`TaskTimeLimitInSeconds`)，以及工作者入口網站中可供工作者使用的任務剩餘時間 (`TaskAvailabilityLifetimeInSeconds`)。
+ (選用) 對於[某些任務類型](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-annotation-consolidation.html)，您可以讓多個工作者為 `NumberOfHumanWorkersPerDataObject` 參數輸入大於一的數字來標籤單一資料物件。如需註釋合併的詳細資訊，請參閱[註釋整合](sms-annotation-consolidation.md)。
+ (選用) 若要建立自動化資料標籤工作，請在 `LabelingJobAlgorithmsConfig` 中指定 [LabelingJobAlgorithmSpecificationArn](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_LabelingJobAlgorithmsConfig.html) 中列出的其中一個 ARN。此 ARN 可識別自動化資料標籤工作中使用的演算法。與此 ARN 關聯的任務類型須符合您指定的 `PreHumanTaskLambdaArn` 和 `AnnotationConsolidationLambdaArn` 的任務類型。下列任務類型支援自動化資料標籤：影像分類、邊界框、語意分割和文字分類。自動化資料標籤允許的物件數量下限為 1,250，但強烈建議您至少提供 5,000 個物件。若要進一步了解自動化資料標籤工作，請參閱[自動資料標籤](sms-automated-labeling.md)。
+ (選用) 您可以提供 [https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html#API_CreateLabelingJob_RequestSyntax](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html#API_CreateLabelingJob_RequestSyntax)，在符合條件的情況下，使標籤工作停止。您可以使用停止條件來控制標籤工作的成本。

## 範例
<a name="sms-create-labeling-job-api-examples"></a>

下列程式碼範例示範如何使用 `CreateLabelingJob` 建立標籤工作。您也可以在 [SageMaker AI 範例儲存庫](https://github.com/aws/amazon-sagemaker-examples/tree/master/ground_truth_labeling_jobs)中的 GitHub 上查看這些範例筆記本。

------
#### [ 適用於 Python (Boto3) 的 AWS SDK ]

以下是 [AWS Python SDK (Boto3) 請求](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_labeling_job)的範例，可使用私有人力資源為美國東部 (維吉尼亞北部) 區域中的內建任務類型建立標籤工作。將所有*紅色斜體文字*取代為您的標籤工作資源和規格。

```
response = client.create_labeling_job(
    LabelingJobName="example-labeling-job",
    LabelAttributeName="label",
    InputConfig={
        'DataSource': {
            'S3DataSource': {
                'ManifestS3Uri': "s3://bucket/path/manifest-with-input-data.json"
            }
        },
        'DataAttributes': {
            'ContentClassifiers': [
                "FreeOfPersonallyIdentifiableInformation"|"FreeOfAdultContent",
            ]
        }
    },
    OutputConfig={
        'S3OutputPath': "s3://bucket/path/file-to-store-output-data",
        'KmsKeyId': "string"
    },
    RoleArn="arn:aws:iam::*:role/*",
    LabelCategoryConfigS3Uri="s3://bucket/path/label-categories.json",
    StoppingConditions={
        'MaxHumanLabeledObjectCount': 123,
        'MaxPercentageOfInputDatasetLabeled': 123
    },
    HumanTaskConfig={
        'WorkteamArn': "arn:aws:sagemaker:region:*:workteam/private-crowd/*",
        'UiConfig': {
            'UiTemplateS3Uri': "s3://bucket/path/custom-worker-task-template.html"
        },
        'PreHumanTaskLambdaArn': "arn:aws:lambda:us-east-1:432418664414:function:PRE-tasktype",
        'TaskKeywords': [
            "Images",
            "Classification",
            "Multi-label"
        ],
        'TaskTitle': "Multi-label image classification task",
        'TaskDescription': "Select all labels that apply to the images shown",
        'NumberOfHumanWorkersPerDataObject': 1,
        'TaskTimeLimitInSeconds': 3600,
        'TaskAvailabilityLifetimeInSeconds': 21600,
        'MaxConcurrentTaskCount': 1000,
        'AnnotationConsolidationConfig': {
            'AnnotationConsolidationLambdaArn': "arn:aws:lambda:us-east-1:432418664414:function:ACS-"
        },
    Tags=[
        {
            'Key': "string",
            'Value': "string"
        },
    ]
)
```

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

以下是使用 AWS [Amazon Mechanical Turk 人力資源](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-workforce-management-public.html)在美國東部 （維吉尼亞北部） 區域中為內建任務類型建立標籤工作的 CLI 請求範例。有關詳細資訊，請參閱[啟動-人工循環](https://docs.aws.amazon.com/cli/latest/reference/sagemaker/create-labeling-job.html)在*[AWS CLI 命令參考](https://docs.aws.amazon.com/cli/latest/reference/)*。將所有*紅色斜體文字*取代為您的標籤工作資源和規格。

```
$ aws --region us-east-1 sagemaker create-labeling-job \
--labeling-job-name "example-labeling-job" \
--label-attribute-name "label" \
--role-arn "arn:aws:iam::account-id:role/role-name" \
--input-config '{
        "DataAttributes": {
            "ContentClassifiers": [
                "FreeOfPersonallyIdentifiableInformation",
                "FreeOfAdultContent"
            ]
        },
        "DataSource": {
            "S3DataSource": {
                "ManifestS3Uri": "s3://bucket/path/manifest-with-input-data.json"
            }
        }
    }' \
--output-config '{
        "KmsKeyId": "",
        "S3OutputPath": "s3://bucket/path/file-to-store-output-data"
    }' \
--human-task-config '{
        "AnnotationConsolidationConfig": {
            "AnnotationConsolidationLambdaArn": "arn:aws:lambda:us-east-1:432418664414:function:ACS-"
        },
        "TaskAvailabilityLifetimeInSeconds": 21600,
        "TaskTimeLimitInSeconds": 3600,
        "NumberOfHumanWorkersPerDataObject": 1,
        "PreHumanTaskLambdaArn":  "arn:aws:lambda:us-east-1:432418664414:function:PRE-tasktype",
        "WorkteamArn": "arn:aws:sagemaker:us-east-1:394669845002:workteam/public-crowd/default",
        "PublicWorkforceTaskPrice": {
            "AmountInUsd": {
                "Dollars": 0,
                "TenthFractionsOfACent": 6,
                "Cents": 3
            }
        },
        "TaskDescription": "Select all labels that apply to the images shown",
        "MaxConcurrentTaskCount": 1000,
        "TaskTitle": "Multi-label image classification task",,
        "TaskKeywords": [
            "Images",
            "Classification",
            "Multi-label"
        ],
        "UiConfig": {
            "UiTemplateS3Uri": "s3://bucket/path/custom-worker-task-template.html"
        }
    }'
```

------

如需此操作的詳細資訊，請參閱 [CreateLabelingJob](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateLabelingJob.html)。如需如何使用其他語言特定 SDK 的資訊，請參閱 `CreateLabelingJobs` 主題中的[另請參閱](https://docs.aws.amazon.com/sagemaker/latest/dg/API_CreateLabelingJob.html#API_CreateLabelingJob_SeeAlso)。