我是Sagemaker的新手,并试图将Sagemaker与python SDK结合使用,并使用aws提供的示例Minist代码,并将其命名为sm_mnist.py
:
import boto3
import sagemaker
import tensorflow as tf
import argparse
import os
import numpy as np
import json
from sagemaker import get_execution_role
def model(x_train, y_train, x_test, y_test):
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(1024, activation=tf.nn.relu),
tf.keras.layers.Dropout(0.4),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
model.fit(x_train, y_train)
model.evaluate(x_test, y_test)
return model
def _load_training_data(base_dir):
x_train = np.load(os.path.join(base_dir, 'train_data.npy'))
y_train = np.load(os.path.join(base_dir, 'train_labels.npy'))
return x_train, y_train
def _load_testing_data(base_dir):
x_test = np.load(os.path.join(base_dir, 'eval_data.npy'))
y_test = np.load(os.path.join(base_dir, 'eval_labels.npy'))
return x_test, y_test
def _parse_args():
parser = argparse.ArgumentParser()
# Data, model, and output directories
# model_dir is always passed in from SageMaker. By default this is a S3 path under the default bucket.
parser.add_argument('--model_dir', type=str)
parser.add_argument('--sm_model_dir', type=str, default=os.environ.get('SM_MODEL_DIR'))
parser.add_argument('--train', type=str, default=os.environ.get('SM_CHANNEL_TRAINING'))
#parser.add_argument('--hosts', type=list, default=json.loads(os.environ.get('SM_HOSTS')))
#parser.add_argument('--currenthost', type=str, default=os.environ.get('SM_CURRENT_HOST'))
return parser.parse_known_args()
if __name__ == "__main__":
args, unknown = _parse_args()
train_data, train_labels = _load_training_data(args.train)
eval_data, eval_labels = _load_testing_data(args.train)
mnist_classifier = model(train_data, train_labels, eval_data, eval_labels)
if args.current_host == args.hosts[0]:
# save model to an S3 directory with version number '00000001'
mnist_classifier.save(os.path.join(args.sm_model_dir, '000000001'), 'my_model.h5')
我创建了Tensorflow估算器train.py
:
from sagemaker.tensorflow import TensorFlow
role = 'AmazonSageMaker-ExecutionRole-20200928T205562'
mnist_estimator = TensorFlow(entry_point='train.py',
role=role,
train_instance_count=2,
train_instance_type= 'ml.p3.2xlarge', #'local',
framework_version= '1.15.2',#,'2.1.0'
py_version='py3',
script_mode=True)
training_data_uri = 's3://my-dataset-us-east-1/mnist'
mnist_estimator.fit(training_data_uri)
这是我的dockerfile:
FROM tensorflow/tensorflow:1.15.2-gpu
# Install sagemaker-training toolkit to enable SageMaker Python SDK
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git
RUN pip3 install --upgrade pip && \
pip3 install sagemaker-training
# Copies the training code inside the container
COPY train.py opt/ml/code/train.py
COPY sm_mnist.py opt/ml/code/mnist.py
COPY requirements.txt .
RUN pip3 install -r requirements.txt
# Defines train.py as script entrypoint
ENV SAGEMAKER_PROGRAM train.py
ENTRYPOINT ["python","opt/ml/code/train.py"]
我可以使用以下方法创建图像:
docker build -t mnist_test:latest .
docker tag mnist_test:latest xxxx.dkr.ecr.us-east-1.amazonaws.com/mnist_test:latest
docker run --rm mnist_test --model_dir s3://my-dataset/models
我遇到了无法解决的错误:
Traceback (most recent call last):
File "opt/ml/code/train.py", line 27, in <module>
sess = sagemaker.Session()
File "/usr/local/lib/python3.6/dist-packages/sagemaker/session.py", line 115, in __init__
sagemaker_runtime_client=sagemaker_runtime_client,
File "/usr/local/lib/python3.6/dist-packages/sagemaker/session.py", line 129, in _initialize
"Must setup local AWS configuration with a region supported by SageMaker."
ValueError: Must setup local AWS configuration with a region supported by SageMaker.
我不知道我的错误在哪里?
答案 0 :(得分:0)
该错误消息表明您在环境中未配置AWS区域。有几种方法可以做到这一点,包括:
AWS CLI (docs):
$ aws configure # follow the prompts
[...]
Default region name [None]: your-region-name
环境变量(docs):
export $AWS_DEFAULT_REGION=your-region-name
答案 1 :(得分:0)
get_execution_role
接受默认为 sagemaker_session
的 None
参数。我能够通过传入预先构建的 Sagemaker 会话来解决此错误,如下所示:
from sagemaker import get_execution_role
sagemaker_session = sagemaker.Session(boto3.session.Session(region_name=AWS_REGION))
sagemaker_session.boto_region_name # make sure this is set and not None
# pass in the sagemaker session as an argument
sagemaker_execution_role = get_execution_role(sagemaker_session=sagemaker_session)
# Output: 'arn:aws:iam::AWS_ACCOUNT_ID:role/EXECUTION_ROLE_NAME'
在AWS documentation中,有一个注释
<块引用>执行角色仅在运行
SageMaker 中的笔记本。如果您在笔记本中运行 get_execution_role
不在 SageMaker 上,预计会出现“区域”错误。
由于目标是获取执行角色 ARN,您也可以使用文档中推荐的方法:
try:
role = sagemaker.get_execution_role()
except ValueError:
iam = boto3.client('iam')
role = iam.get_role(RoleName='AmazonSageMaker-ExecutionRole-20201200T100000')['Role']['Arn']
答案 2 :(得分:0)
我遇到了同样的错误,这对我有用,(取自他们的一个例子)
role = sagemaker.get_execution_role()
region = boto3.Session().region_name