0

谁能提供使用SageMaker Pipeline部署 pytorch 模型的示例?

我使用 SageMaker Studio 的 MLOps 模板(用于模型构建、训练和部署的 MLOps 模板)来构建 MLOps 项目。

该模板使用 sagemaker 管道构建用于预处理、训练和注册模型的管道。部署脚本在 YAML 文件中实现,使用 CloudFormation 运行。模型注册时会自动触发部署脚本。

该模板使用 xgboost 模型来训练数据并部署模型。我想使用 Pytorch 并部署它。我成功用xgboost替换了pytorch,成功预处理了数据,训练了模型,注册了模型。但我没有在我的模型中使用 inference.py。所以我得到模型部署的错误。

更新端点的错误日志是:

FileNotFoundError: [Errno 2] No such file or directory: '/opt/ml/model/code/inference.py'

我试图找到将 inference.py 用于 pytorch 模型的示例,但我找不到任何使用sagemaker 管道RegisterModel的示例。

任何帮助,将不胜感激。

您可以在下面看到用于训练和注册模型的管道的一部分。

from sagemaker.pytorch.estimator import PyTorch
from sagemaker.workflow.pipeline import Pipeline
from sagemaker.workflow.steps import (
    ProcessingStep,
    TrainingStep,
)
from sagemaker.workflow.step_collections import RegisterModel

pytorch_estimator = PyTorch(entry_point= os.path.join(BASE_DIR, 'train.py'),
                            instance_type= "ml.m5.xlarge",
                            instance_count=1,
                            role=role,
                            framework_version='1.8.0',
                            py_version='py3',
                            hyperparameters = {'epochs': 5, 'batch-size': 64, 'learning-rate': 0.1})

step_train = TrainingStep(
        name="TrainModel",
        estimator=pytorch_estimator,

        inputs={
                "train": sagemaker.TrainingInput(
                            s3_data=step_process.properties.ProcessingOutputConfig.Outputs[
                            "train_data"
                            ].S3Output.S3Uri,
                            content_type="text/csv",
                        ),
                "dev": sagemaker.TrainingInput(
                            s3_data=step_process.properties.ProcessingOutputConfig.Outputs[
                            "dev_data"
                            ].S3Output.S3Uri,
                            content_type="text/csv"
                        ),
                "test": sagemaker.TrainingInput(
                            s3_data=step_process.properties.ProcessingOutputConfig.Outputs[
                            "test_data"
                            ].S3Output.S3Uri,
                            content_type="text/csv"
                        ),
        },
)
step_register = RegisterModel(
            name="RegisterModel",
            estimator=pytorch_estimator,
            model_data=step_train.properties.ModelArtifacts.S3ModelArtifacts,
            content_types=["text/csv"],
            response_types=["text/csv"],
            inference_instances=["ml.t2.medium", "ml.m5.large"],
            transform_instances=["ml.m5.large"],
            model_package_group_name=model_package_group_name,
            approval_status=model_approval_status,
        )
    
pipeline = Pipeline(
            name=pipeline_name,
            parameters=[
                processing_instance_type,
                processing_instance_count,
                training_instance_type,
                model_approval_status,
                input_data,
            ],
            steps=[step_process, step_train, step_register],
            sagemaker_session=sagemaker_session,
        )
4

1 回答 1

0

PyTorch api 正在使用基本的 pytorch 图像。当调用 sagemaker.pytorch.deploy 方法时,sagemaker 运行“/opt/ml/model/code/inference.py”

但是在您的基本映像中没有该文件。

因此,如果您想使用部署方法,您可以使用 sagemaker 样式制作“inference.py”(可以在 sagemaker 容器中执行)并构建和推送图像。

然后你就可以使用部署方法了!

这是示例代码 https://sagemaker-workshop.com/custom/containers.html

于 2022-02-10T05:09:33.733 回答