如何从AWS S3将存储桶映像读取到Sagemaker Jupyter实例 [英] How to read bucket image from AWS S3 into Sagemaker Jupyter Instance

查看:67
本文介绍了如何从AWS S3将存储桶映像读取到Sagemaker Jupyter实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对AWS和云环境非常陌生.我是一名机器学习工程师,我计划在AWS环境中构建自定义CNN,以预测给定图像是否存在iPhone.

I am very new to AWS and the cloud environment. I am a machine learning engineer, I am planning to build a custom CNN into the AWS environment to predict a given image has an iPhone present or not.

我所做的事情:

步骤1:

我使用以下文件夹结构为iPhone分类器创建了一个S3存储桶:

I have created a S3 bucket for iPhone classifier with the below folder structure :

 Iphone_Classifier > Train > Yes_iphone_images > 1000 images
                           > No_iphone_images  > 1000 images

                   > Dev   > Yes_iphone_images > 100 images
                           > No_iphone_images  > 100 images

                   > Test  > 30 random images

权限->阻止所有公共访问权限

第2步:

然后我去Amazon Sagemaker,并创建一个实例:

Then I go to Amazon Sagemaker, and create an instance:

我选择以下内容

 Name: some-xyz,
 Type: ml.t2.medium
 IAM : created new IAM role ( root access was enabled.)
 others: All others were in default

然后创建并打开笔记本实例.

Then the notebook instance was created and opened.

第3步:

打开实例后,

1. I used to prefer - conda_tensorflow2_p36 as interpreter
2. Created a new Jupyter notebook and stated.
3. I checked image classification examples but was confused, and most others used CSV files, but I want to retrieve images from S3 buckets. 

问题:

1. How simply can we access the S3 bucket image dataset from the Jupiter Instances of Sagemaker? 
2. I exactly need the reference code to access the S3 bucket images. 
3. Is it a good approach to copy the data to the notebook or is it better to work from the S3 bucket.

我尝试过的是:

import boto3
client = boto3.client('s3')

# I tried this one and failed
#path = 's3://iphone/Train/Yes_iphone_images/100.png'

# I tried this one and failed
path = 's3://iphone/Test/10.png'

# I uploaded to the notebook instance an image file and when I try to read it works
#path = 'thiyaga.jpg'
print(path)

import cv2
from matplotlib import pyplot as plt
print(cv2.__version__)
plt.imshow(img)

推荐答案

如果您的图片是二进制编码的,则可以尝试以下操作:

If your image is binary-encoded, you could try this:

import boto3 
import matplotlib.pyplot as plt 

# Define Bucket and Key 
s3_bucket, s3_key = 'YOUR_BUCKET', 'YOUR_IMAGE_KEY'

with BytesIO() as f:
    boto3.client("s3").download_fileobj(Bucket=s3_bucket, Key=s3_key, Fileobj=f)
    f.seek(0)
    img = plt.imread(f, format='png')

在其他情况下,以下代码有效(基于

in other case, the following code works out (based on the documentation):

s3 = boto3.resource('s3')

img = s3.Bucket(s3_bucket).download_file(s3_key, 'local_image.jpg')

在两种情况下,都可以使用 plt.imshow(img)可视化图像.

In both cases, you can visualize the image with plt.imshow(img).

在您的路径示例 path ='s3://iphone/Test/10.png'中,存储桶和密钥将为 s3_bucket ='iphone' s3_key = Test/10.png

In your path example path = 's3://iphone/Test/10.png', the bucket and key will be s3_bucket = 'iphone' and s3_key=Test/10.png

其他资源: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-download-file.html

这篇关于如何从AWS S3将存储桶映像读取到Sagemaker Jupyter实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆