StyleGAN: a style-based generator
Why a GAN's input noise is entangled, how a mapping network, AdaIN styles and noise inputs split control across layers, what style mixing and truncation give, how to measure disentanglement, and how StyleGAN2 removes the droplets.
Why the code is entangled
The extension Scaling GANs: ProGAN, SAGAN and BigGAN brought adversarial training to sharp faces. Control is another matter. The noise enters the generator once, at the first layer, and moving along one of its coordinates usually changes pose, identity and lighting all at once. Such a code is called entangled.
The reason is the fixed prior. The generator must turn into the data distribution, so every region of -space has to produce a realistic image, at the frequencies of the training set. Suppose two factors, how masculine a face looks and how long its hair is, sit on two axes of , and the data contain no long-haired men. Straight axes would fill that corner with exactly the images the data lack. To avoid it, the generator bends the axes, and then one coordinate of changes both factors. Nothing in the GAN loss pushes back: the discriminator judges whole images, not how the code is organized.
StyleGAN (Karras, Laine and Aila, 2019) no longer feeds to the image layers. A mapping network, an 8-layer MLP, first turns into an intermediate code. We call this network , because is the critic in this course, and write its output as
The distribution of is not fixed in advance; it is whatever makes it. So the bending can happen inside , and is free to keep the factors on straighter axes. Free to, not forced to: the bet is that realistic images are easier to draw from a less entangled code. Two measurements later in this topic check the bet.
The synthesis network and AdaIN
The image itself is drawn by the synthesis network , so the generator is . Like ProGAN's generator, it goes from to with two convolutional layers per resolution, 18 in all; the discriminator is ProGAN's. Its input is not but a learned constant, the same for every image. All variation comes from outside, through .
Each layer has its own learned affine map . It turns into a style for that layer: a scale and a bias for every channel . The style is applied by adaptive instance normalization, AdaIN (Huang and Belongie, 2017). Write for channel of a feature map, and , for its mean and standard deviation over the spatial positions. Then
The fraction is instance normalization from the topic Batch normalization, gradients and convolutions. The style then gives the channel a new scale and offset.
A worked example. A channel with values has mean and standard deviation , so it normalizes to . The style , turns that into .
Replacing by a constant cost nothing: the paper's image-quality score on faces stayed practically the same. The generator does not need at the input once reaches every layer.
Local styles and noise
AdaIN does more than inject a style. Its first step throws away the mean and standard deviation of every channel, the very numbers the previous style set. So a style shapes one convolution, and the next AdaIN overrides it. Each style controls its own layer, and what it passes on is only the spatial pattern inside each channel, not the channel's overall scale.
Styles are global: one scale and one bias per channel, the same at every pixel. They cannot place individual hairs, freckles or pores. A traditional generator has to fake such randomness from its input, which costs capacity and tends to produce repeating patterns. StyleGAN gives it real randomness. After each convolution, a fresh noise image , one value per pixel, is added to every channel with a learned per-channel weight :
Keep fixed and redraw the noise: strands of hair, stubble and skin pores rearrange, while identity and pose stay. Remove the noise and the images look painterly.
Nobody tells the network to split the work this way. The noise is independent from pixel to pixel, so using it for a global property such as pose would produce inconsistent images, and the discriminator punishes those. Global decisions go to the styles, local randomness to the noise.
Style mixing
If every layer receives the same , the network can still learn that neighbouring styles always come together. Style mixing breaks this habit. During training, for 90% of the images, two codes go through the mapping network, and at a random layer the synthesis network switches from to . A layer can no longer rely on its neighbours seeing the same code.
After training, mixing becomes an editing tool. Take the styles of one group of layers from image A and all the others from image B. The paper (Karras et al., 2019, Figure 3) groups the layers by resolution:
- coarse styles, –: pose, general hair style, face shape, eyeglasses;
- middle styles, –: smaller facial features, hair style, eyes open or closed;
- fine styles, –: mainly the colour scheme and the microstructure.
The order makes sense. A low-resolution layer can only decide the large-scale layout, and by the layout is settled, so only colour and texture are left to choose.
A separate code for every layer gives the extended space : 18 codes of 512 numbers instead of one. It can express more images than , which will matter at the end of this topic.
Truncation in W
BigGAN traded variety for quality by resampling large coordinates of (see Scaling GANs: ProGAN, SAGAN and BigGAN). StyleGAN truncates in instead. Let be the centre of mass of , in practice an average of many mapped codes:
Each code is pulled towards it with a factor :
With every code keeps 70% of its distance from the centre. Rare, poorly learned regions of are avoided, and samples become cleaner and less varied. At every sample is the same "mean face". A negative reflects the code through the centre, and attributes flip to their opposites: viewpoint, glasses, age, hair length.
Two practical differences from BigGAN. The generator needs no special training to tolerate truncation, while BigGAN added orthogonal regularization for that. And truncation can be applied to some layers only. The paper's showcase images use at resolutions –: the layout is tamed, the fine detail is left alone.
Measuring disentanglement
Is really less entangled than ? There are no labels for the factors, so the paper measures it in two ways that need no encoder.
Perceptual path length (PPL) asks how smoothly the image changes along a straight path between two codes. In an entangled space the path has to bend around missing combinations, and somewhere along it the image changes abruptly. Take two codes , a random point on the segment between them, and a tiny step . Let be a perceptual distance between two images: a squared distance between their deep-network features. This is LPIPS, which gets its own topic later in the course. With , the path length is
Because is a squared distance, for a small step it is about times the squared speed at which the image changes. Dividing by leaves that squared speed, whatever the step. In the same is done with spherical interpolation, because Gaussian codes concentrate on a shell. On faces, the traditional generator scores in , the style-based one in ; lower is smoother.
Linear separability asks whether each attribute is split by a hyperplane in the code space. Attribute classifiers (gender, smile and so on, 40 in all) label generated images, and a linear SVM predicts each label from the code. With the classifier's label and the SVM's, the score is , using the conditional entropy from Entropy, cross-entropy and KL. It equals when every attribute is perfectly linear. It falls from for the traditional generator in to for StyleGAN in .
A telling detail: put the same mapping network in front of a traditional generator. Its scores , while its scores . The mapping network absorbs the bending, and becomes even more tangled than before.
StyleGAN2: styles in the weights
StyleGAN images carry a tell-tale artifact: a blob like a water droplet, present in the feature maps from about in nearly every image (Karras et al., 2020). The culprit is AdaIN. Normalizing each channel separately erases how strong the channels are relative to one another. The generator smuggles that information past the normalization with a strong local spike: one huge value dominates the channel's statistics and so controls how everything else gets scaled. Remove the normalization and the droplets disappear.
StyleGAN2 keeps the styles but moves them into the weights. A style is now just a scale per input channel, computed from by as before. Write for a convolution weight from input channel to output channel at kernel tap ; the letter is taken by the code. Modulation scales each input channel by its style:
If the inputs are independent with unit variance, output channel then has standard deviation . Demodulation divides by it, so every output channel returns to unit variance (implementations add a tiny constant under the root):
A toy: one output, two inputs, one tap, weights and styles . Modulation gives , and , so demodulation gives . The output variance is , and the first input now carries 95% of it instead of 36%. That is the style at work, without normalizing any actual feature map. The price is an assumption: if the two inputs are correlated with , the variance is , not .
Two more changes. A path length regularizer trains a fixed-size step in to produce a fixed-size change in the image, which directly lowers PPL. And progressive growing gives way to a fixed network with skip connections. With larger networks on top, the paper's quality score on faces improves from to (lower is better; it is FID, from the evaluation topics later in the course).
Where control breaks
All this control works for images the generator made itself, where is known. A real photo comes with no code. To edit one, you first need a , or a point of , whose image matches it. Finding it is called GAN inversion: an optimization for each photo, gradient descent on a reconstruction loss through the frozen . It is slow, and a face outside the generator's range can only be approximated. StyleGAN2's path length regularization makes inversion noticeably easier, but does not make it unnecessary.
Disentanglement itself is a tendency, not a guarantee. The separability of stays well above its floor of , and an attribute such as hair style is spread over the coarse and the middle groups. Each model also covers one category of images, mostly aligned crops: one network for faces, another for cars.
The missing piece is a way back from image to code that comes with the model. The next extension, Autoencoding GANs: AAE and α-GAN, trains an encoder together with the generator. The main line takes another route in The change of variables: normalizing flows make the generator itself invertible, so every image has exactly one , and with it an exact likelihood.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.