Transformer in Transformer

Kai Han, An Xiao, Enhua Wu, Jianyuan Guo, Chunjing Xu, Yunhe Wang

Abstract

The "Transformer in Transformer" (TNT) architecture introduces a nested hierarchy to visual recognition, treating image patches as "visual sentences" and further dividing them into "visual words." By using an inner transformer to model local pixel-level relationships and an outer transformer for global patch-level context, TNT achieves significantly better accuracy on ImageNet (up to 82.9% top-1) with only a marginal increase in computational cost.

Introduction

Standard Vision Transformers (ViT) typically divide an image into a grid of local patches (e.g., 16x16 pixels). These patches are then flattened and treated as a sequence of tokens. While this captures long-range dependencies across the image, it suffers from a "granularity gap." A 16x16 patch contains 256 pixels with rich, local structural information—edges, textures, and color gradients. Standard ViTs use a simple linear projection to turn these patches into embeddings, which can "corrupt" or oversimplify the local structure. The authors argue that modeling the attention inside these patches is just as essential as modeling the attention between them for high-performance computer vision.

Methods

The core innovation is the TNT block, which consists of two nested transformer components:

  1. Inner Transformer Block ($T_{in}$): This block operates at the "word" level. Each 16x16 patch is further divided into smaller sub-patches (e.g., 4x4 "visual words"). The inner transformer calculates self-attention among these words within a single patch to capture local features.
  2. Outer Transformer Block ($T_{out}$): This is the standard transformer architecture that models the relationships between the 16x16 "visual sentences" across the entire image.
  3. The Link: After the inner transformer processes the words, the resulting word embeddings are flattened, linearly projected to match the dimension of the sentence embeddings, and added to them. This "augments" the global patch representation with fine-grained local details.
The TNT architecture featuring nested inner and outer transformer blocks.

To keep the model efficient, the inner transformer's weights are shared across all visual sentences in a given layer. This ensures that the increase in parameters and FLOPs (floating-point operations) remains negligible (roughly 1.14x the FLOPs of a standard ViT).

Experimental Results

The authors evaluated TNT on a wide range of benchmarks, including ImageNet-1K, downstream classification tasks (Oxford Flowers/Pets, CIFAR), object detection (COCO), and semantic segmentation (ADE20K).

Key Results

  • ImageNet Performance: TNT-S (Small) achieved 81.5% top-1 accuracy, which is 1.7% higher than the baseline DeiT-S with similar computational complexity. The larger TNT-B reached 82.9%.
  • Transfer Learning: TNT models consistently outperformed ViT and DeiT across all downstream datasets (e.g., achieving 91.1% on CIFAR-100).
  • Object Detection: When used as a backbone for DETR on the COCO dataset, TNT-S outperformed PVT-Small by 3.5 AP (Average Precision), demonstrating that the fine-grained features are particularly helpful for locating objects.
  • Segmentation: On ADE20K, TNT-S achieved a 43.6% mIoU, outperforming comparable transformer backbones.
Comparison of TNT with other models in terms of Accuracy vs. Parameters and FLOPs.

Discussion

The "Visual Word" analogy provides an intuitive way to apply the hierarchical nature of NLP to the spatial nature of images. Visualizations (t-SNE and feature maps) show that TNT preserves local information much better than standard ViTs, especially in shallower layers. The features produced are more diverse and detail-rich. The decision to share the inner transformer across all patches allows the model to learn complex local patterns without a massive explosion in the number of parameters. Furthermore, TNT uses position encoding at two scales: sentence-level and word-level.

Conclusion

While TNT outperforms many other transformers, it still trails behind the most optimized CNNs (like EfficientNet) in some metrics due to specialized depth-wise convolutions. Additionally, the nested structure might not be as highly optimized for current GPU kernels as "flat" transformers, impacting real-world throughput. Future work may explore different sub-patch sizes or even more levels of nesting to further bridge the gap between pixels and global context.

Summary updated

BibTeX
@inproceedings{han2021transformer,
  author = {Kai Han and An Xiao and Enhua Wu and Jianyuan Guo and Chunjing Xu and Yunhe Wang},
  title = {{Transformer in Transformer}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2021},
  url = {https://openreview.net/forum?id=iFODavhthGZ},
}
Figure