首页 > 解决方案 > 如何部署先前使用 Amazon Sagemaker 训练并存储在 S3 存储桶中的现有 pytorch 模型

问题描述

我使用 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")

但它甚至没有完成部署。

有人能帮忙吗?

标签: amazon-web-servicespytorchamazon-sagemaker

解决方案


我实际上使用 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 文件。


推荐阅读