diff --git a/README.md b/README.md index 35e24655..fbc3c66b 100644 --- a/README.md +++ b/README.md @@ -287,6 +287,7 @@ TF : Tensorflow compatible | Testing CAV (TCAV) | TF | [Paper](https://arxiv.org/pdf/1711.11279.pdf) | | (WIP) Robust TCAV | | | (WIP) Automatic Concept Extraction (ACE) | +| Concept Recursive Activation FacTorization for Explainability (Craft) | TF | [Paper](https://arxiv.org/pdf/2211.10154.pdf) | TF : Tensorflow compatible diff --git a/docs/api/concepts/craft.md b/docs/api/concepts/craft.md new file mode 100644 index 00000000..4e628d0e --- /dev/null +++ b/docs/api/concepts/craft.md @@ -0,0 +1,50 @@ +# CRAFT + +CRAFT or Concept Recursive Activation FacTorization for Explainability is a method for automatically extracting human-interpretable concepts from deep networks. + +This concept activations factorization method aims to explain a pre-trained model's decisions both on a per-class and per-image basis by highlighting both "what" the model saw and “where” it saw it. + +It is made up from 3 ingredients: +1. a method to recursively decompose concepts into sub-concepts +2. a method to better estimate the importance of extracted concepts +3. a method to use any attribution method to create concept attribution maps, using implicit differentiation + +CRAFT requires splitting the model in two parts: $(g, h)$ such that $f(x) = (g \cdot h)(x)$. To put it simply, $g$ is the function that maps our input to the latent space (the penultimate layer of our model), and $h$ is the function that maps this penultimate layer to the output. It is important to note that if the model contains a global average pooling layer, it is strongly recommended to provide CRAFT with the layer before the global average pooling. + +!!!warning + Please keep in mind that the activations must be positives (after relu or any positive activation function) + + +## Example + +```python +from xplique.concepts import CraftTf as Craft +from xplique.plots.craft_visualizations import CraftPlot + +# cut the model in two parts (as explained in the paper) +# first part is g(.) our 'input_to_latent' model, second part is h(.) our 'latent_to_logit' model +g = tf.keras.Model(model.input, model.layers[-3].output) +h = tf.keras.Model(model.layers[-2].input, model.layers[-1].output) + +# Create a Craft concept extractor from these 2 models +craft = Craft(input_to_latent = g, + latent_to_logit = h, + number_of_concepts = 10, + patch_size = 80, + batch_size = 64) + +# Use Craft to get the crops (crops), the embedding of the crops (crops_u), and the concept bank (w) +# 330 is the rabbit class id in imagenet +crops, crops_u, w = craft.fit(images_preprocessed, class_id=rabbit_class) + +# Compute Sobol indices to understand which concept matters +importances = craft.estimate_importance(nb_most_important_concepts=5) + +# Display those concepts by showing the 10 best crops for each concept +craft.plot_concepts_crops(nb_crops=10) + +``` + +{{xplique.concepts.craft.Craft}} + +[^1]: [CRAFT: Concept Recursive Activation FacTorization for Explainability (2023).](https://arxiv.org/pdf/2211.10154.pdf) diff --git a/docs/assets/craft.jpeg b/docs/assets/craft.jpeg new file mode 100644 index 00000000..743144a0 Binary files /dev/null and b/docs/assets/craft.jpeg differ diff --git a/docs/index.md b/docs/index.md index f249772f..28ab6df2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -85,6 +85,17 @@ Finally, the _Metrics_ module covers the current metrics used in explainability.

+ - [**Concepts Methods**: CRAFT: Getting started on Tensorflow](https://colab.research.google.com/drive/1Fk0EObQQfwAtiiFQyClX31HiHgrEzN9_) + [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1Fk0EObQQfwAtiiFQyClX31HiHgrEzN9_) + , or [on Pytorch](https://colab.research.google.com/drive/1_8l5bwzalKBvFkrv5Lvcph97FtUdd3Wp) + [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1_8l5bwzalKBvFkrv5Lvcph97FtUdd3Wp) + +

+ + + +

+ - [**Feature Visualization**: Getting started](https://colab.research.google.com/drive/1st43K9AH-UL4eZM1S4QdyrOi7Epa5K8v) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1st43K9AH-UL4eZM1S4QdyrOi7Epa5K8v) - [**Feature Visualization**: Getting started](https://colab.research.google.com/drive/1st43K9AH-UL4eZM1S4QdyrOi7Epa5K8v) [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1st43K9AH-UL4eZM1S4QdyrOi7Epa5K8v) @@ -267,7 +278,7 @@ Now that Xplique is installed, here are 4 basic examples of what you can do with | Testing CAV (TCAV) | TF | [Paper](https://arxiv.org/pdf/1711.11279.pdf) | | (WIP) Robust TCAV | | | (WIP) Automatic Concept Extraction (ACE) | - + | Concept Recursive Activation FacTorization for Explainability (Craft) | TF | [Paper](https://arxiv.org/pdf/2211.10154.pdf) | TF : Tensorflow compatible ??? abstract "Table of Feature Visualization methods available" diff --git a/docs/tutorials.md b/docs/tutorials.md index 6dd83321..1b70609c 100644 --- a/docs/tutorials.md +++ b/docs/tutorials.md @@ -60,7 +60,10 @@ Here is the lists of the availables tutorial for now: ## Concepts extraction -**WIP** +| Category | **Tutorial Name** | Notebook | +|:------------- | :--------------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------------: | +| CRAFT | CRAFT Tensorflow | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1Fk0EObQQfwAtiiFQyClX31HiHgrEzN9_) | +| CRAFT | CRAFT Pytorch | [![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1_8l5bwzalKBvFkrv5Lvcph97FtUdd3Wp) | ## Features Visualizations diff --git a/mkdocs.yml b/mkdocs.yml index 3a2bfdb2..0f504121 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -39,6 +39,7 @@ nav: - Concept based: - Cav: api/concepts/cav.md - Tcav: api/concepts/tcav.md + - Craft: apt/concepts/craft.md - Feature visualization: - Modern Feature Visualization (MaCo): api/feature_viz/maco.md - Feature visualization: api/feature_viz/feature_viz.md