

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

# 在 Amazon Virtual Private Cloud 執行 Amazon SageMaker Ground Truth 標籤工作
<a name="samurai-vpc-labeling-job"></a>

Ground Truth 支援 Amazon VPC 中的以下功能。
+ 您可以使用 Amazon S3 儲存貯體政策，從特定的 Amazon VPC 端點或特定的 VPC，控制對儲存貯體的存取。如果您啟動標籤工作，且輸入資料位於僅限 VPC 中使用者的 Amazon S3 儲存貯體，您可以新增儲存貯體政策，同時授權 Ground Truth 端點存取儲存貯體的許可。如需詳細資訊，請參閱 [允許 Ground Truth 存取 VPC 限制的 Amazon S3 儲存貯體](#sms-vpc-permissions-s3)。
+ 您可以在 VPC 啟動[自動化資料標籤工作](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-automated-labeling.html)。您可以使用 VPC 組態指定 VPC 子網路和安全群組。SageMaker AI 使用此組態啟動用於 VPC 中自動化資料標籤的訓練和推論工作。如需詳細資訊，請參閱 [在 VPC 建立自動資料標籤工作](#sms-vpc-permissions-automated-labeling)。

您可以透過以下任何方式使用這些選項。
+ 您可以使用這兩種方法，使用受 VPC 保護的 Amazon S3 儲存貯體啟動標籤工作，並啟用自動化資料標籤。
+ 您可以使用任何[內建任務類型](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-task-types.html)，使用受 VPC 保護的儲存貯體啟動標籤工作。
+ 您可以使用受 VPC 保護的儲存貯體啟動[自訂標籤工作流程](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-custom-templates.html)。Ground Truth 會使用 [AWS PrivateLink](https://docs.aws.amazon.com/vpc/latest/privatelink/endpoint-services-overview.html) 端點，與您的註釋前和註釋後的 Lambda 函式互動。

我們建議您在 Amazon VPC 建立標籤工作之前先檢閱[在 VPC 執行 Ground Truth 標籤工作的先決條件](#sms-vpc-gt-prereq)。

## 在 VPC 執行 Ground Truth 標籤工作的先決條件
<a name="sms-vpc-gt-prereq"></a>

在 Amazon VPC 建立 Ground Truth 標籤工作之前，請先檢閱下列先決條件。
+ 如果您是 Ground Truth 的新使用者，請檢閱[入門](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-getting-started.html)，瞭解如何建立標籤工作。
+ 如果您的輸入資料位於受 VPC 保護的 Amazon S3 儲存貯體，則工作者必須從 VPC 存取工作者入口網站。VPC 型標籤工作需要使用私有工作團隊。若要進一步了解如何建立私有工作團隊，請參閱[使用私有人力資源](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-workforce-private.html)。
+ 下列先決條件僅適用於啟動 VPC 中的標籤工作。
  + 請參閱[建立 Amazon S3 VPC 端點](https://docs.aws.amazon.com/sagemaker/latest/dg/train-vpc.html#train-vpc-s3)中的指示。自動化資料標籤工作流程中使用的訓練和推論容器，使用此端點與 Amazon S3 中的儲存貯體通訊。
  + 檢閱[自動化資料標籤](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-automated-labeling.html)進一步了解此功能。請注意，下列[內建任務類型](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-task-types.html)支援自動化資料標籤：[影像分類 (單一標籤)](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-image-classification.html)、[映像語意分割](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-semantic-segmentation.html)、[邊界框](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-bounding-box.html)和[文字分類 (單一標籤)](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-text-classification.html)。串流標籤工作不支援自動化資料標籤。
+ 檢閱 [Ground Truth 安全與許可](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-security-general.html)區段，並確定您已符合下列條件。
  + 建立標籤工作的使用者擁有所有必要的許可
  + 您已建立具有必要許可的 IAM 執行角色。如果使用案例不需要微調的許可，建議您使用[以 Ground Truth 授予一般許可開始使用](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-security-permission.html#sms-security-permissions-get-started)中所述的 IAM 受管政策。
  + 允許您的 VPC 存取 `sagemaker-labeling-data-region` 和 `sm-bxcb-region-saved-task-states` S3 儲存貯體。這些是系統擁有的區域化 S3 儲存貯體，在工作者處理任務時，從工作者入口網站存取即可。我們使用這些儲存貯體與系統管理的資料互動。

## 允許 Ground Truth 存取 VPC 限制的 Amazon S3 儲存貯體
<a name="sms-vpc-permissions-s3"></a>

以下各節就 Ground Truth 使用僅限 VPC 和 VPC 端點存取權的 Amazon S3 儲存貯體啟動標籤工作所需許可，提供詳細資訊。若要了解如何限制 Amazon S3 儲存貯體對 VPC 的存取權，請參閱 Amazon Simple Storage Service 使用者指南中的[使用儲存貯體政策控制 VPC 端點進行存取](https://docs.aws.amazon.com/AmazonS3/latest/userguide/example-bucket-policies-vpc-endpoint.html)。若要了解如何新增政策至 S3 儲存貯體，請參閱[使用 Amazon S3 主控台新增儲存貯體政策](https://docs.aws.amazon.com/AmazonS3/latest/userguide/add-bucket-policy.html)。

**注意**  
修改現有儲存貯體上的政策，可能導致 `IN_PROGRESS` Ground Truth 工作失敗。我們建議您使用新建儲存貯體開始新工作。如果您想繼續使用相同的儲存貯體，您可以執行以下項目之一。  
等待 `IN_PROGRESS` 工作完成。
使用主控台或 AWS CLI終止工作。

您可以使用 [AWS PrivateLink](https://aws.amazon.com/privatelink/) 端點將 Amazon S3 儲存貯體存取限制為 VPC 中的使用者。舉例而言，下列 S3 儲存貯體政策只允許從 `<vpc>` 和端點 `<vpc-endpoint>` 存取特定儲存貯體 `<bucket-name>`。修改此政策時，您必須以您的資源和規格取代所有*紅色斜體文字*。

**注意**  
下列政策*拒絕* VPC 中使用者*以外*的所有實體，執行 `Action` 列出的動作。如果您未在此清單中包含動作，任何有權存取此儲存貯體和執行這些動作之許可的實體，仍可存取這些動作。例如，如果使用者有許可，得在 Amazon S3 儲存貯體上執行 `GetBucketLocation`，則以下政策不會限制使用者在 VPC 以外執行此動作。

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "Policy1415115909152",
    "Statement": [
        {
            "Sid": "AccessToSpecificVPCEOnly",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Effect": "Deny",
            "Resource": [
                "arn:aws:s3:::amzn-s3-demo-bucket",
                "arn:aws:s3:::amzn-s3-demo-bucket/*"
            ],
            "Condition": {
                "StringNotEquals": {
                    "aws:sourceVpce": [
                        "vpce-12345678",
                        "vpce-12345678901234567"
                    ]
                }
            }
        }
    ]
}
```

------

Ground Truth 必須能夠在您用來配置標籤工作之 S3 儲存貯體執行下列 Amazon S3 動作。

```
"s3:AbortMultipartUpload",
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket",
"s3:GetBucketLocation"
```

將 Ground Truth 端點新增到儲存貯體政策 (如前述政策) 即可達成這項目的。下表包含每個 AWS 區域的 Ground Truth 服務端點。在您用來執行標籤工作的相同 [AWS 區域](https://docs.aws.amazon.com/general/latest/gr/rande.html)新增端點至儲存貯體政策。


****  

| AWS 區域 | Ground Truth 端點 | 
| --- | --- | 
| us-east-2 | vpce-02569ba1c40aad0bc | 
| us-east-1 | vpce-08408e335ebf95b40 | 
| us-west-2 | vpce-0ea07aa498eb78469 | 
| ca-central-1 | vpce-0d46ea4c9ff55e1b7 | 
| eu-central-1 | vpce-0865e7194a099183d | 
| eu-west-2 | vpce-0bccd56798f4c5df0 | 
| eu-west-1 | vpce-0788e7ed8628e595d | 
| ap-south-1 | vpce-0d7fcda14e1783f11 | 
| ap-southeast-2 | vpce-0b7609e6f305a77d4 | 
| ap-southeast-1 | vpce-0e7e67b32e9efed27 | 
| ap-northeast-2 | vpce-007893f89e05f2bbf | 
| ap-northeast-1 | vpce-0247996a1a1807dbd | 

例如，下列政策會限制下列位置的 `GetObject` 和 `PutObject` 行動：
+ VPC 中使用者的 Amazon S3 儲存貯體 (`<vpc>`)
+ VPC 端點 (`<vpc-endpoint>`)
+ Ground Truth 服務端點 (`<ground-truth-endpoint>`)

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "1",
    "Statement": [
        {
            "Sid": "DenyAccessFromNonGTandCustomerVPC",
            "Effect": "Deny",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::bucket-name",
                "arn:aws:s3:::bucket-name/*"
            ],
            "Condition": {
              "StringNotEquals": {
                "aws:SourceVpc": "vpc-12345678",
                "aws:sourceVpce": [
                  "vpce-12345678",
                  "vpce-12345678"
                ] 
             }
           }
        }
    ]
}
```

------

如果您希望使用者擁有使用 Ground Truth 主控台啟動標籤工作的許可，您還必須使用 `aws:PrincipalArn` 條件，將使用者 ARN 新增至儲存貯體政策。此使用者還必須擁有許可，才能在您用來啟動標籤工作的儲存貯體執行以下 Amazon S3 動作。

```
"s3:GetObject",
"s3:PutObject",
"s3:ListBucket",
"s3:GetBucketCors",
"s3:PutBucketCors",
"s3:ListAllMyBuckets",
```

下列程式碼是儲存貯體政策的範例，該政策將 S3 儲存貯體 `<bucket-name>` 列於 `Action` 的動作執行許可限制為下列項目。
+ *<role-name>*
+ `aws:sourceVpce` 列出的 VPC 端點
+ 名稱為 *<vpc>* 之 VPC 內的使用者

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Id": "1",
    "Statement": [
        {
            "Sid": "DenyAccessFromNonGTandCustomerVPC",
            "Effect": "Deny",
            "Principal": "*",
            "Action": [
                "s3:GetObject",
                "s3:PutObject"
            ],
            "Resource": [
                "arn:aws:s3:::bucket-name/*",
                "arn:aws:s3:::bucket-name"
            ],
            "Condition": {
                "StringNotEquals": {
                    "aws:SourceVpc": "vpc-12345678",
                    "aws:PrincipalArn": "arn:aws:iam::111122223333:role/role-name"
                },
                "StringNotEquals": {
                    "aws:sourceVpce": [
                        "vpce-12345678",
                        "vpce-12345678"
                    ]
                }
            }
        }
    ]
}
```

------

**注意**  
您用於輸入和輸出資料的 Amazon VPC 介面端點和受保護的 Amazon S3 儲存貯體必須位於您用來建立標記任務的相同 AWS 區域中。

授權 Ground Truth 存取 Amazon S3 儲存貯體的許可後，您可以使用[建立標籤工作](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-create-labeling-job.html)中的其中一個主題啟動標籤工作。為您的輸入和輸出資料儲存貯體，指定受 VPC 限制的 Amazon S3 儲存貯體。

## 在 VPC 建立自動資料標籤工作
<a name="sms-vpc-permissions-automated-labeling"></a>

若要使用 Amazon VPC 建立自動化資料標籤工作，您可以使用 Ground Truth 主控台或 `CreateLabelingJob` API 作業提供 VPC 組態。SageMaker AI 使用您提供的子網路和安全群組，啟動用於自動化標籤的訓練和推論工作。

**重要**  
使用 VPC 組態啟動自動化資料標籤工作之前，請確定已使用要用於標籤工作的 VPC 建立 Amazon S3 VPC 端點。若要了解如何操作，請參閱[建立 Amazon S3 VPC 端點](https://docs.aws.amazon.com/sagemaker/latest/dg/train-vpc.html#train-vpc-s3)。  
此外，如果您使用受 VPC 限制的 Amazon S3 儲存貯體建立自動化資料標籤工作，則必須遵循 [允許 Ground Truth 存取 VPC 限制的 Amazon S3 儲存貯體](#sms-vpc-permissions-s3) 的指示，授予 Ground Truth 存取儲存貯體的權限。

使用下列程序瞭解如何將 VPC 組態新增至標籤工作請求。

**將 VPC 組態新增至自動化資料標籤工作 (主控台)：**

1. 遵循[建立標籤工作 (主控台)](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-create-labeling-job-console.html) 中的指示，完成程序中的每個步驟 (至步驟 15)。

1. 在**工作者**區段中，選取**啟用自動化資料標籤 (Enable automated data labeling)** 旁邊的核取方塊。

1. 選取箭頭，將主控台的 **VPC 組態**區段最大化。

1. 指定您想用於自動資料標籤工作的**虛擬私有雲端 (VPC)**。

1. 選擇**子網路**下的下拉式清單，然後選取一或多個子網路。

1. 選擇**安全群組** 下方的下拉式清單，然後選取一或多個群組。

1. 完成[建立標籤工作 (主控台)](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-create-labeling-job-console.html) 中程序的所有剩餘步驟。

**將 VPC 組態新增至自動化資料標籤工作 (API)：**  
若要使用 Ground Truth API 作業配置標籤工作 `CreateLabelingJob`，請遵循[建立自動化資料標籤工作 (API)](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-automated-labeling.html#sms-create-automated-labeling-api) 中的指示配置請求。除了本文件所述的參數之外，您還必須在 `LabelingJobResourceConfig` 包含 `VpcConfig` 參數，才能使用下列結構描述指定一或多個子網路和安全群組。

```
"LabelingJobAlgorithmsConfig": { 
      "InitialActiveLearningModelArn": "string",
      "LabelingJobAlgorithmSpecificationArn": "string",
      "LabelingJobResourceConfig": { 
         "VolumeKmsKeyId": "string",
         "VpcConfig": { 
            "SecurityGroupIds": [ "string" ],
            "Subnets": [ "string" ]
         }
      }
}
```

以下是 [AWS Python SDK (Boto3) 請求](https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/sagemaker.html#SageMaker.Client.create_labeling_job)的範例，該請求可使用私有人力資源，在美國東部 (維吉尼亞北部) 區域建立自動化資料標籤工作。以您的標籤工作資源和規格取代所有*紅色斜體文字*。若要進一步了解 `CreateLabelingJob` 作業操作，請參閱[建立標籤工作 (API)](https://docs.aws.amazon.com/sagemaker/latest/dg/sms-create-labeling-job-api.html) 教學課程和 [CreateLabelingJob](https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_CreateLabelingJob.html) API 文件。

```
import boto3
client = boto3.client(service_name='sagemaker')

response = client.create_labeling_job(
    LabelingJobName="example-labeling-job",
    LabelAttributeName="label",
    InputConfig={
        'DataSource': {
            'S3DataSource': {
                'ManifestS3Uri': "s3://bucket/path/manifest-with-input-data.json"
            }
        }
    },
    "LabelingJobAlgorithmsConfig": {
      "LabelingJobAlgorithmSpecificationArn": "arn:aws:sagemaker:us-east-1:027400017018:labeling-job-algorithm-specification/tasktype",
      "LabelingJobResourceConfig": { 
         "VpcConfig": { 
            "SecurityGroupIds": [ "sg-01233456789", "sg-987654321" ],
            "Subnets": [ "subnet-e0123456", "subnet-e7891011" ]
         }
      }
    },
    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': "Add task title here",
        'TaskDescription': "Add description of task here for workers",
        'NumberOfHumanWorkersPerDataObject': 1,
        'TaskTimeLimitInSeconds': 3600,
        'TaskAvailabilityLifetimeInSeconds': 21600,
        'MaxConcurrentTaskCount': 1000,
        'AnnotationConsolidationConfig': {
            'AnnotationConsolidationLambdaArn': "arn:aws:lambda:us-east-1:432418664414:function:ACS-tasktype"
        },
    Tags=[
        {
            'Key': "string",
            'Value': "string"
        },
    ]
)
```