Understanding Transformer-based Vision Models through Inversion
Authors: Jan Rathjens, Shirin Reyhanian, David Kappel, Laurenz Wiskott
Organizations: Institute for Neural Computation (INI), Faculty of Computer Science, Ruhr University Bochum, Germany · Center for Cognitive Interaction Technology (CITEC), Faculty of Technology, Bielefeld University, Germany
Understanding the internal mechanisms of deep neural networks remains a central challenge in machine learning. In computer vision, one promising yet only preliminarily explored approach is feature inversion via inverse networks, which reconstructs images from intermediate representations using trained inverse networks. In this study, we revisit feature inversion via inverse networks, introducing a novel, modular variant that enables a computationally more efficient application of the technique while, in some cases, producing semantically more coherent image reconstructions. We apply our method to large-scale transformer-based vision models, specifically Detection Transformer, Vision Transformer, Swin Transformer, and Data-Efficient Image Transformer, analyzing the resulting reconstructions across network depth. Our main analysis compares Detection Transformer and Vision Transformer, which exhibit the most informative differences among the evaluated architectures. At the same time, results for Swin Transformer and Data-Efficient Image Transformer support the broader applicability of our framework. Our findings reveal gradual representational changes across transformer layers as a shared characteristic of Detection Transformer and Vision Transformer, as well as systematic differences in how they preserve contextual shape and fine-grained image details, and in their robustness to color perturbations. These findings deepen understanding of transformer-based vision models and demonstrate the utility of modular feature inversion as an interpretability tool.
Figures & tables
Figure 1: Illustration of our approach. Left : We invert modules of transformer-based vision models, such as the backbone (bb) and encoder (enc). Right : Using these inverted modules, we reconstruct images from different processing stages to analyze DETR and ViT. Here, we recolor a scene and observe that DETR abstracts color into prototypical representations in its encoder, whereas ViT preserves color fidelity throughout its architecture.
Figure 2: Modular feature inversion of DETR. Illustration of the main modules of DETR (blue) and our modular inversion approach (red). Green double-arrows indicate correspondence between representations, i.e., the outputs of a forward module can be used as inputs to its corresponding inverse module during inference, and the inputs of a forward module can be used as a supervision signal during training.
Figure 3: Image reconstructions from processing stages. Column 1 shows the original input images. Columns 2-5 and 6-7 show reconstructions from different processing stages of DETR and ViT, respectively.
Figure 4: Left : Average reconstruction error across processing stages for DETR and ViT on the COCO validation set. mean denotes the reconstruction error between images and the dataset mean image. Center : Reconstruction loss (MSE) at the DETR decoder stage versus object detection performance (AP), evaluated across different values of λ . modular indicates our modular feature inversion approach without finetuning. Right : Reconstruction loss (MSE) at the ViT encoder stage versus classification accuracy (Acc) across varying λ .
Stage
MSE ( ×10−2 ) ↓
SSIM ↑
PSNR ↑
LPIPS ↓
FID ↓
CLIPScore ↑
AP/Acc ↑
DETR
\bb
1.86±0.90
0.50±0.16
17.8±2.2
0.58±0.08
110.6
0.77±0.05
0.05
\enc (m)
4.60±2.39
0.45±0.15
13.8±2.0
0.63±0.08
114.2
0.75±0.05
0.05
\enc (c)
4.28±2.03
0.45±0.15
14.1±2.0
0.63±0.08
134.5
0.72±0.05
0.02
\dec (m)
9.37±5.36
0.37±0.14
10.9±2.3
0.70±0.08
156.1
0.71±0.05
0.03
\dec (c)
6.41±2.75
0.41±0.15
12.3±1.9
0.65±0.07
309.5
0.67±0.04
0.00
Table 1: Quantitative comparison of reconstruction quality for modular (m) and classic (c) inversion across model stages. Results are reported for DETR on the COCO validation set, and for ViT, Swin, and DeiT III on the ImageNet-1k validation set. Values denote mean ± standard deviation, except for FID and task performance metrics. CLIPScore is computed using CLIP ResNet-101. Task performance is evaluated on reconstructed images: Average Precision (AP; IoU = 0.50:0.95, maxDets = 100) for DETR, and top-1 accuracy (Acc) for ViT, Swin, and DeiT III. Best values per stage and metric direction are highlighted in bold. For the first processing stage ( \bb ), modular and classic inversion are identical; therefore, only a single value is reported.
#Params (M)
Time (min/epoch)
Model
Classic
Modular
Classic
Modular
DETR
358
96
114
51
ViT / DeiT III
87
86
473
377
Swin
149
87
550
245
Table 2: Computational efficiency of classic and modular feature inversion. We report parameter count (#Params, in millions) and training time (min/epoch).
Figure 5: Reconstructions with finetuned models. Columns 2–5 show reconstructions from models finetuned with different λ values. Column 1 displays input images, and column 6 shows reconstructions using our modular feature inversion approach. Top : Images from the DETR \dec processing stage. Bottom : Images from the ViT \enc processing stage.
Figure 6: Effects of color perturbations. Rows show images where specific object categories were color-perturbed (from top to bottom: stop sign colored green, bear with colors rotated by 240°, apple with colors rotated by 120°, bus colored blue, giraffe converted to grayscale). Columns 2-4 and 5-6 show reconstructions from different processing stages of DETR and ViT, respectively.
Figure 7: Quantification of color perturbations. Left : Average pairwise MSE between image reconstructions of differently perturbed versions of an image, comparing inputs and reconstructions across processing stages. Shaded area indicates 95% confidence intervals over the COCO validation set. Right : Sensitivity of DETR and ViT to color perturbations (none, red, green, blue, 120° shift, 240° shift, grayscale) in relation to the performance of their objectives.
Figure 8: Structural transformation analysis in DETR and ViT
Figure 9: Image reconstructions from default (unmanipulated) and manipulated representations with DETR, a DETR variant with a local inverse backbone, and ViT. Tokens were replaced with the same random uniform noise at different processing stages before reconstruction. For \encman+ , embeddings were manipulated at the backbone stage, processed through \enc , and then used for reconstruction.
Figure 10: Image reconstructions from various processing stages of DETR. The first and second columns depict input images along with ground truth labels and predictions of DETR, respectively.
Figure 11: Image reconstructions from intermediate encoder and decoder layers. The left y-axis labels indicate the module from which an intermediate representation was extracted. The x-axis denotes the specific layer in that module from which this intermediate representation was extracted. The right y-axis labels indicate into which inverse module this intermediate representation was fed.
Figure 12: Image reconstructions from processing stages of DeiT III
Figure 13: Image reconstructions from the processing stages of DETR trained for image classification on ImageNet-1K. The first column shows the input images together with the ground truth label and the prediction by DETR.
Figure 14: Comparison of stage-wise reconstructions for three DETR variants trained for classification, with full, three blocks, and two blocks of ResNet-50 backbone.
Appendix figures & tables15 assets
Supplementary material from the paper’s appendix.
Appendix
Figure 15: Image reconstructions from various processing stages of DeiT III.
Figure 16: Image reconstructions from various processing stages of DeiT III on color-perturbed images, analogous to Figure 6 .
Figure 17: Image reconstructions from various processing stages of DeiT III on randomly manipulated images, similar to Figure 9 .
Figure 18: Image reconstructions from various processing stages of Swin.
Figure 19: Image reconstructions from various processing stages of Swin on color-perturbed images, analogous to Figure 6 .
Figure 20: Image reconstructions from various processing stages of Swin on randomly manipulated images, similar to Figure 9 .
Figure 21: Image reconstructions obtained with inverse backbones trained with different objectives. The figure shows COCO validation examples, reconstructed from successive DETR stages. Rows compare MSE-, SSIM-, and LPIPS-trained inverse backbones, highlighting how the training objective affects reconstructions while preserving the main qualitative trends across the DETR pipeline.
Figure 22: Quantitative analysis of stage-wise reconstruction consistency across inverse backbone objectives on the COCO validation set. The blue bar in each stage reports the average reconstruction error of the MSE-trained inverse backbone with respect to the original images. The remaining bars report, for each DETR stage, the difference between reconstructions produced by an alternative objective and those produced by the MSE-trained inverse backbone.
Error type
Positive
Negative
Ambiguous
False negatives
92
2
6
False positives
97
1
2
Appendix
Table 3: Quantitative analysis of detection errors traceability
Inverse module
Layer / Block
Output shape
Parameters
\pred−1
Linear + ReLU
[b,100,512]
49,664
Linear + ReLU
[b,100,256]
131,328
Linear + ReLU
[b,100,256]
65,792
\dec−1
Query embedding
[100,b,256]
25,600
6× DETR decoder block
[300,b,256]
9,472,512
Self-attention per layer
[300,b,256]
263,168
Appendix
Table 4: Architecture summary of the inverse modules for DETR. Output shapes are reported for a batch size of b . Repeated transformer blocks are grouped for compactness.
Inverse module
Layer / Block
Output shape
Parameters
\enc−1
12× ViT transformer block
[b,197,768]
85,026,816
LayerNorm before attention per block
[b,197,768]
1,536
Multi-head self-attention per block
[b,197,768]
2,360,064
LayerNorm before MLP per block
[b,197,768]
1,536
MLP per block
[b,197,768]
4,722,432
\bb−1
ConvTranspose2d + BN + ReLU
[b,256,28,28]
787,200
Appendix
Table 5: Architecture summary of the inverse modules for ViT and DeiT III. Output shapes are reported for a batch size of b . Repeated transformer blocks are grouped for compactness.
Inverse module
Layer / Block
Output shape
Parameters
s4−1
2× Swin transformer block
[b,7,7,1024]
25,203,264
LayerNorm before attention per block
[b,7,7,1024]
2,048
Window attention per block
[b,49,1024]
4,203,808
LayerNorm before MLP per block
[b,49,1024]
2,048
MLP per block
[b,49,1024]
8,393,728
PatchSplitting: LayerNorm + Linear
[b,14,14,512]
2,099,200
Appendix
Table 6: Architecture summary of the inverse modules for Swin. Output shapes are reported for a batch size of b . Repeated Swin Transformer blocks are grouped for compactness.
Model
Learning rate
Batch size
\bb−1 for DETR
0.001
32
\enc−1 for DETR
0.001
32
\dec−1 for DETR
0.001
32
\pred−1 for DETR
0.001
32
\bb−1 for ViT
0.001
64
\enc−1 for ViT
0.0001
64
Appendix
Table 7: Training hyperparameters for inverse modules.
Model
Learning rate
\bb−1 for DETR
0.001
\enc−1 for DETR
0.001
\dec−1 for DETR
0.000001
\pred−1 for DETR
0.000001
\bb−1 for ViT
0.001
\enc−1 for ViT
0.0001
Appendix
Table 8: Training hyperparameters for inverse networks.
Hyperparameter
Value
Batch size
256
Optimizer
LAMB
LR
0.003
Gradient Clip.
1.0
Epochs
400
LR decay
cosine
Appendix
Table 9: Hyperparameters used for fine-tuning a ViT with disabled self-attention
Institute for Cancer Genetics and Informatics Oslo University Hospital Oslo, Norway · Department of Informatics University of Oslo Oslo, Norway · SFI Visual Intelligence UiT The Arctic University of Norway Tromsø, Norway