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

Documentation Status 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 and is based on Zennit (https://pypi.org/project/zennit/) and LRP for Transformers (https://pypi.org/project/lxt/). If your are using TensorFlow instead of PyTorch, have a look at https://pypi.org/project/signxai/.

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

Supported Methods

  • Gradient
  • Gradient $\times$ Input
  • Gradient $\times$ SIGN
  • LRP (all variants contained in Zennit. )
  • LRP-$\epsilon$ with SIGN as Input Layer Rule

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.composites import EpsilonStdXSIGN

# Define preprocessing pipeline
transform = Compose([
    Resize((224, 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]

# 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))

# 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)[0]
relevance = attribution.sum(0)
R = relevance / np.abs(relevance).max()

# Visualize the image and relevance map
fig, axs = plt.subplots(1, 2, figsize=(10, 6))
axs[0].imshow(Compose([Resize((224, 224))])(image))
axs[0].set_title('Image')
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.9.tar.gz (2.1 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.9-py3-none-any.whl (28.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: signxai2-0.15.9.tar.gz
  • Upload date:
  • Size: 2.1 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.9.tar.gz
Algorithm Hash digest
SHA256 b38e8239f8c71c85dbfae07eec405a5a2de073800f0948d35f09fcf6c2923bc1
MD5 2cde832f4cc5847d3d8d550c2e88be45
BLAKE2b-256 4b218a448e1be5fcb04955aa8fb694774593cdee8c26d5639a50e199073aa167

See more details on using hashes here.

Provenance

The following attestation bundles were made for signxai2-0.15.9.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.9-py3-none-any.whl.

File metadata

  • Download URL: signxai2-0.15.9-py3-none-any.whl
  • Upload date:
  • Size: 28.8 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.9-py3-none-any.whl
Algorithm Hash digest
SHA256 6389d46fa07a3fd0854e4a99a7c8c4509dbe2becad6a62291bdc1e95e9fc33b4
MD5 a518fd7e5966b41367247ed5400a95f8
BLAKE2b-256 3468008bdc63984eb7eeeb3bbae157caf4116456129b18ec5a98bb67d4513553

See more details on using hashes here.

Provenance

The following attestation bundles were made for signxai2-0.15.9-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