maps
maps
¶
Feature-grid and image-grid attribution map helpers.
get_batch_temporal_shape
¶
Return batch and temporal dimensions from a camera tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_tensor
|
Tensor
|
Camera tensor with shape |
required |
Returns:
| Type | Description |
|---|---|
int
|
|
int
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tensor is neither 4D nor 5D. |
Source code in src/versatil/explainability/attribution/maps.py
get_image_size
¶
Return image height and width from a camera tensor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera_tensor
|
Tensor
|
Camera tensor with shape |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tensor is neither 4D nor 5D. |
Source code in src/versatil/explainability/attribution/maps.py
normalize_map
¶
Normalize each heatmap independently to [0, 1].
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
heatmap
|
Tensor
|
Heatmap tensor with spatial dimensions in the final two axes. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with the same shape as |
Source code in src/versatil/explainability/attribution/maps.py
activation_to_nchw
¶
Convert captured feature-map activations to NCHW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Captured activation or gradient tensor. |
required |
activation_layout
|
str
|
Layout from :class: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/versatil/explainability/attribution/maps.py
nchw_to_activation_layout
¶
Convert NCHW activations back to their captured layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Tensor with shape |
required |
activation_layout
|
str
|
Target layout from :class: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor in |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/versatil/explainability/attribution/maps.py
token_sequence_to_nchw
¶
Convert ViT patch-token tensors to NCHW.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Token tensor with shape |
required |
target
|
VisionExplanationTarget
|
Token target metadata containing prefix-token and patch-grid information. |
required |
tensor_name
|
str
|
Name used in validation errors. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with shape |
Tensor
|
tokens. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target layout is not |
ValueError
|
If |
RuntimeError
|
If patch-grid resolution fails. |
Source code in src/versatil/explainability/attribution/maps.py
target_tensor_to_nchw
¶
Convert a captured target tensor to NCHW for feature-grid attribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Captured activation or gradient tensor. |
required |
target
|
VisionExplanationTarget
|
Target metadata describing tensor kind and layout. |
required |
tensor_name
|
str
|
Name used in validation errors. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Tensor with shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target kind or activation layout is unsupported. |
Source code in src/versatil/explainability/attribution/maps.py
nchw_to_target_tensor
¶
Convert ablated NCHW activations back to the target-layer output layout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tensor
|
Tensor
|
Ablated activation tensor with shape |
required |
target
|
VisionExplanationTarget
|
Target metadata describing whether the activation came from a spatial feature map or a ViT token sequence. |
required |
original_tensor
|
Tensor
|
Original selected target-layer output. Token targets keep prefix tokens from this tensor because they are not part of the patch heatmap grid. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Replacement tensor matching |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target kind or activation layout is unsupported. |
ValueError
|
If a token target does not have a 3D original tensor. |
RuntimeError
|
If token patch-grid metadata disagrees with |
Source code in src/versatil/explainability/attribution/maps.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
compute_gradcam
¶
Compute GradCAM or GradCAM++ from captured target activations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activation
|
Tensor
|
Captured target activation. |
required |
gradient
|
Tensor
|
Gradient of the selected prediction with respect to
|
required |
target
|
VisionExplanationTarget
|
Target metadata describing activation layout. |
required |
explanation_type
|
str
|
GradCAM variant from :class: |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Unnormalized heatmaps with shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target layout is unsupported. |
RuntimeError
|
If token patch-grid resolution fails. |
Source code in src/versatil/explainability/attribution/maps.py
resolve_patch_grid
¶
Resolve patch-token count to a 2D grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
target
|
VisionExplanationTarget
|
Token target metadata. |
required |
token_count
|
int
|
Number of patch tokens after dropping prefix tokens. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
|
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If an explicit grid does not match |
RuntimeError
|
If no grid is configured and |
Source code in src/versatil/explainability/attribution/maps.py
compute_target_map
¶
Compute a feature-grid heatmap from a captured target.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
activation
|
Tensor
|
Captured target activation. |
required |
gradient
|
Tensor
|
Captured gradient for |
required |
target
|
VisionExplanationTarget
|
Target metadata describing target kind and layout. |
required |
explanation_type
|
str
|
Requested gradient explanation type. |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Unnormalized heatmaps on the target feature grid. |
Source code in src/versatil/explainability/attribution/maps.py
resize_feature_heatmap_to_camera
¶
Resize flattened feature heatmaps back to camera image windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
feature_heatmap
|
Tensor
|
Feature-grid heatmap with shape |
required |
camera_tensor
|
Tensor
|
Source camera tensor with shape |
required |
Returns:
| Type | Description |
|---|---|
Tensor
|
Normalized image-space heatmap with shape |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |