Overview
Concrete tool for serving a Random Forest model for MNIST digit classification with AIX explainability support provided by the KServe sample code.
Description
This module defines two classes: PipeStep and RFModel. PipeStep is a simple sklearn pipeline wrapper that turns arbitrary functions into pipeline-compatible transforms (with no-op fitting). RFModel extends kserve.Model and loads a pickled Random Forest pipeline from disk. Its predict() method accepts image instances, converts them to NumPy arrays, reshapes them from 4D image arrays to 2D flattened vectors (n_samples x 2352), runs predict_proba on the model, and reformats the multi-class probability output into a per-class predictions structure.
Usage
Use this class when deploying a Random Forest MNIST classifier as a KServe inference service, particularly when paired with the AIX explainer for generating image-based explanations.
Code Reference
Source Location
Signature
class PipeStep(object):
def __init__(self, step_func):
...
def fit(self, *args):
...
def transform(self, X):
...
class RFModel(kserve.Model):
def __init__(self, name: str):
...
def load(self) -> bool:
...
def predict(
self,
payload: Union[Dict, InferRequest, ModelInferRequest],
headers: Dict[str, str] = None,
) -> Union[Dict, InferResponse, ModelInferResponse]:
...
Import
from rfserver.model import PipeStep, RFModel
I/O Contract
Inputs
PipeStep Constructor
| Name |
Type |
Required |
Description
|
| step_func |
Callable |
Yes |
A function to be used as a pipeline transform step
|
RFModel Constructor
| Name |
Type |
Required |
Description
|
| name |
str |
Yes |
The name of the model for KServe routing
|
predict()
| Name |
Type |
Required |
Description
|
| payload |
Union[Dict, InferRequest, ModelInferRequest] |
Yes |
Dictionary with "instances" key containing image data arrays
|
| headers |
Dict[str, str] |
No |
Optional HTTP headers from the request
|
Outputs
load()
| Name |
Type |
Description
|
| ready |
bool |
Returns True when the model is successfully loaded from pickle
|
predict()
| Name |
Type |
Description
|
| predictions |
Dict |
Dictionary with "predictions" key containing per-class probability lists
|
Usage Examples
Basic Usage
from rfserver.model import RFModel
# Create and load the Random Forest model
model = RFModel("mnist-rf")
model.load()
# Predict on MNIST-like image data (batch of images)
import numpy as np
sample_input = np.random.rand(1, 28, 28, 3).tolist()
result = model.predict({"instances": sample_input})
print(result) # {"predictions": [[0.1, 0.9, ...], ...]}
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.