如何正确编写一个返回一个numpy数组的sagemaker tensorflow input_handler()? [英] How to correctly write a sagemaker tensorflow input_handler() that returns a numpy array?
问题描述
我正在尝试为智者推理容器在inference.py中实现input_handler().
I am trying to implement a input_handler() in inference.py for a sagemaker inference container.
图像/阵列非常大(3D).所以我想传递一个S3 URI,然后input_handler()函数应该从s3加载图像/数组,并返回该模型的实际numpy数组(需要一个张量):
The images/arrays are very big (3D). So I want to pass in a S3 URI, then the input_handler() function should load the image/array from s3 and return the actual numpy array for the model (which expects a tensor):
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版本<一起使用的numpy数组.1.0和input_fn(),但不适用于sagemaker python api>使用的新容器.2.0需要input_handler().
Returning a numpy array worked with the Sagemaker python api version < 1.0 and input_fn(), but does not work with the new container used by sagemaker python api > 2.0 that expects input_handler().
实际的容器映像是"763104351884.dkr.ecr.eu-central-1.amazonaws.com/tensorflow-inference:1.15-gpu".
The actual container image is "763104351884.dkr.ecr.eu-central-1.amazonaws.com/tensorflow-inference:1.15-gpu".
在推断过程中,容器在CloudWatch中出现以下错误:
During inference, I get the following error in CloudWatch thrown by the container:
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&文字...
What is the correct return type? All examples I found were for json & text...
推荐答案
这似乎可行:
返回json.dumps({"inputs":df.tolist()}).
这篇关于如何正确编写一个返回一个numpy数组的sagemaker tensorflow input_handler()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!