如何将图像数据从s3存储桶加载到sagemaker笔记本?

时间:2019-04-02 17:27:14

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

我刚开始使用aws sagemaker。我试图将图像从s3存储桶导入到sagemaker笔记本中。但是我无法将图像导入笔记本。我的图像位置是 s3:// my_bucket / train ,如何将Train文件夹从给定路径导入到Sagemaker笔记本中。我在这里介绍了一些解决方案,这些解决方案适用于CSV文件。我的S3存储桶中的所有图像均为.jpeg格式。

4 个答案:

答案 0 :(得分:0)

您无需从S3存储桶中将图像下载到本地SageMaker实例即可训练模型。如果要拉出它们进行数据探索/分析,则可以从SageMaker笔记本中使用aws cli。您可以使用以下命令下载示例图像。这会将sample.jpg复制到您images中的pwd目录中。

aws s3 cp s3://my_bucket/train/sample.jpg ./images/sample.jpg

尝试查看amazon-sagemaker-examples存储库,以了解如何在SageMaker上使用图像格式。

答案 1 :(得分:0)

您要使用哪个SageMaker示例笔记本?

如果输入数据在S3存储桶中,则不必将其下载到SageMaker笔记本实例。此示例显示了数据已上传到S3存储桶:https://github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-fulltraining.ipynb

出于培训目的,您可以将SELECT TOP 1 ID, StartDate, MIN(EndDate), DesigCode FROM [TABLENAME] GROUP BY ID, StartDate, DesigCode; 配置到输入数据段。

这是内置图像分类的图像输入格式的推论。

答案 2 :(得分:0)

您可以使用s3fs轻松访问您的存储桶以及其中的图像文件。

import s3fs

fs = s3fs.S3FileSystem()

# To List 5 files in your accessible bucket
fs.ls('s3://bucket-name/data/')[:5]

# open it directly
with fs.open(f's3://bucket-name/data/image.png') as f:
    display(Image.open(f))

答案 3 :(得分:0)

下面的代码片段将帮助您将图像文件夹从 s3 存储桶加载到 SageMaker 实例;


import boto3 
from botocore.exceptions import ClientError # Not necessary

# Remember to enter the cirrect bucket region below
s3 = boto3.resource('s3', region_name='us-west-2') 
# Replace the place holder with your correct bucket name
bucket = s3.Bucket('my_bucket') 
for my_bucket_object in bucket.objects.all():    
    key = my_bucket_object.key    
    print(key)    
    if not os.path.exists(os.path.dirname(key)):           
        os.makedirs(os.path.dirname(key))

# The following is basically for exception handling and not necessary to include     
    try:         
        bucket.download_file(key, key)     
    except ClientError as e:         
        if e.response['Error']['Code'] == "404":             
            print("No object with this key.")        
        else:             
            raise

或者,您也可以尝试从 Sagemaker 笔记本单元运行以下脚本;

!aws s3 cp s3://$my_bucket//train/images train/images/ --recursive

解决方案来自this website

Documentation on using cp

相关问题