Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Image data from s3 bucket to sagemaker notebook?

I just started to use aws sagemaker. I tried to import images from my s3 bucket to sagemaker notebook. But I can't import images to the notebook. my image location is s3://my_bucket/train how can I import the train folder from the given path to my sagemaker notebook. I've gone through some of the solution in here and the solutions are for CSV file. All the images in my S3 bucket are in .jpeg format.

like image 217
labib Chowdhury Avatar asked Apr 02 '19 17:04

labib Chowdhury


People also ask

Can we read file from S3 without downloading?

Reading objects without downloading them Similarly, if you want to upload and read small pieces of textual data such as quotes, tweets, or news articles, you can do that using the S3 resource method put(), as demonstrated in the example below (Gist).


2 Answers

you could use s3fs to easily access your bucket as well as an image file in it.

from PIL import Image
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))
like image 142
CircleOnCircles Avatar answered Oct 06 '22 12:10

CircleOnCircles


You don't have to download images from S3 bucket to local SageMaker instance for training the model. If you are trying to pull them for data exploration/analysis you can use aws cli from your SageMaker notebook. You can use following command to download a sample image. This will copy sample.jpg to images directory in your pwd.

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

Try looking at amazon-sagemaker-examples repo to learn how to work with image formats on SageMaker.

like image 36
raj Avatar answered Oct 06 '22 13:10

raj