Ir para o conteúdo

Como posso usar um modelo de interface de usuário personalizado com as funções do Lambda fornecidas pela AWS no Ground Truth?

3 minuto de leitura
0

Quero usar um modelo de interface de usuário personalizado do Amazon SageMaker Ground Truth e funções do AWS Lambda para um trabalho de rotulagem.

Resolução

Crie um modelo de interface de usuário personalizado para o trabalho de rotulagem, conforme mostrado no exemplo a seguir:

  1. Para trabalhos de segmentação semântica, defina a variável name como crowd-semantic-segmentation, conforme mostrado no exemplo a seguir. Para trabalhos de caixa delimitadora, defina a variável name como boundingBox. Para ver uma lista completa de elementos HTML aprimorados para modelos personalizados, consulte Referência do Crowd HTML Elements.

    <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. Crie um arquivo JSON para os rótulos. Exemplo:

    {
      "labels": [
        {
          "label": "Chair"
        },
      ...
        {
          "label": "Oven"
          }
       ]
    }
  3. Crie um arquivo de manifesto de entrada para as imagens. Exemplo:

    {"source-ref":"s3://awsdoc-example-bucket/input_manifest/apartment-chair.jpg"}
    {"source-ref":"s3://awsdoc-example-bucket/input_manifest/apartment-carpet.jpg"}
  4. Faça upload dos arquivos HTML, de manifesto e JSON para o Amazon Simple Storage Service (Amazon S3). Exemplo:

    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. Recupere os nomes dos recursos da Amazon (ARNs) para as funções do Lambda de pré-processamento e consolidação de anotações. Por exemplo, aqui estão os ARNs de segmentação semântica:
    arn:aws:lambda:eu-west-1:111122223333:function:PRE-SemanticSegmentation
    arn:aws:lambda:eu-west-1:111122223333:function:ACS-SemanticSegmentation

  6. Para criar o trabalho de rotulagem, use um AWS SDK, como 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"}],
    )

No exemplo anterior, conclua estas etapas:

  • Substitua S3_OUTPUT_PATH pelo caminho de saída do S3
  • Substitua IAM_ROLE_ARN pelo ARN do perfil
  • Substitua WORKTEAM_ARN pelo ARN da equipe de trabalho
  • Substitua INPUT_MANIFEST_IN_S3 pelo URI do manifesto de entrada
  • Substitua LABELS_JSON_IN_S3 pelo URI JSON dos rótulos
  • Substitua HTML_TEMPLATE_IN_S3 pelo URI do modelo HTML

Informações relacionadas

Algoritmo de segmentação semântica

AWS OFICIALAtualizada há 2 anos