torchcam.metrics

Apart from qualitative visual comparison, it is important to have a refined evaluation metric for class activation maps. This submodule is dedicated to the evaluation of CAM methods.

class torchcam.metrics.ClassificationMetric(cam_extractor: _CAM, logits_fn: Callable[[Tensor], Tensor] | None = None)[source]

Implements Average Drop and Increase in Confidence from “Grad-CAM++: Improved Visual Explanations for Deep Convolutional Networks.”.

The raw aggregated metric is computed as follows:

\[\begin{split}\forall N, H, W \in \mathbb{N}, \forall X \in \mathbb{R}^{N*3*H*W}, \forall m \in \mathcal{M}, \forall c \in \mathcal{C}, \\ AvgDrop_{m, c}(X) = \frac{1}{N} \sum\limits_{i=1}^N f_{m, c}(X_i) \\ IncrConf_{m, c}(X) = \frac{1}{N} \sum\limits_{i=1}^N g_{m, c}(X_i)\end{split}\]

where \(\mathcal{C}\) is the set of class activation generators, \(\mathcal{M}\) is the set of classification models, with the function \(f_{m, c}\) defined as:

\[\forall x \in \mathbb{R}^{3*H*W}, f_{m, c}(x) = \frac{\max(0, m(x) - m(E_{m, c}(x) * x))}{m(x)}\]

where \(E_{m, c}(x)\) is the class activation map of \(m\) for input \(x\) with method \(m\), resized to (H, W),

and with the function \(g_{m, c}\) defined as:

\[\begin{split}\forall x \in \mathbb{R}^{3*H*W}, g_{m, c}(x) = \left\{ \begin{array}{ll} 1 & \mbox{if } m(x) < m(E_{m, c}(x) * x) \\ 0 & \mbox{otherwise.} \end{array} \right.\end{split}\]
>>> from functools import partial
>>> from torchcam.metrics import ClassificationMetric
>>> metric = ClassificationMetric(cam_extractor, partial(torch.softmax, dim=-1))
>>> metric.update(input_tensor)
>>> metric.summary()
update(input_tensor: Tensor, class_idx: int | None = None) None[source]

Update the state of the metric with new predictions

Parameters:
  • input_tensor – preprocessed input tensor for the model

  • class_idx – class index to focus on (default: index of the top predicted class for each sample)

summary() Dict[str, float][source]

Computes the aggregated metrics

Returns:

a dictionary with the average drop and the increase in confidence