如何正确编写返回numpy数组的sagemaker tensorflow input_handler()



我正试图在推理.py中为sagemaker推理容器实现一个input_handler((。

图像/阵列非常大(3D(。因此,我想传入一个S3 URI,然后input_handler((函数应该从S3加载图像/数组,并返回模型的实际numpy数组(它需要一个张量(:

def input_handler(data, context):
d = data.read().decode('utf-8')
body = json.loads(d)
s3path = body['s3_path']
s3 = S3FileSystem()
df = np.load(s3.open(s3path))
return df

返回一个使用Sagemaker python api版本<1.0和input_fn((,但不适用于sagemaker-python-api使用的新容器>2.0,期望input_handler((。

实际的容器图像是";763104351884.dkr.ecr.eu-central-1.amazonaws.com/tensorflow推理:1.15-gpu";。

在推理过程中,我在CloudWatch中得到了容器抛出的以下错误:

ERROR:python_service:exception handling request: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all(
Traceback (most recent call last):
File "/sagemaker/python_service.py", line 289, in _handle_invocation_post
res.body, res.content_type = self._handlers(data, context)
File "/sagemaker/python_service.py", line 322, in handler
response = requests.post(context.rest_uri, data=processed_input)
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 116, in post
return request('post', url, data=data, json=json, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/api.py", line 60, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/requests/sessions.py", line 512, in request
data=data or 
{}
,

正确的退货类型是什么?我发现的所有例子都是json&文本

这似乎有效:

return json.dumps({"inputs": df.tolist() }).

最新更新