How deel-ai/xplique works
1. Xplique is a Python explainability toolkit organized as a single distributable package, xplique, with a strong TensorFlow-first core and a narrower PyTorch compatibility layer. The repo is structured around four user-facing analytical domains: attributions, attribution metrics, concepts, and example-based explanations, plus feature visualization and plotting.
2. The shared foundation lives in xplique.commons, xplique.types, and xplique.utils_functions. These modules provide the cross-cutting abstractions that the explanation methods build on: model-call normalization, data conversion, operator composition, callable wrappers, and task-specific helpers for object detection and segmentation. This is the main internal boundary that keeps the higher-level explainers model-agnostic.
3. The attribution subsystem is the largest part of the library. xplique.attributions contains gradient-based, perturbation-based, and sampling-based methods such as Saliency, Grad-CAM, Integrated Gradients, LIME, Kernel SHAP, Occlusion, RISE, SmoothGrad, and global sensitivity methods like Sobol and HSIC. The important architectural pattern is that each explainer exposes a common “explain”/call interface and delegates common mechanics to base.py, gradient_statistics, and global_sensitivity_analysis.
4. Attribution methods are designed around task boundaries rather than a single prediction type. The README and docs show explicit support for classification, regression, object detection, and semantic segmentation, with some methods only valid for specific tasks. Internally this means the attribution layer must adapt target handling, mask generation, and model-output interpretation depending on the task.
5. The metrics subsystem in xplique.metrics consumes explanations as inputs and evaluates their quality against the model and dataset. It covers fidelity, stability, complexity, randomization, and representativity metrics, with implementations such as Deletion, Insertion, Average Drop/Increase/Gain, MuFidelity, MeGe, and model randomization. Architecturally, this is a downstream consumer of attribution outputs rather than a parallel explanation generator.
6. The concepts subsystem in xplique.concepts extracts human-interpretable concepts from models. CAV and TCAV operate on positive/negative concept datasets, while CRAFT is split into TensorFlow and PyTorch variants and uses a manager layer to coordinate concept extraction, importance estimation, and crop visualization. This module bridges model internals and external concept datasets, so its main boundary is between representation extraction and concept scoring.
7. The example-based subsystem in xplique.example_based explains a prediction by searching a dataset for relevant examples. It is divided into dataset harmonization/conversion utilities, projection methods, and search methods for similar examples, counterfactuals, semifactuals, and prototypes. The core data flow here is query sample + reference dataset -> embedding/projection or similarity space -> ranked example outputs.
8. Feature visualization lives in xplique.features_visualizations and is centered on optimization over inputs. It combines objectives, losses, regularizers, transformations, and preconditioning to synthesize inputs that maximize neurons, channels, layers, or custom objectives. This subsystem is optimizer-driven rather than model-output driven, so its main boundary is input synthesis rather than post-hoc interpretation.
9. xplique.plots is a presentation layer for results from the other subsystems. It includes image, tabular, timeseries, and metrics plotting helpers, indicating that outputs are expected to be rendered in multiple modalities, not just images. This module sits at the edge of the library and converts explanation/metric objects into human-readable visualizations.
10. PyTorch support is intentionally isolated in xplique.wrappers.pytorch.TorchWrapper and in the PyTorch-specific branches of several explainers/metrics. The README makes clear that TensorFlow remains the primary runtime, while PyTorch is supported through wrappers and selective method implementations. That makes the framework boundary explicit: the core abstractions normalize model access, and the wrapper adapts PyTorch models into that contract.
11. The tests mirror the package structure very closely, which is a useful indicator of the architecture. There are dedicated test suites for attributions, commons, concepts, example-based methods, feature visualizations, metrics, plots, wrappers, and utilities. This suggests each subsystem is intended to be independently verifiable and loosely coupled through shared abstractions.
12. Tooling and delivery are handled as a standard Python package with strong project hygiene. pyproject.toml, tox.ini, Makefile, and .pre-commit-config.yaml support packaging, linting, and local automation; GitHub Actions handle tests, linting, torch-specific checks, documentation deployment, and publishing to PyPI. Documentation is a major part of the repo, generated from mkdocs.yml and the docs/ tree, with API pages organized exactly by subsystem.
Main entry points: plots/__init__.py Plots · attributions/__init__.py Attributions · base.py Attr base · commons/__init__.py Commons
Components 20
Core foundation
Shared abstractions, type helpers, and runtime adapters used across explainers.
- Commons · Model-call normalization, data conversion, operators, and callable wrappers.
xplique/commons/__init__.py - Types · Shared custom types and runtime contracts.
xplique/types/__init__.py - Utilities · Task-specific helpers for object detection and segmentation.
xplique/utils_functions/__init__.py - Torch wrapper · PyTorch-to-core adapter that normalizes model access.
xplique/wrappers/pytorch.py
Explanation engines
Primary analytical subsystems that generate explanations or scores.
- Attributions · User-facing attribution entrypoint spanning gradient, perturbation, and sampling methods.
xplique/attributions/__init__.py - Attr base · Common explain/call mechanics shared by attribution methods.
xplique/attributions/base.py - Grad stats · SmoothGrad, VarGrad, and related gradient-statistics explainers.
xplique/attributions/gradient_statistics/__init__.py - GSA · Sobol and HSIC attribution machinery with samplers, estimators, and perturbations.
xplique/attributions/global_sensitivity_analysis/__init__.py - Metrics · Downstream quality metrics for explanations.
xplique/metrics/__init__.py - Metric base · Shared evaluation scaffolding for fidelity, stability, complexity, and randomization.
xplique/metrics/base.py - Concepts · User-facing concept extraction and scoring entrypoint.
xplique/concepts/__init__.py - Concept base · Shared concept-extraction and scoring abstractions.
xplique/concepts/base.py - CRAFT mgr · Coordinates concept extraction, importance estimation, and crop visualization.
xplique/concepts/craft_manager.py - Examples · User-facing similar-example, counterfactual, semifactual, and prototype methods.
xplique/example_based/__init__.py - Example base · Shared query-to-dataset explanation workflow.
xplique/example_based/base_example_method.py - Projections · Projection and harmonization layer for reference data and queries.
xplique/example_based/projections/__init__.py - Search · Nearest-neighbor, prototype, and counterfactual search strategies.
xplique/example_based/search_methods/__init__.py
Input synthesis
Optimization-driven feature visualization pipeline.
- Feature viz · Optimization-driven input synthesis entrypoint.
xplique/features_visualizations/__init__.py - Optimizer · Objectives, losses, regularizers, transformations, and preconditioning.
xplique/features_visualizations/optim.py
Presentation
Plotting and reporting layer for explanation outputs.
- Plots · Rendering helpers for images, tables, timeseries, and metrics.
xplique/plots/__init__.py
Connections 22
- Torch wrapper → Commons: adapts to · PyTorch models are normalized into the shared model interface.
- Utilities → Commons: extends · Task helpers build on the shared callable and conversion utilities.
- Types → Commons: supports · Type definitions back the core contracts used throughout the package.
- Attributions → Attr base: routes through · The attribution API delegates common execution to the base layer.
- Attr base → Commons: depends on · Attribution methods rely on common model and data abstractions.
- Grad stats → Attr base: specializes · Gradient-statistics explainers reuse the attribution base mechanics.
- GSA → Attr base: specializes · Global-sensitivity methods share the attribution base contract.
- Attributions → Grad stats: exposes · Gradient-statistics methods are part of the public attribution surface.
- Attributions → GSA: exposes · Global sensitivity methods are part of the public attribution surface.
- Metrics → Metric base: routes through · Metric entrypoints delegate to shared evaluation scaffolding.
- Metric base → Attributions: consumes · Metrics evaluate explanation outputs produced by attribution methods.
- Concepts → Concept base: routes through · Concept methods share a common concept-extraction foundation.
- CRAFT mgr → Concept base: coordinates · CRAFT management builds on the concept base abstractions.
- Examples → Example base: routes through · Example-based methods share the query-and-search workflow.
- Example base → Projections: uses · Example explanations often project data into a comparison space.
- Example base → Search: uses · Ranked example selection is handled by search strategies.
- Feature viz → Optimizer: drives · Feature visualization is optimization-driven around objectives and regularization.
- Plots → Attributions: renders · Plots visualize explanation outputs from the analytical subsystems.
- Plots → Metrics: renders · Plots also present evaluation metrics.
- Plots → Examples: renders · Plots cover example-based explanation outputs as well.
- Plots → Concepts: renders · Plots can present concept-analysis results.
- Plots → Feature viz: renders · Plots support synthesized feature-visualization outputs.