簡體   English   中英

如何通過 AWS Lambda ZC1C425268E17985D1AB5074 對 AWS SageMaker 上托管的 keras model 進行推斷?

[英]How to make inference to a keras model hosted on AWS SageMaker via AWS Lambda function?

我有一個預先訓練的keras model 我使用AWS SageMakerAWS上托管。 我有一個endpoint ,可以使用Amazon SageMaker Notebook instance進行成功的predictions

我在那里做的是提供如下所示的.PNG image ,並且 model 給了我正確的預測。

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)

現在我想使用mobile application進行預測。 為此,我必須在 python 中寫一個Lambda function並將一個API gateway到它。 我的Lambda function如下。

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' object has no attribute 'read' 知道我在這里缺少什么嗎?

如果io.BytesIO(decoded_file_name)正確表示您的圖像數據(盡管名稱decoded_file_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
        

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM