AWS Summit 2018 Seoul 에서 진행한 [AWS Lambda를 통한 Tensorflow 및 Keras 기반 추론 모델 서비스하기] 세션 발표자료
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ળߧ CFPNJ!OFYPODPLS/&90/,PSFB"84-BNCEBܳాೠ5FOTPSGMPX,FSBTӝ߈୶ۿݽ؛ࢲ࠺झೞӝ
View Slide
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.חળߧCFPNJ!OFYPODPLS*OUFMMJHFODF-BCT"OUJBCVTJOH5FBNؘఠ࠙ࢳਢূפয݂XJUI1:5)0/
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.য়טঠӝೡղਊٜࣁח٩۞,FSBT۽ࡅܰѱ٩۞ݽ؛ٜ݅,FSBT9DFQUJPOݽ؛ӝ߈$MBTTJGJDBUJPOݽ؛ٜ݅ӝ%PDLFS"NB[PO-JOVY-BNCEB1BDLBHF;*1"84-BNCEB4"1*(BUFXBZਢࢲ࠺झ2O"%FFQ-FBSOJOH!/&90/
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.য়טҙ"84࠺ਊѐߊ࠙ࢳоदр࠺ਊऔѱחܻ࠙ੋࠁח࢚
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ࣁח٩۞ ,FSBT۽ࡅܰѱ٩۞ݽ؛ٜ݅
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.חঌѷחؘ ,FSBTחޥоਃ
,FSBT• 5FOTPSGMPX$/5,5IFBOPਤীࢲزೞח Ҋࣻળ/FVSBM/FU"1*ۄ࠳۞ܻ• 5FOTPSGMPXࠗఠղؽ ߹بࢸਃ9• /FVSBM/FUਸ٘۽ҳഅоמ• *NBHF/FUӝ߈ਵ۽1SFUSBJOFEػݽ؛ٜ• ਃ્ীחъചणҗҙ۲೧ࢲبਬݺ೧
,FSBT۽ࡅܰѱݽ؛ٜ݅ӝ• 4FRVFOUJBMݽ؛ୡ݅ীࢎਊೞӝ4FRVFOUJBM.PEFMࢤࢿmodel = Sequential()model.add(Dense(1, activation='sigmoid', input_dim=100))from tensorflow.python.keras.models import Sequentialfrom tensorflow.python.keras.layers import Denseimport numpy as np4FRVFOUJBM.PEFM*NQPSU.PEFMDPNQJMFmodel.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])4BNQMFEBUBdata = np.random.random((1000, 100))labels = np.random.randint(2, size=(1000, 1)).PEFM'JUUJOHmodel.fit(data, labels, epochs=10, batch_size=32)
*NBHF/FU1SFUSBJOFE.PEFMTJO,FSBT• 9DFQUJPO• 7((• 7((• 3FT/FU• *ODFQUJPOW• *ODFQUJPO3FT/FUW• .PCJMF/FUW
*NBHF/FU1SFUSBJOFE.PEFMTJO,FSBT5PEBZT$IPJDF• 9DFQUJPO• 7((• 7((• 3FT/FU• *ODFQUJPOW• *ODFQUJPO3FT/FUW• .PCJMF/FUW
*NBHF/FU1SFUSBJOFE.PEFMTJO,FSBT• ࢜ݽ؛ਸJNQPSUೞח҃1SFUSBJOFE.PEFMਸ۽٘• ೞ݅पઁழझథݽ؛ਸࢲࡂೞח҃ীח ݽ؛ੌਸ۽ஸীೞѢաT١ਸా೧ࢲࡂ• ইې٘৬эрױೞѱ.PEFMਸࢎਊоמfrom tensorflow.python.keras.applications.xception import Xceptionmodel = Xception(weights='imagenet')୶оणহ*NBHF/FUӝ߈ਵ۽оणػݽ؛
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.9DFQUJPO৬ೣԋೞח ݽ؛ઁद
9DFQUJPO• ,FSBTઁݫੋపցੋ'SBOçPJT$IPMMFUоઁ• $//ӝ߈*NBHF/FU1SFUSBJOFEݽ؛• %FQUIXJTF4FQBSBCMF$POWPMVUJPOبੑ• ݽ؛ࢎૉ .#• Ҋࢿמ
9DFQUJPOJO,FSBT• ,FSBTীղغযҍ߄۽ࢎਊоמ زਵ۽ݽ؛۽٘from tensorflow.python.keras.applications.xception import Xceptionfrom tensorflow.python.keras.preprocessing import imagefrom tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictionsimport numpy as npmodel = 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])
9DFQUJPOJO,FSBT• 9DFQUJPOח*NBHF/FUਸਤ೧ҳࢿػݽ؛• णؘఠܳ֍যળٍݽ؛ਸणदெঠೠ• ؘఠࣇ݆ݶࠗఠ࠼ݽ؛ਸणदఃחѪоמ• ೞ݅അपਵ۽חજؘఠࣇҳೞӝоয۰ ݆ਵݶݻୌѐ ٜ݅ӝبয۰
ؘఠ۽֫ഛبXFJHIUദٙೞӝ• ؘఠծഛب• 9DFQUJPO৬э1SFUSBJOFEXFJHIUਸоઉ৬ 5SBOTGFS-FBSOJOHਸా೧ؘఠ۽ب ؘఠࣇীౠചػݽ؛оദٙоמ• ؘఠনਸૐೞӝ ؘఠܳઑӘঀ߸ഋೞݴࢎਊ IUUQTCMPHLFSBTJPCVJMEJOHQPXFSGVMJNBHFDMBTTJGJDBUJPONPEFMTVTJOHWFSZMJUUMFEBUBIUNM
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.అҗೖܳҳ࠙ೞחݽ؛ݶ
9DFQUJPO.PEFM5SBJOJOHद• అࢎҗೖࢎ णਊ• అࢎҗೖࢎ పझਊ• 9DFQUJPOݽ؛ӝ߈5SBOTGFS-FBSOJOHfrom tensorflow.python.keras.applications.xception import Xceptionmodel = Xception(include_top=False, weights='imagenet')'VMM$POOFDUFE-BZFSઁѢ5SBOTGFS-FBSOJOH
ইې৬эؘఠоݶ1:5)0/۽ંಫ؊USBJOJOHQZUSBJOJOHQJ[[B ೖࢎۨࣇUSBJOJOHDIJDLFO అࢎۨࣇUFTUQJ[[B ೖࢎపझࣇUFTUDIJDLFO అࢎపझࣇ
ۨೣࣻdef train(train_data_dir, validation_data_dir, model_path):base_model = Xception(weights='imagenet', include_top=False)x = base_model.outputx = GlobalAveragePooling2D()(x)predictions = Dense(nb_classes, activation='softmax')(x)model = Model(base_model.input, predictions)transformation_ratio = .05train_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)पઁणदఆݽ؛ۨࣇܳ߸ഋ೧ؘఠૐ9DFQUJPO߬झ
ۨೣࣻ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)ૐؘఠࢤࢿӝݽ؛$PNQJMFݽ؛5SBJOJOH
ۨਸ݃Ҋ• ݽ؛ۨਸ݃ݶKTPOੌҗIੌ۽&YQPSU ппݽ؛җݽ؛о• पઁ*OGFSFODFܳ೯ೡٸח ਤীࢲ&YQPSUೠݽ؛җݽ؛о݅ਸ੍যࢎਊ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)ݽ؛XFJHIU
ݽ؛5FTU"DDVSBDZஏೞӝdef inference(trained_model_dir, test_data_dir, results_path):# load json and create modeljson_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 Datatest_datagen = ImageDataGenerator(rescale=1. / 255)test_generator = test_datagen.flow_from_directory(test_data_dir,batch_size=batch_size,shuffle=False)# Calculate class posteriors probabilitiesy_probabilities = model.predict_generator(test_generator,val_samples=test_generator.nb_sample)# Calculate class labelsy_classes = probas_to_classes(y_probabilities)filenames = [filename.split('/')[1] for filename in test_generator.filenames]ids = [filename.split('.')[0] for filename in filenames]ݽ؛੍ӝо੍ӝ
ࠗఠ݅ٚݽ؛ਸॳҊरযਃ• ,FSBTоইצ5FOTPSGMPX݅ਵ۽ࢿೠݽ؛بࢎਊоמ• 5FOTPSGMPXࡺ݅ইפۄ1Z5PSDIبࢎਊоמ• ण(16ജ҃ীࢲ *OGFSFODFࢲ࠺झח-BNCEBীࢲ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.٩۞ݽ؛ਸ -BNCEBীࢲج۰ࠁҊरযਃ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.Ӓր&$ীࢲجܻݶউغաਃ ৵-BNCEBܳॳաਃ
৵-BNCEBܳॳաਃ• -BNCEBחࢲߡܻझইఃఫܳҳࢿೞחؘࢎਊ• ࢲߡҙܻীೠࠗ• ߹ب۽٘ߖ۠ࢲҳࢿহࢲ࠺झ߽۳ܻചоמ• ࠗೞоѦܻח҃ূפয݂गо ೠߣҳઑܳੜਵݶূפযبহ ؘఠ࠙ࢳооݽ؛ࢲ࠺झয়߂ਬоמ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.-BNCEBWT&$ઁ ޖਸॄঠೞա
-BNCEBWT&$• زदܻܳрಞೞѱ"84ױীࢲਊоמ ٘ࣻਃೞঋ• ۈೣࣻחೣࣻী೧3FHJPO߹ زदীѐೣ߽ࣻ۳प೯оמ• "844VQQPSU$FOUFSܳా೧زदܻૐооמ• $16৬(16ীٮۄݽ؛ࣘبରоঋ҃ *NBHF/FU*OGFSFODF.PEFM 0 /FVSBM4UZMF5SBOTGFS 9 (16۽بוؘܽ दрت-BNCEBח9
• ઓಁఃನೣ.#ղ۽ҳࢿೞחѪоמೠ҃ ಁఃറ߄۽ߓನоמ• ݽ؛ਊೠ҃ .#_.# 4ীࢲ-BNCEB۽оઉয়חदр-BNCEBप೯࠺ਊ• ೠߣীৈ۞ѐܳزदীप೯ೡ߽۳ܻоਃೠ҃ ೞաप೯दрѱਃೞঋ҃-BNCEBWT&$
-BNCEBWT&$• 'BSHBUFܳਊ೧ࢲߡܻझ৬ਬࢎೞѱҳࢿоמ ইఃఫҙܻੋ۱ਃ• ױة&$ܳࢎਊೡ҃೦࢚ெઉݶ0O%FNBOE 4-"ࠁਃহݶ4105*OTUBODF۽ҳࢿоמ ".*ܳਊೡࣻب• (16ੋझఢझܳࢎਊೡࣻ ୭ࣗदр
• ઓಁఃನೣ.#ղ۽ҳࢿೞחѪࠛоמೠ҃ ઓಁఃܳଂѐUNQী֍যܻоמೞ݅ ࠛಞೞҊ-BNCEBप೯दрਸো ت• ݽ؛ਊ҃ ৈ۞ݽ؛ਸࢎਊೞח҃ UNQীח୭.#݅֍ਸࣻ 7((҃ݽ؛݅.#• ೞաೞաѤী೧ࡅܻܲоਃ೧(16оਃೠ҃ *OQVU%BUBীೠܻоਃೠ҃١-BNCEBWT&$
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.9DFQUJPOݽ؛*OGFSFODFೣࣻ -BNCEBೣࣻ۽ٜ݅ӝ
-BNCEBೣࣻী׳غחࠁ• -BNCEBחFWFOU৬DPOUFYUёܳా೧߮ܳ׳• ܻоࢎਊೞח4'JMFVQMPBEFEחFWFOUё۽׳
# 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'}}}]}#VDLFU/BNF'JMF1BUIJO#VDLFU
-BNCEBীৢܾ)BOEMFSೣٜࣻ݅ӝ• &WFOUੋীࢲࠁযয়ӝ• ইې৬эFWFOUղ3FDPSETܳా೧ࠁӝ• ઁ[email protected]৬[email protected]ܳా೧ੌঘࣁझоמ• ੌੌ0CKFDUܳоઉৢٸח#PUPਸਊ# ਪࠗ࠙ ࢤۚdef handler(event, context):bucket_name = event['Records'][0]['s3']['bucket']['name']file_path = event['Records'][0]['s3']['object']['key']
4ীࢲੌ۽٘স۽٘• #PUPਸਊ೧ੌ۽٘߂স۽٘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)-BNCEBীܻജ҃߸ࣻ۽"84ఃद݁١۾
• খࢲ݅ٚIBOEMFSೣࣻղ۽٘୶о• AUNQAಫ؊ղ .ઁೠ ৡݫݽܻ'JMF0CKFDU۽ҳࢿ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)4ীࢲੌ۽٘স۽٘-BNCEBীࢲ'JMFXSJUFח AUNQAղীࢲ݅оמܲҔীॳݶ*0&SSPSߊࢤ
9DFQUJPOݽ؛۽٘• 4ߡఉীࢲݽ؛XFJHIUੌ I۽٘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'),FSBTח_LFSBT৬UNQLFSBTفҔীࢲੌਸইࢎਊೞ݅ -BNCEBীࢲח_LFSBTীॳӝӂೠহਵ۽UNQLFSBTܳࢎਊ
9DFQUJPO1SFEJDUೣࣻ• AQSFEJDUAۄחܴਵ۽9DFQUJPOݽ؛ࢎਊೞӝfrom tensorflow.python.keras.applications.xception import Xceptionfrom tensorflow.python.keras.preprocessing import imagefrom tensorflow.python.keras.applications.resnet50 import preprocess_input, decode_predictionsimport numpy as npdef 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]҃۽ܳੋ۽߉ই1SFEJDUܳ݃Ѿҗчਸ߈ജױࣽೠѾҗч3FUVSOन%ZOBNP%#١ী࠙ܨറಫ؊߹4স۽٘
दೠߣ-BNCEBݫੋೣࣻܳࣻೞݶ• 4&WFOU%PXOMPBE*NBHF.PEFM1SFEJDUdef 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
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.-BNCEBস۽٘ਊQBDL[JQٜ݅ӝਃೠ1ZUIPO%FQFOEFODZٜݽفݽৈח୷ੌٜ݅ӝ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ઓಁఃо݆ইਃ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.$য҅ৌઓۄ࠳۞ܻ1BOEBT/VNQZIQZ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.04߹۽ܲXIMੌ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.࠽٘
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved."84-BNCEB04"NB[PO-JOVY
-BNCEBPO"NB[PO-JOVY• ".*BN[[email protected]• -JOVYழօBN[[email protected]• CJU߄ցܻ݅ਗ• 1ZUIPOࢎਊоמ #PUP CPUPDPSF ࢸغয• $FOU04ۢ:VNࢎਊоמ୭नࠁ IUUQTEPDTBXTBNB[PODPNMBNCEBMBUFTUEHDVSSFOUTVQQPSUFEWFSTJPOTIUNM
"84-BNCEB1BDLBHJOH• -BNCEBীॆੌਸৢܻӝਤ೧ࢲח ೠ[JQੌীઓࢿಁఃܳݽفޘযࢲৢ۰ঠೣ• $PEF%FQFOEFODZਊઁೠ "84$POTPMFীࢲস۽٘оמೠ[JQ୭ӝ.# ؊ਊ4ਸѢস۽٘оמ ୷೧ઁೠ࢚క.#ղ9DFQUJPOݽ؛بড.# ݽ؛ਸэಁఃೞחѪয۰ਕਃ
• 5FOTPSGMPX /VNQZ 1BOEBT١$MBOHઓੋಁఃח 04ীݏ࠽٘সਸ೯೧ঠೣ• *TTVFѐߊജ҃.BD048JOEPXT6CVOUVੋؘ "84-BNCEB"NB[PO-JOVYਤীࢲҳز• ٮۄࢲ"NB[PO-JOVYਤীࢲ࠽٘ਃ• &$ܳࢎਊ೧ழझథ࠽٘୶о࠺ਊߊࢤ• %PDLFSܳా೧۽ஸী"NB[PO-JOVYജ҃ҳ୷റ࠽٘"84-BNCEB1BDLBHJOH
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.بழоਃೞ
بழח৵ਃೠоਃ• بழஶపցӝ߈য়ࣗझо࢚ചۖಬ• ӝઓ7.ࢿמೞহழօਸҕਬয়ߡ٘
• 7.ਸਊ೧"NB[PO-JOVYܳࢎਊ೧بغ݅ ࠽٘ࣘبо࢚ਵ۽بழী࠺೧וܿ दझమܻࣗझܳࢎਊ• بழীחबয"NB[PO-JOVYب ݺ۸যೠ۽"NB[PO-JOVYஶపցܳڸࣻ• -BNCEBীৢܾ[JQಂਸٜ݅ٸبழܳࢎਊಞܻبழח৵ਃೠоਃ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved."NB[PO-JOVY
"NB[PO-JOVYPO%PDLFS• AEPDLFSAۄחݺ۸যܳࢎਊೡࣻب۾ %PDLFS$PNNVOJUZ&EJUJPOਸࢸפ• ইېݺ۸য۽"NB[PO-JOVYӝ߈ بழஶపցܳप೯ೡࣻणפ• ਤݺ۸যܳࢎਊೞݶ࢜بழஶపցоڸਕݴ അಫ؊ܳبழஶపցղAPVUQVUTAীোѾפdocker run -v $(pwd):/outputs --name lambdapack -d amazonlinux:latest tail -f /dev/null
بழղীࢲप೯ೡ࠽٘4IFMM4DSJQUٜ݅ӝ• :VNਵ۽ಁఃসؘॆࢸ• ॆо࢚ജٜ҃݅ҊSFRVJSFNFOUTࢸ• о࢚ജ҃TJUFQBDLBHFTղݽٚੌز ױ UFTUੌҗDBDIFੌઁ৻• ۽ஸীח -BNCEBীৢۄт1ZUIPOੌࠂࢎ• 1ZUIPOੌҗಁః୷ ಫ؊оইצੌٜ୷
%PDLFSীZVNਵ۽ਃಁఃࢸdev_install() {yum -y updateyum -y upgradeyum install -y \wget \gcc \gcc-c++ \cmake \python36-devel \python36-virtualenv \python36-pip \findutils \zlib-devel \zip \unzip \blas-devel lapack-devel atlas-devel}ӝࠄಁః୭नসؘ1ZUIPOࢸӝఋಁఃࢸ
%PDLFSղWJSUVBMFOW۽ਃಁః݅ࢸmkvirtualenv() {cd /home/rm -rf envpython3 -m virtualenv env --python=python3source env/bin/activate}pip_install() {source /home/env/bin/activatepip install -U pip wheelpip install --use-wheel tensorflow==1.7.0 --no-depspip install protobuf html5lib bleach --no-depspip install --use-wheel pillow==4.0.0pip install h5py}FOWо࢚ജ҃ࢤࢿਃઓࢿಁఃࢸOPEFQTઓಁఃࢸ9೧ಁః݅ࢸ
EFQFOEFODZJOEFYQZQBDL[JQgather_pack() {cd /home/ && rm -rf pack && mkdir pack && cd packcp -R /home/env/lib/python3.6/site-packages/* .cp -R /home/env/lib64/python3.6/site-packages/* .cp /outputs/index.py /home/pack/index.pyfind . -type d -name "test" -exec rm -rf {} +find -name "*.so" | xargs stripfind -name "*.so.*" | xargs striprm -r pip && rm -r pip-* && rm -r wheel && rm -r wheel-*find . | grep -E "(__pycache__|\.pyc$)" | xargs rm -rfecho "stripped size $(du -sh /home/pack | cut -f1)"zip -FS -r1 /outputs/pack.zip * > /dev/nullecho "compressed size $(du -sh /outputs/pack.zip | cut -f1)"}୷ೡಫ؊ࢤࢿॆۄ࠳۞ܻࠂࢎपઁزೞחJOEFYQZ4IBSFE0CKFDU TPTUSJQQZDBDIFઁѢQBDL[JQਵ۽୷
"84ࣛীࢲ-BNCEBೣٜࣻ݅ӝ• %FQFOEFODZоݽفࢸػQBDL[JQੌਸ4ীস۽٘• "NB[PO4ীࢲੌস۽٘২࣌ਵ۽QBDL[JQস۽٘• 3VOUJNF1ZUIPO• )BOEMFSॆੌܴೣܴࣻ• ݫݽܻ(࢚ઁೠदрୡ࢚• ജ҃߸ࣻࢸറ
• QBDL[JQੌ.#ೞੋ҃ ۽ஸBXTDMJജ҃ীࢲ߄۽ೣࣻসؘоמ• QBDL[JQੌ.#ୡҗೞח҃ 4ীੌস۽٘റ4҃۽ #VDLFU ,FZా೧সؘ• ੌস۽٘৬ೣࣻসؘݽف#PUP۽೯46QMPBE-BNCEB'VODUJPOVQEBUF
46QMPBE-BNCEB'VODUJPOVQEBUFX#PUPimport boto3def 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')QBDL[JQস۽٘MBNCEBೣࣻসؘ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.%FQFOEFODZܳݽفଢ଼؊פ .#оֈযߡ۷যਃ
.#ೠ҅ܳֈয• ୶ୌ9• ೞ݅աחԙ-BNCEBীࢲجܻѷয ৈӝࢲޛ۞զࣻחহۄחࢤпदۄݶ • 1BDL[JQفѐ۽ଂѐӝ %FQFOEFODZ݅חQBDL[JQ• -BNCEBೣࣻੌઁੌখীଂђEFQFOEFODZ[JQੌ߉ӝ• UNQղ୷೧ઁ• TZTQBUIীUNQಫ؊١۾
%FQFOEFODZଂѐӝdef download_s3_object(strBucket, s3_path):import iofile_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_objpack2 = download_s3_object('bucket-name', 'pack2.zip')import zipfilezip_ref = zipfile.ZipFile(pack2)zip_ref.extractall('/tmp')zip_ref.close()import syssys.path.append("/tmp")ੌਸ߉݅ৡݫݽܻ#ZUFT*0ীUNQಫ؊ী%FQFOEFODZ୷೧ઁпઙۄ࠳۞ܻJNQPSUJNQPSU1"5)୶о
• ٩۞ݽ؛җ%FQFOEFODZੌয٣ࢲоઉয়ա 4• 4ীࢲоઉয়ݶ࣠࠺ਊহաਃ э3FHJPOղ࣠࣠࠺ਊߊࢤೞঋणפ• 4ীࢲоઉয়חؘदрٜঋաਃ -BNCEBदрઑӘ؊য়ېѦܻ݅ ਊݽ؛ੌਸࢎਊೡࣻחणפ• ਊઁೠহաਃ पઁੌ۽ॵݶ୭.# ৡݫݽܻ۽୭(ղपઁ۽ࢎਊೞӝীޙઁঋաਃ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.-BNCEB4"1*(BUFXBZ 8FC4FSWJDF
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.-BNCEB5SJHHFSJOH&WFOU
-BNCEB5SJHHFSJOH• -BNCEBח߮ী೧ഥप೯غח 4UBUFMFTT'VODUJPO• ٮۄࢲ&WFOUীࠁܳই׳• নೠ&WFOU5SJHHFSо݅ ߣীח4VQMPBEFEࢎਊ• ౠߡఉղౠ1SFGJY ಫ؊ղ ࢜۽ੌࢤӝח҃ -BNCEBܳप೯ೞ
-BNCEB5SJHHFSJOHࢎਸৢܻݶ4ীস۽٘࢜߮-BNCEBܻѢ-BNCEBೣࣻоزೞҊѾҗܳ߈ജ//.PEFM*OGFSFODF
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved. Ӓؘ۠4ীܳযڌѱৢܻ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.41045"1*(BUFXBZपઁস۽٘স۽٘ӂೠࠗৈ
41045"65)X"1*(BUFXBZ• 4ীੌਸৢܾٸ4҃۽ী1045)551ਃਸా೧ ੌਸҍ߄۽ৢܻחѪоמ• ٮۄࢲ'SPOUFOEਢಕীࢲױࣽੌস۽٘ಬਵ۽ب 4ীੌਸস۽٘ೞחѪоמ• ೞ݅ߡఉী1VCMJD8SJUFӂೠࠗৈغযঋਵݶ ਤ৬эࢎਊೞӝয۰• ٮۄࢲӂೠࠗৈܳਤೠ-BNCEBೣࣻ"1*(BUFXBZ۽ ੋૐਊ"1*ࢤࢿ
41045"65)X"1*(BUFXBZ4JOHMF46QMPBE4JHOJOHੌস۽٘оࢤ҂٩۞ਸجܻղࢤпীח ࢎఅ
41045"65)X"1*(BUFXBZ.VMUJ[JQস۽٘୷ਸಽযࠁৡݫݽܻ ୭(ࢤӣ࠺زӝ߽۳ܻ٩۞ਸجܻݽٚоܻغݶ[JQ೧ࠁղ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ױࣽೞѱ-BNCEBജ҃ীࢲप೯݅೧ࠁҊरݶ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.%PDLFS-BNCEB
© 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ױࣽೣࣻܳप೯ೞ۰ݶ&WFOUੋܳ׳ೞ۰ݶdocker run --rm -v "$PWD":/var/task lambci/lambda:python3.6my_module.my_handler '{"some": "event"}'
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.%FFQ-FBSOJOH!
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ѱഥࢎীࢲ٩۞
$MJFOU-PHT4FSWFS-PHT0UIFS -PHTೞܖ5#
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ࢎۈ׀ਸन೧ࠁ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.'14೨ӝ
© 2018, Amazon Web Services, Inc. or Its Affiliates. All rights reserved.ࢎۈࠁޅೠ1BUUFSOਸ
© 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.ܻэੌ೧ਃ8FSF)JSJOH
2O"