Skip to content

How can I use a custom UI template with AWS provided Lambda functions in Ground Truth?

3 minute read
0

I want to use an Amazon SageMaker Ground Truth custom UI template and AWS Lambda functions for a labeling job.

Resolution

Create a custom UI template for the labeling job, as shown in the following example:

  1. For semantic segmentation jobs, set the name variable to crowd-semantic-segmentation, as shown in the following example. For bounding box jobs, set the name variable to boundingBox. For a full list of enhanced HTML elements for custom templates, see Crowd HTML elements reference.

    <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. Create a JSON file for the labels. Example:

    {
      "labels": [
        {
          "label": "Chair"
        },
      ...
        {
          "label": "Oven"
          }
       ]
    }
  3. Create an input manifest file for the images. Example:

    {"source-ref":"s3://amzn-s3-demo-bucket/input_manifest/apartment-chair.jpg"}
    {"source-ref":"s3://amzn-s3-demo-bucket/input_manifest/apartment-carpet.jpg"}

    Note: Replace amzn-s3-demo-bucket with your S3 bucket.

  4. Upload the HTML, manifest, and JSON files to Amazon Simple Storage Service (Amazon S3). Example:

    import boto3
    import os
    
    bucket = 'amzn-s3-demo-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')

    Note: Replace amzn-s3-demo-bucket with your S3 bucket.

  5. Retrieve the Amazon Resource Names (ARNs) for the pre-processing and annotation consolidation Lambda functions. For example, here are the semantic segmentation ARNs:
    arn:aws:lambda:eu-west-1:111122223333:function:PRE-SemanticSegmentation
    arn:aws:lambda:eu-west-1:111122223333:function:ACS-SemanticSegmentation

  6. To create the labeling job, use an AWS SDK, such as boto3:

    import boto3
    
    client = boto3.client("sagemaker")
    client.create_labeling_job(
        LabelingJobName="SemanticSeg-CustomUI",
        LabelAttributeName="output-ref",
        InputConfig={
            "DataSource": {"S3DataSource": {"ManifestS3Uri": "s3://amzn-s3-demo-bucket/GroundTruthCustomUI/input.manifest"}},
            "DataAttributes": {
                "ContentClassifiers": [
                    "FreeOfPersonallyIdentifiableInformation",
                ]
            },
        },
        OutputConfig={"S3OutputPath": "s3://amzn-s3-demo-bucket/GroundTruthCustomUI/output/"},
        RoleArn="arn:aws:iam::111122223333:role/SageMakerExecutionRole",
        LabelCategoryConfigS3Uri="s3://amzn-s3-demo-bucket/GroundTruthCustomUI/testLabels.json",
        StoppingConditions={"MaxPercentageOfInputDatasetLabeled": 100},
        HumanTaskConfig={
            "WorkteamArn": "arn:aws:sagemaker:eu-west-1:111122223333:workteam/private-crowd/ExampleWorkteam",
            "UiConfig": {"UiTemplateS3Uri": "s3://amzn-s3-demo-bucket/GroundTruthCustomUI/customUI.html"},
            "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"}],
    )

Note: Replace the following values with your own:

  • Replace amzn-s3-demo-bucket/GroundTruthCustomUI/input.manifest with your input manifest S3 URI
  • Replace amzn-s3-demo-bucket/GroundTruthCustomUI/output/ with your S3 output path
  • Replace 111122223333 with your AWS account ID
  • Replace SageMakerExecutionRole with your IAM role name
  • Replace ExampleWorkteam with your workteam name
  • Replace amzn-s3-demo-bucket/GroundTruthCustomUI/testLabels.json with your labels JSON S3 URI
  • Replace amzn-s3-demo-bucket/GroundTruthCustomUI/customUI.html with your HTML template S3 URI

Related information

Semantic segmentation algorithm

AWS OFFICIALUpdated 2 years ago