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:

  • attributions

    (tuple[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_mask

    (tuple[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. If None, a mask of all-True values is used.

  • multi_target

    (bool, default: False ) –

    If True, attributions must be a list of tuples, one per target. Default: False.

  • positive_attributions

    (bool, default: True ) –

    If True, only positive attribution values contribute to the numerator. Default: True.

  • weighted

    (bool, default: False ) –

    If True, multiply the score by the inverse mask-coverage fraction, rewarding small, precise masks. Default: False.

  • return_dict

    (bool, 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 when return_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
def attribution_localization(
    attributions: tuple[torch.Tensor, ...] | list[tuple[torch.Tensor, ...]],
    feature_mask: tuple[torch.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.

    Args:
        attributions (tuple[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_mask (tuple[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. If ``None``, a mask of all-True values is used.
        multi_target (bool): If ``True``, ``attributions`` must be a list of tuples, one per target.
            Default: ``False``.
        positive_attributions (bool): If ``True``, only positive attribution values contribute to the
            numerator. Default: ``True``.
        weighted (bool): If ``True``, multiply the score by the inverse mask-coverage fraction,
            rewarding small, precise masks. Default: ``False``.
        return_dict (bool): If ``True``, return ``{"score": ...}`` instead of a bare tensor.
            Default: ``False``.

    Returns:
        Tensor or list[Tensor] or dict: Localization score per example, shape ``(batch_size,)``.
        Returns a list when ``multi_target=True``, and a dict when ``return_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)
    """
    if feature_mask is None:
        feature_mask = tuple(torch.ones_like(attributions[0][0], dtype=torch.bool))

    if multi_target:
        assert isinstance(attributions, list), (
            "For multi-target attributions, a list of attributions per target must be provided."
        )
        scores_per_target = [
            _attribution_localization_impl(
                attributions=attribution_per_sample,
                feature_mask=feature_mask,
                positive_attributions=positive_attributions,
                weighted=weighted,
            )
            for attribution_per_sample in attributions
        ]
        if return_dict:
            return {"score": scores_per_target}
        else:
            return scores_per_target
    else:
        assert isinstance(attributions, tuple | torch.Tensor), (
            "For single-target attributions, a tuple or list of attributions must be provided."
        )
        assert all(isinstance(t, torch.Tensor) for t in attributions), (
            "Attributions must be a tensor or a tuple of tensors."
        )
        score = _attribution_localization_impl(
            attributions=attributions,
            feature_mask=feature_mask,
            positive_attributions=positive_attributions,
            weighted=weighted,
        )
        return {"score": score} if return_dict else score