Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Allow predict for sklearn standalone server as wll as predict_proba #757

Merged
merged 1 commit into from
Aug 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions doc/source/servers/sklearn.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,29 @@ spec:

```

## Sklearn Method

By default the server will call `predict_proba` on your loaded model/pipeline. If you wish for it to call `predict` instead you can pass a parameter `method` and set it to `predict`. For example:

```
apiVersion: machinelearning.seldon.io/v1alpha2
kind: SeldonDeployment
metadata:
name: sklearn
spec:
name: iris-predict
predictors:
- graph:
children: []
implementation: SKLEARN_SERVER
modelUri: gs://seldon-models/sklearn/iris
name: classifier
parameters:
- name: method
type: STRING
value: predict
name: default
replicas: 1
```

Try out a [worked notebook](../examples/server_examples.html)
18 changes: 18 additions & 0 deletions servers/sklearnserver/samples/iris_predict.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: machinelearning.seldon.io/v1alpha2
kind: SeldonDeployment
metadata:
name: sklearn
spec:
name: iris-predict
predictors:
- graph:
children: []
implementation: SKLEARN_SERVER
modelUri: gs://seldon-models/sklearn/iris
name: classifier
parameters:
- name: method
type: STRING
value: predict
name: default
replicas: 1
30 changes: 23 additions & 7 deletions servers/sklearnserver/sklearnserver/SKLearnServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,41 @@
from seldon_core.user_model import SeldonComponent
from typing import Dict, List, Union, Iterable
import os
import logging

logger = logging.getLogger(__name__)


JOBLIB_FILE = "model.joblib"


class SKLearnServer(SeldonComponent):
def __init__(self, model_uri: str):
def __init__(self, model_uri: str = None, method: str = "predict_proba"):
super().__init__()
self.model_uri = model_uri
self.method = method
self.ready = False
print("Model uri:",self.model_uri)
print("method:",self.method)
self.load()

def load(self):
print("load")
model_file = os.path.join(seldon_core.Storage.download(self.model_uri), JOBLIB_FILE)
print("model file",model_file)
print("model file", model_file)
self._joblib = joblib.load(model_file)
self.ready = True

def predict(self, X: np.ndarray, names: Iterable[str], meta: Dict = None) -> Union[np.ndarray, List, str, bytes]:
print("predict")
if not self.ready:
self.load()
result = self._joblib.predict(X)
return result
try:
if not self.ready:
self.load()
if self.method == "predict_proba":
logger.info("Calling predict_proba")
result = self._joblib.predict_proba(X)
else:
logger.info("Calling predict")
result = self._joblib.predict(X)
return result
except Exception as ex:
logging.exception("Exception during predict")
Loading