跳至內容

如何在 Ground Truth 中將自訂 UI 範本與 AWS 提供的 Lambda 函式結合使用?

2 分的閱讀內容
0

我想要使用 Amazon SageMaker Ground Truth 自訂 UI 範本和 AWS Lambda 函式來執行標記作業。

解決方法

為標記作業建立自訂 UI 範本,如以下範例所示:

  1. 對於語義分割作業,請將 name 變數設定為 crowd-semantic-segmentation,如以下範例所示。對於邊界框作業,請將 name 變數設定為 boundingBox。如需自訂範本增強型 HTML 元素的完整清單,請參閱 Crowd HTML 元素參考

    <script src="https://assets.crowd.aws/crowd-html-elements.js"></script>
    <crowd-form>
        <crowd-semantic-segmentation name="crowd-semantic-segmentation" src="{{ task.input.taskObject | grant_read_access }}" header= "{{ task.input.header }}" labels="{{ task.input.labels | to_json | escape }}">
    
            <full-instructions header= "Segmentation Instructions">
                <ol>
                    <li>Read the task carefully and inspect the image.</li>
                    <li>Read the options and review the examples provided to understand more about the labels.</li>
                    <li>Choose the appropriate label that best suits the image.</li>
                </ol>
            </full-instructions>
    
            <short-instructions>
                <p>Use the tools to label the requested items in the image</p>
            </short-instructions>
        </crowd-semantic-segmentation>
    </crowd-form>
  2. 建立標籤的 JSON 檔案。範例:

    {
      "labels": [
        {
          "label": "Chair"
        },
      ...
        {
          "label": "Oven"
          }
       ]
    }
  3. 為影像建立輸入資訊清單檔案。範例:

    {"source-ref":"s3://awsdoc-example-bucket/input_manifest/apartment-chair.jpg"}
    {"source-ref":"s3://awsdoc-example-bucket/input_manifest/apartment-carpet.jpg"}
  4. 將 HTML、資訊清單和 JSON 檔案上傳至 Amazon Simple Storage Service (Amazon S3)。範例:

    import boto3import os
    
    bucket = 'awsdoc-example-bucket'
    prefix = 'GroundTruthCustomUI'
    
    boto3.Session().resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'customUI.html')).upload_file('customUI.html')
    boto3.Session().resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'input.manifest')).upload_file('input.manifest')
    boto3.Session().resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'testLabels.json')).upload_file('testLabels.json')
  5. 擷取預先處理註釋整合 Lambda 函式的 Amazon Resource Name (ARN)。例如,以下是語義分割 ARN:
    arn:aws:lambda:eu-west-1:111122223333:function:PRE-SemanticSegmentation
    arn:aws:lambda:eu-west-1:111122223333:function:ACS-SemanticSegmentation

  6. 若要建立標記作業,請使用 AWS SDK,例如 boto3:

    import boto3
    
    client = boto3.client("sagemaker")
    client.create_labeling_job(
        LabelingJobName="SemanticSeg-CustomUI",
        LabelAttributeName="output-ref",
        InputConfig={
            "DataSource": {"S3DataSource": {"ManifestS3Uri": "INPUT_MANIFEST_IN_S3"}},
            "DataAttributes": {
                "ContentClassifiers": [
                    "FreeOfPersonallyIdentifiableInformation",
                ]
            },
        },
        OutputConfig={"S3OutputPath": "S3_OUTPUT_PATH"},
        RoleArn="IAM_ROLE_ARN",
        LabelCategoryConfigS3Uri="LABELS_JSON_FILE_IN_S3",
        StoppingConditions={"MaxPercentageOfInputDatasetLabeled": 100},
        HumanTaskConfig={
            "WorkteamArn": "WORKTEAM_ARN",
            "UiConfig": {"UiTemplateS3Uri": "HTML_TEMPLATE_IN_S3"},
            "PreHumanTaskLambdaArn": "arn:aws:lambda:eu-west-1:111122223333:function:PRE-SemanticSegmentation",
            "TaskKeywords": [
                "SemanticSegmentation",
            ],
            "TaskTitle": "Semantic Segmentation",
            "TaskDescription": "Draw around the specified labels using the tools",
            "NumberOfHumanWorkersPerDataObject": 1,
            "TaskTimeLimitInSeconds": 3600,
            "TaskAvailabilityLifetimeInSeconds": 1800,
            "MaxConcurrentTaskCount": 1,
            "AnnotationConsolidationConfig": {
                "AnnotationConsolidationLambdaArn": "arn:aws:lambda:eu-west-1:111122223333:function:ACS-SemanticSegmentation"
            },
        },
        Tags=[{"Key": "reason", "Value": "CustomUI"}],
    )

在前述範例中,完成以下步驟:

  • S3_OUTPUT_PATH 替換為 S3 輸出路徑
  • IAM_ROLE_ARN 替換為角色 ARN
  • WORKTEAM_ARN 替換為工作團隊 ARN
  • INPUT_MANIFEST_IN_S3 替換為輸入資訊清單 URI
  • LABELS_JSON_IN_S3 替換為標籤 JSON URI
  • HTML_TEMPLATE_IN_S3 替換為 HTML 範本 URI

相關資訊

語義分割演算法

AWS 官方已更新 2 年前