如何解决烧瓶模型部署的 Sagemaker 超时
下面是 ECR Container 中的 predict.py。 Sagemaker 端点在重试 10-12 分钟后给出“状态:失败”输出。 /ping 和 /invocations 方法都可用
activation.jar
解决方法
您的预测器文件旨在测试模型是否已加载到 /ping 中,以及您是否可以在 /invocations 中执行推理。如果您已在 SageMaker 上训练您的模型,则需要按如下方式从 /opt/ml 目录加载它。
prefix = "/opt/ml/"
model_path = os.path.join(prefix,"model")
class ScoringService(object):
model = None # Where we keep the model when it's loaded
@classmethod
def get_model(rgrs):
"""Get the model object for this instance,loading it if it's not already loaded."""
if rgrs.model == None:
with open(os.path.join(model_path,"rf-model.pkl"),"rb") as inp:
rgrs.model = pickle.load(inp)
return rgrs.model
@classmethod
def predict(rgrs,input):
"""For the input,do the predictions and return them.
Args:
input (a pandas dataframe): The data on which to do the predictions. There will be
one prediction per row in the dataframe"""
rf = rgrs.get_model()
return rf.predict(input)
该类有助于加载您的模型,然后我们可以在 /ping 中对其进行验证。
# The flask app for serving predictions
app = flask.Flask(__name__)
@app.route("/ping",methods=["GET"])
def ping():
"""Determine if the container is working and healthy. In this sample container,we declare
it healthy if we can load the model successfully."""
health = ScoringService.get_model() is not None # You can insert a health check here
status = 200 if health else 404
return flask.Response(response="\n",status=status,mimetype="application/json")
此处 SageMaker 将测试您是否已正确加载模型。对于 /invocations,包括您传递到模型预测功能的任何数据格式的推理逻辑。
@app.route("/invocations",methods=["POST"])
def transformation():
data = None
# Convert from CSV to pandas
if flask.request.content_type == "text/csv":
data = flask.request.data.decode("utf-8")
s = io.StringIO(data)
data = pd.read_csv(s,header=None)
else:
return flask.Response(
response="This predictor only supports CSV data",status=415,mimetype="text/plain"
)
print("Invoked with {} records".format(data.shape[0]))
# Do the prediction
predictions = ScoringService.predict(data)
# Convert from numpy back to CSV
out = io.StringIO()
pd.DataFrame({"results": predictions}).to_csv(out,header=False,index=False)
result = out.getvalue()
return flask.Response(response=result,status=200,mimetype="text/csv")
确保如上所示设置或配置您的 predictor.py,以便 SageMaker 能够正确理解/检索您的模型。
我为 AWS 工作,我的意见是我自己的。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。