SwinStyleformer is a Favorable Choice for Image Inversion

Jiawei Mao, Guangyi Zhao, Xuesong Yin, Yuanqi Chang

TL;DR

SwinStyleformer is the first pure Transformer-based network designed for GAN image inversion, successfully overcoming the "inversion failure" that typically plagues standard Transformer architectures. By combining a modified Swin Transformer backbone with learnable queries and multi-scale connections, it accurately captures both global object structures and fine-grained local details for high-fidelity image editing.

What Problem Does This Paper Address?

Image inversion is the process of mapping a real image into the latent space ($W$ or $W+$) of a pre-trained generative model like StyleGAN. Doing so allows researchers and creators to leverage the generative power of the model to perform realistic image editing, super-resolution, or style mixing.

Traditionally, image inversion relies on Convolutional Neural Networks (CNNs). However, CNNs suffer from a limited local receptive field, making it difficult to capture long-range dependencies and learn the global structure of an object. This often results in structural mismatches, such as distorted facial outlines or misaligned features.

While Vision Transformers (ViTs) are naturally excellent at capturing global structure, simply replacing a CNN backbone with a standard Transformer causes complete inversion failure. This happens because standard self-attention over-focuses on coarse layouts while ignoring local details, lacks robust multi-scale feature aggregation, and creates a severe distribution mismatch with StyleGAN’s native style vectors. SwinStyleformer bridges this gap.

Comparison of structural alignment between CNN and Transformer backbones.

Methods

The authors introduce SwinStyleformer, a pure Transformer architecture optimized specifically for image inversion through several novel core designs:

  • Tailored Swin Transformer Backbone: To preserve local details, the model utilizes a Swin Transformer backbone with a tiny window size (size 2) in its first two stages, scaling up to size 8 in the final two stages to capture global structure.
  • Learnable Queries in Attention: Instead of standard Window Multi-Head Self-Attention (W-MSA) which uses fixed linear mappings of input features, the intermediate map2style network introduces random, learnable queries. This gives the model the flexibility to dynamically update attention weights based on specific tasks, forcing it to focus on fine-grained details and reconstruction errors rather than just global layout.
  • Multi-scale Connections: Feature maps at different scales are aggregated using residual connections to all previous scales in the feature pyramid, preventing the loss of detail during global modeling.
  • Distribution Alignment Loss & Inversion Discriminator: To fix the latent space distribution mismatch, the authors introduce a KL-divergence-based distribution alignment loss. They couple this with a specialized contrastive-learning-inspired inversion discriminator that utilizes a momentum encoder to amplify reconstruction discrepancies.

Datasets

The model was evaluated across multiple distinct domains using well-established benchmarks:

  • Facial Domain: Trained on FFHQ (70K images) and tested on CelebA-HQ (6K images).
  • Animal Domain: Evaluated using the AFHQ Wild dataset.
  • Church Domain: Evaluated using the LSUN Church dataset.
  • Semantic Segmentation: Face synthesis from masks was tested on the CelebAMask-HQ dataset.

Key Insights and Ideas

  • The Fixed-Query Trap: Standard Transformer self-attention fails at pixel-level inversion because its fixed-query projection inherently favors dominant global structures. Introducing learnable queries breaks this rigid loop, allowing the model to adaptively allocate attention to poorly reconstructed local regions.
  • The Latent Distribution Gap: Even if a Transformer extracts highly descriptive image features, they do not naturally align with the distribution expected by a pre-trained StyleGAN generator. Explicitly enforcing distribution alignment via KL-divergence is a prerequisite for artifact-free generation.

Key Results

SwinStyleformer achieves state-of-the-art (SOTA) performance across image inversion, editing, and restoration tasks:

  • Quantitative Performance: On the facial domain, SwinStyleformer achieves an LPIPS of 0.159 and an SSIM of 0.617, outperforming popular CNN baselines like pSp (LPIPS: 0.176, SSIM: 0.603) and e4e.
  • Downstream Tasks: In image editing tasks (e.g., altering age, gender, or expressions), human evaluators preferred SwinStyleformer over baselines 95.1% of the time due to its ability to preserve identity and background details cleanly.
  • Super-Resolution: At extreme $32\times$ downsampling, SwinStyleformer accurately recovers fine details like eye shadow and lip shapes where other models generate noticeable artifacts.
Overview of SwinStyleformer performance across image inversion, editing, and synthesis tasks.

Limitations and Future Work

The authors note that due to computational resource constraints, the model was trained with a restricted batch size of 6 on a single NVIDIA RTX 3090. Consequently, the training efficiency and scalability of the architecture under massive distributed computing setups remain unexplored. Future work could also investigate adapting this pure Transformer inversion approach to video domains or non-rigid object manipulation.

Summary updated

BibTeX
@article{mao2025swinstyleformer,
  author = {Jiawei Mao and Guangyi Zhao and Xuesong Yin and Yuanqi Chang},
  title = {{SwinStyleformer is a Favorable Choice for Image Inversion}},
  journal = {IEEE TCSVT},
  year = {2025},
  doi = {10.1109/tcsvt.2024.3454549},
}
Figure