Passer au contenu

Comment puis-je utiliser un modèle d'interface utilisateur personnalisé avec les fonctions Lambda fournies par AWS dans Ground Truth ?

Lecture de 3 minute(s)
0

Je souhaite utiliser un modèle d'interface utilisateur personnalisé Amazon SageMaker Ground Truth et les fonctions AWS Lambda pour une tâche d'étiquetage.

Résolution

Créez un modèle d'interface utilisateur personnalisé pour la tâche d'étiquetage, comme illustré dans l'exemple suivant :

  1. Pour les tâches de segmentation sémantique, définissez la variable name sur crowd-semantic-segmentation, comme illustré dans l'exemple suivant. Pour les tâches de cadre de délimitation, définissez la variable name sur boundingBox. Pour une liste complète des éléments HTML améliorés pour les modèles personnalisés, consultez la section Référence des éléments HTML Crowd.

    <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. Créez un fichier JSON pour les étiquettes. Exemple :

    {
      "labels": [
        {
          "label": "Chair"
        },
      ...
        {
          "label": "Oven"
          }
       ]
    }
  3. Créez un fichier manifeste d'entrée pour les images. Exemple :

    {"source-ref":"s3://awsdoc-example-bucket/input_manifest/apartment-chair.jpg"}
    {"source-ref":"s3://awsdoc-example-bucket/input_manifest/apartment-carpet.jpg"}
  4. Chargez les fichiers HTML, manifeste et JSON sur Amazon Simple Storage Service (Amazon S3). Exemple :

    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. Récupérez les Amazon Resource Names (ARN) pour les fonctions Lambda de prétraitement et de consolidation des annotations. Par exemple, les ARN de segmentation sémantique sont présentés ci-dessous :
    arn:aws:lambda:eu-west-1:111122223333:function:PRE-SemanticSegmentation
    arn:aws:lambda:eu-west-1:111122223333:function:ACS-SemanticSegmentation

  6. Pour créer la tâche d'étiquetage, utilisez un kit SDK AWS, tel que 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"}],
    )

Dans l'exemple précédent, procédez comme suit :

  • Remplacez S3_OUTPUT_PATH par le chemin de sortie S3
  • Remplacez IAM_ROLE_ARN par l’ARN du rôle
  • Remplacez WORKTEAM_ARN par l'ARN de l'équipe de travail
  • Remplacez INPUT_MANIFEST_IN_S3 par l'URI du manifeste d'entrée
  • Remplacez LABELS_JSON_IN_S3 par l'URI JSON des étiquettes
  • Remplacez HTML_TEMPLATE_IN_S3 par l'URI du modèle HTML

Informations connexes

Algorithme de segmentation sémantique

AWS OFFICIELA mis à jour il y a 2 ans