SageMaker:调用 InvokeEndpoint 操作时发生错误 (ModelError):无法评估提供的负载
SageMaker: An error occurred (ModelError) when calling the InvokeEndpoint operation: unable to evaluate payload provided
我在 Jupyter notebook 的 Amazon SageMaker(图像分类算法)中有一个运行良好的端点。在 Lambda 函数中也工作正常,当我从 API 网关调用 Lambda 函数时,从 API 网关的测试中,也工作正常。
问题是当我根据这个答案从 Postman 调用 API 时:
Lambda 中的代码是:
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
}
错误是:
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. ```
我用 this post:
解决了
谢谢大家
最后lambda函数中的代码是:
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
}
我在 Jupyter notebook 的 Amazon SageMaker(图像分类算法)中有一个运行良好的端点。在 Lambda 函数中也工作正常,当我从 API 网关调用 Lambda 函数时,从 API 网关的测试中,也工作正常。
问题是当我根据这个答案从 Postman 调用 API 时:
Lambda 中的代码是:
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
}
错误是:
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. ```
我用 this post:
解决了谢谢大家
最后lambda函数中的代码是:
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
}