/
sklearn.py
82 lines (63 loc) · 2.68 KB
/
sklearn.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
import joblib
from typing import List
from mlserver import types
from mlserver.model import MLModel
from mlserver.errors import InferenceError
from mlserver.utils import get_model_uri
from mlserver.codecs import NumpyCodec
PREDICT_OUTPUT = "predict"
PREDICT_PROBA_OUTPUT = "predict_proba"
VALID_OUTPUTS = [PREDICT_OUTPUT, PREDICT_PROBA_OUTPUT]
WELLKNOWN_MODEL_FILENAMES = ["model.joblib", "model.pickle", "model.pkl"]
class SKLearnModel(MLModel):
"""
Implementation of the MLModel interface to load and serve `scikit-learn`
models persisted with `joblib`.
"""
async def load(self) -> bool:
# TODO: Log info message
model_uri = await get_model_uri(
self._settings, wellknown_filenames=WELLKNOWN_MODEL_FILENAMES
)
self._model = joblib.load(model_uri)
self.ready = True
return self.ready
async def predict(self, payload: types.InferenceRequest) -> types.InferenceResponse:
payload = self._check_request(payload)
return types.InferenceResponse(
model_name=self.name,
model_version=self.version,
outputs=self._predict_outputs(payload),
)
def _check_request(self, payload: types.InferenceRequest) -> types.InferenceRequest:
if len(payload.inputs) != 1:
raise InferenceError(
"SKLearnModel only supports a single input tensor "
f"({len(payload.inputs)} were received)"
)
if not payload.outputs:
# By default, only return the result of `predict()`
payload.outputs = [types.RequestOutput(name=PREDICT_OUTPUT)]
else:
for request_output in payload.outputs:
if request_output.name not in VALID_OUTPUTS:
raise InferenceError(
f"SKLearnModel only supports '{PREDICT_OUTPUT}' and "
f"'{PREDICT_PROBA_OUTPUT}' as outputs "
f"({request_output.name} was received)"
)
return payload
def _predict_outputs(
self, payload: types.InferenceRequest
) -> List[types.ResponseOutput]:
model_input = payload.inputs[0]
default_codec = NumpyCodec()
input_data = self.decode(model_input, default_codec=default_codec)
outputs = []
for request_output in payload.outputs: # type: ignore
predict_fn = getattr(self._model, request_output.name)
y = predict_fn(input_data)
# TODO: Set datatype (cast from numpy?)
response_output = default_codec.encode(name=request_output.name, payload=y)
outputs.append(response_output)
return outputs