Shortcuts

slideflow.grad

This submodule contains tools for calculating and display pixel attribution, or saliency, maps. See Saliency Maps for more information.

class SaliencyMap(model: Callable, class_idx: int)[source]

Class to assist with calculation and display of saliency maps.

Parameters:
  • model (Callable) – Differentiable model from which saliency is calculated.

  • class_idx (int) – Index of class for backpropagating gradients.

__init__(model: Callable, class_idx: int) None[source]

Class to assist with calculation and display of saliency maps.

Parameters:
  • model (Callable) – Differentiable model from which saliency is calculated.

  • class_idx (int) – Index of class for backpropagating gradients.

all(img: ndarray) Dict[source]

Calculate all saliency map methods.

Parameters:

img (np.ndarray) – Pre-processed input image in W, H, C format.

Returns:

Dictionary mapping name of saliency method to saliency map.

Return type:

Dict

blur_integrated_gradients(img: ndarray, batch_size: int = 20, smooth: bool = False, **kwargs) ndarray[source]

Calculate saliency map using blur integrated gradients.

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • batch_size (int, optional) – Batch size. Defaults to 20.

  • smooth (bool, optional) – Smooth gradients. Defaults to False.

Returns:

Saliency map.

Return type:

np.ndarray

gradcam(img: ndarray, layer: str, smooth: bool = False, **kwargs) ndarray[source]

Calculate gradient-based saliency map.

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • smooth (bool, optional) – Smooth gradients. Defaults to False.

Returns:

Saliency map.

Return type:

np.ndarray

guided_integrated_gradients(img: ndarray, x_steps: int = 25, max_dist: float = 1.0, fraction: float = 0.5, smooth: bool = False, **kwargs) ndarray[source]

Calculate saliency map using guided integrated gradients.

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • x_steps (int, optional) – Steps for gradient calculation. Defaults to 25.

  • max_dist (float, optional) – Maximum distance for gradient calculation. Defaults to 1.0.

  • fraction (float, optional) – Fraction for gradient calculation. Defaults to 0.5.

  • smooth (bool, optional) – Smooth gradients. Defaults to False.

Returns:

Saliency map.

Return type:

np.ndarray

integrated_gradients(img: ndarray, x_steps: int = 25, batch_size: int = 20, smooth: bool = False, **kwargs) ndarray[source]

Calculate saliency map using integrated gradients.

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • x_steps (int, optional) – Steps for gradient calculation. Defaults to 25.

  • max_dist (float, optional) – Maximum distance for gradient calculation. Defaults to 1.0.

  • smooth (bool, optional) – Smooth gradients. Defaults to False.

Returns:

Saliency map.

Return type:

np.ndarray

vanilla(img: ndarray, smooth: bool = False, **kwargs) ndarray[source]

Calculate gradient-based saliency map.

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • smooth (bool, optional) – Smooth gradients. Defaults to False.

Returns:

Saliency map.

Return type:

np.ndarray

xrai(img: ndarray, batch_size: int = 20, **kwargs) ndarray[source]

Calculate saliency map using XRAI.

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • batch_size (int, optional) – Batch size. Defaults to 20.

Returns:

Saliency map.

Return type:

np.ndarray

xrai_fast(img: ndarray, batch_size: int = 20, **kwargs) ndarray[source]

Calculate saliency map using XRAI (fast implementation).

Parameters:
  • img (np.ndarray) – Pre-processed input image in W, H, C format.

  • batch_size (int, optional) – Batch size. Defaults to 20.

Returns:

Saliency map.

Return type:

np.ndarray

Submodule for calculating/displaying pixel attribution (saliency maps).

grayscale(image_3d, vmax=None, vmin=None, percentile=99)[source]

Returns a 3D tensor as a grayscale 2D tensor. This method sums a 3D tensor across the absolute value of axis=2, and then clips values at a given percentile.

comparison_plot(original: ndarray, maps: Dict[str, ndarray], cmap: Any = 'plt.cm.gray', n_rows: int = 3, n_cols: int = 3) None[source]

Plots comparison of many saliency maps for a single image in a grid.

Parameters:
  • original (np.ndarray) – Original (unprocessed) image.

  • maps (dict(str, np.ndarray)) – Dictionary mapping saliency map names to the numpy array maps.

  • cmap (matplotlib colormap, optional) – Colormap for maps. Defaults to plt.cm.gray.

inferno(img)[source]
multi_plot(raw_imgs: List[ndarray], processed_imgs: List[ndarray], method: Callable, cmap: str = 'inferno', xlabels: List[str] | None = None, ylabels: List[str] | None = None, **kwargs) None[source]

Creates a plot of saliency maps and overlays for a given set of images.

The first row will be the raw images. The second row will be an overlay of the saliency map and the raw image. The third row will be the saliency maps.

Parameters:
  • raw_imgs (List[np.ndarray]) – Raw, unprocessed images.

  • processed_imgs (List[np.ndarray]) – Processed images.

  • method (Callable) – Saliency method.

  • cmap (str, optional) – Colormap. Defaults to ‘inferno’.

  • xlabels (Optional[List[str]], optional) – Labels for x-axis. Defaults to None.

  • ylabels (Optional[List[str]], optional) – Labels for y-axis. Defaults to None.

Raises:
  • ValueError – If length of raw_imgs, processed_imgs are not equal.

  • ValueError – If xlabels is provided and not a list.

  • ValueError – If ylabels is provided and not a list.

  • ValueError – If xlabels is provided and length does not equal raw_imgs.

  • ValueError – If ylabels is provided and length does not equal raw_imgs.

oranges(img)[source]
overlay(image, mask)[source]
saliency_map_comparison(orig_imgs: List[ndarray], saliency_fn: List[Callable], process_fn: Callable, saliency_labels: List[str] | None = None, cmap: str = 'inferno', **kwargs: Any) None[source]

Plots several saliency maps for a list of images.

Each row is a unique image. The first column is the original image. Each column after is a saliency map generated each of the functions provided to saliency_fn.

Parameters:
  • orig_imgs (list(np.ndarray)) – Original (unprocessed) images for which to generate saliency maps.

  • saliency_fn (list(Callable)) – List of saliency map functions.

  • process_fn (Callable) – Function for processing images. This function will be applied to images before images are passed to the saliency map function.

  • saliency_labels (list(str), optional) – Labels for provided saliency maps. Defaults to None.

  • cmap (str, optional) – Colormap for saliency maps. Defaults to ‘inferno’.