Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

AWS Lambda를 통한 Tensorflow 및 Keras 기반 추론 모델 서비스하기 :: 이준범 :: AWS Summit Seoul 2018

3,346 views

Published on

AWS Lambda를 통한 Tensorflow 및 Keras 기반 추론 모델 서비스하기 :: 이준범 :: AWS Summit Seoul 2018

Published in: Technology
  • If you’re looking for a great essay service then you should check out ⇒ www.WritePaper.info ⇐. A friend of mine asked them to write a whole dissertation for him and he said it turned out great! Afterwards I also ordered an essay from them and I was very happy with the work I got too.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Girls for sex in your area are there: tinyurl.com/areahotsex
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Sex in your area is here: www.bit.ly/sexinarea
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: www.bit.ly/2AJerkH
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • accessibility Books Library allowing access to top content, including thousands of title from favorite author, plus the ability to read or download a huge selection of books for your pc or smartphone within minutes ,Download or read Ebooks here ... ......................................................................................................................... Download FULL PDF EBOOK here { https://urlzs.com/UABbn }
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

AWS Lambda를 통한 Tensorflow 및 Keras 기반 추론 모델 서비스하기 :: 이준범 :: AWS Summit Seoul 2018

  1. 1. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  2. 2. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  3. 3. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  4. 4. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  5. 5. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 

  6. 6. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  7. 7. • 
 • • • •
  8. 8. • model = Sequential() model.add(Dense(1, activation='sigmoid', input_dim=100)) from tensorflow.python.keras.models import Sequential from tensorflow.python.keras.layers import Dense import numpy as np model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy']) data = np.random.random((1000, 100)) labels = np.random.randint(2, size=(1000, 1)) model.fit(data, labels, epochs=10, batch_size=32)
  9. 9. • • • • • • •
  10. 10. • • • • • • •
  11. 11. • • 
 • from tensorflow.python.keras.applications.xception import Xception model = Xception(weights='imagenet')
  12. 12. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 

  13. 13. • ç • • • •
  14. 14. • from tensorflow.python.keras.applications.xception import Xception from tensorflow.python.keras.preprocessing import image from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as np model = Xception(weights='imagenet') img_path = 'elephant.jpg' img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) print('Predicted:', decode_predictions(preds, top=3)[0])
  15. 15. • • • • 

  16. 16. • • 
 
 • 
 

  17. 17. • • 
 
 • 
 

  18. 18. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  19. 19. • • • from tensorflow.python.keras.applications.xception import Xception model = Xception(include_top=False, weights='imagenet')
  20. 20. 
 
 
 

  21. 21. def train(train_data_dir, validation_data_dir, model_path): base_model = Xception(weights='imagenet', include_top=False) x = base_model.output x = GlobalAveragePooling2D()(x) predictions = Dense(nb_classes, activation='softmax')(x) model = Model(base_model.input, predictions) transformation_ratio = .05 train_datagen = ImageDataGenerator(rescale=1. / 255, rotation_range=transformation_ratio, shear_range=transformation_ratio, zoom_range=transformation_ratio, cval=transformation_ratio, horizontal_flip=True, vertical_flip=True)
  22. 22. def train(train_data_dir, validation_data_dir, model_path): ...( )... train_generator = train_datagen.flow_from_directory(train_data_dir, batch_size=32, class_mode='categorical') validation_generator = validation_datagen.flow_from_directory(validation_data_dir, batch_size=32, class_mode='categorical') model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) top_weights_path = os.path.join(os.path.abspath(model_path), 'top_model_weights.h5') callbacks_list = [ ModelCheckpoint(top_weights_path, monitor='val_acc', verbose=1, save_best_only=True), EarlyStopping(monitor='val_acc', patience=5, verbose=0) ] model.fit_generator(train_generator, samples_per_epoch=train_generator.nb_sample, nb_epoch=nb_epoch / 5, validation_data=validation_generator, nb_val_samples=validation_generator.nb_sample, callbacks=callbacks_list)
  23. 23. • 
 • 
 final_weights_path = os.path.join(os.path.abspath(model_path), 'model_weights.h5') model.save_weights(final_weights_path) model_json = model.to_json() json_file = open(os.path.join(os.path.abspath(model_path), 'model.json'), 'w') json_file.write(model_json)
  24. 24. def inference(trained_model_dir, test_data_dir, results_path): # load json and create model json_file = open(os.path.join(trained_model_dir, model_name), 'r') loaded_model_json = json_file.read() json_file.close() model = model_from_json(loaded_model_json) model.load_weights(os.path.join(trained_model_dir, model_weights)) # Read Data test_datagen = ImageDataGenerator(rescale=1. / 255) test_generator = test_datagen.flow_from_directory(test_data_dir, batch_size=batch_size, shuffle=False) # Calculate class posteriors probabilities y_probabilities = model.predict_generator(test_generator, val_samples=test_generator.nb_sample) # Calculate class labels y_classes = probas_to_classes(y_probabilities) filenames = [filename.split('/')[1] for filename in test_generator.filenames] ids = [filename.split('.')[0] for filename in filenames]
  25. 25. • • • 

  26. 26. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 

  27. 27. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  28. 28. • • • • 
 

  29. 29. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  30. 30. • 
 • 
 • • 
 
 
 

  31. 31. • 
 • 
 • 

  32. 32. • 
 • 
 
 • 

  33. 33. • 
 
 • 
 
 • 

  34. 34. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  35. 35. • •
  36. 36. # event { 'Records': [ { 'eventVersion': '2.0', 'eventSource': 'aws:s3', 'awsRegion': 'ap-northeast-2', # 'eventTime': '2017-12-13T03:28:13.528Z', # 'eventName': 'ObjectCreated:Put', 'userIdentity': {'principalId': 'AFK2RA1O3ML1F'}, 'requestParameters': {'sourceIPAddress': '123.24.137.5'}, 'responseElements': { 'x-amz-request-id': '1214K424C14C384D', 'x-amz-id-2': 'BOTBfAoB/gKBbn412ITN4t2psTW499iMRKZDK/CQTsjrkeSSzSdsDUMGabcdnvHeYNtbTDHoHKs=' }, 's3': { 's3SchemaVersion': '1.0', 'configurationId': 'b249eeda-3d48-4319-a7e2-853f964c1a25', 'bucket': { 'name': 'aws-summit-kr-2018', # 'ownerIdentity': { 'principalId': 'AFK2RA1O3ML1F' }, 'arn': 'arn:aws:s3:::aws-summit-kr-2018' }, 'object': { 'key': 'img/test_img.png', # 'size': 11733, # 'eTag': 'f2d12d123aebda1cc1fk17479207e838', 'sequencer': '125B119E4D7B2A0A48' } } } ] }
  37. 37. • • • • # def handler(event, context): bucket_name = event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key']
  38. 38. • ACCESS_KEY = os.environ.get('ACCESS_KEY') SECRET_KEY = os.environ.get('SECRET_KEY') def downloadFromS3(strBucket, s3_path, local_path): s3_client = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) s3_client.download_file(strBucket, s3_path, local_path) def uploadToS3(bucket, s3_path, local_path): s3_client = boto3.client('s3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) s3_client.upload_file(local_path, bucket, s3_path)
  39. 39. • • 
 def handler(event, context): bucket_name = event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key'] file_name = file_path.split('/')[-1] downloadFromS3(bucket_name, file_path, '/tmp/'+file_name) 

  40. 40. • def handler(event, context): bucket_name = event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key'] file_name = file_path.split('/')[-1] downloadFromS3(bucket_name, file_path, '/tmp/'+file_name) downloadFromS3( 'aws-summit-kr-2018', 'xception_weights_tf_dim_ordering_tf_kernels.h5', '/tmp/.keras/xception_weights_tf_dim_ordering_tf_kernels.h5' )
  41. 41. • from tensorflow.python.keras.applications.xception import Xception from tensorflow.python.keras.preprocessing import image from tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictions import numpy as np def predict(img_path): model = Xception(weights='imagenet') img = image.load_img(img_path, target_size=(224, 224)) x = image.img_to_array(img) x = np.expand_dims(x, axis=0) x = preprocess_input(x) preds = model.predict(x) return decode_predictions(preds, top=3)[0]
  42. 42. • def handler(event, context): bucket_name = event['Records'][0]['s3']['bucket']['name'] file_path = event['Records'][0]['s3']['object']['key'] file_name = file_path.split('/')[-1] downloadFromS3(bucket_name, file_path, '/tmp/'+file_name) downloadFromS3( 'aws-summit-kr-2018', 'xception_weights_tf_dim_ordering_tf_kernels.h5', '/tmp/.keras/xception_weights_tf_dim_ordering_tf_kernels.h5' ) result = predict('/tmp/'+file_name) return result
  43. 43. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  44. 44. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  45. 45. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  46. 46. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  47. 47. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  48. 48. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  49. 49. • • • • 
 • 

  50. 50. • 
 • 
 
 

  51. 51. • 
 • 
 • • •
  52. 52. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  53. 53. • •
  54. 54. • 
 
 • 
 •
  55. 55. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  56. 56. • 
 • 
 • 
 docker run -v $(pwd):/outputs --name lambdapack -d amazonlinux:latest tail -f /dev/null
  57. 57. • • • 
 • • 

  58. 58. dev_install() { yum -y update yum -y upgrade yum install -y wget gcc gcc-c++ cmake python36-devel python36-virtualenv python36-pip findutils zlib-devel zip unzip blas-devel lapack-devel atlas-devel }
  59. 59. mkvirtualenv() { cd /home/ rm -rf env python3 -m virtualenv env --python=python3 source env/bin/activate } pip_install() { source /home/env/bin/activate pip install -U pip wheel pip install --use-wheel tensorflow==1.7.0 --no-deps pip install protobuf html5lib bleach --no-deps pip install --use-wheel pillow==4.0.0 pip install h5py }
  60. 60. gather_pack() { cd /home/ && rm -rf pack && mkdir pack && cd pack cp -R /home/env/lib/python3.6/site-packages/* . cp -R /home/env/lib64/python3.6/site-packages/* . cp /outputs/index.py /home/pack/index.py find . -type d -name "test" -exec rm -rf {} + find -name "*.so" | xargs strip find -name "*.so.*" | xargs strip rm -r pip && rm -r pip-* && rm -r wheel && rm -r wheel-* find . | grep -E "(__pycache__|.pyc$)" | xargs rm -rf echo "stripped size $(du -sh /home/pack | cut -f1)" zip -FS -r1 /outputs/pack.zip * > /dev/null echo "compressed size $(du -sh /outputs/pack.zip | cut -f1)" }
  61. 61. • • • • • •
  62. 62. • 
 • 
 •
  63. 63. import boto3 def upload_to_s3(bucket, s3_path, local_path): client = boto3.client('s3', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY) client.upload_file(local_path, bucket, s3_path) def update_lambda(function_name, bucket, s3_path): client = boto3.client('lambda', aws_access_key_id=ACCESS_KEY,aws_secret_access_key=SECRET_KEY) client.update_function_code( FunctionName=function_name, S3Bucket=bucket, S3Key=s3_path, ) uploadToS3(' ', ' /pack.zip', './pack.zip') update_lambda(' ', ' ', ' /pack.zip')
  64. 64. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 

  65. 65. • • 
 
 • • • •
  66. 66. def download_s3_object(strBucket, s3_path): import io file_obj = io.BytesIO() s3_client = boto3.client( 's3', aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY ) s3_client.download_fileobj(strBucket, s3_path, file_obj) return file_obj pack2 = download_s3_object('bucket-name', 'pack2.zip') import zipfile zip_ref = zipfile.ZipFile(pack2) zip_ref.extractall('/tmp') zip_ref.close() import sys sys.path.append("/tmp")
  67. 67. • • 
 • 
 
 • 

  68. 68. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. 

  69. 69. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  70. 70. • 
 • • 
 • 

  71. 71. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  72. 72. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  73. 73. • 
 • 
 • 
 • 

  74. 74.
  75. 75. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  76. 76. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  77. 77. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6 my_module.my_handler docker run --rm -v "$PWD":/var/task lambci/lambda:python3.6 my_module.my_handler '{"some": "event"}'
  78. 78. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  79. 79. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  80. 80.
  81. 81. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  82. 82. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  83. 83. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  84. 84. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  85. 85. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.
  86. 86. © 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.

×