我有同样的问题,但我想处理 jpeg 请求。
准备好后,您model_data
可以使用以下几行来部署它。
from sagemaker.tensorflow.model import TensorFlowModel
sagemaker_model = TensorFlowModel(
model_data = 's3://path/to/model/model.tar.gz',
role = role,
framework_version = '1.12',
entry_point = 'train.py',
source_dir='my_src',
env={'SAGEMAKER_REQUIREMENTS': 'requirements.txt'}
)
predictor = sagemaker_model.deploy(
initial_instance_count=1,
instance_type='ml.m4.xlarge',
endpoint_name='resnet-tensorflow-classifier'
)
你的笔记本应该有一个my_src
目录,其中包含一个文件train.py
和一个requirements.txt
文件。该train.py
文件应该input_fn
定义一个函数。对我来说,该函数处理图像/jpeg 内容,但模式是相同的。
import io
import numpy as np
from PIL import Image
from keras.applications.resnet50 import preprocess_input
from keras.preprocessing import image
JPEG_CONTENT_TYPE = 'image/jpeg'
CSV_CONTENT_TYPE = 'text/csv'
# Deserialize the Invoke request body into an object we can perform prediction on
def input_fn(request_body, content_type=JPEG_CONTENT_TYPE):
# process an image uploaded to the endpoint
if content_type == JPEG_CONTENT_TYPE:
img = Image.open(io.BytesIO(request_body)).resize((300, 300))
img_array = np.array(img)
expanded_img_array = np.expand_dims(img_array, axis=0)
x = preprocess_input(expanded_img_array)
return x
# you would have something like this:
if content_type == CSV_CONTENT_TYPE:
# handle input
return handled_input
else:
raise errors.UnsupportedFormatError(content_type)
如果您的train.py
代码导入了一些模块,则必须提供requirements.txt
定义这些依赖项(这是我在文档中找不到的部分)。
希望这对将来的某人有所帮助。