Measures how well attributions concentrate on the ground-truth relevant region (e.g. segmentation mask). Computes the proportion of total attribution mass that falls inside the annotated region. ↑ better.
attribution_localization
attribution_localization(
attributions: tuple[Tensor, ...] | list[tuple[Tensor, ...]],
feature_mask: tuple[Tensor, ...] | None,
multi_target: bool = False,
positive_attributions: bool = True,
weighted: bool = False,
return_dict: bool = False,
) -> (
TensorOrTupleOfTensorsGeneric
| list[TensorOrTupleOfTensorsGeneric]
| dict[str, TensorOrTupleOfTensorsGeneric]
| dict[str, list[TensorOrTupleOfTensorsGeneric]]
)
Ratio of positive attribution mass inside a ground-truth region to total attribution. ↑ better.
Measures how well the explanation concentrates on the annotated target region. A high score means the most-attributed features belong to the relevant object class.
References
Kohlbrenner et al.: "Towards Best Practice in Explaining Neural Network Decisions with LRP." IJCNN (2020): 1–7.
Parameters:
-
(attributionstuple[Tensor, ...] or list[tuple[Tensor, ...]]) –Attribution tensors, each of shape
(batch_size, *input_shape). For multi-target mode, pass a list of tuples — one tuple per target. -
(feature_masktuple[Tensor, ...] or None) –Boolean segmentation masks with the same shape as
attributions, marking the ground-truth relevant region. The same mask is used for all targets. IfNone, a mask of all-True values is used. -
(multi_targetbool, default:False) –If
True,attributionsmust be a list of tuples, one per target. Default:False. -
(positive_attributionsbool, default:True) –If
True, only positive attribution values contribute to the numerator. Default:True. -
(weightedbool, default:False) –If
True, multiply the score by the inverse mask-coverage fraction, rewarding small, precise masks. Default:False. -
(return_dictbool, default:False) –If
True, return{"score": ...}instead of a bare tensor. Default:False.
Returns:
-
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric] | dict[str, TensorOrTupleOfTensorsGeneric] | dict[str, list[TensorOrTupleOfTensorsGeneric]]–Tensor or list[Tensor] or dict: Localization score per example, shape
(batch_size,). -
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric] | dict[str, TensorOrTupleOfTensorsGeneric] | dict[str, list[TensorOrTupleOfTensorsGeneric]]–Returns a list when
multi_target=True, and a dict whenreturn_dict=True.
Example
import torch attributions = (torch.tensor([[0.1, 0.5, 0.3, 0.1]]),) mask = (torch.tensor([[False, True, True, False]]),) score = attribution_localization(attributions, feature_mask=mask)
Source code in torchxai/metrics/diagnosis/attribution_localization.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | |