-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_function.py
43 lines (29 loc) · 924 Bytes
/
lambda_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import tflite_runtime.interpreter as tflite
from keras_image_helper import create_preprocessor
preprocessor = create_preprocessor('xception', target_size = (299, 299))
interpreter = tflite.Interpreter(model_path = 'clothing_model.tflite')
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]['index']
output_index = interpreter.get_output_details()[0]['index']
classes = ['dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt']
#url = 'http://bit.ly/mlbookcamp-pants'
def predict(url):
X = preprocessor.from_url(url)
interpreter.set_tensor(input_index, X)
interpreter.invoke()
preds = interpreter.get_tensor(output_index)
float_predictions = preds[0].tolist()
return dict(zip(classes, float_predictions))
def lambda_handler(event, context):
url = event['url']
result = predict(url)
return result