amazon-web-services - 使用 Lambda 和 API 网关调用 Sagemaker 端点时出错

标签 amazon-web-services tensorflow aws-lambda amazon-sagemaker

我尝试在 Sagemaker 中使用 TensorFlow-Keras 模型进行预测,但收到以下错误:

在 Amazon Cloudwatch 中,对于 Lambda 函数:

An error occurred (ModelError) when calling the InvokeEndpoint operation: Received client error (415) from model with message "
{
    "error": "Unsupported Media Type: application/x-image"
}

在 Cloudwatch 中,对于 Sagemaker:

F external/org_tensorflow/tensorflow/core/util/tensor_format.h:426] Check failed: index >= 0 && index < num_total_dims Invalid index from the dimension: 3, 0, C

数据是以base64发送的图像,Lambda函数将此图像转换为字节,Lambda函数是:

def lambda_handler(event, context):
    print("Received event: " + json.dumps(event, indent=2))
    
    data = json.loads(json.dumps(event))
    payload = data['foto']
    image = base64.b64decode(payload)
    print(type(image))
    
    try:
        response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                            ContentType='application/x-image',
                                            Body=image)
        print(response)
    except Exception as e:
        print("error en inferencia:")
        print(e)

    return payload # only for test

最佳答案

您似乎正在使用 SageMaker v2。使用 v2,您不直接设置 content_type,而是在 Serializer 实例中设置内容类型。您可以在 Predictor 的构造函数中执行此操作,也可以通过随后设置 predictor.serializer 来执行此操作。请注意,您可以使用已创建的序列化程序类,该类允许您指定 content_type,或者实现一个自定义类来处理 content_type

关于amazon-web-services - 使用 Lambda 和 API 网关调用 Sagemaker 端点时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66316372/

相关文章:

android - 在 AWS android 改造上上传文件时出错

tensorflow - 如何抑制详细的 TensorFlow 日志记录?

amazon-web-services - 当用户添加到用户池时,Cognito 启动 lambda

python - AWS lambda : Python function with Pandas dependency

python - 如何为 AWS Lambdas 安装 Numpy 和 Pandas?

amazon-web-services - 为什么 AWS Auto Scaling 在扩展期间启动多个实例?

amazon-web-services - 是否可以使用 Cloudformation 删除 aws 安全组默认导出允许所有规则?

python - Tensorflow:为什么我的代码运行越来越慢?

python - 仅评估 tf.Tensor 的非零值

amazon-web-services - 有什么方法可以将触发器添加到 Terraform 中的 lambda 函数?