LatentSwap: An Efficient Latent Code Mapping Framework for Face Swapping

Changho Choi, Minho Kim, Junhyeok Lee, Hyoung-Kyu Song, Younggeun Kim, Seungryong Kim

TL;DR

LatentSwap is an efficient, dataset-free face swapping framework that operates entirely within the latent space of a pretrained generator (like StyleGAN2). By training a lightweight "latent mixer" module on randomly sampled latent codes, it eliminates the need for expensive training datasets while achieving state-of-the-art attribute preservation. Furthermore, the framework seamlessly generalizes to 3D-aware generators like StyleNeRF and allows for intuitive attribute editing.

What Problem Does This Paper Address?

Traditional face swapping methods typically rely on Generative Adversarial Networks (GANs) trained on massive datasets of human faces. However, these approaches suffer from notoriously slow and unstable training phases. To circumvent this, recent methods have tried leveraging the well-behaved latent space of pretrained generators like StyleGAN2.

Unfortunately, existing latent-based face swappers often require heavy auxiliary tools—such as face segmentation networks or parsing models—to blend the source identity with the target attributes. These extra modules prevent the network from fully exploiting the natural disentanglement of the generator's latent space. LatentSwap addresses these inefficiencies by introducing a simple, fast-training architecture that requires zero training images and no auxiliary networks.

Methods

The core innovation of LatentSwap is the Latent Mixer, a remarkably simple architecture consisting of just 5 fully-connected (FC) layers with Swish activations and a residual connection.

Figure 2: Overall training scheme of the LatentSwap model. We sample source and target latent codes randomly from a normal distribution, which are mapped onto W and subsequently copied 18-fold as a vector on W+ space. The W+ space vector is piped into the latent mixer (whose detailed structure is in the black dotted box). The swapped latent codes are then fed into the generator (the StyleGAN2 pre-trained weights) at different layers to generate the final face-swapped image. The gradient can still flow through the generator, but it remains freezed throughout training.

The Pipeline

  1. Sampling: During training, source and target latent codes ($l_s^Z, l_t^Z$) are randomly sampled from a standard normal distribution and mapped to the $W+$ latent space.
  2. Mixing: The Latent Mixer takes the concatenated source and target codes and predicts a residual vector, which is added back to the target code to produce the swapped latent code ($l_{swap}$).
  3. Generation: This swapped code is fed into a frozen pretrained generator (e.g., StyleGAN2) to produce the final face-swapped image.

Loss Functions

The network is optimized using a simple three-term loss function:

  • Identity Loss ($\mathcal{L}_{id}$): Minimizes the cosine distance between the identity embeddings of the source and swapped images using a pretrained Smooth-Swap embedder.
  • Latent Penalty Loss ($\mathcal{L}_{lp}$): An MSE loss that prevents the swapped latent code from drifting too far from the target latent code, ensuring attribute preservation.
  • Shape Loss ($\mathcal{L}_s$): Uses 3D Morphable Model (3DMM) keypoints as geometric supervision to constrain the face shape.

For handling real-world images at inference time, the framework uses Pivotal Tuning Inversion (PTI) to accurately map real images into the generator's latent space before passing them to the Latent Mixer.

Datasets

  • Training: None! The model trains exclusively on randomly sampled latent vectors.
  • Pretrained Weights: The generator uses weights pretrained on the FFHQ (Flickr-Faces-HQ) dataset.
  • Evaluation: The model is evaluated and quantitatively compared against competitors using the FaceForensics++ (FF++) dataset.

Key Insights and Ideas

  • Dataset-Free Training: By training purely on random latent codes, LatentSwap leverages the rich structural priors already embedded within StyleGAN2. This bypasses the dataset collection bottleneck entirely.
  • The Role of Latent Penalty ($\mathcal{L}_{lp}$): The authors discovered that without the latent penalty ($\lambda = 0$), the swapped latent codes undergo a "random walk" akin to classical diffusion, eventually drifting outside the valid image domain and producing unrealistic artifacts.
  • Layer-Wise Disentanglement: By analyzing the 18 layers of StyleGAN2's $W+$ space, the authors mapped out exactly where features reside: coarse layers control geometry/glasses, middle layers dominate identity transfer, and fine layers handle subtle color and texture details.
  • 3D-Awareness Out-of-the-Box: Because the framework targets the latent space rather than 2D pixel space, replacing StyleGAN2 with a 3D-aware generator like StyleNeRF instantly yields multi-view consistent 3D face swapping without changing the training pipeline.

Key Results

LatentSwap achieves highly competitive results while being significantly more lightweight than alternative models.

Figure 4: Face swapping results of LatentSwap compared to other face swapping models on FF++. We used the code and checkpoints from their official implementation. Our model performs source identity and target attribute preser- vation well compared to other models.

Quantitative Performance (on FaceForensics++)

  • Attribute Preservation: LatentSwap achieves an Expression score of 0.0673 and a Pose score of 4.06, significantly outperforming SOTA models like SimSwap, FaceShifter, and HifiFace.
  • Efficiency: The Latent Mixer contains only 87M parameters, compared to MegaFS (338M) and InfoSwap (251M).
  • Speed: Core inference through the latent mixer takes a mere 0.2 seconds.

Limitations and Future Work

  • Inversion Bottleneck: While the Latent Mixer itself takes only 0.2 seconds, processing real-world images requires GAN inversion (PTI), which pushes the total inference time to roughly 98 seconds per pair. Integrating faster encoder-based inversion methods is a clear next step.
  • Hairstyle Bias: The swapped images occasionally retain the hairstyle of the source image rather than the target. The authors attribute this to the smooth identity embedder used during training, suggesting that finer control over hair disentanglement remains an open challenge.

Summary updated

BibTeX
@misc{choi2024latentswap,
  author = {Changho Choi and Minho Kim and Junhyeok Lee and Hyoung-Kyu Song and Younggeun Kim and Seungryong Kim},
  title = {{LatentSwap: An Efficient Latent Code Mapping Framework for Face Swapping}},
  year = {2024},
  eprint = {2402.18351},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2402.18351},
}
Figure