Skip to main content

SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability and efficiency.

Project description

Contributors Forks Stargazers Issues MIT License


SignalGrad-CAM

SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability and efficiency.

Explore the docs

Report Bug or Request Feature

Table of Contents
  1. About The Project
  2. Installation
  3. Usage
  4. Publications
  5. Contacts And Useful Links
  6. License

About The Project

Deep learning models have demonstrated remarkable performance across various domains; however, their black-box nature hinders interpretability and trust. As a result, the demand for explanation algorithms has grown, driving advancements in the field of eXplainable AI (XAI). However, relatively few efforts have been dedicated to developing interpretability methods for signal-based models. We introduce SignalGrad-CAM (SGrad-CAM), a versatile and efficient interpretability tool that extends the principles of Grad-CAM to both 1D- and 2D-convolutional neural networks for signal processing. SGrad-CAM is designed to interpret models for either image or signal elaboration, supporting both PyTorch and TensorFlow/Keras frameworks, and provides diagnostic and visualization tools to enhance model transparency. The package is also designed for batch processing, ensuring efficiency even for large-scale applications, while maintaining a simple and user-friendly structure.

Keywords: eXplainable AI, explanations, local explanation, fidelity, interpretability, transparency, trustworthy AI, feature importance, saliency maps, CAM, Grad-CAM, black-box, deep learning, CNN, signals, time series

Back To Top

Installation

  1. Make sure you have the latest version of pip installed
    pip install --upgrade pip
    
  2. Install SignalGrad-CAM through pip
     pip install signal-grad-cam
    

Back To Top

Usage

Here's a basic example that illustrates SignalGrad-CAM common usage.

First, train a classifier on the data or select an already trained model, then instantiate TorchCamBuilder (if you are working with a PyTorch model) or TfCamBuilder (if the model is built in TensorFlow/Keras).

Besides the model, TorchCamBuilder requires additional information to function effectively. For example, you may provide a list of class labels, a preprocessing function, or an index indicating which dimension corresponds to time. These attributes allow SignalGrad-CAM to be applied to a wide range of models.

The constructor displays a list of available Grad-CAM algorithms for explanation, as well as a list of layers that can be used as target for the algorithm. It also identifies any Sigmoid/Softmax layer, since its presence or absence will slightly change the algorithm's workflow.

import numpy as np
import torch
from signal_grad_cam import TorchCamBuilder

# Load model
model = YourTorchModelConstructor()
model.load_state_dict(torch.load("path_to_your_stored_model.pt")
model.eval()

# Introduce useful information
def preprocess_fn(signal):
   signal = torch.from_numpy(signal).float()
   # Extra preprocessing: data resizing, reshaping, normalization...
   return signal
class_labels = ["Class 1", "Class 2", "Class 3"]

# Define the CAM builder
cam_builder = TorchCamBuilder(model=model, transform_fc=preprocess_fc, class_names=class_labels, time_axs=1)

Now, you can use the `cam_builder` object to generate class activation maps from a list of input data using the `get_cams` method. You can specify multiple algorithm names, target layers, or target classes as needed.

The function's attributes allow users to customize the visualization (e.g., setting axis ticks or labels). If a result directory path is provided, the output is stored as a '.png' file; otherwise, it is displayed. In all cases, the function returns a dictionary containing the requested CAMs, along with the model's predictions and importance score ranges.

Finally, several visualization tools are available to gain deeper insights into the model's behavior. The display can be customized by adjusting line width, point extension, aspect ratio, and more:

  • single_channel_output_display plots the selected channels using a color scheme that reflects the importance of each input feature.
  • overlapped_output_display superimposes CAMs onto the corresponding input in an image-like format, allowing users to capture the overall distribution of input importance.

# Prepare data
data_list = [x for x in your_numpy_data_x[:2]]
data_labels_list = [1, 0]
item_names = ["Item 1", "Item 2"]
target_classes = [0, 1]

# Create CAMs
cam_dict, predicted_probs_dict, score_ranges_dict = cam_builder.get_cam(data_list=data_list, data_labels=data_labels_list, 
									target_classes=target_classes, explainer_types="Grad-CAM", 
									target_layer="conv1d_layer_1", softmax_final=True,
                                                            		data_sampling_freq=25, dt=1, axes_names=("Time (s)", "Channels"))

# Visualize single channel importance
selected_channels_indices = [0, 2, 10]
cam_builder.single_channel_output_display(data_list=data_list, data_labels=data_labels_list, predicted_probs_dict=predicted_probs_dict,
					  cams_dict=cam_dict, explainer_types="Grad-CAM", target_classes=target_classes, 
					  target_layers="target_layer_name", desired_channels=selected_channels_indices, 
					  grid_instructions=(1, len(selected_channels_indices), bar_ranges=score_ranges_dict, 
				          results_dir="path_to_your_result_directoory", data_sampling_freq=25, dt=1, line_width=0.5, 
					  axes_names=("Time (s)", "Amplitude (mV)"))

# Visualize overall importance
cam_builder.overlapped_output_display(data_list=data_list, data_labels=data_labels_list, predicted_probs_dict=predicted_probs_dict,
                                      cams_dict=cam_dict, explainer_types="Grad-CAM", target_classes=target_classes, 
				      target_layers="target_layer_name", fig_size=(20 * len(your_data_X), 20), 
				      grid_instructions=(len(your_data_X), 1), bar_ranges=score_ranges_dict, data_names=item_names 
				      results_dir="path_to_your_result_directoory", data_sampling_freq=25, dt=1)

You can also check the python scripts here.

See the open issues for a full list of proposed features (and known issues).

Back To Top

If you use the SignalGrad-CAM software for your projects, please cite it as:

@software{Pe_SignalGrad_CAM_2025,
  author = {Pe, Samuele and Buonocore, Tommaso Mario and Giovanna, Nicora and Enea, Parimbelli},
  title = {{SignalGrad-CAM}},
  url = {https://github.com/bmi-labmedinfo/signal_grad_cam},
  version = {0.0.1},
  year = {2025}
}

Back To Top

Contacts and Useful Links

Back To Top

License

Distributed under MIT License. See LICENSE for more information.

Back To Top

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

signal_grad_cam-0.1.0.tar.gz (25.0 kB view details)

Uploaded Source

Built Distribution

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

signal_grad_cam-0.1.0-py3-none-any.whl (25.5 kB view details)

Uploaded Python 3

File details

Details for the file signal_grad_cam-0.1.0.tar.gz.

File metadata

  • Download URL: signal_grad_cam-0.1.0.tar.gz
  • Upload date:
  • Size: 25.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.0

File hashes

Hashes for signal_grad_cam-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a824c12b557726584cc5195880c7c48bef14a6577b9c942805724a0ee9182ea7
MD5 36cdb76c8b5e0aedc77d53cb346b18c4
BLAKE2b-256 8f47f5829bda37afa06434a56bc09ea43fbe8428aeffb75d9568f3fd1feadd73

See more details on using hashes here.

File details

Details for the file signal_grad_cam-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for signal_grad_cam-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5e41b4ada7685c915dffc768a1c76c955eb7dc02320f22aaf0547eb38804c0a
MD5 03a5fcb3d14ab41a13c1f85bcc8f995f
BLAKE2b-256 d2cc50003dcebb840689e5a5abd5530d9bd0959f530943fd4345acfd33fdd81c

See more details on using hashes here.

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