測試註釋前與註釋後 Lambda 函式 - Amazon SageMaker

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

測試註釋前與註釋後 Lambda 函式

您可在 Lambda 主控台測試註釋前與註釋後 Lambda 函式。如您是 Lambda 的新使用者,可透過 AWS Lambda 開發人員指南的主控台利用建立 Lambda 函式教學課程來了解如何在主控台測試或調用 Lambda 函式。

您可以使用此頁面上的各個部分來學習如何測試透過 AWS Serverless Application Repository (SAR) 提供的「Ground Truth」預註解樣板和後註解樣板。

必要條件

您必須執行下列動作,才能採用本頁所述的測試。

  • 您需要 Lambda 主控台的存取權,且您需要權限以便建立及調用 Lambda 函式。若要了解如何設定這些權限,請參閱授予建立和選取 AWS Lambda 函數的權限

  • 如您尚未部署 Ground Truth SAR 配方,請利用為自訂標籤工作流程建立 Lambda 函式的程序來執行此操作。

  • 若要測試註釋後 Lambda 函式,您必須在 Amazon S3 擁有包含範例註釋資料的資料檔案。對於簡單測試,您可將下列程式碼複製並貼到檔案,然後另存新檔為 sample-annotations.json將其上傳到 Amazon S3。請注意此檔案的 S3 URI,您需要此資訊才能設定註釋後 Lambda 測試。

    [{"datasetObjectId":"0","dataObject":{"content":"To train a machine learning model, you need a large, high-quality, labeled dataset. Ground Truth helps you build high-quality training datasets for your machine learning models."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":8,\"label\":\"verb\",\"startOffset\":3},{\"endOffset\":27,\"label\":\"adjective\",\"startOffset\":11},{\"endOffset\":33,\"label\":\"object\",\"startOffset\":28},{\"endOffset\":51,\"label\":\"adjective\",\"startOffset\":46},{\"endOffset\":65,\"label\":\"adjective\",\"startOffset\":53},{\"endOffset\":74,\"label\":\"adjective\",\"startOffset\":67},{\"endOffset\":82,\"label\":\"adjective\",\"startOffset\":75},{\"endOffset\":102,\"label\":\"verb\",\"startOffset\":97},{\"endOffset\":112,\"label\":\"verb\",\"startOffset\":107},{\"endOffset\":125,\"label\":\"adjective\",\"startOffset\":113},{\"endOffset\":134,\"label\":\"adjective\",\"startOffset\":126},{\"endOffset\":143,\"label\":\"object\",\"startOffset\":135},{\"endOffset\":169,\"label\":\"adjective\",\"startOffset\":153},{\"endOffset\":176,\"label\":\"object\",\"startOffset\":170}]}}"}}]},{"datasetObjectId":"1","dataObject":{"content":"Sift 3 cups of flour into the bowl."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":4,\"label\":\"verb\",\"startOffset\":0},{\"endOffset\":6,\"label\":\"number\",\"startOffset\":5},{\"endOffset\":20,\"label\":\"object\",\"startOffset\":15},{\"endOffset\":34,\"label\":\"object\",\"startOffset\":30}]}}"}}]},{"datasetObjectId":"2","dataObject":{"content":"Jen purchased 10 shares of the stock on Janurary 1st, 2020."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":3,\"label\":\"person\",\"startOffset\":0},{\"endOffset\":13,\"label\":\"verb\",\"startOffset\":4},{\"endOffset\":16,\"label\":\"number\",\"startOffset\":14},{\"endOffset\":58,\"label\":\"date\",\"startOffset\":40}]}}"}}]},{"datasetObjectId":"3","dataObject":{"content":"The narrative was interesting, however the character development was weak."},"annotations":[{"workerId":"private.us-west-2.0123456789","annotationData":{"content":"{\"crowd-entity-annotation\":{\"entities\":[{\"endOffset\":29,\"label\":\"adjective\",\"startOffset\":18},{\"endOffset\":73,\"label\":\"adjective\",\"startOffset\":69}]}}"}}]}]
  • 您必須使用中的指示授授予註釋後 Lambda 權限以便存取註釋予註釋後 Lambda 函數的執行角色權限,以 SageMaker 執行您用來建立標籤工作的執行角色。註釋後的 Lambda 函數使用 SageMaker 執行角色來存取 S3 中的註釋資料檔案。sample-annotations.json

測試註釋前 Lambda 函式

使用下列程序來測試部署 Ground Truth AWS Serverless Application Repository (SAR) 方法時建立的預先註解 Lambda 函數。

測試 Ground Truth SAR 配方註釋前 Lambda 函式
  1. 開啟 Lambda 主控台的 Functions (函式) 頁面

  2. 從 Ground Truth SAR 配方選取部署的註釋前函式。此函式名稱類似 serverlessrepo-aws-sagema-GtRecipePreHumanTaskFunc-<id>

  3. Code source (程式碼來源) 區段,選取 Test (測試) 旁邊的箭頭。

  4. 選取 Configure test event (設定測試事件)。

  5. 保持選取 Create new test event (建立新測試事件) 選項。

  6. 事件範本下,選取 SageMakerGround Truth PreHumanTask

  7. 為測試指定 Event name (事件名稱)。

  8. 選取建立

  9. 再次選取 Test (測試) 旁邊的箭頭,您應會看到已選取您建立的測試 (由事件名稱以點表示)。如未選取,請選取。

  10. 選取 Test (測試),即可執行測試。

在執行測試之後,您可看到 Execution results (執行結果)。您應會在 Function logs (函式日誌) 看到類似下列回應:

START RequestId: cd117d38-8365-4e1a-bffb-0dcd631a878f Version: $LATEST Received event: { "version": "2018-10-16", "labelingJobArn": "arn:aws:sagemaker:us-east-2:123456789012:labeling-job/example-job", "dataObject": { "source-ref": "s3://sagemakerexample/object_to_annotate.jpg" } } {'taskInput': {'taskObject': 's3://sagemakerexample/object_to_annotate.jpg'}, 'isHumanAnnotationRequired': 'true'} END RequestId: cd117d38-8365-4e1a-bffb-0dcd631a878f REPORT RequestId: cd117d38-8365-4e1a-bffb-0dcd631a878f Duration: 0.42 ms Billed Duration: 1 ms Memory Size: 128 MB Max Memory Used: 43 MB

我們可在此回應看到 Lambda 函式的輸出符合所需的註釋前回應語法:

{'taskInput': {'taskObject': 's3://sagemakerexample/object_to_annotate.jpg'}, 'isHumanAnnotationRequired': 'true'}

測試註釋後 Lambda 函式

使用下列程序來測試部署 Ground Truth AWS Serverless Application Repository (SAR) 方法時建立的註解後 Lambda 函數。

測試 Ground Truth SAR 配方註釋後 Lambda
  1. 開啟 Lambda 主控台的 Functions (函式) 頁面

  2. 從 Ground Truth SAR 配方選取部署的註釋後函式。此函式名稱類似 serverlessrepo-aws-sagema-GtRecipeAnnotationConsol-<id>

  3. Code source (程式碼來源) 區段,選取 Test (測試) 旁邊的箭頭。

  4. 選取 Configure test event (設定測試事件)。

  5. 保持選取 Create new test event (建立新測試事件) 選項。

  6. 事件範本下,選取 SageMakerGround Truth AnnotationConsolidation

  7. 為測試指定 Event name (事件名稱)。

  8. 修改提供的範本程式碼,如下所示:

    • 將中roleArn的 Amazon 資源名稱 (ARN) 取代為您用來建立標籤任務之 SageMaker 執行角色的 ARN。

    • s3Uri 的 S3 URI 取代為您新增至 Amazon S3 sample-annotations.json 檔案的 URI。

    在進行這些修改之後,測試會看起來類似以下內容:

    { "version": "2018-10-16", "labelingJobArn": "arn:aws:sagemaker:us-east-2:123456789012:labeling-job/example-job", "labelAttributeName": "example-attribute", "roleArn": "arn:aws:iam::222222222222:role/sm-execution-role", "payload": { "s3Uri": "s3://your-bucket/sample-annotations.json" } }
  9. 選取建立

  10. 再次選取 Test (測試) 旁邊的箭頭,您應會看到已選取您建立的測試 (由事件名稱以點表示)。如未選取,請選取。

  11. 選擇 Test (測試),即可執行測試。

在執行測試之後,您應會在 Function Logs (函式日誌) 看見 -- Consolidated Output -- 區段,其中包含 sample-annotations.json 的所有註釋清單。