Nodes/ComfyUI-StyleGan/Generate StyleGAN Latent
ComfyUI Node

Generate StyleGAN Latent

From seed to latent

By spacepxl·Created 2 years ago·Updated 2 years ago· 21
Generate StyleGAN Latent
  • stylegan_model
  • STYLEGAN_LATENT
seed0
class_label-1
batch_size1

The name undersells it. Generate StyleGAN Latent doesn't just roll some random numbers - it runs the StyleGAN mapping network, which is the part of the model that turns unstructured Gaussian noise into the structured style codes the sampler can actually draw from. It's the "no seed prompt here" generator node: seed in, STYLEGAN_LATENT out, no pixels until you hit the sampler.

How it works

Under the hood it draws z vectors from a seeded normal distribution - one [512] vector per image - and pushes each through the model's mapping() network to get a w code. The w code is the interesting part: unlike the flat z it started from, w is shaped [num_layers, 512], a separate style for every synthesis layer, which is precisely why StyleGAN can hold a face's coarse structure and fine skin texture in one latent. The node maps each batch item separately and concatenates the results, so the output STYLEGAN_LATENT is one tensor with batch_size rows.

The inputs that matter

  • seed - the reproducibility knob. Same seed, same z, same w, same face. StyleGAN latents are fully deterministic given the seed and model, which makes them great for building a searchable face library.
  • batch_size - default 1, up to 1024. Generate a batch of latents at once and the sampler renders them all in one pass. This is your cheap "give me 64 faces on a grid" lever. The README's 64 images/sec figure assumes a large batch, so don't batch one at a time.
  • class_label - default -1, which means unconditional. Only touch this if your loaded model is class-conditional, like a StyleGAN2-ADA trained on ImageNet or CIFAR-10. Set it to a class index and the mapping network steers the generation toward that category. Most people will never move it.

Wiring

Feed STYLEGAN_LATENT into StyleGAN Sampler with the same model, and you've got the minimal StyleGAN loop: Load → Generate → Sample → Preview. It also feeds the interpolation nodes - Blend StyleGAN Latents, Batch Average StyleGAN Latents, StyleGAN Latent From Batch - so a batch of these becomes raw material for morphing and averaging experiments.

Gotchas

Keep the model and latent honest: the latent is only meaningful for the model that produced it. A w from a 512px FFHQ model won't render correctly through a StyleGAN3 1024 generator, so don't cross-wire latents between models - the shape and the learned w distribution differ. And a quirk worth knowing: this node maps latents one at a time in a loop rather than as one batched mapping() call, so a 1024-image batch takes a moment - fine for offline grids, not something you'd hammer in real time. For that, generate fewer and rely on the sampler's speed.

CategoryStyleGAN

Inputs (4)

NameTypeDefaultDescription
stylegan_modelSTYLEGAN
seedINT00–18446744073709550000
class_labelINT-1
batch_sizeINT11–1024

Outputs (1)

NameTypeDescription
STYLEGAN_LATENTSTYLEGAN_LATENT