OcclusionExplainer
Occlusion explainer for computing sliding-window perturbation attributions.
This explainer computes attributions using the Occlusion method, which systematically replaces rectangular regions of the input with baseline values (typically zeros) and measures the resulting change in model output. This approach is particularly effective for image data where spatial regions can be meaningfully occluded. Supports both single-target and multi-target modes for both single-target and multi-target scenarios.
The Occlusion method provides intuitive attributions by directly measuring the importance of spatial regions through systematic perturbation.
Parameters:
-
(modelModule) –The PyTorch model whose output is to be explained.
-
–sliding_window_shapesShape of the occlusion window for each input tensor. Can be a single tuple for all inputs or tuple of tuples for each input.
-
–stridesStride for sliding the occlusion window. Can be int, tuple, or tuple of tuples. If None, defaults to sliding_window_shapes.
-
(multi_targetbool, default:False) –Whether to use multi-target mode. When True, can compute attributions for multiple targets simultaneously. Defaults to False.
-
(internal_batch_sizeint, default:1) –Batch size for internal computations (perturbations per evaluation). Defaults to 1.
Examples:
Single-target usage for image data:
>>> import torch
>>> from torchxai.data_types import SingleTargetAcrossBatch
>>>
>>> model = torch.nn.Sequential(
... torch.nn.Conv2d(3, 16, 3, padding=1), torch.nn.ReLU(),
... torch.nn.AdaptiveAvgPool2d(1), torch.nn.Flatten(),
... torch.nn.Linear(16, 10),
... )
>>> explainer = OcclusionExplainer(model)
>>> inputs = torch.randn(1, 3, 32, 32)
>>> attributions = explainer.explain(
... inputs=inputs,
... sliding_window_shapes=(1, 8, 8),
... target=SingleTargetAcrossBatch(index=0),
... )
>>> attributions.shape # (1, 3, 32, 32)
Multi-target usage:
>>> explainer_mt = OcclusionExplainer(model, multi_target=True)
>>> mt_attributions = explainer_mt.explain(
... inputs=inputs,
... sliding_window_shapes=(1, 8, 8),
... target=[SingleTargetAcrossBatch(index=0), SingleTargetAcrossBatch(index=1)],
... )
>>> len(mt_attributions), mt_attributions[0].shape # 2, (1, 3, 32, 32)
Methods:
-
explain–Compute Occlusion attributions for the given inputs.
Source code in torchxai/explainers/_perturbation/_occlusion.py
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |
explain
explain(
inputs: TensorOrTupleOfTensorsGeneric,
target: ExplanationTargetType | list[ExplanationTargetType],
sliding_window_shapes: tuple[int, ...] | tuple[tuple[int, ...], ...],
strides: None | int | tuple[int, ...] | tuple[int | tuple[int, ...], ...] = None,
baselines: TensorOrTupleOfTensorsGeneric | None = None,
additional_forward_args: tuple[Any, ...] | None = None,
) -> TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]
Compute Occlusion attributions for the given inputs.
Parameters:
-
(inputsTensorOrTupleOfTensorsGeneric) –Input tensor(s) for attribution computation.
-
(targetExplanationTargetType | list[ExplanationTargetType]) –An
ExplanationTargetType(e.g.SingleTargetAcrossBatch) for single-target mode, or a list of them for multi-target mode. -
(baselinesTensorOrTupleOfTensorsGeneric | None, default:None) –Baseline tensors for occlusion (typically zeros). If None, uses zero baselines matching input shape.
-
(additional_forward_argstuple[Any, ...] | None, default:None) –Additional arguments for model forward pass.
Returns:
-
TensorOrTupleOfTensorsGeneric | list[TensorOrTupleOfTensorsGeneric]–Tensor in single-target mode. List of Tensors, one per target, in multi-target mode.
Note
The sliding window and stride parameters are set during initialization. Attribution values represent the importance of each spatial region as measured by occlusion impact.
Examples:
>>> # For image data with 8x8 occlusion windows
>>> attributions = explainer.explain(
... inputs=OrderedDict({"image": torch.randn(1, 3, 224, 224)}),
... target=torch.tensor([285]), # ImageNet class
... baselines=OrderedDict({"image": torch.zeros(1, 3, 224, 224)}),
... )
Source code in torchxai/explainers/_perturbation/_occlusion.py
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 | |