-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.py
48 lines (40 loc) · 1.31 KB
/
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import typing as t
import numpy as np
from PIL.Image import Image as PILImage
import bentoml
from bentoml.io import Image
from bentoml.io import NumpyNdarray
mnist_runner = bentoml.pytorch.load_runner(
"pytorch_mnist",
name="mnist_runner",
predict_fn_name="predict",
)
svc = bentoml.Service(
name="pytorch_mnist_demo",
runners=[
mnist_runner,
],
)
@svc.api(
input=NumpyNdarray(dtype="float32", enforce_dtype=True),
output=NumpyNdarray(dtype="int64"),
)
async def predict_ndarray(
inp: "np.ndarray[t.Any, np.dtype[t.Any]]",
) -> "np.ndarray[t.Any, np.dtype[t.Any]]":
assert inp.shape == (28, 28)
# We are using greyscale image and our PyTorch model expect one
# extra channel dimension
inp = np.expand_dims(inp, 0)
output_tensor = await mnist_runner.async_run(inp)
return output_tensor.numpy()
@svc.api(input=Image(), output=NumpyNdarray(dtype="int64"))
async def predict_image(f: PILImage) -> "np.ndarray[t.Any, np.dtype[t.Any]]":
assert isinstance(f, PILImage)
arr = np.array(f) / 255.0
assert arr.shape == (28, 28)
# We are using greyscale image and our PyTorch model expect one
# extra channel dimension
arr = np.expand_dims(arr, 0).astype("float32")
output_tensor = await mnist_runner.async_run(arr)
return output_tensor.numpy()