-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
25 lines (19 loc) · 784 Bytes
/
service.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
import bentoml
import numpy as np
from bentoml.io import Image
from bentoml.io import JSON
runner = bentoml.keras.get("keras_xception_final:latest").to_runner()
svc = bentoml.Service("keras_xception_final", runners=[runner])
@svc.api(input=Image(), output=JSON())
async def predict(img):
from tensorflow.keras.applications.xception import preprocess_input, decode_predictions
classes = np.asarray(['cup', 'fork', 'glass', 'knife', 'plate', 'spoon'])
img = img.resize((299, 299))
arr = np.array(img)
arr = np.expand_dims(arr, axis=0)
arr = preprocess_input(arr)
preds = await runner.async_run(arr)
#print(decode_predictions(preds, top=6))
#return decode_predictions(preds, top=6)[0]
result = classes[preds.argmax(axis=1)]
return result