Skip to main content
QUICK REVIEW

[Paper Review] Rethinking and Improving Relative Position Encoding for Vision Transformer

Kan Wu, Houwen Peng|arXiv (Cornell University)|Jul 29, 2021
Advanced Image and Video Retrieval TechniquesComputer Science25 references24 citations
TL;DR

This paper proposes image-specific relative position encoding (iRPE) methods that enhance Vision Transformers by modeling directional relative distances and improving query-relative position interactions in self-attention. The approach achieves up to 1.5% top-1 accuracy gain on ImageNet and 1.3% mAP improvement on COCO without hyperparameter tuning, demonstrating that relative position encoding can effectively replace absolute encoding in image classification while remaining essential for object detection.

ABSTRACT

Relative position encoding (RPE) is important for transformer to capture sequence ordering of input tokens. General efficacy has been proven in natural language processing. However, in computer vision, its efficacy is not well studied and even remains controversial, e.g., whether relative position encoding can work equally well as absolute position? In order to clarify this, we first review existing relative position encoding methods and analyze their pros and cons when applied in vision transformers. We then propose new relative position encoding methods dedicated to 2D images, called image RPE (iRPE). Our methods consider directional relative distance modeling as well as the interactions between queries and relative position embeddings in self-attention mechanism. The proposed iRPE methods are simple and lightweight. They can be easily plugged into transformer blocks. Experiments demonstrate that solely due to the proposed encoding methods, DeiT and DETR obtain up to 1.5% (top-1 Acc) and 1.3% (mAP) stable improvements over their original versions on ImageNet and COCO respectively, without tuning any extra hyperparameters such as learning rate and weight decay. Our ablation and analysis also yield interesting findings, some of which run counter to previous understanding. Code and models are open-sourced at https://github.com/microsoft/Cream/tree/main/iRPE.

Motivation & Objective

  • To resolve the controversy over whether relative position encoding (RPE) is effective in vision transformers compared to absolute position encoding.
  • To design RPE methods specifically tailored for 2D image data, addressing limitations of 1D RPE extensions.
  • To improve the modeling of spatial inductive bias in Vision Transformers by incorporating directional relative distance and query-RPE interactions.
  • To provide a lightweight, plug-and-play solution that enhances performance without increasing computational cost significantly.
  • To empirically clarify the roles of RPE in image classification versus object detection tasks.

Proposed method

  • Proposes four new relative position encoding methods—iRPE—designed for 2D images, explicitly modeling horizontal and vertical relative distances.
  • Introduces a contextual product mechanism that computes attention weights by combining query features with relative position embeddings, enhancing interaction modeling.
  • Employs a shared RPE table across multi-head attention heads to reduce parameters while maintaining performance.
  • Optimizes computation by reducing complexity from O(n²d) to O(nkd), where k ≪ n, enabling scalability to high-resolution inputs.
  • Applies the iRPE modules directly into standard transformer blocks, ensuring compatibility and ease of integration.
  • Uses a learnable lookup table for relative position embeddings, with position-specific vectors for each relative offset in height and width.
(a)
(a)

Experimental results

Research questions

  • RQ1Can relative position encoding effectively replace absolute position encoding in image classification tasks using Vision Transformers?
  • RQ2Why does relative position encoding show inconsistent performance across different vision tasks like classification and object detection?
  • RQ3How does directional relative distance modeling affect attention patterns in 2D visual transformers?
  • RQ4What is the computational impact of applying RPE in high-resolution vision tasks such as object detection and semantic segmentation?
  • RQ5How do query-RPE interactions influence the model’s ability to capture local and global spatial dependencies?

Key findings

  • The proposed iRPE methods improve DeiT-S by 1.5% top-1 accuracy on ImageNet and DETR-ResNet50 by 1.3% mAP on COCO without any hyperparameter tuning.
  • Relative position encoding can fully replace absolute position encoding in image classification, where it achieves superior or comparable performance.
  • Absolute position encoding remains necessary for object detection, as it provides essential inductive bias for precise object localization.
  • Directional relative distance modeling significantly improves attention patterns, especially in shallow layers, where the model focuses more on neighboring patches.
  • The contextual product mechanism with query-RPE interaction enhances the model’s ability to capture local spatial structures, mimicking convolutional inductive bias.
  • Shared RPE across attention heads achieves comparable performance to non-shared versions, reducing parameters with minimal accuracy drop.
(b)
(b)

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.