Common Module

The signxai.common module contains framework-agnostic utilities and functions used by both the PyTorch and TensorFlow implementations.

Visualization Tools

The visualization module provides utilities for visualizing and displaying explanation results.

signxai.common.visualization.normalize_relevance_map(relevance_map, percentile=99)[source]

Normalizes a relevance map to a specified percentile range.

Parameters:
  • relevance_map (numpy.ndarray) – The relevance map to normalize

  • percentile (int, optional) – The percentile value for upper/lower bounds normalization

Returns:

Normalized relevance map with values in range [-1, 1]

Return type:

numpy.ndarray

signxai.common.visualization.relevance_to_heatmap(relevance_map, cmap='seismic', clip_values=(-1, 1))[source]

Converts a relevance map to a heatmap visualization.

Parameters:
  • relevance_map (numpy.ndarray) – The relevance map to visualize

  • cmap (str, optional) – The colormap to use for visualization (default: ‘seismic’)

  • clip_values (tuple, optional) – The lower and upper bounds for clipping the relevance values

Returns:

RGB heatmap image

Return type:

numpy.ndarray

signxai.common.visualization.overlay_heatmap(original_image, heatmap, alpha=0.5)[source]

Overlays a heatmap on an original image.

Parameters:
  • original_image (numpy.ndarray) – The original input image

  • heatmap (numpy.ndarray) – The heatmap to overlay

  • alpha (float, optional) – The blending factor between original image and heatmap

Returns:

Overlaid image

Return type:

numpy.ndarray

signxai.common.visualization.aggregate_and_normalize_relevancemap_rgb(relevance_map)

Aggregates and normalizes a relevance map for RGB images.

Parameters:

relevance_map (numpy.ndarray) – The relevance map to aggregate (with channels)

Returns:

Aggregated and normalized relevance map

Return type:

numpy.ndarray

signxai.common.visualization.visualize_comparison(original_image, relevance_maps, method_names, figsize=(15, 5), cmap='seismic')

Creates a side-by-side visualization of multiple relevance maps.

Parameters:
  • original_image (numpy.ndarray) – The original input image

  • relevance_maps (list of numpy.ndarray) – List of relevance maps to visualize

  • method_names (list of str) – List of method names for the relevance maps

  • figsize (tuple, optional) – Figure size for the visualization

  • cmap (str, optional) – Colormap to use

Returns:

Matplotlib figure

Return type:

matplotlib.figure.Figure

Common Validation Functions

The validation module contains utility functions for validating inputs and ensuring compatibility between frameworks.

signxai.common.validation.validate_model(model, backend=None)

Validates that a model is compatible with the specified backend.

Parameters:
  • model – The model to validate

  • backend (str, optional) – The backend to validate against (‘tensorflow’ or ‘pytorch’)

Returns:

True if the model is valid for the backend

Return type:

bool

Raises:

ValueError: If the model is not valid for the backend

signxai.common.validation.validate_input(input_tensor, model, backend=None)

Validates that an input tensor is compatible with the model and backend.

Parameters:
  • input_tensor – The input tensor to validate

  • model – The model to validate against

  • backend (str, optional) – The backend to validate against (‘tensorflow’ or ‘pytorch’)

Returns:

Validated input tensor, possibly converted to the appropriate format

Raises:

ValueError: If the input tensor is not valid for the model/backend

Framework Detection

The common module provides functions for detecting and handling different frameworks.

signxai.common.detect_framework(model)

Detects the framework (TensorFlow or PyTorch) based on the model.

Parameters:

model – The model to check

Returns:

Framework name (‘tensorflow’ or ‘pytorch’)

Return type:

str

Raises:

ValueError: If the framework cannot be determined