20 / 34 · GANs
StarGAN: one generator for many domains
One network that gives a face any hair colour, gender or expression: StarGAN conditions the generator on a target label, and StarGAN v2 replaces the label by a learned style code, giving many outputs per input.
CycleGAN: unpaired image-to-image translation · Wasserstein GANs and Lipschitz constraints
On this page 12
A face dataset labels every photo with its hair colour, gender and age. You want one tool that takes any face and makes it blond, or male, or old, or all three at once. CycleGAN translates between two domains with two generators. Here every request is a different pair of domains, and a separate model per pair does not scale.
StarGAN (Choi et al., 2018) makes the target domain an input of a single generator, and a single discriminator also says which domain an image belongs to. StarGAN v2 (Choi et al., 2020) then removes CycleGAN’s other limit, one output per input, by replacing the label with a style code drawn from a learned space of styles of that domain.
Why not one CycleGAN per pair?#
A domain is a set of images that share an attribute value: blond hair, male, a happy expression. A translator between domains must handle every ordered pair of them, directions. CycleGAN learns the two directions of a pair together, so it needs models and generators: 12 for four domains, 56 for the eight facial expressions of the RaFD dataset.
Each of these generators also learns from the images of two domains only, although the shape of a face or the texture of skin is shared by all of them.
StarGAN makes the target an input. Its generator takes an image and a target label and returns . In training, is drawn at random, so one network learns every direction, and at test time any label can be requested. This is the conditional GAN with an image in place of the noise.
How the generator reads the label#
A label is a vector with entries: binary for attributes that can occur together (on CelebA, black, blond and brown hair, male, young), one-hot for categories that exclude each other (the eight expressions of RaFD). The generator copies each entry over a constant map and concatenates these maps with the three colour channels, so its first layer sees an tensor.
The rest is the CycleGAN generator at with six residual blocks and instance normalization. Each label entry adds only the weights of one input channel of the first convolution; nothing else depends on the number of domains.
The discriminator: real or fake, and which domain#
The discriminator has one convolutional trunk and two heads: scores realism, and outputs one logit per label entry. The second head is the auxiliary classifier of AC-GAN (Odena, Olah and Shlens, 2017). Let be the original label of a real image . The classifier enters two losses:
minimizes the first: on real images it learns their true labels. minimizes the second: a translated image must be classified as the target domain. The classifier is trained on real images only, so it encodes what the domains look like in the data, and the generator is rewarded for producing exactly that.
For a one-hot label, is a softmax probability and of it a cross-entropy. The entries of a binary attribute vector can co-occur, so each gets its own sigmoid, and the attributes are treated as independent given the image. With logits ,
so is a sum of binary cross-entropies, which is what the authors’ implementation computes for CelebA.
Worked example: classifying a fake#
Use an original synthetic tile with four independent binary attributes: striped, bright, wide and tilted. The requested label is and the example logits are . No image dataset or trained classifier supplies these values. Each attribute contributes :
| striped | bright | wide | tilted | |
|---|---|---|---|---|
| target | 1 | 0 | 1 | 0 |
| logit | 0.8 | −0.3 | 1.1 | 0.4 |
| 0.690 | 0.426 | 0.750 | 0.599 | |
| loss term | 0.371 | 0.554 | 0.287 | 0.913 |
The sum is approximately . The largest contribution is “tilted”: the target is zero, but its predicted probability exceeds one half. Changing the striped and wide logits to and raises the loss by , to about . The logit gradient is ; during a generator update it propagates through the fixed classifier into . It rewards matching the target label, but says nothing by itself about preserving the input’s content.
Adversarial loss and the way back#
For the realism head, StarGAN uses the WGAN-GP objective, with a critic with a real-valued output:
where is drawn on segments between real and translated images and . The discriminator maximizes , with the generated images detached for that update. The generator minimizes only ; the gradient penalty is a discriminator regularizer and is not differentiated through .
Neither the adversarial nor the classification objective explicitly compares the translated image with its input: any realistic image of domain satisfies both, whatever was. This is CycleGAN’s problem with the adversarial loss alone, and StarGAN uses the same remedy, a cycle-consistency loss:
The way back is the same generator with the original label . Inverting a translation is just another translation, so no second network is needed.
The full objective#
Each player has its own classification term, so the two minimize different objectives:
with and in all experiments. Training uses Adam with an initial learning rate , five critic updates per generator update as in WGAN-GP, and batches of 16. The authors’ implementation draws target labels by shuffling the labels of the batch, so every requested label is a combination that occurs in the data.
One StarGAN training step: translate, fool D, return
Training on several datasets: the mask vector#
CelebA labels hair colour, gender and age; RaFD (4,824 images of 67 people) labels expressions. A model trained on both could give CelebA faces the expressions learned from RaFD. The obstacle is the reconstruction loss: it needs the full original label , and each dataset knows only its own part of it.
StarGAN appends a mask vector , a one-hot vector over the datasets that says which part of the label is known, and sets the unknown parts to zero:
For CelebA and RaFD the unified label has entries. A blond young woman from CelebA is ; the request “make her happy” is one-hot for happy . The classifier predicts all labels, but on a batch from one dataset only the outputs for that dataset’s labels enter and , and training alternates between the datasets.
The model learns to read the mask. Given an expression label with the wrong mask , it ignores the expression, reads the all-zero CelebA part as “not young”, and ages the face (Choi et al., 2018, Figure 7).
One output per image and domain#
Sharing helps: a model for two RaFD expressions sees about 1,000 images, StarGAN about 4,000. An expression classifier that errs on 0.45% of real test images errs on 2.12% of StarGAN’s translations and 5.99% of CycleGAN’s (Choi et al., 2018, Table 3).
But is a function: the same image and label always give the same translation. A domain contains many appearances. “Female” covers every hairstyle and every kind of makeup, and StarGAN picks one of them for each input. On CelebA-HQ it changes the gender of a face only locally, mostly by applying makeup (Choi et al., 2020).
The obvious fix is to give the generator noise as well. In the reported StarGAN v2 ablation, the authors tried it: feed a latent code to next to the domain and ask an encoder to recover it from the output. The outputs became varied, but the codes had no way to separate domains, the authors conjecture that this favoured domain-shared variations, such as colour, over domain-specific variations, such as hairstyle. The fix that worked maps the noise to a style of a particular domain first.
StarGAN v2: a style code instead of a label#
The v2 explanation and local schematic below adapt the method described by Yunjey Choi, Youngjung Uh, Jaejun Yoo and Jung-Woo Ha in StarGAN v2, arXiv v2, licensed CC BY 4.0. Wording, organization and diagram layout are changed; see the scoped attribution. The article license does not license the separately distributed code, models or images used as training data.
Four networks#
From here on, is the original domain of , a target domain, a latent code in and a style code. StarGAN v2 has four networks.
The generator translates to an image with the style . It receives no domain label, because the style code already belongs to a domain. The mapping network turns a latent code into a style code of a chosen domain, : an MLP with four shared layers and a branch of four more for each domain. The style encoder reads the style of an image of domain , , with a shared trunk and one output head per domain. The discriminator has one output head per domain.
Latent-guided translation samples and uses , so varying can produce different outputs; the losses below encourage the model to use that variation. Reference-guided translation uses for a real image of the target domain, and the output takes on its style.
How the style enters#
The style enters through adaptive instance normalization, as in StyleGAN: each channel is normalized over its positions, then scaled and shifted by numbers computed from . The official implementation writes the scale as , so a zero style map starts as plain instance normalization (Clova AI Research, 2020). The downsampling blocks use plain instance normalization; the last two bottleneck blocks and all upsampling blocks use AdaIN.
This is where StarGAN’s padding problem disappears. StarGAN’s label entered before normalization and relied on a boundary-induced spatial variation. The style acts after the normalization, which therefore cannot remove it.
One discriminator head per domain#
StarGAN v2’s discriminator asks one question per domain: is this a real image of domain ? Each head is a binary real-versus-fake classifier, and there is no classification loss. The authors report that this multi-task discriminator (Mescheder, Geiger and Nowozin, 2018; Liu et al., 2019) lets the generator change the global structure of an image, where the auxiliary classifier led to local edits.
Losses#
Draw a target domain and a latent code , and let . The head of the target domain judges the translation:
where is a probability, a sigmoid of the head’s logit. In practice the generator uses the non-saturating loss .
Three more terms tie the output to the style and to the input. Style reconstruction asks the style encoder to read back from the output, penalizing when it ignores distinct styles. This term alone does not prevent the mapping network from collapsing different latent codes to one style. Style diversification draws a second code and rewards the generator for a second output that differs from the first. The cycle returns to with the input’s own estimated style :
StarGAN’s way back used the original label , which says only which domain to return to. Here the return must restore the particular appearance of the input. The generator, mapping network and style encoder play against the discriminator:
with and on CelebA-HQ, 2 on the animal-face dataset AFHQ. The diversity term is maximized. On an unconstrained output space it can grow without bound; even with bounded pixels it can reward unrealistic differences. The authors linearly decay its weight to zero during training to stabilize the competing objectives; this is a training choice, not a proof of stability. The same objective is also trained with reference-guided styles in place of .
One StarGAN v2 training step: a style, a translation and three checks
R1: a penalty on real images only#
StarGAN used WGAN-GP. StarGAN v2 uses the non-saturating loss and adds the R1 penalty of Mescheder, Geiger and Nowozin (2018) to the discriminator’s loss:
with , where is the discriminator’s logit and the data. The penalty acts on real images only and pulls the gradient towards zero, where WGAN-GP pulls it towards norm 1 on interpolates. A linear logit with shows the difference: , so the WGAN-GP penalty is 0, while R1 is . R1 asks for a discriminator that is flat on the data.
Why flat? Mescheder et al. answer with the smallest possible GAN, the Dirac-GAN. The data are a point mass at 0, the generator is a point mass at , and the discriminator’s logit is . With , the value is : the real point scores and the fake one . The discriminator maximizes , the R1 penalty being because ; the generator minimizes . Gradient ascent and descent in continuous time, linearized at the equilibrium with , give
Without the penalty, , the distance to the equilibrium is conserved: the two players circle it forever. With it shrinks. The eigenvalues of this linear system solve , so for both equal and the players approach the equilibrium at the rate , up to a linear factor. This calculation concerns the local linearized continuous-time system, not a general neural training trajectory. Their Theorem 4.1 proves local convergence of simultaneous and alternating updates under assumptions I–III: a realizable equilibrium, a discriminator zero in a neighbourhood of the data support, regular parameterization manifolds and nondegeneracy/expressivity conditions, with sufficiently small learning rates. It is not a global convergence guarantee for StarGAN v2.
How translation models are measured#
The comparisons of StarGAN v2 follow a fixed protocol (Choi et al., 2020, Appendix C). For realism, every test image of a source domain is translated to a target domain with 10 random latent codes, and the FID between these translations and the training images of the target domain is computed and averaged over pairs of domains. For diversity, the LPIPS distances between the pairs of outputs of each test image are averaged.
Selected rows from the authors’ Table 1, with abbreviated labels, under CC BY 4.0: the CelebA-HQ ablation compares successive changes. These are published research measurements, not results of our synthetic practice.
| Configuration | FID | LPIPS |
|---|---|---|
| A. StarGAN | 98.4 | – |
| C. + multi-task , R1, AdaIN | 80.5 | – |
| D. + latent code fed to | 32.3 | 0.312 |
| E. + mapping network and style code | 17.1 | 0.405 |
| F. + style diversification: StarGAN v2 | 13.7 | 0.452 |
The authors attribute the C-to-D gap to the number of output samples: D supplies ten outputs per input where C supplies one. That comparison mixes a model change with sample-size bias in FID. Equal output counts in D, E and F make the comparison more informative, but a lower FID is still a distributional score, not proof of better input-preserving translations; empirical FID is not guaranteed to decrease in every larger finite sample. For reference, the FID between the training and the test images of CelebA-HQ is 14.8, and StarGAN v2 lies below it; with a small test set, that number is no floor.
Neither score looks at the input: a generator that ignores and draws good faces scores well. A fair report adds domain accuracy, as in StarGAN’s RaFD experiment, and a check that pose and identity survive.
Where it breaks#
Diversity scores count artifacts as diversity. Against two-domain models trained once per pair of domains, StarGAN v2 has the lowest FID on AFHQ, 16.2 against 41.5 for MUNIT. Yet MUNIT’s LPIPS, 0.511, is higher than StarGAN v2’s 0.450 (Table 2). The authors conjecture that spurious artifacts in MUNIT’s outputs explain this: two outputs that differ by artifacts are far apart in LPIPS, and the score reads that as variety.
The published StarGAN v2 setup assigns each training image one domain index, because , and are heads indexed by it. Overlapping attributes are not interchangeable with that single categorical index: one CelebA face is male, young and smiling at once, so directly treating its 40 attributes as 40 mutually exclusive domains would discard this overlap. A usable partition takes one binary attribute, mutually exclusive values of one attribute, or the products of a few attributes, such as male or female times young or old. The mask vector, which joined datasets with different label sets, has no counterpart.
What changed#
| CycleGAN | StarGAN | StarGAN v2 | |
|---|---|---|---|
| Networks for domains | generators and discriminators | one , one | one each of , , , |
| Condition | none | target label, tiled and concatenated | style code through AdaIN |
| Discriminator | one per domain | critic and domain classifier | one real/fake head per domain |
| Tie to the input | two cycles, identity loss | cycle with the original label | cycle with the input’s style |
| Outputs per input and target | one | one | many |
| Labels | two unlabelled collections | any combination of attributes | one domain per image |
What you should remember#
- One replaces pairwise generators and learns from all domains.
- classifies real images; must make fakes it assigns to the target; the way back is .
- A mask vector marks which dataset’s labels are known, so one model trains on several datasets.
- StarGAN v2: a style or enters through AdaIN; style, diversity and cycle losses.
- R1 penalizes on real data; local convergence results require the stated equilibrium and regularity assumptions.
- FID depends on sample count; diversity scores can reward artifacts.
Notebook#
Run python3 scripts/notes/generative-practice.py stargan. The synthetic diagnostic represents an object by three coordinates: two preserve its content, and the third stores a domain offset plus style. Domain offsets are −2 and 2; style lies between −0.4 and 0.4. Three content points are authored directly in the script. There are no faces, overlapping attribute labels or model checkpoints.
- Translate from the negative to the positive domain while retaining the first two coordinates; restore the original domain and style. The saved output has zero content and cycle error, with 100% target-domain accuracy on these three constructed points.
- Apply the identity map instead. It also has zero cycle error, but 0% target-domain accuracy. Cycle consistency alone cannot tell whether a translation happened.
- Supply styles −0.4 and 0.4 to the same target domain. The style coordinate changes by 0.8. A map that ignores the supplied style has separation zero. These checks distinguish content preservation, domain control and style diversity. The transformation is analytic; no StarGAN networks or perceptual-quality metric are trained or evaluated.
The example, script and observations were newly authored for this course with Codex assistance; the numerical results are from the linked run. A separate standard-library verifier recomputes the four-attribute classifier example, the padding/normalization mechanism and the local linearized R1 dynamics; its saved results are also reproducible without downloads.
Next#
StarGAN v2 gives many styles across many domains with one generator, but it is one route among several. The next note follows the others: splitting every image into content and style (MUNIT), dropping the cycle in favour of a contrastive loss between patches (CUT), generating a photograph from a label map (SPADE), and adding plausible detail to a small image (SRGAN).
Where to see the images#
These models are best judged by their samples; figure numbers refer to the linked arXiv versions.
- StarGAN (Choi et al., 2018; arXiv v3): CelebA faces given RaFD expressions in Figure 1; single and multiple attribute transfer in Figure 4; RaFD expressions in Figure 5; repository.
- StarGAN v2 (Choi et al., 2020; arXiv v2): diverse outputs on CelebA-HQ and AFHQ in Figure 1; reference-guided synthesis in Figure 4; comparisons with the baselines in Figures 5 and 6; repository.
References#
- Y. Choi, M. Choi, M. Kim, J.-W. Ha, S. Kim, J. Choo. StarGAN: Unified generative adversarial networks for multi-domain image-to-image translation. CVPR, 2018. arXiv v3: the losses in Section 3.1, the mask vector in Section 3.2, the networks in Section 4 and Appendix 7.2, results in Table 3 and Figures 6–7.
- Y. Choi, Y. Uh, J. Yoo, J.-W. Ha. StarGAN v2: Diverse image synthesis for multiple domains. CVPR, 2020. arXiv v2: the networks and losses in Section 2, their architecture in Appendix E, the ablation in Section 3.1 and Table 1, comparisons in Table 2, training in Appendix B, the evaluation protocol in Appendix C.
- A. Odena, C. Olah, J. Shlens. Conditional image synthesis with auxiliary classifier GANs. ICML, 2017.
- L. Mescheder, A. Geiger, S. Nowozin. Which training methods for GANs do actually converge?. ICML, 2018. The Dirac-GAN in Section 2, the R1 penalty and Theorem 4.1 in Section 4.
- M.-Y. Liu, X. Huang, A. Mallya, T. Karras, T. Aila, J. Lehtinen, J. Kautz. Few-shot unsupervised image-to-image translation. ICCV, 2019. The multi-task adversarial discriminator.
- Y. Choi. StarGAN, official PyTorch implementation, 2018.
model.py: label tiling and the remark on padding;solver.py: binary cross-entropy for CelebA labels, target labels by shuffling the batch. - Clova AI Research. StarGAN v2, official PyTorch implementation, 2020.
core/model.py: AdaIN with scale .