[英]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.