美国焊接学会培训标准

2024-04-26 00:07:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图在本地GPU上为Amazon Sagemaker运行example code。我已将Jupyter笔记本中的代码复制到以下Python脚本中:

import boto3
import subprocess
import sagemaker
from sagemaker.mxnet import MXNet
from mxnet import gluon
from sagemaker import get_execution_role
import os

sagemaker_session = sagemaker.Session()
instance_type = 'local'
if subprocess.call('nvidia-smi') == 0:
    # Set type to GPU if one is present
    instance_type = 'local_gpu'
# role = get_execution_role()

gluon.data.vision.MNIST('./data/train', train=True)
gluon.data.vision.MNIST('./data/test', train=False)

# successfully connects and uploads data
inputs = sagemaker_session.upload_data(path='data', key_prefix='data/mnist')

hyperparameters = {
    'batch_size': 100,
    'epochs': 20,
    'learning_rate': 0.1,
    'momentum': 0.9,
    'log_interval': 100
}

m = MXNet("mnist.py",
          role=role,
          train_instance_count=1,
          train_instance_type=instance_type,
          framework_version="1.1.0",
          hyperparameters=hyperparameters)

# fails in Docker container
m.fit(inputs)
predictor = m.deploy(initial_instance_count=1, instance_type=instance_type)
m.delete_endpoint()

其中引用的mnist.py文件与Github上指定的完全相同。脚本在Docker容器中的m.fit上失败,错误如下:

^{pr2}$

我感到困惑的是,我可以在容器外对S3进行身份验证(以加载训练/测试数据),但不能在Docker容器中进行验证。所以我猜问题与将AWS凭证传递给Docker容器有关。以下是生成的Docker compose文件:

networks:
  sagemaker-local:
    name: sagemaker-local
services:
  algo-1-1DUU4:
    command: train
    environment:
    - AWS_REGION=us-west-2
    - TRAINING_JOB_NAME=sagemaker-mxnet-2018-10-07-00-47-10-435
    image: 123456789012.dkr.ecr.us-west-2.amazonaws.com/sagemaker-mxnet:1.1.0-gpu-py2
    networks:
      sagemaker-local:
        aliases:
        - algo-1-1DUU4
    stdin_open: true
    tty: true
    volumes:
    - /tmp/tmpSkaR3x/algo-1-1DUU4/input:/opt/ml/input
    - /tmp/tmpSkaR3x/algo-1-1DUU4/output:/opt/ml/output
    - /tmp/tmpSkaR3x/algo-1-1DUU4/output/data:/opt/ml/output/data
    - /tmp/tmpSkaR3x/model:/opt/ml/model
version: '2.1'

是否应该将AWS证书作为环境变量传入?在

在阅读了Using boto3 in install local mode?之后,我将我的sagemaker安装升级到了,但是没有任何效果。我检查了在Sagemaker会话(容器外部)中获取的凭据,它们似乎是空白的,即使我有一个~/.aws/config和{}文件:

{'_token': None, '_time_fetcher': <function _local_now at 0x7f4dbbe75230>, '_access_key': None, '_frozen_credentials': None, '_refresh_using': <bound method AssumeRoleCredentialFetcher.fetch_credentials of <botocore.credentials.AssumeRoleCredentialFetcher object at 0x7f4d2de48bd0>>, '_secret_key': None, '_expiry_time': None, 'method': 'assume-role', '_refresh_lock': <thread.lock object at 0x7f4d9f2aafd0>}

我是AWS新手,所以我不知道如何诊断有关AWS凭据的问题。我的.aws/config文件包含以下信息(带占位符值):

[default]
output = json
region = us-west-2
role_arn = arn:aws:iam::123456789012:role/SageMakers
source_profile = sagemaker-test

[profile sagemaker-test]
output = json
region = us-west-2

其中,sagemaker-test配置文件在IAM管理控制台中有AmazonSageMakerFullAccess。在

.aws/credentials文件包含以下信息(由占位符值表示):

[default]
aws_access_key_id = 1234567890
aws_secret_access_key = zyxwvutsrqponmlkjihgfedcba
[sagemaker-test]
aws_access_key_id = 0987654321
aws_secret_access_key = abcdefghijklmopqrstuvwxyz

最后,这些是来自pip freeze的适用库的版本:

awscli==1.16.19
boto==2.48.0
boto3==1.9.18
botocore==1.12.18
docker==3.5.0
docker-compose==1.22.0
mxnet-cu91==1.1.0.post0
sagemaker==1.11.1

如果我遗漏了任何相关信息,请告诉我,并感谢您提供的任何帮助/反馈。在

更新:感谢各位的帮助!在尝试您建议的一些修复程序时,我注意到boto3已过期,并将其更新(为boto3-1.9.26和{}),这似乎解决了这个问题。我找不到任何有关boto3==1.9.18问题的文档。如果有人能帮助我理解boto3的问题所在,我很乐意将他们的答案标记为正确。在


Tags: instancekeydockertestimportawsoutputdata
3条回答

SageMaker本地模式旨在获取boto3会话中可用的任何凭证,并将其作为环境变量传递到docker容器中。在

但是,您使用的sagemaker sdk版本(1.11.1及更早版本)将忽略包含令牌的凭据,因为这通常表示短期凭据的有效期不会足够长,无法完成培训作业或终结点有用。在

如果您使用的是临时凭据,请尝试将其替换为永久凭据,或从ec2实例(或SageMaker笔记本)运行分配了适当的实例角色。在

另外,sagemaker sdk在v1.11.2及更高版本中对凭据的处理发生了更改--临时凭据将传递到本地模式容器,但会显示一条警告消息。所以你可以升级到一个更新的版本然后再试一次(pip install -U sagemaker)。在

另外,尝试升级boto3可能会更改,因此请尝试使用最新版本。在

我刚刚确认他的例子在我的本地机器上运行。请确保您使用的角色具有使用名称以sagemaker开头的存储桶的权限。默认情况下,Sagemaker创建前缀为sagemaker的存储桶。在

看起来您已经在主机上配置了凭证~/.aws/credentials,但正试图在主机上运行的docker容器上访问它们。在

最简单的解决方案似乎是,将您的aws凭证安装到容器的预期位置。您似乎正在使用^{}映像,它似乎使用根用户。基于此,如果您更新docker compose文件中的卷以使algo-1-1DUU4包含:

volumes:
  ...
  ~/.aws/:/root/.aws/

这将把您的凭证挂载到容器中的根用户上,这样您的python脚本应该能够访问它们。在

相关问题 更多 >