forked from DataTalksClub/machine-learning-zoomcamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda_function.py
52 lines (35 loc) · 1.03 KB
/
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
38
39
40
41
42
43
44
45
46
47
48
49
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-v4.tflite')
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
input_index = input_details[0]['index']
output_details = interpreter.get_output_details()
output_index = output_details[0]['index']
def predict(X):
interpreter.set_tensor(input_index, X)
interpreter.invoke()
preds = interpreter.get_tensor(output_index)
return preds[0]
labels = [
'dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt'
]
def decode_predictions(pred):
result = {c: float(p) for c, p in zip(labels, pred)}
return result
def lambda_handler(event, context):
url = event['url']
X = preprocessor.from_url(url)
preds = predict(X)
results = decode_predictions(preds)
return results