Nodes/ComfyUI-StyleGan/StyleGAN Inversion
ComfyUI Node

StyleGAN Inversion

Pushing a real photo back into StyleGAN's latent space

By spacepxl·Created 2 years ago·Updated 2 years ago· 21
StyleGAN Inversion
  • stylegan_model
  • image
  • training_latents
  • final_latent
seed0
num_steps1000
w_avg_samples10000
initial_learning_rate0.1000
initial_noise_factor0.050
lr_rampdown_length0.25
lr_rampup_length0.05
noise_ramp_length0.75
regularize_noise_weight100000.00

Every StyleGAN node so far goes latent → pixels. StyleGAN Inversion runs the other direction: give it a real image and a StyleGAN model, and it optimizes a latent that, when rendered, reproduces that image. This is the heavyweight of the pack - the node that makes face editing possible, because once a photo lives in latent space, every latent trick in this pack (blend, average, extrapolate) becomes a way to edit it.

How it works - the mechanism

This is the NVlabs projector (adapted from the stylegan3-projector fork), and it's a small optimization loop, not a neural net. In rough order:

  1. It samples w_avg_samples random latents (default 10,000) through the mapping network to compute the mean w and standard deviation - the statistical center of this model's latent space, which is where optimization starts.
  2. It loads VGG16 from ComfyUI/models/VGG/ (downloaded automatically from NVIDIA on first run) to extract perceptual features.
  3. Then it runs num_steps (default 1000) of AdamW on a w vector plus the generator's per-layer noise buffers, minimizing the squared difference between the target image's VGG features and the synthesized image's. Perceptual loss, not pixel loss - that's what keeps reconstructions looking like the person rather than a blurry copy.
  4. Along the way it anneals: random noise is added to w early and ramped down via noise_ramp_length, the learning rate ramps up then down via lr_rampup_length / lr_rampdown_length, and regularize_noise_weight keeps the optimized noise from turning into artifacts.

Two outputs come out: training_latents (the w at every step - all 1000 of them) and final_latent (just the last one, which is what you actually use).

The inputs a beginner should touch

There are ten inputs. Ignore eight of them:

  • stylegan_model and image - the model (from Load StyleGAN Model) and the photo. The node resizes the image to the model's native resolution with area interpolation, so you don't need to pre-size it.
  • num_steps - the one you'll actually adjust. 1000 is thorough and slow. Drop it to 200–300 for a rough latent in a fraction of the time, then let it ride at 1000 for quality.
  • seed - sets the RNG for the initial latent and samples. Different seeds land on different (but equally valid) solutions; retry a few if the reconstruction fights you.

The rest - w_avg_samples, initial_learning_rate, initial_noise_factor, lr_rampdown_length, lr_rampup_length, noise_ramp_length, regularize_noise_weight - are the projector's defaults and they're tuned. Change them only when you know what you're doing.

Realistic expectations

This is not instant. Each step is a full synthesis pass plus a VGG forward, so 1000 steps is real minutes on a midrange GPU. Plan around it: do your iteration at low num_steps, then commit to a long run for the final latent. And reconstruction isn't identity-perfect - inversion captures the face's structure and general likeness, but fine details and exact likeness are traded for the ability to manipulate it. That's the deal with all inversion approaches, not a bug in this node.

The payoff

The killer use of training_latents: feed it into StyleGAN Latent From Batch at increasing indices and render each step - you've just made an inversion-progress animation showing the image snapping into focus step by step. Or take final_latent into the sampler for a reconstruction, then blend it toward the Batch Average latent for an "un-face" edit. Once your photo is a latent, this whole pack becomes your editing suite.

CategoryStyleGAN

Inputs (11)

NameTypeDefaultDescription
stylegan_modelSTYLEGAN
imageIMAGE
seedINT00–18446744073709550000
num_stepsINT1000
w_avg_samplesINT100001–100000
initial_learning_rateFLOAT0.10000.00001–1
initial_noise_factorFLOAT0.0500–1
lr_rampdown_lengthFLOAT0.250–1
lr_rampup_lengthFLOAT0.050–1
noise_ramp_lengthFLOAT0.750–1
regularize_noise_weightFLOAT100000.000–10000000

Outputs (2)

NameTypeDescription
training_latentsSTYLEGAN_LATENT
final_latentSTYLEGAN_LATENT