Abstract
Zigzag flattening (ZF) is the de facto standard for unfolding 2D image matrices into 1D sequences in computer vision, notably in Vision Transformers (ViT). However, ZF often fails to preserve spatial locality and scale invariance, particularly for multi-scale objects. This paper investigates Hilbert flattening (HF), based on Space-Filling Curves (SFC), as a superior alternative. We demonstrate that HF maintains high spatial locality and robustness across scales. By integrating HF into a novel architecture called Localformer, we alleviate locality bias issues in shallow ViT layers, leading to consistent performance improvements across various visual tasks.
The Problem with Zigzag Flattening
Modern vision architectures like Transformers and MLP-Mixers require 2D grids of pixels or patches to be converted into 1D sequences. Most models employ "Zigzag Flattening" (ZF), a simple raster scan similar to reading text. While intuitive, ZF has two fundamental flaws in a visual context:
- Locality Disruption: Vertically adjacent pixels in a 2D image can become extremely distant in a 1D sequence. For instance, pixels at coordinates (0,1) and (1,1) are separated by an entire row's length in ZF.
- Scale Instability: Resizing an image radically alters the relative positions of points in a ZF sequence. This instability makes it difficult for models to maintain feature consistency across different resolutions.
Hilbert Flattening: A Fractal Solution
To address these issues, we explore Space-Filling Curves (SFCs), specifically the Hilbert curve. Unlike the standard raster scan or the Morton (Z-order) curve, the Hilbert curve snakes through 2D space in a fractal pattern that preserves spatial relationships more effectively.
Theoretical Superiority
A key metric introduced is the dilation factor—the ratio of 1D distance to 2D distance. Mathematically, as resolution increases, the dilation factor for Zigzag and Morton curves approaches infinity. In contrast, for the Hilbert curve, it converges to a constant (6). This mathematical property ensures that Hilbert flattening is fundamentally more stable across different image resolutions.
Methods and Implementation
We propose Hilbert Patch Embedding (HPE) as a drop-in replacement for standard patch embedding. By ordering tokens along a Hilbert path, we introduce a natural locality bias. We also developed FPN-MLPs, a novel architecture designed to evaluate how these flattening strategies handle multi-scale representations.
One striking demonstration of HF's power is 1D interpolation. If an image is flattened to 1D, resized in that 1D space, and then "unfolded" back to 2D, the Hilbert-flattened version remains recognizable, whereas the Zigzag-flattened version becomes a garbled mess. This suggests HF captures the spatial "essence" of the 2D structure within a 1D format.
Key Results
Our experiments on CIFAR-10 and a custom synthetic toy dataset yield several insights:
- Performance Gains: Replacing standard flattening with HPE improved the Top-1 accuracy of the MLP-Mixer by 1.2% and our FPN-MLPs by 4.29%.
- Scale Robustness: Using Dynamic Time Warping (DTW) distances on our toy dataset, the Hilbert method showed significantly lower distances when comparing objects at different scales, proving superior structural consistency.
- Locality Bias: By keeping nearby pixels close in the sequence, HF allows models to capture local dependencies like edges and textures more efficiently, reducing the need for massive datasets to learn basic spatial relationships.
Conclusion and Future Work
Hilbert Flattening offers a mathematically elegant and practically effective alternative to the standard raster scan. While currently tested primarily on MLP-based architectures and smaller datasets, the potential for HF to improve large-scale Transformers and other 2D/3D tasks like medical imaging is significant. Future research will focus on optimizing these algorithms for high-speed training and exploring their utility in complex tasks like object detection.
Summary updated