StyleGAN Inversion
Pushing a real photo back into StyleGAN's latent space
- stylegan_model
- image
- training_latents
- final_latent
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:
- It samples
w_avg_samplesrandom latents (default 10,000) through the mapping network to compute the meanwand standard deviation - the statistical center of this model's latent space, which is where optimization starts. - It loads VGG16 from
ComfyUI/models/VGG/(downloaded automatically from NVIDIA on first run) to extract perceptual features. - Then it runs
num_steps(default 1000) of AdamW on awvector 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. - Along the way it anneals: random noise is added to
wearly and ramped down vianoise_ramp_length, the learning rate ramps up then down vialr_rampup_length/lr_rampdown_length, andregularize_noise_weightkeeps 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_modelandimage- 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.
Inputs (11)
| Name | Type | Default | Description |
|---|---|---|---|
| stylegan_model | STYLEGAN | — | |
| image | IMAGE | — | |
| seed | INT | 00–18446744073709550000 | — |
| num_steps | INT | 1000 | — |
| w_avg_samples | INT | 100001–100000 | — |
| initial_learning_rate | FLOAT | 0.10000.00001–1 | — |
| initial_noise_factor | FLOAT | 0.0500–1 | — |
| lr_rampdown_length | FLOAT | 0.250–1 | — |
| lr_rampup_length | FLOAT | 0.050–1 | — |
| noise_ramp_length | FLOAT | 0.750–1 | — |
| regularize_noise_weight | FLOAT | 100000.000–10000000 | — |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| training_latents | STYLEGAN_LATENT | — |
| final_latent | STYLEGAN_LATENT | — |