Notebook helpers
Sentences, charts and images drawn the way the dashboard draws them, for tables, photos and network activations.
Three modules turn records into sentences and charts, drawn and worded the way the dashboard shows them. They import lazily: import kaimei.tabular loads neither pandas nor matplotlib until a function needs them.
| Module | Functions | For |
|---|---|---|
kaimei.tabular | explanation_summary, plot_explanation (waterfall from the base value), counterfactual_summary, plot_counterfactual, compare_explanations (up to three models side by side), plot_insight, plot_insights | Models whose inputs are table rows. Pass model= to add units and the output's name. |
kaimei.images | load_image (resize-and-centre-crop like torchvision, EXIF rotation, the feature's value range), explanation_summary, render_explanation (heatmap, spotlight or original), evidence_images, numbered_regions, focus_mask, region_mask, overlay_heatmap, plot_explanation | Image models. Byte for byte the dashboard's rendering, NumPy and Pillow only. |
kaimei.activations | decode, stage, feature_maps, saliency, unit_values, class_attention, plot_activations (the dashboard's Sight strip) | What every layer computed, from client.models.activations. |
from kaimei.tabular import explanation_summary, plot_explanation, plot_insight
model = client.models.retrieve("titanic-forest")
explanation = client.explanations.create(
model=model.slug, input=passenger, method="shap",
options={"counterfactual": True, "frozen_features": ["Sex", "Age"]},
)
print(explanation_summary(explanation, model=model))
# The model predicts died: P(survived) = 7.4%, against 41.1% on average over the reference data.
# Pulling P(survived) down: Sex = male (−12.1 pts), Title = Mr (−11.0 pts), Pclass = 3 (−4.5 pts).
plot_explanation(explanation, model=model)
for insight in client.insights.list(model=model.slug).data:
plot_insight(insight, model=model)from kaimei.images import load_image, render_explanation, explanation_summary, plot_explanation
model = client.models.retrieve("cats-dogs-photos")
photo = load_image("my_dog.jpg", model=model) # the model's size and value range
explanation = client.explanations.create(model=model.slug, input={"photo": photo}, method="gradcam")
print(explanation_summary(explanation))
# The model looks mostly at the centre of the photo: half of its evidence for dog sits in 21% ...
render_explanation(photo, explanation).save("dog_gradcam.png")
render_explanation(photo, explanation, mode="spotlight").save("dog_spotlight.png")
plot_explanation(photo, explanation) # the dashboard's Explain viewfrom kaimei.activations import class_attention, feature_maps, plot_activations, saliency, stage
result = client.models.activations("cats-dogs-photos", input={"photo": photo}, channels=8)
last = stage(result) # the last detailed feature map
maps = feature_maps(last) # {channel: (h, w) array}, strongest first
where = saliency(last) # (h, w): mean |activation| over channels
plot_activations(result, image=photo)
focused = client.models.activations("cats-dogs-photos", input={"photo": photo},
nodes=[last["node_id"]], cam=[last["node_id"]], target="dog")
heat = class_attention(stage(focused, last["node_id"])) # (H, W) Grad-CAM in [0, 1]