如何部署之前使用 Amazon Sagemaker 训练并存储在 S3 存储桶中的现有 pytorch 模型

How to deploy an existing pytorch model previously trained with Amazon Sagemaker and stored in S3 bucket

我已经使用 SageMaker 训练了一个 Pytorch 模型,该模型现在存储在 S3 存储桶中。 我正在尝试检索该模型并进行部署。

这是我使用的代码:

estimator = sagemaker.model.FrameworkModel(
    model_data= #link to  model location in s3
    image=  # image
    role=role,
    entry_point='train.py', 
    source_dir='pytorch_source',
    sagemaker_session = sagemaker_session
) 

predictor = estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge")

但是在部署过程之后(看起来 运行 很顺利),预测器只是一个 NoneType。 我在日志中没有发现任何奇怪的消息...

我也尝试过以下代码:

estimator = PyTorchModel(model_data= #link to model location in s3 
                             role=role,
                             image= #image
                             entry_point='pytorch_source/train.py',
                            predictor_cls = 'pytorch_source/train.py',
                           framework_version = '1.1.0')

predictor = estimator.deploy(initial_instance_count=1, instance_type="ml.p2.xlarge")

但它甚至没有完成部署。

有人可以帮忙吗?

我实际上使用具有以下设置的 PyTorchModel 解决了问题:

estimator = PyTorchModel(model_data='#path to model, 
                             role=role,
                             source_dir='pytorch_source',
                             entry_point='deploy.py',
                            predictor_cls = ImgPredictor,
                           framework_version = '1.1.0')

ImgPredictor 在哪里

from sagemaker.predictor import RealTimePredictor, json_deserializer

class ImgPredictor(RealTimePredictor):
    def __init__(self, endpoint_name, sagemaker_session):
        super(ImgPredictor, self).__init__(endpoint_name, sagemaker_session, content_type='application/x-image', 
                                           deserializer = json_deserializer ,accept='application/json')

和 deploy.py 包含所需函数 input_fn、output_fn、model_fn 和 predict_fn。 此外,源目录中缺少 requirements.txt 文件。