Fine-tuning Image Transformers using Learnable Memory

Mark Sandler, Andrey Zhmoginov, Max Vladymyrov, Andrew Jackson

TL;DR

This paper introduces "memory tokens"—a small set of learnable embedding vectors added to each layer of a Vision Transformer (ViT)—to enable parameter-efficient fine-tuning. By using these tokens alongside a clever attention-masking strategy, the authors achieve near-full fine-tuning accuracy with a fraction of the parameters, while allowing a single model to handle multiple downstream tasks simultaneously without "forgetting" or interference.

What Problem Does This Paper Address?

Fine-tuning large Vision Transformers (ViTs) presents a difficult trade-off. Full fine-tuning (updating all weights) yields the best accuracy but is incredibly expensive; you must store and deploy a massive, separate model for every new task. On the other end, head-only fine-tuning (only updating the final classifier) is efficient but often leaves significant accuracy on the table because the underlying features aren't adapted to the new data.

Furthermore, standard fine-tuning suffers from "catastrophic forgetting"—once you tune a model for a new task, it loses its ability to perform the original one. The authors sought a middle ground: a method that is parameter-efficient, highly accurate, and capable of "computation reuse," where one backbone can serve multiple tasks at once.

Methods

The core innovation is the Memory Token. Instead of just passing image patches through the transformer, the authors append $m$ learnable vectors (the "memory") to the input of each encoder layer.

Key technical details include:

  • Layer-wise Injection: Unlike previous "prefix-tuning" methods that only add tokens at the first layer, this approach introduces fresh learnable memory at every layer.
  • One-Way Attention: Crucially, image tokens can attend to these memory tokens to gain contextual information, but the memory tokens do not attend to the image tokens or to each other. They also do not propagate to the next layer; each layer has its own dedicated set of memory parameters.
  • Attention Masking: To support multiple tasks, the authors use a masking matrix. This ensures that Task A's class token only "sees" the image patches and Task A's specific memory tokens, ignoring any tokens belonging to Task B. This prevents interference and allows for "model concatenation," where multiple independently trained task heads and memories are plugged into the same frozen backbone.
Figure 1. Structure of an encoder with learned memory. The input to each encoder layer is augmented with learned memory, which is used as an optional source of attention by the embeddings that propagate from previous layers. Memory tokens do not attend to other tokens. For detailed design of individual layer see Fig. 2.

Datasets

The researchers evaluated their approach using a ViT-B/32 model pre-trained on ImageNet-21K. They tested fine-tuning performance on four diverse downstream datasets:

  • CIFAR-100: General object classification.
  • iNaturalist: Fine-grained species classification.
  • Places-365: Scene recognition.
  • SUN-397: Large-scale scene understanding.

Key Insights and Ideas

  • Context at Every Level: By adding memory to every layer, the model can learn task-specific "concepts" at varying levels of abstraction—from low-level textures in early layers to complex semantic concepts in later ones.
  • The "Sweet Spot" of Memory: The authors found that adding just 5 to 10 memory tokens per layer captures most of the benefit. Adding more (e.g., 20+) often leads to overfitting rather than better accuracy.
  • Non-Destructive Multi-Tasking: The attention-masking strategy is a "free lunch" for deployment. It allows you to run inference for multiple tasks (e.g., identifying a bird and its habitat) in a single forward pass with only a tiny incremental cost in FLOPs.
  • Late Layers Matter More: Ablation studies showed that while all layers benefit from memory, adding tokens to the final four layers of the transformer provides a larger performance boost than adding them to the first four.
Figure 2. Detailed design of encoder layer with memory. The designs on the right are the alternative options that we explore in Sec. 4. The rightmost one is similar to the MemTransformer design in [5]. Here we use rounded boxes to indicate functions and square boxes to indicate input/output to highlight data propagation.

Key Results

The "Memory + Head" approach consistently outperformed head-only fine-tuning and narrowed the gap to full fine-tuning significantly:

  • Accuracy Gains: On iNaturalist, head-only tuning reached 46.3% accuracy. Adding just 5 memory cells per layer boosted this to 50.1%, moving significantly closer to the full fine-tuning benchmark of 54.0%.
  • Efficiency: The memory approach uses roughly 1/100th of the trainable parameters compared to full fine-tuning. For a ViT-B/16, the memory tokens add only about 46K parameters, whereas the full model has 80M.
  • Zero Interference: Using attention masking, the authors combined four different models (SUN, Places, iNat, CIFAR) into one. The original accuracy for each task was preserved perfectly, whereas "naive" concatenation without masking caused accuracy on iNaturalist to plummet from 50.0% to 17.3%.

Limitations and Future Work

  • Scaling Diminishing Returns: There is a clear ceiling; adding more memory tokens eventually hurts generalization. The authors suggest exploring "Top-K" attention to memory to filter out noise.
  • Model Extension vs. Concatenation: Interestingly, training tasks sequentially (extension) didn't perform better than training them separately and sticking them together (concatenation). The authors hypothesize that intermediate datasets might need to be larger or more relevant to see the benefits of sequential learning.
  • Domain Expansion: The current work focuses on vision. A natural next step is applying this specific "non-propagating" memory architecture to Natural Language Processing (NLP) and other transformer-based domains.

Summary updated

BibTeX
@misc{sandler2022fine,
  author = {Mark Sandler and Andrey Zhmoginov and Max Vladymyrov and Andrew Jackson},
  title = {{Fine-tuning Image Transformers using Learnable Memory}},
  year = {2022},
  eprint = {2203.15243},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2203.15243},
}
Figure