Context can strongly affect object representations, sometimes leading to undesired biases, particularly when objects appear in out-of-distribution (OOD) backgrounds at inference. At the same time, many object-centric tasks require to leverage context for identifying the relevant image regions. We posit that this conundrum, in which context is simultaneously needed and a potential nuisance, can be addressed by an attention-based approach that uses learned binary attention masks to ensure that only attended image regions influence the prediction.
To test this hypothesis, we evaluate a two-stage framework: stage 1 processes the full image to discover object parts and identify task-relevant regions, for which context cues are likely to be needed, while stage 2 leverages input attention masking to restrict its receptive field to these regions, enabling a focused analysis while filtering out potentially spurious information. Both stages are trained jointly, allowing stage 2 to refine stage 1. The explicit nature of the semantic masks also makes the model's reasoning auditable, enabling powerful test-time interventions to further enhance robustness. Extensive experiments across diverse benchmarks demonstrate that this approach significantly improves robustness against spurious correlations and out-of-distribution backgrounds.
Conventional attention-based methods (Late Masking) learn a selector \(h_{\theta_s}(\cdot)\) and a feature extractor \(h_{\theta_p}(\cdot)\), computing the output as:
Because \(h_{\theta_p}(\mathbf{x})\) is a deep feature tensor whose tokens have already exchanged information globally, spurious background cues leak into the foreground features.
In contrast, iFAM (Early Masking) computes a binary token selection mask \(\mathbf{s} \in \{0, 1\}^N\) from Stage 1, and enforces hard input masking inside Stage 2 by directly modulating the Vision Transformer's multi-head self-attention:
where the elements of the attention mask \(\mathbf{M} \in \mathbb{R}^{N \times N}\) are defined as:
Setting \(M_{ij} = -\infty\) forces attention weights to and from masked-out tokens to be exactly zero after the softmax, guaranteeing that background tokens cannot contaminate the final image representation.
Stage 1 discovers \(K\) distinct foreground object parts plus 1 background channel using prototype-driven part discovery guided by classification and Total Variation priors. The resulting foreground part maps are discretized using a Gumbel-Softmax straight-through gradient estimator: hard binary assignments are used in the forward pass while continuous soft probabilities backpropagate gradients, allowing Stage 2 to end-to-end refine Stage 1.
Because iFAM’s part representations are semantically consistent and auditable, users can perform training-free interventions at inference:
Jump to Full Interventions Section, Part Maps & Empirical Results ↓
iFAM is extensively benchmarked across four standard out-of-distribution and debiasing datasets: MetaShift (Cat vs. Dog with spurious context), Waterbirds (waterbirds/landbirds with conflicting backgrounds), ImageNet-9 Backgrounds Challenge, and SIIM-ACR Pneumothorax chest radiographs.
| Category | Method | Arch. | MetaShift (%) | Waterbird (%) | ||||
|---|---|---|---|---|---|---|---|---|
| \(K\) | Avg. Acc (AA) | Worst-Group (WGA) \(\uparrow\) | \(K\) | Avg. Acc (AA) | Worst-Group (WGA) \(\uparrow\) | |||
| Upper Bounds | Early mask (GT)† | ViT-B | -- | -- | -- | 1 | 99.2 | 97.2 |
| Supervised | ERM | ViT-B | -- | 75.8 | 62.5 | -- | 95.0 | 80.7 |
| Self-Supervised | DinoV2 (Frozen) | ViT-B | -- | 83.2 | 72.6 | -- | 95.9 | 88.5 |
| DinoV2 (Fine-tuned) | ViT-B | -- | 84.7 | 76.8 | -- | 98.6 | 95.8 | |
| Specialized De-biasing | GroupDRO | R-50 | -- | 73.6 | 66.0 | -- | 91.8 | 90.6 |
| DISC | R-50 | -- | 75.5 | 73.5 | -- | 93.8 | 88.7 | |
| Late Masking | DinoV2 + AIM | ViT-B | -- | 88.9 | 83.0 | -- | 97.6 | 93.9 |
| PDiscoFormer | ViT-B | 4 | 83.2 | 75.5 | 8 | 94.2 | 84.3 | |
| Early Masking | iFAM (Ours) | ViT-B | 4 | 88.7 | 88.6 | 8 | 99.0 | 97.0 |
† Uses ground truth bounding boxes/segmentations as upper bound. Shaded columns denote out-of-distribution robustness metrics (WGA).
| Category | Method | Arch. | IN-1K | IN-9O | Mixed-Same (MS) | Mixed-Rand (MR) | BG-GAP \(\downarrow\) |
|---|---|---|---|---|---|---|---|
| Supervised | ERM | R-50 | 81.2 | 96.4 | 90.0 | 84.6 | 5.4 |
| ERM | ViT-B | 83.8 | 97.9 | 92.4 | 87.9 | 4.6 | |
| Self-Supervised | DinoV2 | ViT-B | 84.6 | 98.1 | 93.1 | 87.1 | 6.0 |
| DinoV2 | ViT-L | 86.7 | 98.3 | 95.5 | 90.2 | 5.3 | |
| Specialized De-biasing | LLE | R-50 | 76.3 | 95.5 | 88.3 | 83.4 | 4.9 |
| Late Masking | PDiscoFormer (\(K=1\)) | ViT-B | 83.3 | 98.4 | 93.9 | 88.6 | 5.3 |
| Early Masking | iFAM (Ours, \(K=1\)) | ViT-B | 84.3 | 97.5 | 93.5 | 91.1 | 2.4 |
\(\text{BG-GAP} = \text{MS} - \text{MR}\) measures reliance on spurious background cues (lower is better). iFAM slashes BG-GAP to just 2.4%.
| Category | Method | \(K\) | CUB (In-distribution) | Waterbird200 (OOD Backgrounds) \(\uparrow\) |
|---|---|---|---|---|
| Upper Bounds | Early mask (GT Seg)† | 1 | 91.4 | 88.8 |
| Late mask (GT Seg)† | 1 | 90.7 | 74.8 | |
| Self-Supervised | ViT-B DinoV2 (Frozen) | -- | 89.2 | 76.6 |
| ViT-B DinoV2 (Fine-tuned) | -- | 91.6 | 68.4 | |
| Late Masking | PDiscoFormer | 4 | 89.1 | 76.0 |
| PDiscoFormer | 8 | 88.8 | 76.8 | |
| PDiscoFormer | 16 | 88.7 | 75.8 | |
| Early Masking | iFAM (Ours) | 4 | 90.1 | 86.1 |
| iFAM (Ours) | 8 | 90.4 | 86.2 | |
| iFAM (Ours) | 16 | 90.6 | 86.2 |
Unlike black-box models, iFAM's explicit part decomposition and Early Hard Masking allow practitioners to audit and intervene at inference—mitigating spurious shortcuts with zero model retraining.
When a conventional deep neural network or late-masking model learns a spurious shortcut (e.g., predicting cat from an indoor sofa, or detecting pneumothorax based on chest drainage tubes), remediating the bias typically demands collecting expensive counterfactual data or retraining from scratch.
In contrast, iFAM provides inherent auditability: Stage 1 discovers semantically consistent, discrete part masks, while Stage 2 guarantees that unmasked regions are physically excluded from the Vision Transformer's receptive field. This unlocks two powerful, training-free intervention strategies that can be executed manually by a human expert or programmatically via automated validation:
The Mechanism: When the number of parts \(K\) is set high, weakly supervised part discovery may allocate an entire part to a strongly correlated background feature or medical artifact. Because learned parts are shared across classes and semantically consistent across all images, a user can visually inspect a handful of images to spot the spurious part, or an automated Leave-One-Out (LOO) validation search can programmatically exclude parts whose removal improves worst-group performance:
Because Stage 2 enforces early hard masking at the attention layer, the excluded part is strictly eliminated from the model's receptive field, preventing the classifier from exploiting the confounder.
Part assignment maps (top/bottom rows: pure, middle row: spurious).
| Active Parts | WGA (%) | Impact |
|---|---|---|
| All Parts | 78.8 | Baseline |
| -- Part 1 | 64.7 | -14.1 |
| -- Part 2 | 75.8 | -3.0 |
| -- Part 3 | 75.8 | -3.0 |
| -- Part 4 | 78.8 | 0.0 |
| -- Part 5 | 75.5 | -3.3 |
| -- Part 6 (Brown) | 81.7 | +2.9 (Spurious Indoor Cue) |
| -- Part 7 | 77.1 | -1.7 |
| -- Part 8 | 69.9 | -8.9 |
Finding: Part 6 consistently tracked indoor furniture correlated with cats. Dropping this single part boosts Worst-Group Accuracy by +2.9%.
Part assignment maps + ground truth pneumothorax occurrence heatmap.
| Active Parts | WG AUC (%) | Impact |
|---|---|---|
| All Parts | 65.9 | Baseline |
| -- Part 1 | 65.0 | -0.9 |
| -- Part 2 | 59.9 | -6.0 |
| -- Part 3 | 63.7 | -2.2 |
| -- Part 4 (Red) | 67.3 | +1.4 (Drainage Tube Artifact) |
| -- Part 5 | 65.2 | -0.7 |
| -- Part 6 | 65.6 | -0.3 |
| -- Part 7 | 66.7 | +0.8 |
| -- Part 8 | 65.5 | -0.4 |
Finding: Part 4 covers the central chest where chest drainage tubes (a confounding artifact) are placed, far from peripheral pneumothorax lesions. Removing it boosts Worst-Group AUC by +1.4%.
The Mechanism: On out-of-distribution (OOD) test inputs with unfamiliar backgrounds or noisy artifacts, Stage 1 can produce false-positive token activations. We prune unconfident tokens by comparing their feature distance to the learned part prototypes:
The threshold \(\tau_k^q\) is calibrated on the training set using a high percentile \(q\) (e.g., \(q = 97\%\) or \(99\%\)), representing the proportion of training tokens assigned to part \(k\) closer than \(\tau_k^q\) to prototype \(\mathbf{p}_k\). Tokens exceeding this distance at test time are reassigned to background.
| Method | MetaShift (\(K=8\)) | Waterbird (\(K=16\)) | SIIM-ACR (\(K=8\)) | Waterbird200 (OOD) | |||||
|---|---|---|---|---|---|---|---|---|---|
| AA (%) | WGA (%) | AA (%) | WGA (%) | Avg AUC | WG AUC | \(K=4\) | \(K=8\) | \(K=16\) | |
| iFAM (Baseline) | 84.5 | 78.8 | 98.8 | 97.0 | 92.1 | 65.9 | 86.1 | 86.2 | 86.2 |
| Token-Removal (\(q=97\%\)) | 84.7 (+0.2) | 79.1 (+0.3) | 98.7 (-0.1) | 96.6 (-0.4) | 92.0 (-0.1) | 66.0 (+0.1) | 86.8 (+0.7) | 86.7 (+0.5) | 87.3 (+1.1) |
| Token-Removal (\(q=99\%\)) | 84.7 (+0.2) | 80.1 (+1.3) | 98.8 (0.0) | 97.4 (+0.4) | 92.2 (+0.1) | 66.4 (+0.5) | 86.6 (+0.5) | 86.9 (+0.7) | 86.9 (+0.7) |
Token-removal filtering consistently improves OOD generalization across all benchmarks while preserving in-distribution accuracy.
Because Part-Removal operates at the macro semantic level (pruning entire confounded part channels) and Token-Removal operates at the micro confidence level (pruning noisy boundary tokens across all remaining parts), their effects are highly complementary.
Crucially, Table 4 demonstrates that Early Masking is an architectural prerequisite for interventions to succeed:
| Model | Intervention Strategy | MetaShift | SIIM-ACR | ||
|---|---|---|---|---|---|
| AA (%) | WGA (%) | Avg AUC (%) | Worst-Group AUC (%) | ||
| PDiscoFormer (Late Masking) |
No Intervention | 83.2 | 75.5 | 92.6 | 48.1 |
| + LOO Part-Removal | 85.2 | 76.8 (+1.3) | 92.6 | 48.1 (0.0) | |
| + LOO + Token-Removal (\(q=99\%\)) | 85.4 | 76.8 (+1.3) | 92.6 | 48.2 (+0.1) | |
| iFAM (Ours) (Early Hard Masking) |
No Intervention | 84.5 | 78.8 | 92.1 | 65.9 |
| + LOO Part-Removal | 84.7 | 81.7 (+2.9) | 90.6 | 67.3 (+1.4) | |
| + LOO + Token-Removal (\(q=99\%\)) | 84.8 | 83.0 (+4.2) | 91.1 | 69.0 (+3.1) | |
Visualizations of discovered semantic regions across diverse benchmarks from the main paper and supplementary material. iFAM selectively segments the task-relevant object foreground across various part granularities (\(K\)), preventing spurious background correlations from entering the Vision Transformer's receptive field.
Qualitative results on CUB (\(K=8\)), corresponding out-of-distribution (OOD) images from Waterbird-200 with conflicting synthetic backgrounds, and the effect of test-time thresholding intervention (\(q=99\%\) and \(q=97\%\)) from Supplementary Fig. 1.
| CUB Input | iFAM (CUB) | Waterbird-200 (OOD) | iFAM (WB200) | Intervention (\(q=99\%\)) | Intervention (\(q=97\%\)) |
|---|---|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Observations: On Waterbird-200, synthetic background replacement causes small background false positives. Token-removal intervention with \(\tau_k^q\) successfully reclassifies low-confidence boundary tokens as background, recovering crisp part segmentations without model retraining.
Qualitative results of iFAM trained on CUB for different numbers of foreground parts \(K\) (from Supplementary Fig. 4).
| Image | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|---|---|---|---|
| \(K=1\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=4\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=8\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=16\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
At \(K=1\), the model extracts holistic foreground. At higher \(K\) (\(4, 8, 16\)), it automatically discovers consistent fine-grained anatomical components (head, wings, belly, tail, legs) with zero background leakage.
Qualitative results of iFAM trained on Waterbirds across different numbers of foreground parts \(K\) (from Supplementary Fig. 5).
| Image | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|---|---|---|---|
| \(K=1\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=4\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=8\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=16\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Water ripples, forest canopies, and marshland backgrounds are discarded across all part settings. Notice the high part consistency across varied bird species and orientations.
Qualitative results of iFAM trained on MetaShift across different numbers of foreground parts \(K\) (from Supplementary Fig. 6).
| Image | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
|---|---|---|---|---|---|---|---|---|---|
| \(K=1\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=2\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=4\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
| \(K=8\) | ![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
![]() |
Even with extreme background diversity (indoor furniture, beds, outdoor streets), iFAM reliably discovers torso, head, and facial regions of cats and dogs across granularities.
Discovered torso part maps on chest radiographs alongside the dataset-wide ground truth pneumothorax occurrence heatmap.
On chest radiographs, standard classifiers latch onto chest drainage tubes (a confounding artifact). iFAM localizes true lung pathologies, and the central tube region can be pruned using our test-time intervention.
All pre-trained iFAM models across Waterbirds, CUB, MetaShift, and SIIM-ACR are hosted on the Hugging Face Hub and can be loaded with minimal lines of code via PyTorchModelHubMixin:
from models import FullTwoStageModelDoubleClassify, FullTwoStageModelDoubleClassifyHF
# Load pre-trained Waterbirds model (8 parts, timm ViT-B backbone)
model = FullTwoStageModelDoubleClassify.from_pretrained("ananthu-aniraj/ifam-waterbirds-k8")
model.eval()
# Load pre-trained SIIM-ACR Pneumothorax medical model (transformers-based backbone)
model_med = FullTwoStageModelDoubleClassifyHF.from_pretrained("ananthu-aniraj/ifam-siim-acr-k8")
model_med.eval()
# Run forward pass:
# output contains stage 1 discovered masks and stage 2 robust classification logits
output = model(images)
@inproceedings{aniraj2026ifam,
title={Two-stage Vision Transformers and Hard Masking offer Robust Object Representations},
author={Aniraj, Ananthu and Dantas, Cassio F. and Ienco, Dino and Marcos, Diego},
booktitle={International Conference on Pattern Recognition (ICPR)},
year={2026},
organization={Springer},
doi={10.1007/978-3-032-31673-8_20},
url={https://link.springer.com/chapter/10.1007/978-3-032-31673-8_20}
}