Rethinking the Zigzag Flattening for Image Reading

Qingsong Zhao, Zhipeng Zhou, Shuguang Dou, Yangguang Li, Rui Lu, Yin Wang, Cairong Zhao · OpenReview

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:

  1. 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.
  2. 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.
Spatial transformations like scaling and shuffling illustrate the challenges of maintaining structural consistency during image flattening.

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.

Comparison of Zigzag (red) and Hilbert (blue) curves across resolutions from 8x8 to 128x128. Hilbert curves maintain significantly better locality and scale robustness.

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

Abstract

Zigzag flattening (ZF) is commonly used in computer vision as a default option to unfold matrices, \eg in patch slicing for Vision Transformer (ViT). However, when decomposing multi-scale-object web images, ZF cannot preserve the smoothness of local information well. To address this, we draw inspiration from Space-Filling Curves (SFC) and investigate Hilbert flattening (HF) as an alternative for visual models. We provide a comprehensive theoretical discussion and practical analysis, demonstrating the superiority of HF over other SFC in locality and multi-scale robustness. We leverage HF to alleviate the problem of the lack of locality bias in the shallow layers of ViT, which formulates our Localformer. Extensive experiments demonstrate that Localformer consistently improves performance for several common visual tasks. Additionally, upon inspection, we find that Localformer enhances representation learning and length extrapolation abilities of ViT.
BibTeX
@misc{zhao2022rethinking,
  author = {Qingsong Zhao and Zhipeng Zhou and Shuguang Dou and Yangguang Li and Rui Lu and Yin Wang and Cairong Zhao},
  title = {{Rethinking the Zigzag Flattening for Image Reading}},
  year = {2022},
  eprint = {2202.10240},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2202.10240},
}
Figure