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()