XAIToolkit — Explain any PyTorch CNN in minutes
A lightweight, batteries-included toolkit to explain image-classification models (your own CNN, torchvision models, or timm models) using:
- Gradient-based XAI: Saliency, SmoothGrad, Integrated Gradients
- Model-agnostic XAI: RISE, Occlusion
- Local surrogate XAI: LIME-Stratified (superpixels) (stable neighborhood sampling)
- Region / tree-style XAI: Axis-aligned SHAP-like attributions (rectangle partitioning)
This repo starts with a teaching-first notebook and also ships as a small Python package + CLI.
Quick start
1) Install (editable for development)
pip install -e .
2) Run the notebook
Open:
notebooks/01_grad_based_xai.ipynb
It loads a CNN, predicts Top-5 classes, and visualizes three gradient-based explainers in a 2×3 figure.
3) Explain an image from the CLI
Torchvision model:
xai-explain --image assets/cat_dog.jpg --model tv:resnet50 --methods saliency smoothgrad ig --outdir outputs
timm model (pretrained):
xai-explain --image assets/flamingo.jpg --model timm:swin_tiny_patch4_window7_224 --methods rise lime_strat shap_axis --outdir outputs
Your checkpoint + architecture:
xai-explain --image assets/cat_dog.jpg --model ckpt:checkpoints/best.pt --arch tv:resnet50 --methods rise occlusion --outdir outputs
What gets saved
For each method:
outputs/<method>_heatmap.pngoutputs/<method>_overlay.png
Plus outputs/original.png.
Methods
Gradient-based
saliency: ∂logit/∂input (absolute, channel-mean)smoothgrad: noise-averaged saliencyig: Integrated Gradients (manual implementation, no Captum)
Model-agnostic
rise: Randomized Input Sampling for Explanation (RISE)occlusion: Sliding-window occlusion sensitivity
Surrogate / region-based
lime_strat: LIME Image using stratified sampling of the neighborhood (bins on model output)shap_axis: Axis-aligned SHAP-like attributions using hierarchical rectangle splits
Each method file includes references and canonical links at the top.
Bring your own model
You can load models in four ways:
tv:<name>— torchvision, e.g.tv:resnet50timm:<name>— timm pretrained modelsckpt:<path>+--arch tv:<name>|timm:<name>— load checkpoint into a known architecturepy:<file.py>:<factory_fn>— load a custom model factory that returnstorch.nn.Module
Project layout
src/xaitoolkit/— package codescripts/— small wrappers (CLI lives here)notebooks/— teaching notebook(s)assets/— demo imagesoutputs/— generated artifacts (gitignored)
Citation & credits
- ResNet: https://arxiv.org/abs/1512.03385
- Integrated Gradients: https://arxiv.org/abs/1703.01365
- SmoothGrad: https://arxiv.org/abs/1706.03825
- Grad-CAM: https://arxiv.org/abs/1610.02391
- RISE: https://arxiv.org/abs/1806.07421
Metadata
Release files for xaitoolkit 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| xaitoolkit-0.1.1.tar.gz | 14.5 kB | Without provenance | Details for xaitoolkit-0.1.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| xaitoolkit-0.1.1-py3-none-any.whl | Python 3 | none | any | Details for xaitoolkit-0.1.1-py3-none-any.whl |
Total release size: 32.9 kB
Release files / xaitoolkit-0.1.1.tar.gz
| Download URL | xaitoolkit-0.1.1.tar.gz |
|---|---|
| Size | 14.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
18eccec47567998237925d13d2c4b68d12d743802842311598a5c091ff1a7b7b
|
|
BLAKE2b-256 checksum How to use checksums |
4cdca33ef6006b2817cf35e58784f973b50abe3cece2a1ba13abf5ace7ef51e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.0
|
Release files / xaitoolkit-0.1.1-py3-none-any.whl
| Download URL | xaitoolkit-0.1.1-py3-none-any.whl |
|---|---|
| Size | 18.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
523ab65fb76ff052b2cc84a491993ef481019a330d2e6a3ee77be7b327a527bb
|
|
BLAKE2b-256 checksum How to use checksums |
fa41a286116c817b3ef42cdef7a11f42cfa62912feaadadf95c978a5fde0a82e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.0
|
Security for xaitoolkit 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.