Sagemaker培训作业未上传/保存培训模型到S3输出路径

时间:2020-07-29 02:05:24

标签: python amazon-web-services pytorch amazon-sagemaker

好吧,我已经在Sagemaker中处理了近一个星期的问题,我已经准备好拔头发了。在BYO算法Docker部署类型方案中,我有一个定制的培训脚本与一个数据处理脚本配对。这是一个使用Python 3.x构建的Pytorch模型,BYO Docker文件最初是为Python 2构建的,但是我看不到我遇到的问题……这是在成功培训之后运行Sagemaker不会将模型保存到目标S​​3存储桶。

我进行了广泛搜索,似乎在任何地方都找不到合适的答案。所有这些都在Notebook实例内部完成。注意:我将其用作承包商,并且没有对其余AWS的完整权限,包括下载Docker映像。

Dockerfile:

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 Image Build:

%%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)

上面的脚本可以正常工作。我的脚本中有打印语句,它们正在将预期结果打印到控制台。这样可以按预期运行,完成,并说它在IT绝对没有部署时正在部署模型工件。

模型部署:

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"
        },

这是我到目前为止尝试过的:

  1. 我尝试将其上传到其他存储桶。我认为我的权限是问题所在,因此我将其指向一个新权限,使我可以像以前那样将其上传到该存储桶。没有骰子。
  2. 我曾尝试将脚本反向移植到Python 2.x,但这引起了比它可能解决的问题更多的问题,而且我还真不明白那到底是怎么回事。
  3. 我确保笔记本的IAM角色具有足够的权限,并且确实具有SagemakerFullAccess策略

困扰我的是,我看不到任何错误日志。如果我能直接得到指示,我也会很高兴,但是如果有一些我不知道的隐藏的贤者功夫,我将永远感激不已。


编辑

培训作业将按预期运行并打印到Jupyter单元和CloudWatch。从那以后,我已经失去了笔记本中的单元输出,但是下面是CloudWatch中的最后几行。第一个数字是纪元,其余的是各种自定义模型指标。

enter image description here

2 个答案:

答案 0 :(得分:0)

您是否尝试过保存到本地文件并将其移至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)")

答案 1 :(得分:0)

您可以从训练作业日志中验证您的训练脚本是否正在运行吗?看来您的Docker映像似乎无法响应SageMaker所需的命令train,因此我怀疑您的模型实际上并没有被训练/保存到/opt/ml/model

有关SageMaker如何运行Docker容器的AWS文档:https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo-dockerfile.html

编辑:总结以下注释-训练脚本还必须将模型保存到/opt/ml/model(模型不会自动保存)。