Retrieval-Augmented Transformer for Image Captioning

Sara Sarto, Marcella Cornia, Lorenzo Baraldi, Rita Cucchiara · DOI

Abstract

Traditional image captioning models rely on parametric memory, storing knowledge within fixed model weights. This approach requires massive parameter scaling to improve performance. We present the Retrieval-Augmented Transformer (RA-Transformer), the first image captioning framework to integrate an external retrieval-based memory. By utilizing a $k$-nearest neighbor ($k$NN) search to retrieve relevant captions from a large-scale database, the model leverages a "cheat sheet" of textual context. Our results on the COCO dataset demonstrate that the RA-Transformer achieves state-of-the-art performance with improved descriptive accuracy and attribute identification without the need for excessive parameter expansion.

Introduction

In the field of computer vision and natural language processing, image captioning has traditionally been dominated by "parametric" models. These architectures store all world knowledge—ranging from object recognition to linguistic syntax—within their internal weights. To enhance the descriptive capabilities of these models, researchers typically resort to increasing model depth or training on exponentially larger datasets, both of which incur significant computational costs.

In contrast, Large Language Models (LLMs) have recently benefited from retrieval-augmented architectures, such as RETRO, which allow models to query external databases for relevant information. The RA-Transformer bridges this gap for multimodal tasks. By allowing the model to "look up" how similar images were described in the past, it can generate more precise and contextually rich captions by referencing existing human-written text.

Methodology

The RA-Transformer architecture is a fully-attentive model composed of three primary modules designed to integrate external knowledge into the generation process.

1. Knowledge Retriever

When processing a query image, the model employs a CLIP-based visual encoder to generate a high-dimensional embedding. This embedding is used to perform an approximate $k$-nearest neighbor ($k$NN) search using the Faiss library against an external memory index. This index contains millions of image-caption pairs, and the retriever identifies the $k$ most visually similar examples to provide as context.

2. External Memory Encoder

The retrieved captions are processed through a dedicated Transformer encoder. This module transforms the raw text of the neighbors into a set of "keys" and "values." These representations serve as the external knowledge base that the decoder can reference during the word-by-word generation of the new caption.

3. $k$NN-Augmented Attention

The core innovation lies in the Transformer decoder's attention mechanism. The authors introduce a specialized attention layer that utilizes a learned gating mechanism ($\alpha$). This gate dynamically balances two distinct information streams:

  • Local Context: The sequence of words already generated for the current image.
  • Retrieved Context: The information extracted from the external memory.

This allows the model to decide, at each time step, whether to rely on its internal representation of the image or to incorporate specific phrases or attributes found in the retrieved captions.

Experimental Setup

The model was evaluated on the industry-standard MS-COCO dataset using the Karpathy splits. The training set consisted of approximately 120,000 images, each paired with five human-annotated captions. The external retrieval index was constructed using the COCO training set itself. To ensure the integrity of the evaluation, the model was prohibited from retrieving captions belonging to the specific image it was currently attempting to describe.

Training followed a two-stage process: initial pre-training using standard cross-entropy loss, followed by fine-tuning via Self-Critical Sequence Learning (SCST). SCST is a reinforcement learning strategy that directly optimizes for the CIDEr metric, which correlates highly with human judgment of caption quality.

Results and Discussion

Performance Metrics

The RA-Transformer achieved a state-of-the-art CIDEr score of 136.5 on the COCO test set, a notable improvement over the baseline Transformer's score of 135.3. This improvement suggests that the retrieval mechanism provides a significant boost in the model's ability to generate high-quality, relevant descriptions.

Ablation Studies

Ablation experiments highlighted the importance of the gating mechanism. During the pre-training phase, simply adding external memory improved the CIDEr score from 121.6 to 122.5. However, the introduction of the learned gate $\alpha$ pushed the score further to 124.5, demonstrating that the model's ability to selectively ignore noisy or irrelevant retrieved data is crucial for performance.

Qualitative Analysis

Qualitative observations indicate that the RA-Transformer excels at identifying specific actions and fine-grained attributes. For instance, when the retrieved "hint" captions contain specific descriptors like "jumping" or "elderly," the model is significantly more likely to correctly apply these labels to the target image compared to standard parametric models.

Limitations and Future Directions

While the RA-Transformer offers superior accuracy, it introduces a trade-off in terms of computational efficiency. The retrieval step and additional attention layers increase inference latency by approximately 26% (from 0.170s to 0.215s per caption). Additionally, the model remains sensitive to the quality of the retrieval; if the $k$NN search returns irrelevant images, the model can occasionally produce erroneous descriptions, though the gating mechanism mitigates this risk.

Future work will focus on scaling the external memory to web-scale datasets containing trillions of tokens. By expanding the "knowledge base" beyond the COCO training set, the researchers aim to determine if the performance gains of retrieval-augmentation continue to scale linearly with the size of the available memory.

Summary updated

BibTeX
@inproceedings{sarto2022retrieval,
  author = {Sara Sarto and Marcella Cornia and Lorenzo Baraldi and Rita Cucchiara},
  title = {{Retrieval-Augmented Transformer for Image Captioning}},
  booktitle = {CBMI},
  year = {2022},
  doi = {10.1145/3549555.3549585},
}
Figure