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

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.sign 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.4.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.4-py3-none-any.whl (21.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: signxai2-0.15.4.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.4.tar.gz
Algorithm Hash digest
SHA256 ba040cc59f98c770c04aaaa2074a3d69fcd884143387602f83101e094dece94d
MD5 046e96bcce999dd41ce8e293fb3f2583
BLAKE2b-256 95f93adbb848afcc5a15d5ae3321e4f401a8b7a3f8aa0cf435fdf18b47afa057

See more details on using hashes here.

Provenance

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

File metadata

  • Download URL: signxai2-0.15.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 bec2925275db997bb20370c419d0d4c7c280f250733621c73d5ca2bfc4d6e83a
MD5 58d665ce306a2a11d9b2de5139689b48
BLAKE2b-256 af8f04d8d0a3898feed883b67d6ef88ebbf5879448b5f31b8bdc36afc79a81f2

See more details on using hashes here.

Provenance

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