Skip to main content

Extended variant of ZeNNit with SIGNed explanations - Unveiling relevant features by reducing bias

Project description

SIGNed XAI for Image and Time Series Models

SIGNXAI-Example

PyPI Version License

SIGN (Sign-based Improvement of Gradient-based explaNations) is a novel XAI method intended to reduce bias in explanations that are intrinsically induced by several state-of-the-art XAI methods. The SIGN-XAI-2 package enables simple application of this method in your projects using the established Zennit (Zennit explains neural networks in torch) package pypi.org/project/zennit/.

SIGN-based explanations are particularly well suited for generating bias-reduced heatmaps for both image and time series data, enhancing interpretability by more reliably uncovering relevant features.

If you use this package or parts of it in your own work, please consider citing our paper:

 @article{Gumpfer2023SIGN,
    title = {SIGNed explanations: Unveiling relevant features by reducing bias},
    author = {Nils Gumpfer and Joshua Prim and Till Keller and Bernhard Seeger and Michael Guckert and Jennifer Hannig},
    journal = {Information Fusion},
    pages = {101883},
    year = {2023},
    issn = {1566-2535},
    doi = {https://doi.org/10.1016/j.inffus.2023.101883}
}

Documentation

The latest documentation is available from timexaigroup.github.io/signxai2.

Install

To install the package directly from PyPI using pip, use:

$ pip install signxai2

Usage

SIGN-XAI-2 is based on Zennit and works with PyTorch. If you want to know more about Zennit and its usage, visit github.com/chr5tphr/zennit. There is also a version of SIGN available for usage with TensorFlow environments pypi.org/project/signxai/.

The below example code (see vgg16_simple.py) demonstrates the basic usage of a SIGN-based composite as an extension to LRP-Epsilon in Zennit. To run this code, have a look at the requirements and the setup script.

import matplotlib.pyplot as plt
import numpy as np
import torch

from torchvision.models import vgg16, VGG16_Weights
from torchvision.transforms import Compose, Resize, ToTensor, Normalize
from zennit.attribution import Gradient

from signxai2.misc import get_example_image
from signxai2.sign import EpsilonStdXSIGN

# Define the preprocessing pipeline
transform = Compose([
    Resize(224),
    ToTensor(),
    Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),
])

# Load and preprocess image
image = get_example_image(1)
data = transform(image)[None]  # Add batch dimension

# Load pretrained VGG16 model
weights=VGG16_Weights.IMAGENET1K_V1
model = vgg16(weights=weights).eval()

# Get model prediction
output = model(data)
pred = output.argmax(1)[0].item()
target = torch.eye(1000)[[pred]]

# Get the class label
label = weights.meta['categories'][pred]
print('Predicted class: {}'.format(label))

# Visualize the original image and relevance map
fig, axs = plt.subplots(1, 2, figsize=(12, 5))
axs[0].imshow(image)
axs[0].set_title('Image')

# Compute attribution
composite = EpsilonStdXSIGN(mu=0, stdfactor=0.3, signstdfactor=0.3)
with Gradient(model=model, composite=composite) as attributor:
    _, attribution = attributor(data, target)

# Prepare relevance map
attribution = np.nan_to_num(attribution)
relevance = attribution.sum(1)
R = relevance[0] / np.abs(relevance).max()

# Plot relevance map
axs[1].matshow(R, cmap='seismic', clim=(-1, 1))
axs[1].set_title('LRP-Epsilon-SIGN')

# Switch off axes and labels
for ax in axs:
    ax.axis('off')

# Plot to screen
plt.tight_layout()
plt.show()

For more details and examples, have a look at our documentation.

Examples

The above code can be used to generate heatmaps as shown for the example images below. Comparing the baseline LRP-Epsilon method with its SIGN-augmented counterpart highlights notable differences in pixel-level attributions and image-induced contrast, with SIGN mitigating this explanatory bias and distortion inherent to the standard variant.

Example 1

Image Source: Coastal Roamer, CC BY-SA 4.0, via Wikimedia Commons

Example 2

Image Source: randomwild, CC0, via Wikimedia Commons

License

SIGN-XAI-2 and Zennit are licensed under the GNU LESSER GENERAL PUBLIC LICENSE VERSION 3 OR LATER -- see the COPYING and COPYING.LESSER files for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

signxai2-0.15.3.tar.gz (1.7 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

signxai2-0.15.3-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

Details for the file signxai2-0.15.3.tar.gz.

File metadata

  • Download URL: signxai2-0.15.3.tar.gz
  • Upload date:
  • Size: 1.7 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for signxai2-0.15.3.tar.gz
Algorithm Hash digest
SHA256 bd1c3a360d3ae1a7c51994acbe0dd05cd1c681a45e43817423a74d925acc13c5
MD5 d7457d70457982d8367f86a1d3a9569c
BLAKE2b-256 12313bdadbc0478a1e7762abec60f1b5826176f7e0c514e28727425cd362fd7c

See more details on using hashes here.

Provenance

The following attestation bundles were made for signxai2-0.15.3.tar.gz:

Publisher: auto-release.yml on TimeXAIgroup/signxai2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file signxai2-0.15.3-py3-none-any.whl.

File metadata

  • Download URL: signxai2-0.15.3-py3-none-any.whl
  • Upload date:
  • Size: 21.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for signxai2-0.15.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e4092fd84d349d4fa28351c07696cbe72e2582fb67125daf905348d7f7819eba
MD5 e3f14f1bc3e893a38c9c029bfd037cca
BLAKE2b-256 885e7fe8e0a342d50949464335d6e4ac30a279dbcdd098fb8f99f03f66643755

See more details on using hashes here.

Provenance

The following attestation bundles were made for signxai2-0.15.3-py3-none-any.whl:

Publisher: auto-release.yml on TimeXAIgroup/signxai2

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page