Explainable Vision Transformer

Machine Learning
Introduced by
Chefer, H., Gur, S., & Wolf, L. (attribution framework); Dosovitskiy et al. (base ViT)
Year
2021
Type
Post-hoc explainability applied to Vision Transformer
Family
Deep learning / NLP / CV

What does it do?

Explainable Vision Transformer applies post-hoc attribution to a Vision Transformer (ViT), producing maps that show which image patches contributed to a given prediction.

The approach is aimed at researchers and practitioners who need to audit a ViT's decisions and who work with a model whose accuracy is already judged adequate. The cited attribution framework (Chefer et al., 2021) computes relevance through the transformer's attention and skip-connection structure rather than visualizing raw attention.

The intuition behind the method

A Vision Transformer splits an image into fixed-size patches, treats them as tokens, and lets self-attention layers relate every patch to every other patch. This global mixing makes ViTs accurate but opaque: it is not obvious which image regions drove a prediction. Explainability methods assign a relevance score to each patch by tracing the model's computation backward through its layers, and the resulting heatmap shows which parts of the image the model relied on for the predicted class.

History

Dosovitskiy et al. introduced the Vision Transformer as an attention-based architecture for image recognition. Chefer, Gur, and Wolf (CVPR 2021) noted that existing explanation methods relied either on attention maps or on heuristic propagation along the attention graph, and proposed a relevance computation for Transformers based on Deep Taylor Decomposition. Their method was benchmarked on recent visual Transformer networks and on a text classification problem.

When to use it

Use Explainable ViT when you need both good recognition accuracy from a ViT and a way to inspect the image regions behind its individual predictions, for example when a fine-tuned ViT must be checked on a labelled image set before it is trusted. It fits best when a ViT has already been trained or fine-tuned and validated on your data, and the remaining need is per-prediction attribution for debugging, error analysis, or documentation. The cited work benchmarks visual Transformer networks on image classification and a text classification problem. For other domains, including high-stakes ones such as medical imaging or autonomous driving, suitability must be tested on your own data with expert review. Do not use it when the training data are too limited to fine-tune a ViT reliably, when a CNN already meets your accuracy and explanation needs with a simpler attribution method, or when the deployment environment cannot absorb the extra cost of extracting attention and running the attribution pass.

Limitations and considerations

  • Attention extraction and the attribution pass add memory and time cost at inference, which can be significant for high-resolution inputs with many patches.
  • Attribution quality depends on the underlying ViT: if the model is underfitted because training data were insufficient, its explanations describe a poor model.
  • Output resolution is bounded by the patch grid, so the heatmap cannot localise features more finely than a patch.
  • Other attribution methods applied to the same ViT can produce visibly different heatmaps, so the choice of method affects the explanation and should be reported.

Source conditions do not replace independent scientific review or validation by an analysis engine.

Strengths

  • Applies a transformer-specific relevance computation, which the cited work presents as an improvement over earlier explanation methods that rely on attention maps or heuristic propagation.
  • Propagates relevance through attention layers and skip connections, so the explanation accounts for the residual structure of the network rather than only the final attention map.
  • Works on pre-trained or fine-tuned ViT models as a post-hoc step, without changing the architecture or retraining.
  • Produces class-specific maps, so the explanation can be examined for each predicted label.

Applications

  • Image classification with Vision Transformer networks, where per-prediction heatmaps show which image regions the model weighted for the predicted class. This is the setting benchmarked in the cited work.
  • Error analysis and debugging of a fine-tuned ViT on a labelled image set, for example checking whether highlighted regions fall on the target structure or on background, combined with a perturbation-based faithfulness test.
  • Documentation of per-prediction behaviour for a ViT already validated on your data, where heatmaps are reviewed alongside accuracy metrics rather than in place of them.

How it works

How it works

1

The input image is divided into a regular grid of non-overlapping patches (commonly 16x16 pixels). Each patch is projected into an embedding vector and given a positional encoding so the model knows where each patch came from. A class token is prepended, and its final representation carries the classification decision.

2

As the patch tokens pass through the multi-head self-attention layers, the attention weights are recorded at each layer and head. Raw attention shows how information is routed between tokens, not how much each token contributed to the class decision, so these recorded matrices serve only as inputs to the attribution step.

3

The cited framework (Chefer et al., 2021) assigns local relevance following the Deep Taylor Decomposition principle and propagates it through the layers, from the class output backward across attention layers and skip connections. Attention layers and skip connections are what challenge existing methods, and the formulation is designed to maintain the total relevance across layers. The exact per-layer rules are given in the original paper and should be consulted before implementation.

4

The per-patch relevance scores are reshaped to the spatial grid of patches and up-sampled (for example, bilinearly) to the original image resolution. The resulting heatmap, often overlaid on the input image, marks high-relevance regions and low-relevance regions for the predicted class. Its resolution is limited by the patch grid, so fine detail within a patch is not resolved.

5

Explanation quality is commonly checked with perturbation-based faithfulness tests, such as masking the most relevant patches and measuring how much the model's class score or accuracy falls. Agreement with expert annotations can complement these tests, but a plausible-looking heatmap does not by itself establish that the highlighted regions caused the prediction. In high-stakes settings, expert review of the highlighted regions is needed before the explanations are relied on.

Common pitfalls

  • Using raw attention weights as explanations: attention shows information routing and can highlight tokens that do not support the predicted class.
  • Applying the method before validating the ViT's accuracy on your data: explaining a poorly performing model yields explanations that cannot be trusted.
  • Presenting 16x16-patch heatmaps as pixel-precise explanations without stating the spatial resolution limit.
  • Reporting only qualitative heatmaps without a faithfulness test, which cannot separate genuine attribution from visual artefacts.
  • Reading highlighted regions as causes: the map shows what the model's computation weighted for a prediction, not what causes the real-world outcome.

Frequently asked questions

Is raw attention visualization the same as an explanation?

No. Raw attention weights indicate where the model routes information, not which tokens contributed to the predicted class. Attribution methods such as the relevance propagation of Chefer et al. (2021) are designed to produce class-specific relevance scores.

Do I need to retrain the ViT to make it explainable?

Generally no. The attribution step is post-hoc and works on a trained ViT without changing its architecture. It requires access to the model's internal attention and to the computation needed for the attribution pass.

How do I know if the heatmap is trustworthy?

Use a perturbation-based faithfulness test: mask the patches the heatmap marks as most relevant and measure how quickly the model's accuracy or class score falls. A faithful explanation should produce a clear drop when those patches are removed.

Can Explainable ViT be applied to video or 3D data?

The cited work does not evaluate video or volumetric data. Its benchmarks cover visual Transformer networks on image classification and a text classification problem. Any use on video or 3D data would need separate validation on your own data, and the computational cost is likely to be higher because the number of tokens grows.

How does this compare to GradCAM on a CNN?

GradCAM is a gradient-based method for CNNs that uses gradient-weighted activations of a convolutional layer. The cited work reports a clear advantage over existing explainability methods on its benchmarks, but it does not specifically report a comparison with GradCAM. Test both methods on your own data, since the transformer method is computationally heavier.

Related methods

Sources

  1. Chefer, H., Gur, S., & Wolf, L. (2021). Transformer interpretability beyond attention visualization. In Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR), 782–791.

    Open DOI reference
  2. Dosovitskiy, A., Beyer, L., Kolesnikov, A., Weissenborn, D., Zhai, X., Unterthiner, T., … Houlsby, N. (2021). An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale. In International Conference on Learning Representations (ICLR).

    Open publication reference