如何正确编写返回 numpy 数组的 sagemaker tensorflow input_handler()? [英] How to correctly write a sagemaker tensorflow input_handler() that returns a numpy array?

查看:19
本文介绍了如何正确编写返回 numpy 数组的 sagemaker tensorflow input_handler()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 inference.py 中为 sagemaker 推理容器实现 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...

推荐答案

这似乎有效:

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

这篇关于如何正确编写返回 numpy 数组的 sagemaker tensorflow input_handler()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆