简体   繁体   English

如何使用Lambda和API网关部署由AWS Sagemaker创建的乳腺癌预测终端节点?

[英]How to deploy breast cancer prediction endpoint created by AWS Sagemaker using Lambda and API gateway?

I am trying to deploy the existing breast cancer prediction model on Amazon Sagemanker using AWS Lambda and API gateway. 我正在尝试使用AWS Lambda和API网关在Amazon Sagemanker上部署现有的乳腺癌预测模型。 I have followed the official documentation from the below url. 我遵循以下网址中的官方文档。

https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/ https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/

I am getting a type error at "predicted_label". 我在“ predicted_label”处遇到类型错误。

 result = json.loads(response['Body'].read().decode())
 print(result)
 pred = int(result['predictions'][0]['predicted_label'])
 predicted_label = 'M' if pred == 1 else 'B'

 return predicted_label

please let me know if someone could resolve this issue. 请让我知道是否有人可以解决此问题。 Thank you. 谢谢。

By printing the result type by print(type(result)) you can see its a dictionary. 通过使用print(type(result))结果类型,您可以看到其字典。 now you can see the key name is "score" instead of "predicted_label" that you are giving to pred. 现在您可以看到键名是“ score”,而不是您为pred提供的“ predicted_label”。 Hence replace it with 因此,将其替换为

pred = int(result['predictions'][0]['score'])

I think this solves your problem. 我认为这可以解决您的问题。

here is my lambda function: 这是我的lambda函数:

import os
import io
import boto3
import json
import csv

# 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['data']
   print(payload)

   response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
                                      ContentType='text/csv',
                                      Body=payload)
   #print(response)
   print(type(response))
   for key,value in response.items():
       print(key,value)
   result = json.loads(response['Body'].read().decode())
   print(type(result))
   print(result['predictions'])
   pred = int(result['predictions'][0]['score'])
   print(pred)
   predicted_label = 'M' if pred == 1 else 'B'

   return predicted_label

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

相关问题 使用 Lambda 和 API 网关调用 Sagemaker 端点时出错 - Error in Call to Sagemaker Endpoint with Lambda and API Gateway 我如何使用 API 网关调用 sagemaker 推理端点 - How can i call sagemaker inference endpoint using API gateway 如何将 Hapi.js API 部署到 AWS Lambda 和 API 网关? - how to deploy a Hapi.js API to AWS Lambda and API gateway? 如何使用 AWS Websocket API Gateway Lambda 端点回调函数 - How to use AWS Websocket API Gateway Lambda endpoint callback function 无法调用使用 AWS-Lambda 在 sagemaker 中创建的 XG-Boost 端点 - Unable to call XG-Boost endpoint created in sagemaker using AWS-Lambda 如何使用javascript获取通过SAM创建的AWS API网关的端点 - How to get endpoint of AWS API gateway created through SAM using javascript 是否可以使用AWS API为Lambda函数设置AWS API Gateway端点? - Is it possible to set up an AWS API Gateway endpoint for a Lambda function, using the AWS API? aws api 网关和 lambda:多个端点/功能与单个端点 - aws api gateway & lambda: multiple endpoint/functions vs single endpoint 如何使用 CDK、sagemaker 部署 AWS? - How to deploy AWS using CDK, sagemaker? 从 API Gateway 获取 terraform 中 AWS lambda 的端点 - Get endpoint from API Gateway for an AWS lambda in terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM