Masked Jigsaw Puzzle: A Versatile Position Embedding for Vision Transformers

Yahui Liu, Bin Ren, Song Yue, Wei Bi, Nicu Sebe, Wei Wang, Wang, Wei · DOI

Abstract: Position Embeddings (PEs), an arguably indispensable component in Vision Transformers (ViTs), have been shown to improve the performance of ViTs on many vision tasks. However, PEs have a potentially high risk of privacy leakage since the spatial information of the input patches is exposed. This caveat naturally raises a series of interesting questions about the impact of PEs on the accuracy, privacy, prediction consistency, etc. To tackle these issues, we propose a Masked Jigsaw Puzzle (MJP) position embedding method. In particular, MJP first shuffles the selected patches via our block-wise random jigsaw puzzle shuffle algorithm, and their corresponding PEs are occluded. Meanwhile, for the non-occluded patches, the PEs remain the original ones but their spatial relation is strengthened via our dense absolute localization regressor. The experimental results reveal that 1) PEs explicitly encode the 2D spatial relationship and lead to severe privacy leakage problems under gradient inversion attack; 2) Training ViTs with the naively shuffled patches can alleviate the problem, but it harms the accuracy; 3) Under a certain shuffle ratio, the proposed MJP not only boosts the performance and robustness on large-scale datasets (i.e., ImageNet-1K and ImageNet-C, -A/O) but also improves the privacy preservation ability under typical gradient attacks by a large margin.

1. Introduction

Vision Transformers (ViTs) have revolutionized computer vision by treating images as sequences of patches. However, because Transformers are inherently permutation-invariant, they rely on Position Embeddings (PEs) to incorporate spatial information. While PEs improve performance, they introduce a significant vulnerability: they explicitly encode the 2D grid structure, creating a "privacy-performance" trade-off. This spatial transparency allows attackers to reconstruct original images from model gradients, a major concern in privacy-sensitive applications like federated learning. Furthermore, standard ViTs are often brittle to input perturbations. To address these issues, we introduce the Masked Jigsaw Puzzle (MJP) position embedding method.

2. The Masked Jigsaw Puzzle (MJP) Framework

The MJP method aims to decouple content from rigid spatial positions during training to enhance robustness and privacy without sacrificing accuracy. The framework consists of three core components:

  • Block-wise Random Jigsaw Puzzle Shuffle: A subset of image patches, controlled by a ratio $\gamma$, is randomly shuffled. This breaks the static mapping between patch content and its original grid location.
  • Masked Position Embeddings: For shuffled patches, original PEs are replaced with a shared, learnable unknown embedding ($E_{unk}$). This prevents the model from receiving contradictory positional signals while maintaining the presence of the patch.
  • Dense Absolute Localization (DAL) Regressor: To preserve necessary spatial context, an auxiliary self-supervised task is used for unshuffled patches. A regressor predicts their absolute 2D coordinates from their PEs, strengthening the spatial priors of these "anchor" patches.
Overview of the MJP pipeline, illustrating the patch shuffling process and the role of the DAL regressor.

3. Experimental Analysis

The authors evaluated MJP on ImageNet-1K for accuracy, ImageNet-C for robustness, and ImageNet-A/O for adversarial performance.

3.1 Spatial Encoding and Privacy

Visualizing standard PEs using UMAP or PCA reveals that they explicitly reconstruct a 2D coordinate system. This "map" is what gradient-based attacks exploit to recover private data. MJP mitigates this by occluding these embeddings during the shuffling process, significantly hindering gradient inversion attacks.

UMAP and PCA projections of standard Position Embeddings, demonstrating how they explicitly capture the 2D spatial grid.

3.2 Performance and Robustness

On ImageNet-1K, MJP improved the Top-1 accuracy of a DeiT-S model from 79.8% to 80.5%. More significantly, it enhanced prediction consistency—the model's ability to maintain its output despite input scrambling—from 64.3% to 82.9%. Robustness to common corruptions also improved, with the mean Corruption Error (mCE) on ImageNet-C dropping from 54.6 to 40.78. These results suggest that MJP forces the model to rely more on diverse, content-aware features rather than just memorizing spatial positions.

4. Conclusion

MJP effectively addresses the privacy-performance trade-off in ViTs. By partially shuffling patches and masking their positional information, the method improves accuracy, robustness, and privacy preservation. While the DAL regressor adds some training overhead, using a simple MLP makes it computationally efficient. Future work will explore MJP's applicability to other modalities like video and 3D point clouds.

Summary updated

BibTeX
@inproceedings{liu2023masked,
  author = {Yahui Liu and Bin Ren and Song Yue and Wei Bi and Nicu Sebe and Wei Wang and Wang, Wei},
  title = {{Masked Jigsaw Puzzle: A Versatile Position Embedding for Vision Transformers}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2023},
  doi = {10.1109/cvpr52729.2023.01952},
}
Figure