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

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

问题描述

我在S3中有一些数据,我想创建一个lambda函数来预测已部署的aws sagemaker端点的输出,然后将输出再次放入S3中​​.在这种情况下是否有必要创建一个

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 \ n,65.23 \ n,22.56 \ n,...'

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\n,65.23\n,22.56\n,...'

那么如何将此输出作为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.

调用调用端点时,实际上是在调用SageMaker端点,该端点与API Gateway端点不同.

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网关收到请求,然后调用授权者,然后 调用您的Lambda;
  2. 具有的Lambda在输入数据中进行一些解析,然后调用SageMaker预测端点,然后处理结果并返回到您的应用程序.
  1. API Gateway with receives a request then calls an authorizer, then invoke your Lambda;
  2. A Lambda with does some parsing in your input data, then calls your SageMaker prediction endpoint, then, handles the result and returns to your application.

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

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

希望对您有所帮助.

致谢.

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

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