![](/img/trans.png)
[英]Specify S3 location for model output to sagemaker training job duplication issue
[英]Sagemaker Training Job Not Uploading/Saving Training Model to S3 Output Path
好的,我已經在 Sagemaker 中處理這個問題將近一個星期了,我已經准備好拔掉我的頭發了。 在 BYO 算法 Docker 部署類型場景中,我有一個自定義訓練腳本與一個數據處理腳本配對。 It's a Pytorch model built with Python 3.x, and the BYO Docker file was originally built for Python 2, but I can't see an issue with the problem that I am having.....which is that after a successful training運行 Sagemaker 不會將 model 保存到目標 S3 存儲桶。
我進行了廣泛搜索,似乎無法在任何地方找到適用的答案。 這一切都在 Notebook 實例中完成。 注意:我作為承包商使用它,並且沒有 AWS 的 rest 的完全權限,包括下載 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 鏡像構建:
%%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}
環境設置和 session 啟動:
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 工件,而它肯定沒有。
Model 部署:
model = tree.create_model()
predictor = tree.deploy(1, 'ml.m4.xlarge')
這會引發無法找到 model 的錯誤。 對aws sagemaker describe-training-job
的調用顯示培訓已完成,但我發現上傳 model 所需的時間非常快,因此顯然某處存在錯誤,它沒有告訴我。 值得慶幸的是,它不僅僅是將其上傳到以太。
{
"Status": "Uploading",
"StartTime": 1595982984.068,
"EndTime": 1595982989.994,
"StatusMessage": "Uploading generated training model"
},
這是我到目前為止所嘗試的:
困擾我的是我看不到錯誤日志。 如果我能被引導到那我也會很高興,但如果有一些我不知道的隱藏的 Sagemaker 功夫,我會永遠感激不盡。
編輯
訓練作業按預期運行並打印到 Jupyter 單元和 CloudWatch。 從那以后,我在筆記本中丟失了單元格 output,但下面是 CloudWatch 中的最后幾行。 第一個數字是紀元,rest 是各種自定義 model 指標。
您能否從訓練作業日志中驗證您的訓練腳本正在運行? 看起來您的 Docker 圖像不會響應命令train
,這是 SageMaker 所需要的,所以我懷疑您的 model 實際上並沒有得到訓練/保存到/opt/ml/model
。
關於 SageMaker 如何運行 Docker 容器的 AWS 文檔: https://docs.aws.amazon.com/sagemaker/latest/dg/your-algorithms-training-algo-dockerfile.ZFC35FDC70D5FC69DA2568
編輯:從下面的評論中總結 - 訓練腳本還必須將 model 保存到/opt/ml/model
(model 不會自動保存)。
您是否嘗試過保存到本地文件並將其移動到 S3? 我會將它保存在本地(到腳本的根目錄)並通過 boto3 上傳。
sagemaker session object 可能沒有初始化存儲桶屬性。 明確地這樣做並不是一個額外的步驟。
import boto3
s3 = boto3.client('s3')
with open("FILE_NAME", "rb") as f:
s3.upload_fileobj(f, "BUCKET_NAME", "DESTINATION_NAME(optional)")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.