Git Product home page Git Product logo

xplique's Introduction

Xplique


🦊 Xplique (pronounced \ɛks.plik\) is a Python toolkit dedicated to explainability. The goal of this library is to gather the state of the art of Explainable AI to help you understand your complex neural network models. Originally built for Tensorflow's model it also works for PyTorch models partially.
📘 Explore Xplique docs | Explore Xplique tutorials 🔥

Attributions · Concept · Feature Visualization · Metrics

The library is composed of several modules, the Attributions Methods module implements various methods (e.g Saliency, Grad-CAM, 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.


🔥 Tutorials

We propose some Hands-on tutorials to get familiar with the library and its api:

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 requires a version of python higher than 3.7 and several libraries including Tensorflow and Numpy. Installation can be done using Pypi:

pip install xplique

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

Attributions Methods Let's start with a simple example, by computing Grad-CAM for several images (or a complete dataset) on a trained model.
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.

Attributions Metrics

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.

Concepts Extraction

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.

Feature Visualization

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

PyTorch with Xplique

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 4 modules in Xplique, Attribution methods, Attribution metrics, Concepts, and Feature visualization. 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. The methods compatible with such task are highlighted in the following table:

Table of attributions available
Attribution Method Type of Model Source Images Time Series and Tabular Data Tutorial
Deconvolution TF Paper C✔️ OD❌ SS❌ C✔️ R✔️ Open In Colab
Grad-CAM TF Paper C✔️ OD❌ SS❌ Open In Colab
Grad-CAM++ TF Paper C✔️ OD❌ SS❌ Open In Colab
Gradient Input TF, PyTorch** Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Guided Backprop TF Paper C✔️ OD❌ SS❌ C✔️ R✔️ Open In Colab
Integrated Gradients TF, PyTorch** Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Kernel SHAP TF, PyTorch**, Callable* Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Lime TF, PyTorch**, Callable* Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Occlusion TF, PyTorch**, Callable* Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Rise TF, PyTorch**, Callable* Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Saliency TF, PyTorch** Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
SmoothGrad TF, PyTorch** Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
SquareGrad TF, PyTorch** Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
VarGrad TF, PyTorch** Paper C✔️ OD✔️ SS✔️ C✔️ R✔️ Open In Colab
Sobol Attribution TF, PyTorch** Paper C✔️ OD✔️ SS✔️ 🔵 Open In Colab
Hsic Attribution TF, PyTorch** Paper C✔️ OD✔️ SS✔️ 🔵 Open In Colab
FORGrad enhancement TF, PyTorch** Paper C✔️ OD✔️ SS✔️ Open In Colab

TF : Tensorflow compatible

C : Classification | R : Regression | OD : Object Detection | SS : Semantic Segmentation (SS)

* : See the Callable documentation

** : See the Xplique for PyTorch documentation, and the PyTorch models: Getting started notebook.

✔️ : Supported by Xplique | ❌ : Not applicable | 🔵 : Work in Progress

Table of attribution's metric available
Attribution Metrics Type of Model Property Source
MuFidelity TF, PyTorch** Fidelity Paper
Deletion TF, PyTorch** Fidelity Paper
Insertion TF, PyTorch** Fidelity Paper
Average Stability TF, PyTorch** Stability Paper
MeGe TF, PyTorch** Representativity Paper
ReCo TF, PyTorch** Consistency Paper
(WIP) e-robustness

TF : Tensorflow compatible

** : See the Xplique for PyTorch documentation, and the PyTorch models: Getting started notebook.

Table of concept methods available
Concepts method Type of Model Source Tutorial
Concept Activation Vector (CAV) TF Paper
Testing CAV (TCAV) TF Paper
CRAFT Tensorflow TF Paper Open In Colab
CRAFT PyTorch PyTorch** Paper Open In Colab
(WIP) Robust TCAV
(WIP) Automatic Concept Extraction (ACE)

TF : Tensorflow compatible

** : See the Xplique for Pytorch documentation, and the PyTorch's model: Getting started Open In Colab notebook

Table of Feature Visualization methods available
Feature Visualization (Paper) Type of Model Details
Neurons TF Optimizes for specific neurons
Layer TF Optimizes for specific layers
Channel TF Optimizes for specific channels
Direction TF Optimizes for specific vector
Fourrier Preconditioning TF Optimize in Fourier basis (see preconditioning)
Objective combination TF Allows to combine objectives
MaCo TF Fixed Magnitude optimisation, see Paper

TF : Tensorflow compatible

👍 Contributing

Feel free to propose your ideas or come and contribute with us on the Xplique toolbox! We have a specific document where we describe in a simple way how to make your first pull request: just here.

👀 See Also

This library is one approach of many to explain your model. We don't expect it to be the perfect solution, we create it to explore one point in the space of possibilities.

Other interesting tools to explain your model:
  • Lucid the wonderful library specialized in feature visualization from OpenAI.
  • Captum the PyTorch library for Interpretability research
  • Tf-explain that implement multiples attribution methods and propose callbacks API for tensorflow.
  • Alibi Explain for model inspection and interpretation
  • SHAP a very popular library to compute local explanations using the classic Shapley values from game theory and their related extensions
To learn more about Explainable AI in general:
More from the DEEL project:
  • deel-lip a Python library for training k-Lipschitz neural networks on TF.
  • deel-torchlip a Python library for training k-Lipschitz neural networks on PyTorch.
  • Influenciae Python toolkit dedicated to computing influence values for the discovery of potentially problematic samples in a dataset.
  • LARD Landing Approach Runway Detection (LARD) is a dataset of aerial front view images of runways designed for aircraft landing phase
  • PUNCC Puncc (Predictive uncertainty calibration and conformalization) is an open-source Python library that integrates a collection of state-of-the-art conformal prediction algorithms and related techniques for regression and classification problems
  • OODEEL OODeel is a library that performs post-hoc deep OOD detection on already trained neural network image classifiers. The philosophy of the library is to favor quality over quantity and to foster easy adoption
  • DEEL White paper a summary of the DEEL team on the challenges of certifiable AI and the role of data quality, representativity and explainability for this purpose.

🙏 Acknowledgments

DEEL Logo
This project received funding from the French ”Investing for the Future – PIA3” program within the Artificial and Natural Intelligence Toulouse Institute (ANITI). The authors gratefully acknowledge the support of the DEEL project.

👨‍🎓 Creators

This library was started as a side-project by Thomas FEL who is currently a graduate student at the Artificial and Natural Intelligence Toulouse Institute under the direction of Thomas SERRE. His thesis work focuses on explainability for deep neural networks.

He then received help from some members of the DEEL team to enhance the library namely from Lucas Hervier and Antonin Poché.

🗞️ Citation

If you use Xplique as part of your workflow in a scientific publication, please consider citing the 🗞️ Xplique official paper:

@article{fel2022xplique,
  title={Xplique: A Deep Learning Explainability Toolbox},
  author={Fel, Thomas and Hervier, Lucas and Vigouroux, David and Poche, Antonin and Plakoo, Justin and Cadene, Remi and Chalvidal, Mathieu and Colin, Julien and Boissin, Thibaut and Bethune, Louis and Picard, Agustin and Nicodeme, Claire 
          and Gardes, Laurent and Flandin, Gregory and Serre, Thomas},
  journal={Workshop on Explainable Artificial Intelligence for Computer Vision (CVPR)},
  year={2022}
}

📝 License

The package is released under MIT license.

xplique's People

Contributors

antoninpoche avatar dejeanph avatar fel-thomas avatar fredericboisnard avatar justinplakoo avatar lucashervier avatar paulnovello avatar s4b1n3 avatar teodorchiaburu avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

xplique's Issues

[Enhancement] - Your suggestion/feature request

Is your feature request related to a problem? Please describe.
Workflow dependant on Python 3.6 is deprecated. We should however cover Python 3.9 and maybe 3.10

Describe the solution you'd like
Update workflow

xplique: handle pytorch and onnx

As a general improvment, we would like to extend the library so that you can work with the PyTorch Framework and the ONNX open standard format.

Handle model that are aggregation of models

As hotfix for the issue regarding the order of input tensor, I created a model that is an aggregation of two models. In the first one I added a permute layer to handle the channel order.
The hotfix is working well in forward pass (tested). Unfortunately following error is raised by xplique. I think aggregation of models is not handled by Xplique.

ValueError                                Traceback (most recent call last)
/tmp/ipykernel_8110/184323267.py in <module>
      7 
      8 # create an explainer and generate explanations
----> 9 explainer = GradCAM(modper)
     10 explanations = explainer(X_preprocessed, Y) # `explainer.explain(inputs, labels)` also works
     11 

~/.local/lib/python3.7/site-packages/xplique/attributions/grad_cam.py in __init__(self, model, output_layer, batch_size, conv_layer)
     41                  batch_size: Optional[int] = 32,
     42                  conv_layer: Optional[Union[str, int]] = None):
---> 43         super().__init__(model, output_layer, batch_size)
     44 
     45         # find the layer to apply grad-cam

~/.local/lib/python3.7/site-packages/xplique/attributions/base.py in __init__(self, model, output_layer, batch_size)
    123             # reconfigure the model (e.g skip softmax to target logits)
    124             target_layer = find_layer(model, output_layer)
--> 125             model = tf.keras.Model(model.input, target_layer.output)
    126 
    127             # sanity check, output layer before softmax

/opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    528     self._self_setattr_tracking = False  # pylint: disable=protected-access
    529     try:
--> 530       result = method(self, *args, **kwargs)
    531     finally:
    532       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/keras/engine/functional.py in __init__(self, inputs, outputs, name, trainable, **kwargs)
    107     generic_utils.validate_kwargs(kwargs, {})
    108     super(Functional, self).__init__(name=name, trainable=trainable)
--> 109     self._init_graph_network(inputs, outputs)
    110 
    111   @tf.__internal__.tracking.no_automatic_dependency_tracking

/opt/conda/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py in _method_wrapper(self, *args, **kwargs)
    528     self._self_setattr_tracking = False  # pylint: disable=protected-access
    529     try:
--> 530       result = method(self, *args, **kwargs)
    531     finally:
    532       self._self_setattr_tracking = previous_value  # pylint: disable=protected-access

/opt/conda/lib/python3.7/site-packages/keras/engine/functional.py in _init_graph_network(self, inputs, outputs)
    191     # Keep track of the network's nodes and layers.
    192     nodes, nodes_by_depth, layers, _ = _map_graph_network(
--> 193         self.inputs, self.outputs)
    194     self._network_nodes = nodes
    195     self._nodes_by_depth = nodes_by_depth

/opt/conda/lib/python3.7/site-packages/keras/engine/functional.py in _map_graph_network(inputs, outputs)
    982                              'The following previous layers '
    983                              'were accessed without issue: ' +
--> 984                              str(layers_with_complete_input))
    985         for x in tf.nest.flatten(node.outputs):
    986           computable_tensors.add(id(x))

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 3, 512, 612), dtype=tf.float32, name='input.1'), name='input.1', description="created by layer 'input.1'") at layer "87_pad". The following previous layers were accessed without issue: []

[BUG] - Metrics not working with Tensor inputs

Subject
Metrics

Describe the bug
The metrics inheriting from CausalFidelity (like Deletion for example) do not work with EagerTensor.

Screenshots

...
File ***\venv\lib\site-packages\xplique\metrics\fidelity.py:179, in CausalFidelity.__init__(self, model, inputs, targets, batch_size, causal_mode, baseline_mode, steps, max_percentage_perturbed)
    176 self.baseline_mode = baseline_mode
    178 self.nb_features = np.prod(inputs.shape[1:-1])
--> 179 self.inputs_flatten = inputs.reshape((len(inputs), self.nb_features, inputs.shape[-1]))
    181 assert 0.0 < max_percentage_perturbed <= 1.0, "`max_percentage_perturbed` must be" \
    182                                               "in ]0, 1]."
    183 self.max_nb_perturbed = tf.math.floor(self.nb_features * max_percentage_perturbed)

File ***\venv\lib\site-packages\tensorflow\python\framework\ops.py:437, in Tensor.__getattr__(self, name)
    433 def __getattr__(self, name):
    434   if name in {"T", "astype", "ravel", "transpose", "reshape", "clip", "size",
    435               "tolist", "data"}:
    436     # TODO(wangpeng): Export the enable_numpy_behavior knob
--> 437     raise AttributeError(
    438         f"{type(self).__name__} object has no attribute '{name}'. " + """
    439       If you are looking for numpy-related methods, please run the following:
    440       from tensorflow.python.ops.numpy_ops import np_config
    441       np_config.enable_numpy_behavior()
    442     """)
    443   self.__getattribute__(name)

AttributeError: EagerTensor object has no attribute 'reshape'. 
        If you are looking for numpy-related methods, please run the following:
        from tensorflow.python.ops.numpy_ops import np_config
        np_config.enable_numpy_behavior()

To Reproduce
Minimal example :

import tensorflow as tf
from xplique.metrics import Deletion
import numpy as np

inputs = tf.convert_to_tensor(np.zeros((1,10,10,3)))
targets= tf.convert_to_tensor([1])
model = tf.keras.Model()
Deletion(None, inputs, targets)

Expected behavior
According to the documentation, inputs provided as tensor should work :

inputs: Union[tf.data.Dataset, tf.Tensor, np.ndarray],

Additional context
In my code, it works with a numpy array, but will stop working if i convert the inputs to a tensorflow Tensor of same shape and content.

Contribution: Add a protocol to raise issues

It would be a good practice to have a template when people submit issues. It could even be different template depending on the issue: do we need to reproduce the error, should we have the machine configuration, is it a comfort issue (e.g I can have a hotfix but it would be a nice enhancement that I do not use it), etc....

@justinplakoo do you have any idea how we should manage that ? I think we can add an issue template, what do you think ?

[BUG] - Getting started notebook fails as `plot_attributions` can't hande 3-dim explanations

Select the modules to which the bug refers:

  • Attributions Methods
  • Feature Visualization
  • Concepts
  • Metrics
  • Documentation

Describe the bug
Some returned explanations are 4 dimensional (batch dim + hwc images), which does not work with the plot_attributions method in xplique/plots/image.py as the method is suited to handle only 2D images with no channel dimension. The bug affects the following methods, which seem to return 4-dimensional explanations, which have explanations of shape (6, 224, 224, 3) as opposed to (6, 224, 224):
GradientInput, GuidedBackprop, IntegratedGradients, SmoothGrad, SquareGrad, VarGrad

Screenshots
Stack trace:

[/usr/local/lib/python3.7/dist-packages/xplique/plots/image.py](https://localhost:8080/#) in plot_attributions(explanations, images, cmap, alpha, clip_percentile, absolute_value, cols, img_size, **plot_kwargs)
    154     rows = ceil(len(explanations) / cols)
    155     # get width and height of our images
--> 156     l_width, l_height = explanations.shape[1:]
    157 
    158     # define the figure margin, width, height in inch

ValueError: too many values to unpack (expected 2)

Desktop (please complete the following information):
Using default Google Collab notebook.

To Reproduce
Simply run all in Getting_started.ipynb

Expected behavior
plot_attributions should be able to handle images with multiple channels to produce the visualizations.

Additional context
None

[Bug] [Important] attributions of sklearn wrapped models


name: Bug report
about: attributions of sklearn wrapped models
title: "[BUG] - attributions of sklearn wrapped models are incoherent"
labels: ''wrapper", "bug", "attributions"
assignees: ''


Select the modules to which the bug refers:

  • Attributions Methods
  • Feature Visualization
  • Concepts
  • Metrics
  • Documentation

Describe the bug
When a wrapper is used on regression model from sklearn, the obtained attributions are not coherent. (Problem can be larger).

Screenshots
wrapper_bug

Desktop:

  • OS - windows
  • Python version - 3.7.12
  • Sklearn - 1.0.2
  • Xplique - 0.2.6

To Reproduce

import numpy as np
from numpy.random import seed, normal
import sklearn
import xplique
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split

seed(0)

# dataset parameters
features_coef = np.array([1, -2, -3, 4, 0, 0, 0, 0])
nb_samples = 1000
nb_features = len(features_coef)


# create dataset
dataset = normal(0, 1, (nb_samples, nb_features))
noise = normal(0, 1, nb_samples)
target = dataset.dot(features_coef) + noise

# split dataset
X_train, X_test, y_train, y_test = train_test_split(dataset, target, test_size=0.05, shuffle=True)


# train model
sk_linear = LinearRegression().fit(X_train, y_train)

# Create the wrapper class
class Wrapper():
    # The init method is necessary for every class
    def __init__(self, model):
        self.model = model

    # The call method calls the predict method
    def __call__(self, inputs):
        return self.model.predict(inputs)

# wrap model
sk_model = Wrapper(sk_linear)

# adapt inputs/outputs
inputs_tf = tf.cast(X_test, tf.float32)
targets_tf = tf.ones((X_test.shape[0], 1))

# instanciate explainer
explainer = KernelShap(
    model,
    nb_samples=200,  # 2000
    ref_value=0.0
)

# compute explanations
explanations = abs(explainer(inputs_tf, targets_tf))

print(np.array(explanations).mean(axis=0))

# [4.328179, 4.357149, 4.6055717, 5.554367, 3.5661576, 4.1552, 3.5590754, 4.7494626]

To ease the debugging, here is a minimal example notebook (There are several lines for the visualization, but you can jump to the end of the notebook to see the different attributions) : https://colab.research.google.com/drive/1zvt4I9lVpvzg1oWPUNZoFs39w8MPSz_b?usp=sharing

Expected behavior
The 4 last attributions values should be close to 0, far inferior to the 4 first.

Additional context
_

Handle model with tensor that has channel first

The explaination does not work for models that ask for the channel as the first dimension of the input tensor (channel, height, width.
When such a model is provided, the output shape of the explanation is (channel x height) instead of (height x width)

[BUG] - Error when using tf.data.Dataset on metrics

Select the modules to which the bug refers:

  • Metrics

Describe the bug
Passing a tf.data.Dataset to metrics constructors derives into the program incorrectly accessing an element_spec of the aforementioned dataset as if it were a list when it is not the case.

Screenshots

2022-03-17 11:44:57.703227: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-03-17 11:44:57.704477: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2022-03-17 11:45:01.816089: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-03-17 11:45:01.817565: W tensorflow/stream_executor/platform/default/dso_loader.cc:60] Could not load dynamic library 'nvcuda.dll'; dlerror: nvcuda.dll not found
2022-03-17 11:45:01.817889: W tensorflow/stream_executor/cuda/cuda_driver.cc:326] failed call to cuInit: UNKNOWN ERROR (303)
2022-03-17 11:45:01.822735: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:169] retrieving CUDA diagnostic information for host: B201801349
2022-03-17 11:45:01.823202: I tensorflow/stream_executor/cuda/cuda_diagnostics.cc:176] hostname: B201801349
2022-03-17 11:45:01.824379: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-03-17 11:45:01.826257: I tensorflow/compiler/jit/xla_gpu_device.cc:99] Not creating XLA devices, tf_xla_enable_xla_devices not set
WARNING:tensorflow:Model was constructed with shape (1, 32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(1, 32, 32, 3), dtype=tf.float32, name='conv2d_input'), name='conv2d_input', description="created by layer 'conv2d_input'"), but it was called on an input with incompatible shape (64, 32, 32, 3).
2022-03-17 11:45:03.985713: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:116] None of the MLIR optimization passes are enabled (registered 2)
WARNING:tensorflow:Model was constructed with shape (1, 32, 32, 3) for input KerasTensor(type_spec=TensorSpec(shape=(1, 32, 32, 3), dtype=tf.float32, name='conv2d_input'), name='conv2d_input', description="created by layer 'conv2d_input'"), but it was called on an input with incompatible shape (56, 32, 32, 3).
WARNING:tensorflow:AutoGraph could not transform <function <lambda> at 0x000001D6E9D67D30> and will run it as-is.
Cause: could not parse the source code of <function <lambda> at 0x000001D6E9D67D30>: found multiple definitions with identical signatures at the location. This error may be avoided by defining each lambda on a single line and with unique argument names.
Match 0:
(lambda z, w: z)

Match 1:
(lambda z, w: w)

To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
WARNING:tensorflow:AutoGraph could not transform <function <lambda> at 0x000001D6E9D67E50> and will run it as-is.
Cause: could not parse the source code of <function <lambda> at 0x000001D6E9D67E50>: found multiple definitions with identical signatures at the location. This error may be avoided by defining each lambda on a single line and with unique argument names.
Match 0:
(lambda z, w: z)

Match 1:
(lambda z, w: w)

To silence this warning, decorate the function with @tf.autograph.experimental.do_not_convert
Traceback (most recent call last):
  File "C:/Users/a-m.picard/AppData/Roaming/JetBrains/PyCharmCE2021.3/scratches/scratch_1.py", line 24, in <module>
    metric = Deletion(model, dataset.map(lambda z, w: z), dataset.map(lambda z, w: w))
  File "C:\Users\a-m.picard\Anaconda3\envs\scouter-tf2\lib\site-packages\xplique\metrics\fidelity.py", line 324, in __init__
    super().__init__(model, inputs, targets, batch_size, "deletion",
  File "C:\Users\a-m.picard\Anaconda3\envs\scouter-tf2\lib\site-packages\xplique\metrics\fidelity.py", line 174, in __init__
    super().__init__(model, inputs, targets, batch_size)
  File "C:\Users\a-m.picard\Anaconda3\envs\scouter-tf2\lib\site-packages\xplique\metrics\base.py", line 36, in __init__
    self.inputs, self.targets = numpy_sanitize(inputs, targets)
  File "C:\Users\a-m.picard\Anaconda3\envs\scouter-tf2\lib\site-packages\xplique\commons\data_conversion.py", line 69, in numpy_sanitize
    inputs, targets = tensor_sanitize(inputs, targets)
  File "C:\Users\a-m.picard\Anaconda3\envs\scouter-tf2\lib\site-packages\xplique\commons\data_conversion.py", line 35, in tensor_sanitize
    dataset_shape = inputs.element_spec[0].shape
TypeError: 'TensorSpec' object is not subscriptable

Desktop (please complete the following information):

  • OS: Windows, Linux
  • Python version: 3.9.5
  • Tensorflow version: 2.4.1 - 2.7

To Reproduce

import tensorflow as tf
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, GlobalAveragePooling2D, Dense
from xplique.attributions import Saliency
from xplique.metrics import Deletion

x = tf.random.normal((120, 32, 32, 3))
y = tf.random.normal((120, 10))
dataset = tf.data.Dataset.from_tensor_slices((x, y))
model = Sequential([
    Conv2D(8, 3, strides=2, padding='same'),
    Conv2D(8, 3, strides=2, padding='same'),
    GlobalAveragePooling2D(),
    Dense(10)
])
model(tf.random.normal((1, 32, 32, 3)))
explainer = Saliency(model, -1, batch_size=64)
explanations = []
for batch_x, batch_y in dataset.batch(64):
    exp = explainer(batch_x, batch_y)
    explanations.extend(exp)
metric = Deletion(model, dataset.map(lambda z, w: z), dataset.map(lambda z, w: w))
metric_score = metric(explanations)

Expected behavior
I would expect the metric to be computed, but whilst instantiating the Deletion object, the constructor attempts to figure out the shapes of the inputs of the individual points of a tf.data.Dataset. Whilst typically straight-forward, a simple subscript bug appears, probably stemming from a incorrectly treated edge case.

Additional context
The property element_spec of a dataset is not a list, and thus, the subscript is unnecessary for the desired function.

feature attribution: refactor sub-methods

Some static methods are useless, and we have decided to make them private. We conjecture that the number of users wishing to access these methods to create / modify a new method is low.

Documentation : few holes

minor

Some missing elements in the documentation

Visibility of single method tutoriels

  • In the readme, there are tables referencing all methods, maybe adding a column with the link to the tutoriels will make them much more visible.

Common API documentation

  • A documentation on the attribution API, common for all attribution methods. As many thing are common (inheritance), a common documentation could be added. Detail on the fact that no common definition of attribution exist and that each method define one, thus some metrics tend to favorize some methods.
  • A documentation for the metrics API, same reason as above.
  • A documentation for the feature visualization API, same reason as above.
  • A documentation for the concept based API, same reason as above.

Points which need further explanations

  • In attribution method, the targets argument of the evaluate method is really important. Depending on its value, the aim of the explanation is completly different. This should be further explained.
  • Some method or metrics can be far longer than others. It may be necessary to precise it, for example KernelShap or Stability.

Error or typo

  • The documentation of the init method is not visible for Lime, KernelShap and Gradient Input.

[BUG] - ndarray not hashable in AverageStability metric

Select the modules to which the bug refers:

  • Attributions Methods
  • Feature Visualization
  • Concepts
  • Metrics
  • Documentation

Describe the bug
While running the Average Stability metric, I receive the unhashable type error regardless of whether I pass the images and labels as tensor or numpy array.

Screenshots
image

Desktop (please complete the following information):

  • Windows 10
  • Python version: 3.8.10
  • Tensorflow version: 2.10.1
  • Packages used version: numpy: 1.23.5

To Reproduce
Run AverageStability metric with np array of images and labels array of the same length.

Expected behavior
I expected to just receive a score just like with Deletion and Insertion metrics, which worked right away.

documentation: refactor first page

Refactor of the Readme & Documentation first page.

This page should:

  • introduce the modules
  • present deel-team
  • show a starter code
  • how to contribute
  • thanks other libs

feature visualization: add preprocess parameter to handle various input range

Each model has its own input range, actually we only handle [0, 1] : the output of the sigmoid or clip.
We could investigate what solution should be adopted:

  • rescale after the current procedure: x = x * (x_max - x_min) - x_min
  • allow the user to pass a predefined function
  • manipulate in [0, 255] then use the appropriate preprocess at the end

[BUG] - new_targets_tf = tf.ones((78,1)) in Attribution methods of regression model

Select the modules to which the bug refers:

  • [*] Attributions Methods
  • Feature Visualization
  • Concepts
  • Metrics
  • Documentation

Describe the bug
A clear and concise description of what the bug is.

Screenshots
If applicable, add screenshots to help explain your problem. Especially, if you have an error please provide the complete logs.

Desktop (please complete the following information):

  • OS
  • Python version
  • Tensorflow version
  • Packages used version (PyTorch, Numpy, scikit-learn, etc..)

To Reproduce
Steps to reproduce the behavior. Even better if you can provide a minimal working example to
reproduce the behavior or example of code to reproduce the bug.

Expected behavior
A clear and concise description of what you expected to happen.

Additional context
Add any other context about the problem here.
Suppose the regression has 5 outputs (multi-output regression), will new_targets_tf = tf.ones((78,1)) change to new_targets_tf = tf.ones((78,5)) in such a case?

feature attribution: adapt api for bounding box

Add the ability to explain predictions using feature attribution on model that predict bounding box.
A state of the art and an inventory of the methods that could work is necessary before proposing a solution.

[Mistakes] - Problems in documentation and SmoothGrad batching

Select the modules to which the bug refers:

  • Attributions Methods
  • Documentation

Describe the bug
There is a problem in the implementation of SmoothGrad :

  • The SmoothGrad method only batch samples on the inputs dimension, not with the nb_samples parameter. Thus is you set a value of 10000 for this parameter, it will make an out of memory error.

There are also several problems in the documentation:

  • There is no documentation on the plots api.
  • In the documentation, there should be a link to the source code for each method.
  • The methods are not in alphabetic order, it inders fluidity.
  • The readme methods table should include link to the documentation (maybe the name of the method).
  • The documentation for DeconvNet and GuidedBackprop are empty (no description nor link to the paper).
  • The wrong paper is provided for GradientInput, it was actually introduced with DeepLift in https://arxiv.org/abs/1704.02685
  • In xplique.plots.metrics.fidelity_curves the detailed_scores type should be changed from Dict[str, np.ndarray] to Dict[str, Dict[int, float]].

Expected behavior
The SmoothGrad method should support high values for nb_samples as inferences do not need to be grouped together.

tests: test_common.py

Test common should cover more use cases, at least toward the variety of inputs data

[BUG] - L2 norm missing square root in AverageStability

Select the modules to which the bug refers:
Metrics

Describe the bug
L2 norm is missing square root in AverageStability.

l.56-57 of stability.py :
elif distance == 'l2':
self.distance = lambda x, y: tf.reduce_sum((x-y)**2.0)

Desktop (please complete the following information):
Windows

To Reproduce
You can check the difference between L1 et L2 explanation when the explanations values are high.
My explanations were for an image (512,512), with maximum of 2400 and average of 25.
On theses explanations :

  • Average stability with L1 norm outputs a value per pixel of around 16.7.
  • Average stability with L2 norm outputs a value per pixel of more than 1800.

Expected behavior
Sqrt added to the L2 norm.

Warning appearing while calling some tabular visualization functions

A warning appears when calling the explanation visualization functions plot_feature_impact and plot_mean_feature_impact.
The problem is that the warning appears when calling the virtualization of each explanation..

It can be solved by adding the line in the python file xplique/plots/tabular.py (in the function plot_feature_impact):
axes.set_ylabel('')
This line can be added at the line 202 of the python file.

Interpreting Semantic Segmentations

Hey,
Is it possible to run GradCam visuals on a semantic segmentation model using this library?
If yes, kindly help me out with how I can proceed.

Specifically, I have trained a FCN segmentation model using VGG-19 as the backbone, in Keras, and have the trained model, and wish to interpret the segmentation behaviour using gradcam/gradcam plus etc.
(my model, has only two categories, background and foreground)

Any help is appreciated,
Thank you.

Warning appearing while calling some tabular visualization functions

A warning appears when calling the explanation visualization functions plot_feature_impact and plot_mean_feature_impact.
The problem is that the warning appears when calling the virtualization of each explanation..

It can be solved by adding the line in the python file xplique/plots/tabular.py (in the function plot_feature_impact): axes.set_ylabel('')
This line can be added at the line 202 of the python file.

Fidelity evolution curve visualization

The insertion and deletion metrics were added a detailed_evaluate() method, this method returns the score on the different perturbed input. This output can be used to make the evolution curves for insertion and deletion.

Those curves possess more information than the usually used auc.

This visualization should support the plot of several curves on the same graphic so that explanation methods can be compared.

ci: add mypy

In order to check that incoming code respect typing

[Feature Request] map_to_interpret_space function for Lime and Kernel Shap

The methods Lime and KernelShap need a map_to_interpret_space function. By default, the function is the quickshift segmentation algorithm. For better results, watershed function on images have to be used. This algorithm, in the skimage, package needs a parameter (makers) which value is dedicated to the image treated and not for all images of the dataset.
The current API of Lime and KernelShap cannot allowed such configuration (function + dedicated value).

A solution could be to have a parameter for the image maps computed in advance instead of compute them every time in the explicability method function.

[BUG] - Can't import the BoundingBoxesExplainer module

Select the modules to which the bug refers:

  • Attributions Methods
  • Feature Visualization
  • Concepts
  • Metrics
  • Documentation

Describe the bug
I have a module that use Xplique==0.2.6 and I want to use the BoundingBoxesExplainer module.
I have Xplique installed and imported, yet when it seems that this specific module is missing:

Screenshots
If applicable, add screenshots to help explain your problem. Especially, if you have an error please provide the complete logs.

      7 tf.config.run_functions_eagerly(True)
      8 from xplique.attributions import KernelShap, Occlusion, Rise, Lime
----> 9 from xplique.attributions.object_detector import BoundingBoxesExplainer
     10 from xplique.metrics import MuFidelity, Deletion, Insertion, AverageStability
     11 from sklearn import linear_model

ModuleNotFoundError: No module named 'xplique.attributions.object_detector'

Yet one can find it here in the library.

Desktop (please complete the following information):

  • OS: Linux
  • Python version '3.9.5 (default, Jun 4 2021, 12:28:51) \n[GCC 7.5.0]'
  • Tensorflow version 2.8.0
  • Packages used version (PyTorch, Numpy, scikit-learn, etc..)

To Reproduce

! pip install Xplique==0.2.6
import xplique
from xplique.attributions.object_detector import BoundingBoxesExplainer

Expected behavior
It should import the BoundingBoxesExplainer module

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.