簡體   English   中英

如何將圖像數據從 s3 存儲桶加載到 sagemaker 筆記本?

[英]How to load Image data from s3 bucket to sagemaker notebook?

我剛開始使用 aws sagemaker。 我嘗試將圖像從我的 s3 存儲桶導入 sagemaker 筆記本。 但我無法將圖像導入筆記本。 我的圖像位置是s3://my_bucket/train如何將 train 文件夾從給定的路徑導入到我的 sagemaker 筆記本。 我已經在這里完成了一些解決方案,這些解決方案適用於 CSV 文件。 我的 S3 存儲桶中的所有圖像都是 .jpeg 格式。

您無需將圖像從S3存儲桶下載到本地SageMaker實例即可訓練模型。 如果要拉出它們進行數據探索/分析,則可以使用SageMaker筆記本中的aws cli 您可以使用以下命令下載示例圖像。 這會將sample.jpg復制到pwd images目錄。

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

嘗試查看amazon-sagemaker-examples回購,以了解如何在SageMaker上使用圖像格式。

您要使用哪個SageMaker示例筆記本?

如果輸入數據在S3存儲桶中,則不必將其下載到SageMaker筆記本實例。 此示例顯示數據已上傳到S3存儲桶: https : //github.com/awslabs/amazon-sagemaker-examples/blob/master/introduction_to_amazon_algorithms/imageclassification_caltech/Image-classification-fulltraining.ipynb

為了進行培訓,您可以將s3_train配置為輸入數據存儲桶。

這是內置圖像分類的圖像輸入格式的推論。

您可以使用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))

下面的代碼片段將幫助您將圖像文件夾從 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

解決方案來自這個網站

有關使用cp文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM