简体   繁体   English

AWS Lambda 将图像文件发送到 Amazon Sagemaker

[英]AWS Lambda send image file to Amazon Sagemaker

I have the next problem, I try send image file in base64 to Lambda function(write in Python), for invoke Sagemaker Endpoint, this is my Lambda Function: I have the next problem, I try send image file in base64 to Lambda function(write in Python), for invoke Sagemaker Endpoint, this is my Lambda Function:

import os
import io
import boto3
import json
import base64

# grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= boto3.client('runtime.sagemaker')

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    
    data = json.loads(json.dumps(event))
    payload = data['foto']
    img = base64.b64decode(payload)
    #body = json.dumps({"instances": img})

    try:
        response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                            Body=img)
        print(response)
    except Exception as e:
        print("Inference Error:")
        print(e)

    return { "img":"ok" }

And sagemaker endpoint invoke in Jupyter works fine with this code, Jupyter 中的 sagemaker 端点调用可以正常使用此代码,

from tensorflow.keras.preprocessing.image import ImageDataGenerator

test_datagen = ImageDataGenerator(rescale=1./255)
test_generator = test_datagen.flow_from_directory('./data/val', target_size=(224,224))

instance = test_generator[1][0]
print(instance.shape)
array = instance.reshape((1,) + instance.shape)
payload = { 'instances': array.tolist() }

resp = tf_predictor.predict(payload)['predictions']
print(resp)

The error in Lambda says that the data type is unknown, and CloudWatch error in dimensions. Lambda 中的错误表示数据类型未知,CloudWatch 维度错误。

I think, that the error is in Body of invoke_endpoint method, but i not found how convert bytes type to list with image data.我认为,错误出现在 invoke_endpoint 方法的主体中,但我没有找到如何将字节类型转换为带有图像数据的列表。 I need custom Lambda Function with numpy, or other library?我需要自定义 Lambda Function 和 numpy 或其他库? or isn't necesary或者不是必需的

How are you invoking the Lambda function?您如何调用 Lambda function? If you are using API Gateway to pass the image data to the SageMaker Runtime endpoint, you may need to set the contentHandling properties on the integration to handle binary encoding.如果您使用 API 网关将图像数据传递到 SageMaker Runtime 端点,您可能需要在集成上设置 contentHandling 属性以处理二进制编码。 See here:看这里:

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-payload-encodings.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM