python - Sagemaker:指定自定义入口点会导致未找到错误
问题描述
我正在尝试将使用 tensorflow 训练的对象检测模型部署到 sagemaker。我能够在模型创建期间在不指定任何入口点的情况下部署它,但事实证明这样做仅适用于小尺寸图像(Sagemaker 的限制为 5MB)。我用于此的代码如下:
from sagemaker.tensorflow.serving import Model
# Initialize model ...
model = Model(
model_data= s3_path_for_model,
role=sagemaker_role,
framework_version="1.14",
env=env)
# Deploy model ...
predictor = model.deploy(initial_instance_count=1,
instance_type='ml.t2.medium')
# Test using an image ...
import cv2
import numpy as np
image_content = cv2.imread("PATH_TO_IMAGE",
1).astype('uint8').tolist()
body = {"instances": [{"inputs": image_content}]}
# Works fine for small images ...
# I get predictions perfectly with this ...
results = predictor.predict(body)
所以,我四处搜索,发现我需要传递一个entry_point
forModel()
才能预测更大的图像。就像是:
model = Model(
entry_point="inference.py",
dependencies=["requirements.txt"],
model_data= s3_path_for_model,
role=sagemaker_role,
framework_version="1.14",
env=env
)
但是这样做会给出FileNotFoundError: [Errno 2] No such file or directory: 'inference.py'。请在这里提供一点帮助。我正在使用sagemaker-python-sdk
. 我的文件夹结构如下:
model
|__ 001
|__saved_model.pb
|__variables
|__<contents here>
|__ code
|__inference.py
|__requirements.txt
注意:我也尝试过 ./code/inference.py 和 /code/inference.py。
解决方案
5MB 是实时端点的硬限制。
您确定需要传递如此大的图像进行预测吗?大多数用例适用于较小、较低分辨率的图像。
如果您需要实时预测,一种解决方法是在预测请求中传递图像 S3 URI(而不是图像本身),然后从 S3 加载图像。
如果您不需要实时预测,您应该查看批量转换,它不会强制执行该大小限制:https ://docs.aws.amazon.com/sagemaker/latest/dg/batch-transform.html
推荐阅读
- javascript - 有什么方法可以检测 DOM 树中不直接来自 React 的变化?
- c++ - 60603 (Fedora Cygwin 5.4.0-2) 使用 std::cout 时
- gremlin - gremlin-AWS Neptune 文本谓词
- c# - 如何在特定条件下重置 TempData 但可以在一种方法中使用它
- c# - 在 C# 中验证预期的 json 结构
- python - Maya python:TypeError:强制转换为Unicode:需要字符串或缓冲区,找到int
- c# - GetContent 根 Web API .NET Core xunit ClassFixture
- exception-handling - 使用 try catch 和 finally 返回值
- scala - gradle kotlin DSL scala & 想法
- javascript - 未捕获的类型错误:无法读取未定义、电子 2、角度 6 的属性“fn”?