This repository has been archived by the owner on Jul 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
107 lines (93 loc) · 3.16 KB
/
model.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import io
from typing import List
import torch
import requests
import numpy as np
import onnxruntime as ort
from torchvision import transforms
from PIL import Image
from instill.helpers.const import DataType
from instill.helpers.ray_io import serialize_byte_tensor, deserialize_bytes_tensor
from instill.helpers.ray_config import instill_deployment, InstillDeployable
from instill.helpers import (
construct_infer_response,
construct_metadata_response,
Metadata,
)
@instill_deployment
class MobileNet:
def __init__(self):
self.categories = self._image_labels()
self.model = ort.InferenceSession(
"model.onnx", providers=["CUDAExecutionProvider"]
)
self.tf = transforms.Compose(
[
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
def _image_labels(self) -> List[str]:
categories = []
url = (
"https://raw.githubusercontent.com/pytorch/hub/master/imagenet_classes.txt"
)
labels = requests.get(url, timeout=10).text
for label in labels.split("\n"):
categories.append(label.strip())
return categories
def ModelMetadata(self, req):
resp = construct_metadata_response(
req=req,
inputs=[
Metadata(
name="input",
datatype=str(DataType.TYPE_STRING.name),
shape=[1],
),
],
outputs=[
Metadata(
name="output",
datatype=str(DataType.TYPE_STRING.name),
shape=[1000],
),
],
)
return resp
async def __call__(self, req):
b_tensors = req.raw_input_contents[0]
input_tensors = deserialize_bytes_tensor(b_tensors)
batch_out = []
for enc in input_tensors:
img = Image.open(io.BytesIO(enc.astype(bytes)))
image = np.array(img)
np_tensor = self.tf(Image.fromarray(image, mode="RGB")).numpy()
batch_out.append(np_tensor)
batch_out = np.asarray(batch_out)
out = self.model.run(None, {"input": batch_out})
# shape=(1, batch_size, 1000)
# tensor([[207], [294]]), tensor([[0.7107], [0.7309]])
score, cat = torch.topk(torch.from_numpy(out[0]), 1)
s_out = [
bytes(f"{score[i][0]}:{self.categories[cat[i]]}", "utf-8")
for i in range(cat.size(0))
]
out = serialize_byte_tensor(np.asarray(s_out))
out = np.expand_dims(out, axis=0)
return construct_infer_response(
req=req,
outputs=[
Metadata(
name="output",
datatype=str(DataType.TYPE_STRING.name),
shape=[len(batch_out), 1000],
)
],
raw_outputs=out,
)
entrypoint = InstillDeployable(MobileNet).get_deployment_handle()