Overview
Concrete tool for generating anchor-based explanations for image classification models using the Alibi library provided by the KServe sample code.
Description
AnchorImages extends ExplainerWrapper and wraps the Alibi AnchorImage explainer. During construction, it receives a prediction function and a pre-built Alibi AnchorImage explainer instance. The explain() method converts input data to a NumPy array, checks whether the predictor returns class labels or probabilities (applying ArgmaxTransformer if needed to ensure class predictions), and then calls the Alibi anchor image explanation algorithm on the first image in the batch. The result identifies which image regions (superpixels) are sufficient for the classification decision.
Usage
Use this class as part of the Alibi explainer server pipeline when you need to generate visual explanations for image classification models, showing which regions of an image drive the model's prediction.
Code Reference
Source Location
Signature
class AnchorImages(ExplainerWrapper):
def __init__(
self,
predict_fn: Callable,
explainer: Optional[alibi.explainers.AnchorImage],
**kwargs,
):
...
def explain(self, inputs: List, headers: Dict[str, str] = None) -> Explanation:
...
Import
from alibiexplainer.anchor_images import AnchorImages
I/O Contract
Inputs
Constructor
| Name |
Type |
Required |
Description
|
| predict_fn |
Callable |
Yes |
Prediction function that accepts input arrays and returns predictions
|
| explainer |
Optional[alibi.explainers.AnchorImage] |
Yes |
A pre-built Alibi AnchorImage explainer instance (must not be None)
|
| **kwargs |
dict |
No |
Additional keyword arguments passed to the Alibi explain call
|
explain()
| Name |
Type |
Required |
Description
|
| inputs |
List |
Yes |
List of image arrays to explain (first image is used)
|
| headers |
Dict[str, str] |
No |
Optional HTTP headers
|
Outputs
explain()
| Name |
Type |
Description
|
| anchor_exp |
Explanation |
Alibi Explanation object containing anchor regions and metadata
|
Usage Examples
Basic Usage
from alibiexplainer.anchor_images import AnchorImages
import alibi
import numpy as np
# Assume a pre-built AnchorImage explainer and a predict function
def my_predict_fn(images):
# Returns prediction probabilities
return np.array([[0.1, 0.9]])
anchor_image_explainer = alibi.explainers.AnchorImage(predictor=my_predict_fn, image_shape=(28, 28, 3))
wrapper = AnchorImages(predict_fn=my_predict_fn, explainer=anchor_image_explainer)
# Generate an explanation for an image
sample_image = np.random.rand(1, 28, 28, 3).tolist()
explanation = wrapper.explain(sample_image)
print(explanation.anchor)
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.