Python 3.x 如何通过AWS Lambda函数对AWS SageMaker上托管的keras模型进行推理?
我有一个预先培训过的Python 3.x 如何通过AWS Lambda函数对AWS SageMaker上托管的keras模型进行推理?,python-3.x,amazon-web-services,aws-lambda,aws-api-gateway,amazon-sagemaker,Python 3.x,Amazon Web Services,Aws Lambda,Aws Api Gateway,Amazon Sagemaker,我有一个预先培训过的keras模型,我使用AWS SageMaker在AWS上托管了该模型。我已经有了一个端点,我可以使用Amazon SageMaker笔记本实例进行成功的预测 我在那里做的是提供一个.PNG图像,如下所示,模型给出了正确的预测 file= s3.Bucket(bucketname).download_file(filename_1, 'normal.png') file_name_1='normal.png' import sagemaker from sagemaker
keras
模型,我使用AWS SageMaker
在AWS
上托管了该模型。我已经有了一个端点
,我可以使用Amazon SageMaker笔记本实例
进行成功的预测
我在那里做的是提供一个.PNG图像
,如下所示,模型给出了正确的预测
file= s3.Bucket(bucketname).download_file(filename_1, 'normal.png')
file_name_1='normal.png'
import sagemaker
from sagemaker.tensorflow.model import TensorFlowModel
endpoint = 'tensorflow-inference-0000-11-22-33-44-55-666' #endpoint
predictor=sagemaker.tensorflow.model.TensorFlowPredictor(endpoint, sagemaker_session)
data = np.array([resize(imread(file_name), (137, 310, 3))])
predictor.predict(data)
现在我想使用移动应用程序进行预测。为此,我必须用python编写一个Lambda函数
,并在其上附加一个API网关
。我的Lambda函数如下所示
import os
import sys
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(CWD, "lib"))
import json
import base64
import boto3
import numpy as np
from scipy import signal
from scipy.signal import butter, lfilter
from scipy.io import wavfile
import scipy.signal as sps
import io
from io import BytesIO
import matplotlib.pylab as plt
from matplotlib import pyplot as plt
import matplotlib.image as mpimg
from datetime import datetime
from skimage.io import imread
from skimage.transform import resize
from PIL import Image
ENDPOINT_NAME = 'tensorflow-inference-0000-11-22-33-44-55-666'
runtime= boto3.client('runtime.sagemaker')
def lambda_handler(event, context):
s3 = boto3.client("s3")
# retrieving data from event.
get_file_content_from_postman = event["content"]
# decoding data.
decoded_file_name = base64.b64decode(get_file_content_from_postman)
image = Image.open(io.BytesIO(decoded_file_name))
data = np.array([resize(imread(image), (137, 310, 3))])
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME, ContentType='text/csv', Body=data)
result = json.loads(response['Body'].read().decode())
return result
最后第三行给出的错误是,“PngImageFile”对象没有属性“read”
。
你知道我在这里遗漏了什么吗?如果io.BytesIO(decoded\u file\u name)
正确地表示你的图像数据(尽管名称decoded\u file\u name
表明它是唯一的文件名,而不是实际的图像数据),那么你就不需要使用PIL。直接使用即可:
data = np.array([resize(imread(io.BytesIO(decoded_file_name)), (137, 310, 3))])
我遗漏了一件导致这个错误的事情。收到图像数据后,我使用python列表,然后使用json.dump
该列表(列表)。以下是参考代码
import os
import sys
CWD = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, os.path.join(CWD, "lib"))
import json
import base64
import boto3
import numpy as np
import io
from io import BytesIO
from skimage.io import imread
from skimage.transform import resize
# grab environment variable of Lambda Function
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= boto3.client('runtime.sagemaker')
def lambda_handler(event, context):
s3 = boto3.client("s3")
# retrieving data from event.
get_file_content_from_postman = event["content"]
# decoding data.
decoded_file_name = base64.b64decode(get_file_content_from_postman)
data = np.array([resize(imread(io.BytesIO(decoded_file_name)), (137, 310, 3))])
payload = json.dumps(data.tolist())
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME, ContentType='application/json', Body=payload)
result = json.loads(response['Body'].read().decode())
return result
是的,我已经检查了io。BytesIO(解码文件名)
表示图像数据。问题是,当我通过API网关发出请求时,它会给我错误信息,即“参数验证失败:\n参数体的类型无效,值:[[0.25353752 0.39823243 0.91831497]\n[0.25353752 0.39823243 0.91831497]\n…类型:,有效类型:,类文件对象”
@MuhammadArsalanHassan这是一个新问题。正如我之前写的,您可能有多个问题。由于您的示例不可复制,您只能逐个解决,而不是一次解决所有问题。