在 Amazon SageMaker 中,我尝试使用 Scikit-Learn 模型部署自定义创建的 Docker 容器,但部署过程中不断出现错误。
这些是我的步骤:
在我的本地计算机上创建了一个脚本 (script.py) 并分割了训练和测试数据。该脚本包含一个主要部分,接受参数“output-train-dir”、“model-dir”、“train”和“test”,并包含函数 model_fn、input_fn、output_fn 和 Predict_fn
在本地测试了脚本,有效
- python script.py --train 。 - 测试 。 --模型目录。
基于默认 Python 镜像 (Python 3.9) 创建了 Docker 镜像并推送到 Amazon ECR,以下是我使用过的命令
> docker pull python create Dockerfile, containing FROM python:3.9 RUN pip3 install --no-cache scikit-learn numpy pandas joblib sagemaker-training > docker build -t mymodel . > aws ecr create-repository --repository-name mymodel > docker tag 123456789012 123456789123.dkr.ecr.eu-central-1.amazonaws.com/mymodel > docker push 123456789123.dkr.ecr.eu-central-1.amazonaws.com/mymodel
已将训练和测试数据上传到 s3 (mybucket)
使用本地模式训练脚本
aws_sklearn = SKLearn(entry_point='script.py', framework_version='0.23-1', image_uri='123456789123.dkr.ecr.eu-central-1.amazonaws.com/mymodel', instance_type='local', role=role) aws_sklearn.fit({'train': mybucket_train_path, 'test': mybucket_test_path, 'model-dir': mybucket_model_path})
成功了
接下来我在 AWS 上进行了培训
aws_sklearn = SKLearn(entry_point='script.py', framework_version='0.23-1', image_uri='123456789123.dkr.ecr.eu-central-1.amazonaws.com/mymodel', instance_type='ml.m4.xlarge', role=role) aws_sklearn.fit({'train': mybucket_train_path, 'test': mybucket_test_path})
这也成功了(但是,提供 model-dir 参数给出了错误,所以我省略了它)
部署时出现错误:
aws_sklearn_predictor = aws_sklearn.deploy(instance_type='ml.t2.medium', initial_instance_count=1)
错误消息:
UnexpectedStatusException: Error hosting endpoint mymodel-2021-01-24-12-52-02-790: Failed. Reason: The primary container for production variant AllTraffic did not pass the ping health check. Please check CloudWatch logs for this endpoint..
Cloudwatch 说:
AWS sagemaker exec: "serve": executable file not found in $PATH
我在某处读到我应该将 RUN chmod +x/opt/program/serve 添加到 Dockerfile,但在我的本地镜像中,不存在服务文件,这是 SageMaker 创建的文件,对吗?
我应该如何或在哪里将服务添加到 $PATH 环境变量或授予服务脚本执行权限?
最佳答案
serve 文件不是 SageMaker 自动创建的;你必须让它成为 Docker 容器的一部分。从技术上讲,这对于 Estimator 作业也是如此(也应该有一个类似的 train 文件;但是您可以通过手动指定 entry_point
来覆盖它)。
This page应该有助于解释当您运行训练和batch_transform作业时SageMaker实际上尝试运行什么。该页面引用this repo您可以将其用作示例。
简而言之,如果您想继续使用自定义 docker 容器,则必须为 serve 命令构建功能(请参阅存储库中的其他脚本来启动 Gunicorn 服务器,该服务器运行 Flask 应用程序的多个实例)并将这些文件添加到您的 Dockerfile 中。
添加 serve
命令功能后,RUN chmod +x/opt/program/serve
命令也将变得更有意义。
关于docker - SageMaker 部署错误 "serve"在 $PATH 中找不到可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65883317/