调用 aws sagemaker 端点 [英] Invoke aws sagemaker endpoint

查看:23
本文介绍了调用 aws sagemaker 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 S3 中有一些数据,我想创建一个 lambda 函数来预测我部署的 aws sagemaker 端点的输出,然后我再次将输出放入 S3.在这种情况下是否有必要创建一个 api 网关,如此 link ?在 lambda 函数中我必须放什么.我期望放(在哪里可以找到数据,如何调用端点,在哪里放数据)

I have some data in S3 and I want to create a lambda function to predict the output with my deployed aws sagemaker endpoint then I put the outputs in S3 again. Is it necessary in this case to create an api gateway like decribed in this link ? and in the lambda function what I have to put. I expect to put (where to find the data, how to invoke the endpoint, where to put the data)

import boto3
import io
import json
import csv
import os


client = boto3.client('s3') #low-level functional API

resource = boto3.resource('s3') #high-level object-oriented API
my_bucket = resource.Bucket('demo-scikit-byo-iris') #subsitute this for your s3 bucket name. 

obj = client.get_object(Bucket='demo-scikit-byo-iris', Key='foo.csv')
lines= obj['Body'].read().decode('utf-8').splitlines()
reader = csv.reader(lines)

import io
file = io.StringIO(lines)

# grab environment variables
runtime= boto3.client('runtime.sagemaker')

response = runtime.invoke_endpoint(
    EndpointName= 'nilm2',
    Body = file.getvalue(),
    ContentType='*/*',
    Accept = 'Accept')

output = response['Body'].read().decode('utf-8')

我的数据是一个包含 2 列浮点数的 csv 文件,没有标题,问题是这些行返回一个字符串列表(每一行都是这个列表的一个元素:['11.55,65.23', '55.68,69.56'...]) 调用运行良好,但响应也是一个字符串: output = '65.23 ,65.23 ,22.56 ,...'

my data is a csv file of 2 columns of floats with no headers, the problem is that lines return a list of strings(each row is an element of this list:['11.55,65.23', '55.68,69.56'...]) the invoke work well but the response is also a string: output = '65.23 ,65.23 ,22.56 ,...'

那么如何将此输出作为 csv 文件保存到 S3

So how to save this output to S3 as a csv file

谢谢

推荐答案

如果您的 Lambda 函数已调度,那么您将不需要 API 网关.但是,如果预测操作将由用户触发,例如由应用程序触发,您将需要.

If your Lambda function is scheduled, then you won't need an API Gateway. But if the predict action will be triggered by a user, by an application, for example, you will need.

当您调用 invoke 端点时,实际上您正在调用 SageMaker 端点,这与 API 网关端点不同.

When you call the invoke endpoint, actually you are calling a SageMaker endpoint, which is not the same as an API Gateway endpoint.

SageMaker 的常见架构是:

A common architecture with SageMaker is:

  1. API Gateway with 接收请求然后调用授权器,然后调用你的 Lambda;
  2. Lambda with 在您的输入数据中进行一些解析,然后调用您的 SageMaker 预测端点,然后处理结果并返回到您的应用程序.

根据你描述的情况,我不能说你的任务是学术性的还是生产性的.

By the situation you describe, I can't say if your task is some academic stuff or a production one.

那么,如何将数据从 Lambda 保存为 CSV 文件?

So, how you can save the data as a CSV file from your Lambda?

我相信您可以解析输出,然后将文件上传到 S3.在这里您将手动或使用 lib 解析,使用 boto3 您可以上传文件.模型的输出取决于您在 SageMaker 图像上的实现.因此,如果您需要另一种格式的响应数据,也许您需要使用 自定义图片.我通常使用自定义图像,我可以定义如何处理请求/响应数据.

I believe you can just parse the output, then just upload the file to S3. Here you will parse manually or with a lib, with boto3 you can upload the file. The output of your model depends on your implementation on SageMaker image. So, if you need the response data in another format, maybe you will need to use a custom image. I normally use a custom image, which I can define how I want to handle my data on requests/responses.

就生产任务而言,我当然建议您查看 SageMaker 的批量转换作业.您可以提供输入文件(S3 路径)和目标文件(另一个 S3 路径).SageMaker 将运行批量预测并将结果保存在一个文件中.此外,您不需要将模型部署到端点,当此作业运行时,将创建端点的实例、下载数据以进行预测、进行预测、上传输出并关闭实例.您只需要一个经过训练的模型.

In terms of a production task, I certainly recommend you check Batch transform jobs from SageMaker. You can provide an input file (the S3 path) and also a destination file (another S3 path). The SageMaker will run the batch predictions and will persist a file with the results. Also, you won't need to deploy your model to an endpoint, when this job run, will create an instance of your endpoint, download your data to predict, do the predictions, upload the output, and shut down the instance. You only need a trained model.

这里有一些关于批量转换作业的信息:

Here some info about Batch transform jobs:

https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html

https://docs.aws.amazon.com/sagemaker/latest/dg/ex1-batch-transform.html

希望对您有所帮助,如果需要更多信息,请告诉我.

I hope it helps, let me know if need more info.

问候.

这篇关于调用 aws sagemaker 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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