Nabajyoti Borah
Nabajyoti Borah

Reputation: 49

Parameter validation failed:\nInvalid type for parameter Body in endpoint

error

 {
      "errorMessage": "Parameter validation failed:\nInvalid type for parameter Body, value: [[[[-0.4588235, -0.27058822, -0.44313723], [-0.4823529, -0.47450978, -0.6], [-0.7490196, -0.70980394, -0.75686276], 
    ....
    [0.18431377, 0.19215691, 0.15294123], [0.0196079, 0.02745104, -0.03529412], [-0.0745098, -0.05882353, -0.16862744], [-0.4823529, -0.5058824, -0.62352943], [-0.38039213, -0.372549, -0.4352941], [-0.4588235, -0.41960782, -0.47450978], [-0.56078434, -0.58431375, -0.62352943], [-0.4352941, -0.41960782, -0.4588235], [-0.40392154, -0.41176468, -0.45098037], [-0.30196077, -0.34117645, -0.372549], [-0.30196077, -0.29411763, -0.34117645], [-0.26274508, -0.2235294, -0.27843136], [0.00392163, -0.01176471, 0.09019613], [0.09019613, 0.09803927, -0.01176471], [0.06666672, 0.12941182, -0.05098039], [0.03529418, 0.09019613, -0.03529412], [0.09019613, 0.10588241, 0.00392163], [-0.01960784, -0.01176471, -0.05882353], [0.03529418, 0.04313731, -0.01960784], [0.13725495, 0.15294123, 0.06666672], [0.06666672, 0.07450986, 0.02745104], [0.16078436, 0.1686275, 0.20000005], [0.43529415, 0.52156866, 0.5686275], [0.5764706, 0.64705884, 0.7019608], [0.67058825, 0.7490196, 0.75686276], [0.5764706, 0.654902, 0.6627451], [0.5921569, 0.67058825, 0.6784314]]]], type: <class 'list'>, valid types: <class 'bytes'>, <class 'bytearray'>, file-like object",
      "errorType": "ParamValidationError",
      "stackTrace": [
        [
          "/var/task/lambda_function.py",
          16528,
          "lambda_handler",
          "[ 0.5921569 ,  0.67058825,  0.6784314 ]]]])"
        ],
        [
          "/var/runtime/botocore/client.py",
          357,
          "_api_call",
          "return self._make_api_call(operation_name, kwargs)"
        ],
        [
          "/var/runtime/botocore/client.py",
          649,
          "_make_api_call",
          "api_params, operation_model, context=request_context)"
        ],
        [
          "/var/runtime/botocore/client.py",
          697,
          "_convert_to_request_dict",
          "api_params, operation_model)"
        ],
        [
          "/var/runtime/botocore/validate.py",
          297,
          "serialize_to_request",
          "raise ParamValidationError(report=report.generate_report())"
        ]
      ]
    }

lambda code

import os
import io
import boto3
import JSON

ENDPOINT_NAME = "tensorflow-training-2021-01-24-03-35-44-884"
runtime= boto3.client('runtime.sagemaker')

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    
    data = json.loads(json.dumps(event))
    payload = data['data']
    print(payload)
    
    response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                       Body=payload)
    print(response)
    result = json.loads(response['Body'].read().decode())
    print(result)

    return result[0]

I trained the model in sagemaker using TensorFlow

Estimator part

pets_estimator = TensorFlow(
    entry_point='train.py',
    role=role,
    train_instance_type='ml.m5.large',
    train_instance_count=1,
    framework_version='2.1.0',
    py_version='py3',
    output_path='s3://imageclassificationtest202/data',
    sagemaker_session=sess
)

I don't know why but I can't send data using JSON for

{
"data" : [[[[90494]...]]
}

My model simply accept NumPy array of dimension (1,128,128,3), and I m sending that data in JSON data field, but its saying invalid format, need byte or byte array

Upvotes: 3

Views: 2258

Answers (2)

Cloudy Swaggy
Cloudy Swaggy

Reputation: 21

If you are following the reference from the AWS Blog, one way to solve the error would be:

data= json.dumps(event).encode('utf-8')

Then in the invokeEndPoint method, we would set Body=data .

Therefore, in reference to your code, I would suggest you set:

payload=json.dumps(data).encode('utf-8')

Upvotes: 2

Harry
Harry

Reputation: 1

you have to set a serializer on your predictor, not sure how to do in your example but when I deploy a model I set this on the returned predictor.

from sagemaker.serializers import CSVSerializer
xgb_predictor.serializer = CSVSerializer() # set the serializer type

Upvotes: 0

Related Questions