-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
64 lines (52 loc) · 1.25 KB
/
lambda_function.py
File metadata and controls
64 lines (52 loc) · 1.25 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# coding: utf-8
import requests
import numpy as np
import tflite_runtime.interpreter as tflite
from keras_image_helper import create_preprocessor
url = './data/Clams/10711395_a16c4c2901_o.jpg'
interpreter = tflite.Interpreter(model_path='sea-creature-model.tflite')
interpreter.allocate_tensors()
preprocessor = create_preprocessor('xception', target_size=(299,299))
input_index = interpreter.get_input_details()[0]['index']
output_index = interpreter.get_output_details()[0]['index']
classes = [
'Clams',
'Corals',
'Crabs',
'Dolphin',
'Eel',
'Fish',
'Jelly Fish',
'Lobster',
'Nudibranchs',
'Octopus',
'Otter',
'Penguin',
'Puffers',
'Seahorse',
'Sea Rays',
'Sea Urchins',
'Seal',
'Sharks',
'Shrimp',
'Squid',
'Starfish',
'Turtle_Tortoise',
'Whale'
]
def preprocess_input(x):
x /= 127.5
x -= 1.
return x
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