Upgrade to Pro — share decks privately, control downloads, hide ads and more …

DeepLearningの本番環境にSageMakerを利用してる話

ota42y
February 23, 2019

 DeepLearningの本番環境にSageMakerを利用してる話

JAWS DAYS 2019でのランチセッションの発表です

ota42y

February 23, 2019
Tweet

More Decks by ota42y

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • @ota42y • αʔόαΠυΤϯδχΞ • RubyͷਓͰ͕͢ࠓ೔͸΄΅PythonͷΈ • Amazon Web

    ServicesαʔόʔϨεϨγϐͬͯຊΛॻ͖·ͨ͠ (AWS Serverless Application Modelʹ͍ͭͯͷ࿩)
  2. TensorFlowͷϞσϧΛಈ͔͢ • (࣮࣭)ϑϧϚωʔδυͳTensorFlow Serving͕࢖͑Δ • ϞσϧΛ༻ҙ͢Ε͹Φʔτεέʔϧ͢ΔREST API͕ੜ͑Δ • S3ʹϞσϧϑΝΠϧΛஔ͍࣮࣭ͯ3ߦॻ͚ͩ͘ from

    sagemaker.tensorflow.serving import Model model = Model(model_data='s3://xxx/model.tar.gz', role=……) model.deploy(initial_instance_count=1, instance_type=‘ml.c5.xlarge')
  3. Before After S3 ECS্ͷ Ruby on Rails ECS্ͷ TensorFlow Serving

    ࣸਅΛS3͔Βऔಘ ࣸਅΛ൑ผͤ͞Δ (gPRC) S3 ECS্ͷ Ruby on Rails SageMaker ࣸਅΛS3͔Βऔಘ ࣸਅΛ൑ผͤ͞Δ (JSON)
  4. • ݩʑ͸ECS্ͷTensorFlow ServingʹgRPCͰૹͬͯͨ • ըૉͷ഑ྻ͸JSONʹ͢Δͱ༰ྔ͕๲ΕΔ • (n*n) pxͷRGB஋͔ΒͳΔ഑ྻΛJSONʹ͢Δͱ໿2ʙ3ഒʹ
 ؒͷεϖʔεແ͠([1,2] rubyͱ͔)

    ؒͷεϖʔε͋Γ([1, 2] pythonͱ͔) • ࠓ·Ͱ͸໰୊ͳ͔͚ͬͨͲJSONͩͱ…ʘ(^o^)ʗ Կ͕໰୊ʹͳ͔ͬͨ 8n2 + 2n + 1 3n2 11n2 + 2n 3n2
  5. • TensorFlow Serving͕ड͚Δͷ͸σʔλͷ഑ྻ • શ෦ࠐΈͰͷΞϓϦͳͷͰ೚ҙͷॲཧΛೖΕΒΕΔ෦෼͸ແ͍ • ͱ͍͏͔ެࣜυΩϡϝϯτʹॻ͍ͯ͋Δ • ଧͭखͳ͔͠…ʘ(^o^)ʗ TensorFlow

    Servingతʹແཧͦ͏ 5FOTPS'MPX4FSWJOHCBTFETFSWFSUPQSPWJEFBTVQFSTFUPGUIF5FOTPS'MPX4FSWJOH 3&45"1*5IJTPQUJPOEPFTOPUSFRVJSF PSBMMPX BDVTUPNQZUIPOTDSJQU
  6. SageMaker্Ͱ΋લॲཧΛ࣮ߦͰ͖Δ • σϓϩΠ࣌ʹ࣮ߦ͢ΔϑΝΠϧΛؚΊΔ͚ͩ
 (class͕ҧ͏ͷͰ஫ҙ) • requirements.txtͰґଘύοέʔδ΋ೖΕΒΕΔ from sagemaker.tensorflow import TensorFlowModel

    model = TensorFlowModel(model_data = 's3://xxx/model.tar.gz', ɹɹɹɹɹɹɹɹɹɹɹɹɹɹ entry_point='pred.py', role=...) model.deploy(initial_instance_count=1, instance_type='ml.c5.xlarge')
  7. entry_point.pyͷྫ import numpy as np def input_fn(serialized_input, content_type): """An input_fn

    that loads a pickled object""" if request_content_type == "application/python-pickle": deserialized_input = pickle.loads(serialized_input) return deserialized_input else: # Handle other content-types here or raise an Exception # if the content type is not supported. pass def output_fn(prediction_result, accepts): """An output_fn that dumps a pickled object as response""" if request_content_type == "application/python-pickle": return np.dumps(prediction_result) else: # Handle other content-types here or raise an Exception # if the content type is not supported. pass
  8. εΫϦϓτ͸͜Μͳײ͡ import boto3 import json import numpy as np from

    PIL import Image import io AWS_S3_BUCKET_NAME = 'NAME' s3 = boto3.resource('s3') def input_fn(serialized_input, content_type): request_json = json.loads(serialized_input.decode()) s3_key = request_json['key'] response = s3.Object(AWS_S3_BUCKET_NAME, s3_key).get() image_bytes = response['Body'].read() img = Image.open(io.BytesIO(image_bytes)) bmp_data = np.asarray(img) return bmp_data[np.newaxis, :, :] ೖྗ͸JSONͰ
 {“key”: “/path/to/a.jpg”}
 S3͔Βը૾ΛऔΓ
 RGBʹม׵ 
 ͜͜ʹ༰ྔ੍ݶ͸ͳ͍
  9. Before After S3 ECS্ͷ Ruby on Rails SageMaker ࣸਅΛ൑ผͤ͞Δ (JSON)

    S3 ECS্ͷ Ruby on Rails SageMaker ࣸਅΛS3͔Βऔಘ S3ͷkey͚ͩૹΔ (༰ྔখ) ࣸਅΛS3͔Βऔಘ ͕͜͜໰୊ͩͬͨ
  10. Before After S3 ECS্ͷ Ruby on Rails SageMaker ࣸਅΛ൑ผͤ͞Δ (JSON)

    S3 ECS্ͷ Ruby on Rails SageMaker ࣸਅΛS3͔Βऔಘ S3ͷkey͚ͩૹΔ (༰ྔখ) ࣸਅΛS3͔Βऔಘ ௨৴ྔ͕ݮͬͯϨεϙϯε΋ૣ͘
  11. TensorFlow, the TensorFlow logo and any related marks are trademarks

    of Google Inc. TensorFlow Logo Copyright © 2019 The TensorFlow authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at https://www.apache.org/licenses/LICENSE-2.0. Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.