Sagemaker 训练作业不是 Uploading/Saving 训练模型到 S3 输出路径
Sagemaker Training Job Not Uploading/Saving Training Model to S3 Output Path
好的,我已经在 Sagemaker 中处理这个问题将近一个星期了,我已经准备好全力以赴了。在 BYO 算法 Docker 部署类型场景中,我有一个自定义训练脚本与数据处理脚本配对。这是一个用 Python 3.x 构建的 Pytorch 模型,BYO Docker 文件最初是为 Python 2 构建的,但我看不出我遇到的问题我有......这是在成功训练后 运行 Sagemaker 不会将模型保存到目标 S3 存储桶中。
我进行了广泛的搜索,似乎无法在任何地方找到适用的答案。这一切都在 Notebook 实例中完成。注意:我作为承包商使用它,没有对 AWS 其余部分的完全权限,包括下载 Docker 图像。
Docker文件:
FROM ubuntu:18.04
MAINTAINER Amazon AI <sage-learner@amazon.com>
RUN apt-get -y update && apt-get install -y --no-install-recommends \
wget \
python-pip \
python3-pip3
nginx \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
RUN wget https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py && \
pip3 install future numpy torch scipy scikit-learn pandas flask gevent gunicorn && \
rm -rf /root/.cache
ENV PYTHONUNBUFFERED=TRUE
ENV PYTHONDONTWRITEBYTECODE=TRUE
ENV PATH="/opt/program:${PATH}"
COPY decision_trees /opt/program
WORKDIR /opt/program
Docker 图像构建:
%%sh
algorithm_name="name-this-algo"
cd container
chmod +x decision_trees/train
chmod +x decision_trees/serve
account=$(aws sts get-caller-identity --query Account --output text)
region=$(aws configure get region)
region=${region:-us-east-2}
fullname="${account}.dkr.ecr.${region}.amazonaws.com/${algorithm_name}:latest"
aws ecr describe-repositories --repository-names "${algorithm_name}" > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ecr create-repository --repository-name "${algorithm_name}" > /dev/null
fi
# Get the login command from ECR and execute it directly
$(aws ecr get-login --region ${region} --no-include-email)
# Build the docker image locally with the image name and then push it to ECR
# with the full name.
docker build -t ${algorithm_name} .
docker tag ${algorithm_name} ${fullname}
docker push ${fullname}
环境设置和会话开始:
common_prefix = "pytorch-lstm"
training_input_prefix = common_prefix + "/training-input-data"
batch_inference_input_prefix = common_prefix + "/batch-inference-input-data"
import os
from sagemaker import get_execution_role
import sagemaker as sage
sess = sage.Session()
role = get_execution_role()
print(role)
培训目录、图像和估算器设置,然后 fit
调用:
TRAINING_WORKDIR = "a/local/directory"
training_input = sess.upload_data(TRAINING_WORKDIR, key_prefix=training_input_prefix)
print ("Training Data Location " + training_input)
account = sess.boto_session.client('sts').get_caller_identity()['Account']
region = sess.boto_session.region_name
image = '{}.dkr.ecr.{}.amazonaws.com/image-that-works:working'.format(account, region)
tree = sage.estimator.Estimator(image,
role, 1, 'ml.p2.xlarge',
output_path="s3://sagemaker-directory-that-definitely/exists",
sagemaker_session=sess)
tree.fit(training_input)
上面的脚本确实有效。我的脚本中有打印语句,它们正在将预期结果打印到控制台。这 运行 就像它应该的那样,结束了,并说它正在部署模型工件,而实际上它确实没有。
模型部署:
model = tree.create_model()
predictor = tree.deploy(1, 'ml.m4.xlarge')
这将引发无法找到模型的错误。调用 aws sagemaker describe-training-job
显示训练已完成,但我发现上传模型所花费的时间非常快,所以显然某处有错误而且它没有告诉我。值得庆幸的是,它不只是将其上传到以太。
{
"Status": "Uploading",
"StartTime": 1595982984.068,
"EndTime": 1595982989.994,
"StatusMessage": "Uploading generated training model"
},
这是我到目前为止尝试过的方法:
- 我试过将它上传到不同的存储桶。我认为我的权限是问题所在,所以我将其指向一个我新允许我上传的权限,就像我之前对那个存储桶所做的那样。没有骰子。
- 我尝试将脚本反向移植到 Python 2.x,但这导致的问题多于它可能解决的问题,而且我真的不明白这到底是怎么回事。
- 我确保笔记本的 IAM 角色具有足够的权限,并且它确实具有 SagemakerFullAccess 策略
困扰我的是我看不到任何错误日志。如果能得到指点我也很高兴,但如果有什么我不知道的贤者秘法我将永远感激不尽
编辑
训练作业 运行 将按预期发送并打印到 Jupyter 单元和 CloudWatch。我已经丢失了笔记本中的单元格输出,但下面是 CloudWatch 中的最后几行。第一个数字是纪元,其余是各种自定义模型指标。
您是否尝试过保存到本地文件并将其移动到 S3?我会将它保存在本地(到脚本的根目录)并通过 boto3 上传。
sagemaker 会话对象可能没有初始化存储桶属性。明确地执行它并不是一个额外的步骤。
import boto3
s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "DESTINATION_NAME(optional)")
您能否从训练作业日志中验证您的训练脚本是 运行?看起来您的 Docker 图像不会响应命令 train
,这是 SageMaker 所要求的,因此我怀疑您的模型实际上并未将 trained/saved 变为 /opt/ml/model
.
关于 SageMaker 如何运行 Docker 容器的 AWS 文档:https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo-dockerfile.html
编辑:根据以下评论总结 - 训练脚本还必须将模型保存到 /opt/ml/model
(模型不会自动保存)。
好的,我已经在 Sagemaker 中处理这个问题将近一个星期了,我已经准备好全力以赴了。在 BYO 算法 Docker 部署类型场景中,我有一个自定义训练脚本与数据处理脚本配对。这是一个用 Python 3.x 构建的 Pytorch 模型,BYO Docker 文件最初是为 Python 2 构建的,但我看不出我遇到的问题我有......这是在成功训练后 运行 Sagemaker 不会将模型保存到目标 S3 存储桶中。
我进行了广泛的搜索,似乎无法在任何地方找到适用的答案。这一切都在 Notebook 实例中完成。注意:我作为承包商使用它,没有对 AWS 其余部分的完全权限,包括下载 Docker 图像。
Docker文件:
FROM ubuntu:18.04
MAINTAINER Amazon AI <sage-learner@amazon.com>
RUN apt-get -y update && apt-get install -y --no-install-recommends \
wget \
python-pip \
python3-pip3
nginx \
ca-certificates \
&& rm -rf /var/lib/apt/lists/*
RUN wget https://bootstrap.pypa.io/get-pip.py && python3 get-pip.py && \
pip3 install future numpy torch scipy scikit-learn pandas flask gevent gunicorn && \
rm -rf /root/.cache
ENV PYTHONUNBUFFERED=TRUE
ENV PYTHONDONTWRITEBYTECODE=TRUE
ENV PATH="/opt/program:${PATH}"
COPY decision_trees /opt/program
WORKDIR /opt/program
Docker 图像构建:
%%sh
algorithm_name="name-this-algo"
cd container
chmod +x decision_trees/train
chmod +x decision_trees/serve
account=$(aws sts get-caller-identity --query Account --output text)
region=$(aws configure get region)
region=${region:-us-east-2}
fullname="${account}.dkr.ecr.${region}.amazonaws.com/${algorithm_name}:latest"
aws ecr describe-repositories --repository-names "${algorithm_name}" > /dev/null 2>&1
if [ $? -ne 0 ]
then
aws ecr create-repository --repository-name "${algorithm_name}" > /dev/null
fi
# Get the login command from ECR and execute it directly
$(aws ecr get-login --region ${region} --no-include-email)
# Build the docker image locally with the image name and then push it to ECR
# with the full name.
docker build -t ${algorithm_name} .
docker tag ${algorithm_name} ${fullname}
docker push ${fullname}
环境设置和会话开始:
common_prefix = "pytorch-lstm"
training_input_prefix = common_prefix + "/training-input-data"
batch_inference_input_prefix = common_prefix + "/batch-inference-input-data"
import os
from sagemaker import get_execution_role
import sagemaker as sage
sess = sage.Session()
role = get_execution_role()
print(role)
培训目录、图像和估算器设置,然后 fit
调用:
TRAINING_WORKDIR = "a/local/directory"
training_input = sess.upload_data(TRAINING_WORKDIR, key_prefix=training_input_prefix)
print ("Training Data Location " + training_input)
account = sess.boto_session.client('sts').get_caller_identity()['Account']
region = sess.boto_session.region_name
image = '{}.dkr.ecr.{}.amazonaws.com/image-that-works:working'.format(account, region)
tree = sage.estimator.Estimator(image,
role, 1, 'ml.p2.xlarge',
output_path="s3://sagemaker-directory-that-definitely/exists",
sagemaker_session=sess)
tree.fit(training_input)
上面的脚本确实有效。我的脚本中有打印语句,它们正在将预期结果打印到控制台。这 运行 就像它应该的那样,结束了,并说它正在部署模型工件,而实际上它确实没有。
模型部署:
model = tree.create_model()
predictor = tree.deploy(1, 'ml.m4.xlarge')
这将引发无法找到模型的错误。调用 aws sagemaker describe-training-job
显示训练已完成,但我发现上传模型所花费的时间非常快,所以显然某处有错误而且它没有告诉我。值得庆幸的是,它不只是将其上传到以太。
{
"Status": "Uploading",
"StartTime": 1595982984.068,
"EndTime": 1595982989.994,
"StatusMessage": "Uploading generated training model"
},
这是我到目前为止尝试过的方法:
- 我试过将它上传到不同的存储桶。我认为我的权限是问题所在,所以我将其指向一个我新允许我上传的权限,就像我之前对那个存储桶所做的那样。没有骰子。
- 我尝试将脚本反向移植到 Python 2.x,但这导致的问题多于它可能解决的问题,而且我真的不明白这到底是怎么回事。
- 我确保笔记本的 IAM 角色具有足够的权限,并且它确实具有 SagemakerFullAccess 策略
困扰我的是我看不到任何错误日志。如果能得到指点我也很高兴,但如果有什么我不知道的贤者秘法我将永远感激不尽
编辑
训练作业 运行 将按预期发送并打印到 Jupyter 单元和 CloudWatch。我已经丢失了笔记本中的单元格输出,但下面是 CloudWatch 中的最后几行。第一个数字是纪元,其余是各种自定义模型指标。
您是否尝试过保存到本地文件并将其移动到 S3?我会将它保存在本地(到脚本的根目录)并通过 boto3 上传。
sagemaker 会话对象可能没有初始化存储桶属性。明确地执行它并不是一个额外的步骤。
import boto3
s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "DESTINATION_NAME(optional)")
您能否从训练作业日志中验证您的训练脚本是 运行?看起来您的 Docker 图像不会响应命令 train
,这是 SageMaker 所要求的,因此我怀疑您的模型实际上并未将 trained/saved 变为 /opt/ml/model
.
关于 SageMaker 如何运行 Docker 容器的 AWS 文档:https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo-dockerfile.html
编辑:根据以下评论总结 - 训练脚本还必须将模型保存到 /opt/ml/model
(模型不会自动保存)。