使用多维数组调用 AWS 中的端点

Invoking an endpoint in AWS with a multidimensional array

我已经按照本教程在 SageMaker Studio 中部署了一个 Tensorflow 模型: https://aws.amazon.com/de/blogs/machine-learning/deploy-trained-keras-or-tensorflow-models-using-amazon-sagemaker/ 该模型需要一个多维数组作为输入。从笔记本本身调用它是有效的:

import numpy as np
import json
data = np.load("testValues.npy")
pred=predictor.predict(data)

但我无法使用以下代码从 boto 3 客户端调用它:

import json
import boto3
import numpy as np
import io
 
client = boto3.client('runtime.sagemaker')
datain = np.load("testValues.npy")
data=datain.tolist();
response = client.invoke_endpoint(EndpointName=endpoint_name, Body=json.dumps(data))
response_body = response['Body']
print(response_body.read())

这会引发错误:

An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (415) from model with message "{"error": "Unsupported Media Type: Unknown"}".

我想原因是 json 媒体类型,但我不知道如何让它恢复原状。 我试过这个:https://github.com/aws/amazon-sagemaker-examples/issues/644 但它似乎没有任何改变

这为我解决了这个问题: 缺少内容类型。

import json
import boto3
import numpy as np
import io

client = boto3.client('runtime.sagemaker',aws_access_key_id=..., aws_secret_access_key=...,region_name=...)
endpoint_name = '...'

data = np.load("testValues.npy")


payload = json.dumps(data.tolist())
response = client.invoke_endpoint(EndpointName=endpoint_name,
                                  ContentType='application/json',
                                   Body=payload)
result = json.loads(response['Body'].read().decode())
res = result['predictions']
print("test")