简体   繁体   中英

SageMaker: An error occurred (ModelError) when calling the InvokeEndpoint operation: unable to evaluate payload provided

I have a endpoint in Amazon SageMaker (Image-classification algorithm) in Jupyter notebook that works fine. In Lambda function works fine too, when I call the Lambda function from API Gateway, from test of API Gateway, works fine too.

The problem is when I call the API from Postman according this answer: "Post Image data using POSTMAN"

The code in Lambda is:

import boto3
import json
import base64

ENDPOINT_NAME = "DEMO-XGBoostEndpoint-Multilabel"
runtime= boto3.client("runtime.sagemaker")
imagen_ = "/tmp/imageToProcess.jpg"

def write_to_file(save_path, data):
    with open(save_path, "wb") as f:
        f.write(base64.b64decode(data))

def lambda_handler(event, context):
    img_json = json.loads(json.dumps(event))

    write_to_file(imagen_, json.dumps(event, indent=2))

    with open(imagen_, "rb") as image:
        f = image.read()
        b = bytearray(f)

    payload = b

    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       ContentType="application/x-image",
                                       Body=payload)

    #print(response)
    result = json.loads(response["Body"].read().decode())
    print(result)
    predicted_label=[]
    classes = ["chair", "handbag", "person", "traffic light", "clock"]
    for idx, val in enumerate(classes):
        print("%s:%f "%(classes[idx], result[idx]), end="")
        predicted_label += (classes[idx], result[idx])

    return {
      "statusCode": 200,
      "headers": { "content-type": "application/json"},
      "body":  predicted_label
}

The error is:

Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 26, in lambda_handler
    Body=payload)
  File "/var/runtime/botocore/client.py", line 316, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/var/runtime/botocore/client.py", line 626, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.errorfactory.ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (400) from model with message "unable to evaluate payload provided". See https://us-east-2.console.aws.amazon.com/cloudwatch/home?region=us-east-2#logEventViewer:group=/aws/sagemaker/Endpoints/DEMO-XGBoostEndpoint-Multilabel in account 866341179300 for more information. ```

I resolved with this post:

Thank all

Finally the code in lambda function is:

                    import os
                    import boto3
                    import json
                    import base64

                    ENDPOINT_NAME = os.environ['endPointName']
                    CLASSES = "["chair", "handbag", "person", "traffic light", "clock"]"
                    runtime= boto3.client("runtime.sagemaker")

                    def lambda_handler(event, context):

                        file_content = base64.b64decode(event['content'])

                        payload = file_content
                        response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                                           ContentType="application/x-image",
                                                           Body=payload)

                        result = json.loads(response["Body"].read().decode())
                        print(result)
                        predicted_label=[]
                        classes = CLASSES
                        for idx, val in enumerate(classes):
                            print("%s:%f "%(classes[idx], result[idx]), end="")
                            predicted_label += (classes[idx], result[idx])

                        return {
                          "statusCode": 200,
                          "headers": { "content-type": "application/json"},
                          "body":  predicted_label
                    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM