LoginSignup
0
1

SageMakerとS3でPytorchモデルをトレーニングするときの便利集

Last updated at Posted at 2023-06-22

S3にファイルをアップロード

import sagemaker, boto3, os

sagemaker_source_path = "./my_image.jpg"
s3_dest_path = "my_image.jpg"

sess = sagemaker.Session(default_bucket='backet_name')
bucket = sess.default_bucket()
boto3.Session().resource('s3').Bucket(bucket).Object(s3_dest_path).upload_file(sagemaker_source_path)

S3からファイルをダウンロード

import sagemaker, boto3, os

s3_source_path = "./my_image.jpg"
sagemaker_dest_path = "my_image.jpg"

sess = sagemaker.Session(default_bucket='backet_name')
bucket = sess.default_bucket()
boto3.Session().resource('s3').Bucket(bucket).Object(s3_source_path).download_file(sagemaker_dest_path)

GoogleDriveからS3にデータを移動

zip形式にして、gdownでダウンロード、s3にアップ。

!pip install gdown
!pip install --upgrade gdown
gdown https://drive.google.com/uc?id=********

S3のディレクトリをダウンロード

import os
import boto3

def download_directory_from_s3(bucket_name, s3_directory, local_directory):
    """
    S3 からディレクトリをダウンロードする関数
    """
    s3 = boto3.client('s3')
    for obj in s3.list_objects(Bucket=bucket_name, Prefix=s3_directory)['Contents']:
        if not os.path.exists(os.path.dirname(obj['Key'])):
            os.makedirs(os.path.dirname(obj['Key']))
        s3.download_file(bucket_name, obj['Key'], obj['Key'])
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1