SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability and efficiency.
Project description
SignalGrad-CAM
SignalGrad-CAM aims at generalising Grad-CAM to one-dimensional applications, while enhancing usability and efficiency.
Table of Contents
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*
Installation
- Make sure you have the latest version of pip installed
pip install --upgrade pip
- Install SignalGrad-CAM through pip
pip install signal-grad-cam
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_displayplots the selected channels using a color scheme that reflects the importance of each input feature.overlapped_output_displaysuperimposes 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=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=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).
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}
}
Contacts and Useful Links
-
Project Link: https://github.com/bmi-labmedinfo/signal_grad_cam
-
Package Link: https://pypi.org/project/signal-grad-cam/
License
Distributed under MIT License. See LICENSE for more information.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file signal_grad_cam-0.0.2.tar.gz.
File metadata
- Download URL: signal_grad_cam-0.0.2.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
70ef9048f98cb1c35af5b7cb65e24259390ca3df2083ad446b42c589e8f1ebcf
|
|
| MD5 |
d2e31ac8022309114255837caea2f1a7
|
|
| BLAKE2b-256 |
efa8312ba30306b00acd5e901a6d312c6a20b0c36de35af40d156c5ce9323e56
|
File details
Details for the file signal_grad_cam-0.0.2-py3-none-any.whl.
File metadata
- Download URL: signal_grad_cam-0.0.2-py3-none-any.whl
- Upload date:
- Size: 25.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
97bd6182d8dcb1e599962ad44d7e2f38b8eb6079439acadd54046b5f9909fcf2
|
|
| MD5 |
79317ccca4e16ae2eb786f6cc20bbc35
|
|
| BLAKE2b-256 |
19b4b2868093bb8e33242b2dc0d48215a5a56eab183865b231e19da238c9a0d8
|