© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Masaki Samejima
Solutions Architect, Amazon Web Services Japan.
2018.10.28
AWS
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
: http://bit.ly/sagemaker_api
1. API Gateway Lambda
SageMaker
2. S3 SageMaker
1. EC2 ( )
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
IAM
• IAM ( )
• AWSCloud9Administrator
• AmazonSageMakerFullAccess
• AWSLambdaFullAccess
• AmazonAPIGatewayAdministrator
• AmazonAPIGatewayInvokeFullAccess
• AmazonEC2FullAccess
• CloudFormation (Fullaccess
• IAM (AWS 1 )
• AWSServiceRoleForAWSCloud9 ( )
• Amazon SageMaker : AmazonSageMakerFullAccess ( ARN )
• AWS Lambda : AWSLambdaFullAccess AmazonSageMakerFullAccess
( )
IAM Administrator
Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AamzonSageMakerFullAccess
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
(Entry point)
Amazon
SageMaker
S3
•
• Amazon SageMaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
https://console.aws.amazon.com/sagemaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
ml.t2.medium
• IAM
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
IAM
S3
S3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• VPC,
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Jupyter Notebook
• InService
Jupyter Notebook
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Upload
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
deploy.ipynb
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Run
---
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
https://ap-northeast-1.console.aws.amazon.com/sagemaker/home?region=ap-
northeast-1#/endpoints
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API Gateway Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
1
API Gateway, Lambda SageMaker
Endpoint
Amazon
API Gateway
AWS
Lambda
Amazon
SageMaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2
S3 API Gateway Lambda
Amazon
API Gateway
AWS
Lambda
Amazon
SageMaker
S3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
1
Cloud9 AWS Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
1
API Gateway, Lambda SageMaker
Endpoint
Amazon
API Gateway
AWS
Lambda
Amazon
SageMaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud9
AWS
Lambda
Amazon API
Gateway
• Cloud9
• GUI Lambda API Gateway Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud9
https://aws.amazon.com/jp/cloud9/
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• AWS
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
create environment
• Cloud9
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Python3
Python 2 → Python 3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
python 3
alias python=python36
alias pip=pip-3.6
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Lambda
• SageMaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• AWS Resources
λ+ Lambda
• Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Blueprint (
)
empty-python
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• API Gateway
• Resource path: /
• Security: NONE
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Role Choose an existing role
• Existing role AWSLambdaFullAccess
AmazonSageMakerFullAccess role
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• (ResourceNotFoundException)
Lambda, API Gateway, CloudFormation
⋯
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda
https://console.aws.amazon.com/lambda/
Cloud9
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda
• Lambda
python
• lambda.py
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
import boto3
import json
from io import BytesIO
from PIL import Image
import base64
import numpy as np
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
labels = ['aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor ]
image = base64.b64decode(event['base64Image'])
image = Image.open(BytesIO(image))
transposed = np.array(image).transpose(2,0,1)
npy = BytesIO()
np.save(npy, transposed)
ByteArr = npy.getvalue()
runtime= boto3.client('sagemaker-runtime )
response = runtime.invoke_endpoint(EndpointName=“ ”,
ContentType='application/x-npy',
Accept = 'application/json',
Body=ByteArr)
predictions = np.array(json.load(response['Body']))
results =[]
for i in range(predictions.shape[1]):
bbox, label_index, score = predictions[:, i]
result = {
'class_name' : labels[label_index],
'y_min': int(bbox[0]),
'x_min': int(bbox[1]),
'y_max': int(bbox[2]),
'x_max': int(bbox[3]),
'score': score
}
results.append(result)
return {
"statusCode": 200,
"body": json.dumps(results)
}
npy
Endpoint
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Lambda Lambda
• Lambda
Cloud9 Lambda
•
cd (Lambda )
pip install numpy -t .
pip install pillow -t .
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
https://console.aws.amazon.com/lambda/
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud9 API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SageMaker Role
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API Gateway
API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
1
API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API
Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• POST method
•
• ANY
POST
• POST
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda Cloud9 ( )
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• image/jpeg
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• image/jpeg
Lambda
Lambda
image =
base64.b64decode(event['base64Image'])
{
"operation": "inference",
"base64Image": "$input.body"
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• image/jpeg
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• json
• Lambda json Empty
: Empty
: application/json
: {}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• POST
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
CORS
•
CORS CORS
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API URL
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
1
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• API
(test_images cats.jpg )
• curl
• Status code 200
{"statusCode": 200, "body": "[{¥"class_name¥": ¥"cat¥", ¥"y_min¥": 170,
¥"x_min¥": 443, ¥"y_max¥": 558, ¥"x_max¥": 812, ¥"score¥":
0.9977871179580688}, {¥"class_name¥": ¥"cat¥", ¥"y_min¥": 82, ¥"x_min¥":
249, ¥"y_max¥": 583, ¥"x_max¥": 465, ¥"score¥": 0.9967633485794067}]"}
curl --request POST -H “Content-Type: image/jpeg” --data-
binary ”@test_images/cats.jpg“ http://(API URL)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Javascript
• object_detect.html API URL
const apiurl = 'https://…..execute-api.ap-northeast-1.amazonaws.com/Prod’
• object_detect.html test_images
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2
Cloud9 AWS Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2
S3 API Gateway Lambda
Amazon
API Gateway
AWS
Lambda
Amazon
SageMaker
S3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud9 Lambda
• AWS Resources
λ+ Lambda
• Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda API Gateway
• Function Name/Application Name:
( : Batchinference)
• Blueprint: empty-python
• Trigger:
• Function Trigger: API Gateway
• Resource Path: /
• Security: AWS_IAM
• Lambda
• Endpoint Role
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda
• lambda_batch.py
•
• lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
lambda_batch.py
import boto3
import json
from io import BytesIO
import time
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
model_name = event['model']
timestamp = time.strftime("%Y%m%d-%H%M%S")
job_name = 'transformjob' + timestamp
indir = event['input']
outdir = event['output'] + timestamp
instance_type = event['instance_type']
instance_count = event['instance_count']
runtime= boto3.client('sagemaker')
response = runtime.create_transform_job(
TransformJobName=job_name,
ModelName=model_name,
TransformInput={
'DataSource': {
'S3DataSource': {
'S3DataType': 'S3Prefix',
'S3Uri': indir
}
},
'ContentType': 'image/jpeg',
},
TransformOutput={
'S3OutputPath': outdir,
'Accept': 'application/json'
},
TransformResources={
'InstanceType': instance_type,
'InstanceCount': instance_count,
}
)
return {
"statusCode": 200,
"body": response
}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
AWS Lambda
https://console.aws.amazon.com/lambda/
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2
API Gateway
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• POST method
•
• ANY
POST
• POST
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Cloud9 Lambda ( )
• 1 Lambda
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• multipart/form-data
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• multipart/form-data
multipart/form-data
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• json
• Lambda json Empty
: Empty
: application/json
: {}
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
• POST
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
CORS
•
CORS CORS
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API URL
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
2
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Javascript
• batch_detect.html apiurl
API Gateway URL
• multipart/form-data (API Gateway)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
S3
• SageMaker S3
test_images
• S3 input
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Model Name
S3 input:
S3 output: S3 input
Instance type: (ml.m4.xlarge)
Instance Count: (1)
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
S3
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
[[[170.97262573242188, 443.8755798339844,
558.6778564453125, 812.5823364257812],
[82.46746063232422, 249.7886505126953,
583.5474853515625, 465.04547119140625]],
[7, 7],
[0.9977871179580688, 0.9967633485794067]]
2
2 (7= )
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• SageMaker
• SageMaker
• Cloud9 (EC2)
• S3
• Lambda
• API Gateway
• CloudFormation
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
https://ap-northeast-1.console.aws.amazon.com/sagemaker/home?region=ap-
northeast-1#/notebook-instances
Stopped
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
SageMaker
https://ap-northeast-
1.console.aws.amazon.com/sagemaker/home?region=ap-
northeast-1#/endpoints
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Cloud9
https://us-west-
2.console.aws.amazon.com/cloud9/home?region=us-
west-2
Delete
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
S3
https://s3.console.aws.amazon.com/s3/
SageMaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Lambda
https://ap-northeast-
1.console.aws.amazon.com/lambda/home?region=ap-
northeast-1#/functions
• Cloud9
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
API Gateway
https://ap-northeast-
1.console.aws.amazon.com/apigateway/home?region=ap-
northeast-1
• API
•
• 1 1
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
CloudFormation
https://ap-northeast-
1.console.aws.amazon.com/cloudformation/home?region
=ap-northeast-1#/stacks?filter=active
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Option:
1
EC2 Web application
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon
SageMaker
Amazon
EC2
Lambda, API Gateway EC2
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon
EC2
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• Cloud9 Cloud9
us-west2 ( ) SageMaker
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
•
•
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
setting.py ALLOWED_HOSTS=[‘*’]
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
cd MyTestApp
python3 manage.py runserver 0.0.0.0:8080
URL
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
from django.http.response import HttpResponse
import boto3
import json
from io import BytesIO
from PIL import Image
import base64
import numpy as np
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
import os
# Create your views here.
def helloworld(request):
labels = ['aeroplane', 'bicycle', 'bird', 'boat',
'bottle', 'bus', 'car', 'cat', 'chair',
'cow', 'diningtable', 'dog', 'horse',
'motorbike', 'person', 'pottedplant',
'sheep', 'sofa', 'train', 'tvmonitor']
#return HttpResponse(os.listdir("MyTestApp"))
image = Image.open("./helloworld/dogs.jpg")
transposed = np.array(image).transpose(2,0,1)
npy = BytesIO()
np.save(npy, transposed)
ByteArr = npy.getvalue()
runtime= boto3.client('sagemaker-runtime')
response = runtime.invoke_endpoint(EndpointName=' ',
ContentType='application/x-npy',
Accept = 'application/json',
Body=ByteArr)
predictions = np.array(json.load(response['Body']))
results =[]
for i in range(predictions.shape[1]):
bbox, label_index, score = predictions[:, i]
result = {
'class_name' : labels[label_index],
'y_min': int(bbox[0]),
'x_min': int(bbox[1]),
'y_max': int(bbox[2]),
'x_max': int(bbox[3]),
'score': score
}
results.append(result)
http_res = {
"statusCode": 200,
"body": json.dumps(results)
}
return HttpResponse(http_res[“body”])
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
INSTALLED_APPS ’helloworld’
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^$', views.helloworld, name='helloworld'),
]
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
from django.contrib import admin
from django.urls import path
from django.conf.urls import include, url
urlpatterns = [
path('admin/', admin.site.urls),
url('helloworld/', include(('helloworld.urls','helloworld'),)),
]
© 2018, Amazon Web Services, Inc. or its Affiliates. All rights reserved.

Amazon SageMaker 推論エンドポイントを利用したアプリケーション開発

  • 1.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Masaki Samejima Solutions Architect, Amazon Web Services Japan. 2018.10.28 AWS
  • 2.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. : http://bit.ly/sagemaker_api 1. API Gateway Lambda SageMaker 2. S3 SageMaker 1. EC2 ( )
  • 3.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • •
  • 4.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. IAM • IAM ( ) • AWSCloud9Administrator • AmazonSageMakerFullAccess • AWSLambdaFullAccess • AmazonAPIGatewayAdministrator • AmazonAPIGatewayInvokeFullAccess • AmazonEC2FullAccess • CloudFormation (Fullaccess • IAM (AWS 1 ) • AWSServiceRoleForAWSCloud9 ( ) • Amazon SageMaker : AmazonSageMakerFullAccess ( ARN ) • AWS Lambda : AWSLambdaFullAccess AmazonSageMakerFullAccess ( ) IAM Administrator Lambda
  • 5.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 6.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AWS Lambda
  • 7.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AamzonSageMakerFullAccess
  • 8.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • Lambda
  • 9.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 10.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • (Entry point) Amazon SageMaker S3 • • Amazon SageMaker
  • 11.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. https://console.aws.amazon.com/sagemaker
  • 12.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • •
  • 13.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • •
  • 14.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • ml.t2.medium • IAM
  • 15.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. IAM S3 S3
  • 16.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • VPC, •
  • 17.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Jupyter Notebook • InService Jupyter Notebook
  • 18.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • •
  • 19.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Upload
  • 20.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. deploy.ipynb
  • 21.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Run ---
  • 22.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. https://ap-northeast-1.console.aws.amazon.com/sagemaker/home?region=ap- northeast-1#/endpoints
  • 23.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API Gateway Lambda
  • 24.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 1 API Gateway, Lambda SageMaker Endpoint Amazon API Gateway AWS Lambda Amazon SageMaker
  • 25.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 2 S3 API Gateway Lambda Amazon API Gateway AWS Lambda Amazon SageMaker S3
  • 26.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 1 Cloud9 AWS Lambda
  • 27.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 1 API Gateway, Lambda SageMaker Endpoint Amazon API Gateway AWS Lambda Amazon SageMaker
  • 28.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Cloud9 AWS Lambda Amazon API Gateway • Cloud9 • GUI Lambda API Gateway Lambda
  • 29.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Cloud9 https://aws.amazon.com/jp/cloud9/
  • 30.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • AWS
  • 31.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • create environment • Cloud9
  • 32.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Python3 Python 2 → Python 3
  • 33.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. python 3 alias python=python36 alias pip=pip-3.6
  • 34.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • Lambda • SageMaker
  • 35.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • AWS Resources λ+ Lambda • Lambda
  • 36.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Blueprint ( ) empty-python
  • 37.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • API Gateway • Resource path: / • Security: NONE
  • 38.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • Role Choose an existing role • Existing role AWSLambdaFullAccess AmazonSageMakerFullAccess role
  • 39.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • (ResourceNotFoundException) Lambda, API Gateway, CloudFormation ⋯
  • 40.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AWS Lambda https://console.aws.amazon.com/lambda/ Cloud9
  • 41.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Lambda • Lambda python • lambda.py •
  • 42.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. import boto3 import json from io import BytesIO from PIL import Image import base64 import numpy as np import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): labels = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor ] image = base64.b64decode(event['base64Image']) image = Image.open(BytesIO(image)) transposed = np.array(image).transpose(2,0,1) npy = BytesIO() np.save(npy, transposed) ByteArr = npy.getvalue() runtime= boto3.client('sagemaker-runtime ) response = runtime.invoke_endpoint(EndpointName=“ ”, ContentType='application/x-npy', Accept = 'application/json', Body=ByteArr) predictions = np.array(json.load(response['Body'])) results =[] for i in range(predictions.shape[1]): bbox, label_index, score = predictions[:, i] result = { 'class_name' : labels[label_index], 'y_min': int(bbox[0]), 'x_min': int(bbox[1]), 'y_max': int(bbox[2]), 'x_max': int(bbox[3]), 'score': score } results.append(result) return { "statusCode": 200, "body": json.dumps(results) } npy Endpoint
  • 43.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • Lambda Lambda • Lambda Cloud9 Lambda • cd (Lambda ) pip install numpy -t . pip install pillow -t .
  • 44.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 45.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. https://console.aws.amazon.com/lambda/
  • 46.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Cloud9 API Gateway
  • 47.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. SageMaker Role
  • 48.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API Gateway API Gateway
  • 49.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 1 API Gateway
  • 50.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API Lambda
  • 51.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 52.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • POST method • • ANY POST • POST
  • 53.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Lambda Cloud9 ( )
  • 54.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 55.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • image/jpeg
  • 56.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • image/jpeg Lambda Lambda image = base64.b64decode(event['base64Image']) { "operation": "inference", "base64Image": "$input.body" }
  • 57.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • image/jpeg
  • 58.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • json • Lambda json Empty : Empty : application/json : {}
  • 59.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • POST •
  • 60.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 61.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 62.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • CORS • CORS CORS
  • 63.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API URL
  • 64.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 1
  • 65.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • API (test_images cats.jpg ) • curl • Status code 200 {"statusCode": 200, "body": "[{¥"class_name¥": ¥"cat¥", ¥"y_min¥": 170, ¥"x_min¥": 443, ¥"y_max¥": 558, ¥"x_max¥": 812, ¥"score¥": 0.9977871179580688}, {¥"class_name¥": ¥"cat¥", ¥"y_min¥": 82, ¥"x_min¥": 249, ¥"y_max¥": 583, ¥"x_max¥": 465, ¥"score¥": 0.9967633485794067}]"} curl --request POST -H “Content-Type: image/jpeg” --data- binary ”@test_images/cats.jpg“ http://(API URL)
  • 66.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Javascript • object_detect.html API URL const apiurl = 'https://…..execute-api.ap-northeast-1.amazonaws.com/Prod’ • object_detect.html test_images
  • 67.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 2 Cloud9 AWS Lambda
  • 68.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 2 S3 API Gateway Lambda Amazon API Gateway AWS Lambda Amazon SageMaker S3
  • 69.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Cloud9 Lambda • AWS Resources λ+ Lambda • Lambda
  • 70.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Lambda API Gateway • Function Name/Application Name: ( : Batchinference) • Blueprint: empty-python • Trigger: • Function Trigger: API Gateway • Resource Path: / • Security: AWS_IAM • Lambda • Endpoint Role
  • 71.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Lambda • lambda_batch.py • • lambda
  • 72.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. lambda_batch.py import boto3 import json from io import BytesIO import time import logging logger = logging.getLogger() logger.setLevel(logging.INFO) def lambda_handler(event, context): model_name = event['model'] timestamp = time.strftime("%Y%m%d-%H%M%S") job_name = 'transformjob' + timestamp indir = event['input'] outdir = event['output'] + timestamp instance_type = event['instance_type'] instance_count = event['instance_count'] runtime= boto3.client('sagemaker') response = runtime.create_transform_job( TransformJobName=job_name, ModelName=model_name, TransformInput={ 'DataSource': { 'S3DataSource': { 'S3DataType': 'S3Prefix', 'S3Uri': indir } }, 'ContentType': 'image/jpeg', }, TransformOutput={ 'S3OutputPath': outdir, 'Accept': 'application/json' }, TransformResources={ 'InstanceType': instance_type, 'InstanceCount': instance_count, } ) return { "statusCode": 200, "body": response }
  • 73.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. AWS Lambda https://console.aws.amazon.com/lambda/
  • 74.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 75.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API Gateway
  • 76.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 2 API Gateway
  • 77.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • POST method • • ANY POST • POST
  • 78.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • Cloud9 Lambda ( ) • 1 Lambda
  • 79.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 80.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • multipart/form-data
  • 81.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 82.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • multipart/form-data multipart/form-data
  • 83.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • json • Lambda json Empty : Empty : application/json : {}
  • 84.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • • POST •
  • 85.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 86.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 87.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • CORS • CORS CORS
  • 88.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API URL
  • 89.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. 2
  • 90.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Javascript • batch_detect.html apiurl API Gateway URL • multipart/form-data (API Gateway)
  • 91.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. S3 • SageMaker S3 test_images • S3 input
  • 92.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Model Name S3 input: S3 output: S3 input Instance type: (ml.m4.xlarge) Instance Count: (1)
  • 93.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. S3
  • 94.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. [[[170.97262573242188, 443.8755798339844, 558.6778564453125, 812.5823364257812], [82.46746063232422, 249.7886505126953, 583.5474853515625, 465.04547119140625]], [7, 7], [0.9977871179580688, 0.9967633485794067]] 2 2 (7= )
  • 95.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 96.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • SageMaker • SageMaker • Cloud9 (EC2) • S3 • Lambda • API Gateway • CloudFormation
  • 97.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. https://ap-northeast-1.console.aws.amazon.com/sagemaker/home?region=ap- northeast-1#/notebook-instances Stopped
  • 98.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. SageMaker https://ap-northeast- 1.console.aws.amazon.com/sagemaker/home?region=ap- northeast-1#/endpoints
  • 99.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Cloud9 https://us-west- 2.console.aws.amazon.com/cloud9/home?region=us- west-2 Delete
  • 100.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. S3 https://s3.console.aws.amazon.com/s3/ SageMaker
  • 101.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Lambda https://ap-northeast- 1.console.aws.amazon.com/lambda/home?region=ap- northeast-1#/functions • Cloud9 •
  • 102.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. API Gateway https://ap-northeast- 1.console.aws.amazon.com/apigateway/home?region=ap- northeast-1 • API • • 1 1
  • 103.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. CloudFormation https://ap-northeast- 1.console.aws.amazon.com/cloudformation/home?region =ap-northeast-1#/stacks?filter=active • •
  • 104.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Option: 1 EC2 Web application
  • 105.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Amazon SageMaker Amazon EC2 Lambda, API Gateway EC2
  • 106.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. Amazon EC2
  • 107.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • Cloud9 Cloud9 us-west2 ( ) SageMaker
  • 108.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. • •
  • 109.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. setting.py ALLOWED_HOSTS=[‘*’]
  • 110.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. cd MyTestApp python3 manage.py runserver 0.0.0.0:8080 URL
  • 111.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 112.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 113.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 114.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. from django.http.response import HttpResponse import boto3 import json from io import BytesIO from PIL import Image import base64 import numpy as np import logging logger = logging.getLogger() logger.setLevel(logging.INFO) import os # Create your views here. def helloworld(request): labels = ['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'] #return HttpResponse(os.listdir("MyTestApp")) image = Image.open("./helloworld/dogs.jpg") transposed = np.array(image).transpose(2,0,1) npy = BytesIO() np.save(npy, transposed) ByteArr = npy.getvalue() runtime= boto3.client('sagemaker-runtime') response = runtime.invoke_endpoint(EndpointName=' ', ContentType='application/x-npy', Accept = 'application/json', Body=ByteArr) predictions = np.array(json.load(response['Body'])) results =[] for i in range(predictions.shape[1]): bbox, label_index, score = predictions[:, i] result = { 'class_name' : labels[label_index], 'y_min': int(bbox[0]), 'x_min': int(bbox[1]), 'y_max': int(bbox[2]), 'x_max': int(bbox[3]), 'score': score } results.append(result) http_res = { "statusCode": 200, "body": json.dumps(results) } return HttpResponse(http_res[“body”])
  • 115.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.
  • 116.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. INSTALLED_APPS ’helloworld’
  • 117.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. from django.conf.urls import url from . import views urlpatterns = [ url(r'^$', views.helloworld, name='helloworld'), ]
  • 118.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved. from django.contrib import admin from django.urls import path from django.conf.urls import include, url urlpatterns = [ path('admin/', admin.site.urls), url('helloworld/', include(('helloworld.urls','helloworld'),)), ]
  • 119.
    © 2018, AmazonWeb Services, Inc. or its Affiliates. All rights reserved.