[!NOTE]
What's new in v2.0: Holistic CRAFT can extract, attribute, and localize concepts (attribute_concepts_to_inputs) across classification and object detection models through HolisticCraftTf and HolisticCraftTorch. The 2.0 series also introduces framework-specific latent extractors, LayeredModelExtractorBuilder, PartialExplainer, and EncodedData, and supports Python 3.10-3.13, TensorFlow 2.18-2.20, and optional PyTorch 2.5-2.10. The optional xplique-adapters companion package is available on PyPI for detector-specific pixel attribution and concept explanations.
The library is composed of several modules, the Attributions Methods module implements various methods (e.g Saliency, Grad-CAM, FEM, Integrated-Gradients...), with explanations, examples and links to official papers.
The Feature Visualization module allows to see how neural networks build their understanding of images by finding inputs that maximize neurons, channels, layers or compositions of these elements.
The Concepts module allows you to extract human concepts from a model and to test their usefulness with respect to a class. Holistic CRAFT extends concept extraction to full activation maps, including object detection models.
Finally, the Metrics module covers the current metrics used in explainability. Used in conjunction with the Attribution Methods module, it allows you to test the different methods or evaluate the explanations of a model. The Example-based module explains predictions by retrieving relevant examples from a dataset.
🔥 Tutorials
You can find a certain number of other practical tutorials just here. This section is actively developed and more contents will be
included. We will try to cover all the possible usage of the library, feel free to contact us if you have any suggestions or recommendations towards tutorials you would like to see.
🚀 Quick Start
Xplique supports Python 3.10 through 3.13 and several libraries including TensorFlow and NumPy. Installation can be done using PyPI:
pip install xplique
For PyTorch models and concept methods, install the optional dependencies:
pip install "xplique[torch]"
For object detection, install the optional companion adapters
with the extra for your model ecosystem:
pip install "xplique-adapters[torchvision]" # RetinaNet, FCOS, Faster R-CNN, SSD
pip install "xplique-adapters[yolo]" # Ultralytics YOLO 11 / YOLO 26
pip install "xplique-adapters[detr]" # HuggingFace DETR
pip install "xplique-adapters[retinanet]" # TensorFlow / KerasCV RetinaNet
Adapters 0.1.0 require Xplique >=2.0.0 and Python 3.10-3.13. Their PyTorch extra
requires PyTorch >=2.6; HuggingFace DETR requires >=2.7 and Transformers 5.x.
See the adapter integration guide
for output formats, model selection, and attribution examples.
Now that Xplique is installed, here are basic examples of what you can do with the available modules.
from xplique.attributions import GradCAM
# load images, labels and model
# ...
explainer = GradCAM(model)
explanations = explainer.explain(images, labels)
# or just `explainer(images, labels)`
All attributions methods share a common API described in the attributions API documentation.
In order to measure if the explanations provided by our method are faithful (it reflects well the functioning of the model) we can use a fidelity metric such as Deletion
from xplique.attributions import GradCAM
from xplique.metrics import Deletion
# load images, labels and model
# ...
explainer = GradCAM(model)
explanations = explainer(inputs, labels)
metric = Deletion(model, inputs, labels)
score_grad_cam = metric(explanations)
All attributions metrics share a common API. You can find out more about it here.
CAV
Concerning the concept-based methods, we can for example extract a concept vector from a layer of a model. In order to do this, we use two datasets, one containing inputs containing the concept: positive_samples, the other containing other entries which do not contain the concept: negative_samples.
from xplique.concepts import Cav
# load a model, samples that contain a concept
# (positive) and samples who don't (negative)
# ...
extractor = Cav(model, 'mixed3')
concept_vector = extractor(positive_samples,
negative_samples)
More information on CAV here and on TCAV here.
CRAFT
Use Craft to investigate a single class.
from xplique.concepts import CraftTf as Craft
# Cut the model in two parts: g and h
# Create a Craft concept extractor from these 2 models
craft = Craft(input_to_latent_model = g,
latent_to_logit_model = h)
# Use Craft to compute the concepts for a specific class
craft.fit(images_preprocessed, class_id=rabbit_class_id)
# Compute Sobol indices to understand which concept matters
importances = craft.estimate_importance()
# Display those concepts by showing the 10 best crops for each concept
craft.plot_concepts_crops(nb_crops=10)
More information in the CRAFT documentation.
Holistic CRAFT
Holistic CRAFT works directly with full activation maps and can be used with classification models through the built-in layered extractor. Detector-specific extractors are provided by the optional xplique-adapters package.
from xplique.concepts import HolisticCraftTf
from xplique.concepts.tf.layered_model_latent_extractor import LayeredModelExtractorBuilder
latent_extractor = LayeredModelExtractorBuilder.build(
model,
split_layer=-3,
batch_size=8,
)
craft = HolisticCraftTf(latent_extractor, number_of_concepts=10)
craft.fit(images)
For object detection, install xplique-adapters[torchvision] and use an architecture-specific extractor:
import torch
from torchvision.models.detection import retinanet_resnet50_fpn
from xplique.concepts import HolisticCraftTorch
from xplique_adapters.concepts.torch.latent_data_retinanet import RetinaNetExtractorBuilder
device = "cuda" if torch.cuda.is_available() else "cpu"
model = retinanet_resnet50_fpn(weights="DEFAULT").eval()
latent_extractor = RetinaNetExtractorBuilder.build(
model,
device=device,
nb_classes=91,
extraction_location="resnet",
extraction_layer=-1,
)
craft = HolisticCraftTorch(latent_extractor, number_of_concepts=10, device=device)
# images: model-ready float32 tensors shaped (N, C, H, W), in [0, 1].
craft.fit(images, class_id=1) # COCO person; fitting stores this as metadata.
Finally, in order to find an image that maximizes a neuron and at the same time a layer, we build two objectives that we combine together. We then call the optimizer which returns our images
from xplique.features_visualizations import Objective
from xplique.features_visualizations import optimize
# load a model...
neuron_obj = Objective.neuron(model, "logits", 200)
channel_obj = Objective.layer(model, "mixed3", 10)
obj = neuron_obj + 2.0 * channel_obj
images, obj_names = optimize(obj)
Want to know more ? Check the Feature Viz documentation
Even though the library was mainly designed to be a Tensorflow toolbox we have been working on a very practical wrapper to facilitate the integration of your PyTorch models into Xplique's framework!
import torch
from xplique.wrappers import TorchWrapper
from xplique.attributions import Saliency
from xplique.metrics import Deletion
# load images, targets and model
# ...
device = 'cuda' if torch.cuda.is_available() else 'cpu'
wrapped_model = TorchWrapper(torch_model, device)
explainer = Saliency(wrapped_model)
explanations = explainer(inputs, targets)
metric = Deletion(wrapped_model, inputs, targets)
score_saliency = metric(explanations)
Want to know more ? Check the PyTorch documentation
📦 What's Included
There are 5 modules in Xplique: Attribution methods, Attribution metrics, Concepts, Feature visualization, and Example-based methods. In particular, the attribution methods module supports a huge diversity of tasks:Classification, Regression, Object Detection, and Semantic Segmentation. For diverse data types: Images, Time Series, and Tabular data. Holistic CRAFT additionally supports concept-based explanations for object detection models. The methods compatible with such task are highlighted in the following table: