Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 56 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

<div align="center">
<a href="#">
<img src="https://img.shields.io/badge/Python-3.10, 3.11, 3.12, 3.13, 3.14-efefef">
<img src="https://img.shields.io/badge/Python-3.10, 3.11, 3.12, 3.13-efefef">
</a>
<a href="#">
<img src="https://img.shields.io/badge/Tensorflow-2.5, ..., 2.15-00008b">
<img src="https://img.shields.io/badge/Tensorflow-2.18, 2.19, 2.20-00008b">
</a>
<a href="https://github.com/deel-ai/xplique/actions/workflows/python-lints.yml">
<img alt="PyLint" src="https://github.com/deel-ai/xplique/actions/workflows/python-lints.yml/badge.svg">
Expand Down Expand Up @@ -45,26 +45,23 @@
·
<a href="https://deel-ai.github.io/xplique/latest/api/attributions/metrics/api_metrics/">Metrics</a>
.
<a href="api/example_based/api_example_based/">Example-based</a>
<a href="https://deel-ai.github.io/xplique/latest/api/example_based/api_example_based/">Example-based</a>
</p>

> [!IMPORTANT]
> With the release of Keras 3.X since TensorFlow 2.16, some methods may not function as expected. We are actively working on a fix. In the meantime, we recommend using TensorFlow 2.15 or earlier versions for optimal compatibility.
> [!NOTE]
> **What's new in v2.0.0:** Holistic CRAFT can extract and attribute concepts from classification and object detection models through `HolisticCraftTf` and `HolisticCraftTorch`. The release also introduces framework-specific latent extractors, `LayeredModelExtractorBuilder`, `PartialExplainer`, and `EncodedData`. Xplique 2.0.0 supports Python 3.10-3.13, TensorFlow 2.18-2.20, and optional PyTorch 2.5-2.10. Detector-specific adapters are under construction in the DEEL AI organization and will be pip-installable soon.

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.
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 _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.

<p align="center" width="100%">
<img width="95%" src="./docs/assets/modules.png">
</p>

<br>

> [!NOTE]
> We are proud to announce the release of the _Example-based_ module! This module is dedicated to methods that explain a model by retrieving relevant examples from a dataset. It includes methods that belong to different families: similar examples, contrastive (counter-factuals and semi-factuals) examples, and prototypes (as concepts based methods have a dedicated sections).

## 🔥 Tutorials

<details>
Expand Down Expand Up @@ -109,6 +106,8 @@ Finally, the _Metrics_ module covers the current metrics used in explainability.
- [**Concepts Methods**: CRAFT: Getting started on Pytorch](https://colab.research.google.com/drive/16Jn2pQy4gi2qQYZFnuW6ZNtVAYiNyJHO)
<sub> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/16Jn2pQy4gi2qQYZFnuW6ZNtVAYiNyJHO) </sub>

- [**Concepts Methods**: Holistic CRAFT (TensorFlow and PyTorch)](https://deel-ai.github.io/xplique/latest/api/concepts/holistic_craft/)

<p align="center" width="100%">
<a href="https://colab.research.google.com/drive/1jmyhb89Bdz7H4G2KfK8uEVbSC-C_aht_">
<img width="95%" src="./docs/assets/craft.jpeg">
Expand All @@ -133,12 +132,18 @@ included. We will try to cover all the possible usage of the library, feel free

## 🚀 Quick Start

Xplique requires a version of python higher than 3.7 and several libraries including Tensorflow and Numpy. Installation can be done using Pypi:
Xplique supports Python 3.10 through 3.13 and several libraries including TensorFlow and NumPy. Installation can be done using PyPI:

```python
pip install xplique
```

For PyTorch models and concept methods, install the optional dependencies:

```bash
pip install "xplique[torch]"
```

Now that Xplique is installed, here are basic examples of what you can do with the available modules.

<details>
Expand Down Expand Up @@ -229,6 +234,41 @@ craft.plot_concepts_crops(nb_crops=10)

More information in the [CRAFT documentation](https://deel-ai.github.io/xplique/latest/api/concepts/craft/).

### Holistic CRAFT

Holistic CRAFT works directly with full activation maps and can be used with classification models through the built-in layered extractor. Object-detection-specific extractor adapters are under construction and will be pip-installable soon.

```python
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, use the architecture-specific extractor builders from the companion `xplique-adapters` package when it becomes available:

```python
# xplique-adapters is under construction and will be pip-installable soon.
from xplique.concepts import HolisticCraftTorch
from xplique_adapters.concepts.torch.latent_data_retinanet import RetinanetExtractorBuilder

latent_extractor = RetinanetExtractorBuilder.build(
model,
device="cuda",
nb_classes=91,
extraction_location="resnet",
extraction_layer=-1,
)
craft = HolisticCraftTorch(latent_extractor, number_of_concepts=10, device="cuda")
craft.fit(images, class_id=class_id)
```

</details>

<details>
Expand Down Expand Up @@ -283,7 +323,7 @@ Want to know more ? Check the [PyTorch documentation](https://deel-ai.github.io/

## 📦 What's Included

There are 4 modules in Xplique, [Attribution methods](https://deel-ai.github.io/xplique/latest/api/attributions/api_attributions/), [Attribution metrics](https://deel-ai.github.io/xplique/latest/api/attributions/metrics/api_metrics/), [Concepts](https://deel-ai.github.io/xplique/latest/api/concepts/cav/), and [Feature visualization](https://deel-ai.github.io/xplique/latest/api/feature_viz/feature_viz/). In particular, the attribution methods module supports a huge diversity of tasks:[Classification](https://deel-ai.github.io/xplique/latest/api/attributions/classification/), [Regression](https://deel-ai.github.io/xplique/latest/api/attributions/regression/), [Object Detection](https://deel-ai.github.io/xplique/latest/api/attributions/object_detection/), and [Semantic Segmentation](https://deel-ai.github.io/xplique/latest/api/attributions/semantic_segmentation/). For diverse data types: [Images, Time Series, and Tabular data](https://deel-ai.github.io/xplique/latest/api/attributions/api_attributions/). The methods compatible with such task are highlighted in the following table:
There are 5 modules in Xplique: [Attribution methods](https://deel-ai.github.io/xplique/latest/api/attributions/api_attributions/), [Attribution metrics](https://deel-ai.github.io/xplique/latest/api/attributions/metrics/api_metrics/), [Concepts](https://deel-ai.github.io/xplique/latest/api/concepts/cav/), [Feature visualization](https://deel-ai.github.io/xplique/latest/api/feature_viz/feature_viz/), and [Example-based methods](https://deel-ai.github.io/xplique/latest/api/example_based/api_example_based/). In particular, the attribution methods module supports a huge diversity of tasks:[Classification](https://deel-ai.github.io/xplique/latest/api/attributions/classification/), [Regression](https://deel-ai.github.io/xplique/latest/api/attributions/regression/), [Object Detection](https://deel-ai.github.io/xplique/latest/api/attributions/object_detection/), and [Semantic Segmentation](https://deel-ai.github.io/xplique/latest/api/attributions/semantic_segmentation/). For diverse data types: [Images, Time Series, and Tabular data](https://deel-ai.github.io/xplique/latest/api/attributions/api_attributions/). Holistic CRAFT additionally supports concept-based explanations for object detection models. The methods compatible with such task are highlighted in the following table:


<details>
Expand Down Expand Up @@ -341,7 +381,6 @@ OD : [Object Detection](https://deel-ai.github.io/xplique/latest/api/attribution
| Sparseness | TF, PyTorch** | Complexity | [Paper](https://proceedings.mlr.press/v119/chalasani20a.html) |
| RandomLogitMetric | TF, PyTorch** | Randomization | [Paper](https://arxiv.org/abs/1810.03292) |
| ModelRandomizationMetric| TF, PyTorch** | Randomization | [Paper](https://arxiv.org/abs/1810.03292) |
| (WIP) e-robustness |

TF : Tensorflow compatible

Expand All @@ -358,12 +397,12 @@ TF : Tensorflow compatible
| Testing CAV (TCAV) | TF | [Paper](https://arxiv.org/pdf/1711.11279.pdf) | |
| CRAFT Tensorflow | TF | [Paper](https://arxiv.org/pdf/2211.10154.pdf) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1jmyhb89Bdz7H4G2KfK8uEVbSC-C_aht_) |
| CRAFT PyTorch | PyTorch** | [Paper](https://arxiv.org/pdf/2211.10154.pdf) | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/16Jn2pQy4gi2qQYZFnuW6ZNtVAYiNyJHO) |
| (WIP) Robust TCAV | | | |
| (WIP) Automatic Concept Extraction (ACE) | | |
| Holistic CRAFT TensorFlow | TF | [Holistic paper](https://arxiv.org/pdf/2306.07304.pdf) | [Documentation](https://deel-ai.github.io/xplique/latest/api/concepts/holistic_craft/) |
| Holistic CRAFT PyTorch | PyTorch** | [Holistic paper](https://arxiv.org/pdf/2306.07304.pdf) | [Documentation](https://deel-ai.github.io/xplique/latest/api/concepts/holistic_craft/) |

TF : Tensorflow compatible

** : See the [Xplique for Pytorch documentation](https://deel-ai.github.io/xplique/latest/pytorch/), and the [**PyTorch's model**: Getting started](https://colab.research.google.com/drive/1bMlO29_0K3YnTQBbbyKQyRfo8YjvDbhe)<sub> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bMlO29_0K3YnTQBbbyKQyRfo8YjvDbhe) </sub> notebook
** : See the [Xplique for PyTorch documentation](https://deel-ai.github.io/xplique/latest/api/attributions/pytorch/), and the [**PyTorch's model**: Getting started](https://colab.research.google.com/drive/1bMlO29_0K3YnTQBbbyKQyRfo8YjvDbhe)<sub> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1bMlO29_0K3YnTQBbbyKQyRfo8YjvDbhe) </sub> notebook

</details>

Expand All @@ -385,7 +424,7 @@ TF : Tensorflow compatible

</details>

Even though we are only at the early stages, we have also recently added an [Example-based methods](api/example_based/api_example_based/) module. Do not hesitate to give us feedback! Currently, the methods available are summarized in the following table:
The [Example-based methods](https://deel-ai.github.io/xplique/latest/api/example_based/api_example_based/) module provides several families of example-based explanations. Feedback and contributions are welcome. The currently available methods are summarized in the following table:

<details>
<summary><b>Table of example-based methods available</b></summary>
Expand Down
10 changes: 8 additions & 2 deletions TUTORIALS.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ Here is the lists of the available tutorial for now:
| Fidelity | Average Drop/Increase/Gain | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1nGP13qiQrsJMBx8TXgA69D-5ALoP3l9p) |
| Complexity | Complexity | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/13boAsXGVKS0LaNzslOdjSkYIrpBJdh7K) |
| Randomization | Randomization | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/13lNkZqKajRJ63XllkQddgrPOYF1Xv9-Y) |
| Stability | AverageStability | **(WIP)** |
| Stability | AverageStability | [Documentation](docs/api/attributions/metrics/avg_stability.md) |

## PyTorch Wrapper

Expand All @@ -72,7 +72,13 @@ Here is the lists of the available tutorial for now:

## Concepts extraction

**WIP**
| Category | **Tutorial Name** | Documentation / Notebook |
|:------------- | :--------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------: |
| Labelled concept methods | CAV + TCAV | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1iuEz46ZjgG97vTBH8p-vod3y14UETvVE) |
| Automatic concept extraction | CRAFT TensorFlow | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1jmyhb89Bdz7H4G2KfK8uEVbSC-C_aht_) |
| Automatic concept extraction | CRAFT PyTorch | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/16Jn2pQy4gi2qQYZFnuW6ZNtVAYiNyJHO) |
| Automatic concept extraction | Holistic CRAFT TensorFlow | [Documentation](docs/api/concepts/holistic_craft.md) |
| Automatic concept extraction | Holistic CRAFT PyTorch | [Documentation](docs/api/concepts/holistic_craft.md) |

## Feature Visualization

Expand Down
5 changes: 4 additions & 1 deletion docs/api/attributions/object_detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

[Attributions: Object Detection tutorial](https://colab.research.google.com/drive/1X3Yq7BduMKqTA0XEheoVIpOo3IvOrzWL) <sub> [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1X3Yq7BduMKqTA0XEheoVIpOo3IvOrzWL) </sub>

!!! tip "Concept-based object detection explanations"
For concept-level explanations of object detection models, see [Holistic CRAFT](../concepts/holistic_craft.md). Its architecture-specific extractors cover RetinaNet, Faster R-CNN, FCOS, SSD, YOLO, and DETR through the companion `xplique-adapters` package, which is currently under construction and will be pip-installable soon.




Expand Down Expand Up @@ -241,4 +244,4 @@ explainer = Saliency(model, operator=custom_operator)
... # All following steps are the same as the examples
```

[^1] [Black-box Explanation of Object Detectors via Saliency Maps (2021)](https://arxiv.org/pdf/2006.03204.pdf)
[^1] [Black-box Explanation of Object Detectors via Saliency Maps (2021)](https://arxiv.org/pdf/2006.03204.pdf)
8 changes: 7 additions & 1 deletion docs/api/attributions/pytorch.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- Other tutorials applying Xplique to PyTorch models: [Attributions: Object Detection](https://colab.research.google.com/drive/1X3Yq7BduMKqTA0XEheoVIpOo3IvOrzWL), [Attributions: Semantic Segmentation](https://colab.research.google.com/drive/1AHg7KO1fCOX5nZLGZfxkZ2-DLPPdSfbX)

- For concept-based explanations of PyTorch models, see [Holistic CRAFT](../concepts/holistic_craft.md), including its object detection support.

!!!note
We should point out that what we did with PyTorch should be possible for other frameworks. Do not hesitate to give it a try and to make a PR if you have been successful!

Expand Down Expand Up @@ -44,7 +46,7 @@ score_saliency = metric(explanations)

## Does it work for every module?

It has been tested on both the `attributions` and the `metrics` modules.
It has been tested on the `attributions`, `metrics`, and `concepts` modules. Holistic CRAFT adds a native PyTorch path for concept extraction, including object detection models.



Expand Down Expand Up @@ -82,6 +84,10 @@ Not yet, but it works for most of them (even for gradient-based ones!):

It works for all tasks covered by Xplique, see [the tasks covered and how to specify them](api_attributions.md#the-tasks-covered).

### Concept-based explanations

PyTorch classification and object detection models can be explained with `HolisticCraftTorch` through the latent extractor builders described in the [Holistic CRAFT documentation](../concepts/holistic_craft.md). Detector-specific builders are provided by the `xplique-adapters` companion package, which is under construction and will be pip-installable soon.




Expand Down
4 changes: 4 additions & 0 deletions docs/api/concepts/cav.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ cav = cav_renderer(positive_examples, random_examples)

```

## Related concept methods

For automatic concept extraction, see [CRAFT](craft.md) and [Holistic CRAFT](holistic_craft.md).

{{xplique.concepts.cav.Cav}}

[^1]: [Interpretability Beyond Feature Attribution: Quantitative Testing with Concept Activation Vectors (TCAV) (2018).](https://arxiv.org/abs/1711.11279)
3 changes: 3 additions & 0 deletions docs/api/concepts/craft.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ The concepts will be extracted from this latent space.
!!!warning
Please keep in mind that the activations must be positives (after relu or any positive activation function)

!!! tip "When to use Holistic CRAFT"
Regular CRAFT extracts concepts from image crops and is a good fit for classification datasets. For object detection, scenes with multiple objects, or models where global spatial context matters, use [Holistic CRAFT](holistic_craft.md), which factorizes full activation maps instead.


## Example

Expand Down
Loading
Loading