15 / 33 · GANs
StarGAN: one generator for many domains
Multi-domain image-to-image translation with one network: StarGAN conditions the generator on a target label, adds a domain classifier to a WGAN-GP discriminator, returns with the original label for reconstruction and trains across datasets with a mask vector; StarGAN v2 replaces the label by a style code from a mapping network or a style encoder, injected with AdaIN, to give many outputs per input.
CycleGAN: unpaired image-to-image translation · Wasserstein GANs and Lipschitz constraints
On this page 20
The CycleGAN note ended with two limits of translating between two domains. Covering domains takes a separate model for every pair, generators in all, and each of them learns from the images of only two domains. And every generator is deterministic: a horse has exactly one zebra version. StarGAN (Choi et al., 2018) removes the first limit. One generator receives the target domain as an input, one discriminator also says which domain an image belongs to, and the cycle loss comes back with the original domain as the target of the way back. StarGAN v2 (Choi et al., 2020) removes the second. The target is no longer a fixed label but a style code drawn from a learned space of styles of that domain, so one input image has many translations.
Two models, one story. StarGAN answers “translate to this combination of labels” with one deterministic generator; StarGAN v2 answers “translate to this domain, in this style” with a mapping network, a style encoder and AdaIN. Both keep the cycle back to the original image. A reader who needs only multi-domain labels can stop after one output per image and domain and go to the limitations; the StarGAN v2 sections are an extension for diverse, style-guided outputs.
Notation. A domain is a set of images that share an attribute value: blond hair, male, a happy expression (Choi et al., 2018, Section 1). is a real image and its original domain label; is a target label. Labels are vectors with entries: binary for attributes that can occur together (hair colour, gender, age), one-hot for categories that exclude each other (facial expressions). is the generator and the discriminator. For StarGAN v2, is the original domain of and a target domain, a latent code and a style code; is the mapping network (not the inverse generator of CycleGAN) and the style encoder. As in the CycleGAN note, is the sum of absolute pixel differences, which implementations average over pixels.
Why not one CycleGAN per pair?#
A translator between domains must handle every ordered pair of them, directions. CycleGAN learns the two directions of a pair together, one generator each, so it needs models with generators and as many discriminators. Four domains take 12 generators; the eight facial expressions of the RaFD dataset take 56. A new domain adds pairs, generators. The count is not the only cost. Each generator learns from the images of two domains, although most of what it needs, the shape of a face or the texture of skin, is shared by all of them (Choi et al., 2018, Section 1 and Figure 2).
StarGAN makes the target an input. Its generator takes an image and a target label and returns the translation, . In training, is drawn at random, so the same 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. The discriminator is shared too: besides real versus fake, it predicts the domain of its input, .
How the generator reads the label#
On CelebA, the label has five binary entries: black, blond and brown hair, male, young. On RaFD it is one-hot over eight expressions. 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 (Choi et al., 2018, Figure 3 and Table 4).
The rest is essentially the CycleGAN generator at , with kernels for down- and upsampling: a convolution, two stride-2 convolutions, six residual blocks, two stride-2 transposed convolutions and a convolution with , with instance normalization after every layer but the last. It has 8.43 million parameters. Each label entry adds weights to the first convolution, and nothing else in the generator depends on the number of domains.
The discriminator: real or fake, and which domain#
The discriminator is six stride-2 convolutions with LeakyReLU (slope 0.01) and no normalization, followed by two heads on the same features (Choi et al., 2018, Table 5). , a convolution, scores realism; for a input it outputs a map in the manner of a PatchGAN, although each of the four scores sees the whole image. , a convolution whose kernel covers the whole feature map, outputs one logit per label entry. The discriminator holds 44.76 million of the model’s 53.19 million parameters.
The second head is the auxiliary classifier of AC-GAN (Odena, Olah and Shlens, 2017). It enters two losses (Choi et al., 2018, Eqs. 2–3):
The first is minimized by : on real images it learns to predict their true labels. The second is minimized by : a translated image must be classified as the target domain. The classifier never learns from fakes. It 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.
What means depends on the label. For a one-hot label it is the softmax probability of the target class, and of it is the cross-entropy. The entries of a binary attribute vector can co-occur, so gives each attribute its own sigmoid and treats them as independent given the image. Then for the logits , and is a sum of binary cross-entropies, which is what the authors’ implementation computes for CelebA (Choi, 2018).
Worked example: classifying a fake#
Take toy logits that might return for a translated face whose target is : blond, male, young. Each attribute contributes :
| black | blond | brown | male | young | |
|---|---|---|---|---|---|
| target | 0 | 1 | 0 | 1 | 1 |
| logit | −2.0 | 1.5 | −1.0 | 0.5 | 2.0 |
| 0.119 | 0.818 | 0.269 | 0.622 | 0.881 | |
| loss term | 0.127 | 0.201 | 0.313 | 0.474 | 0.127 |
The sum is : the classifier gives the full target label a probability of 0.289. The largest term is gender, where it is least sure. If the generator had ignored the hair entries and left the hair black, the black and blond logits might be swapped to and , and the loss would rise to . The gradient of flows through the classifier into and pushes the hair towards blond.
Adversarial loss and the way back#
The adversarial loss is first written as the standard GAN value, , and then replaced by the WGAN-GP objective to stabilize training (Choi et al., 2018, Eq. 8):
with drawn on segments between real and translated images and . is a critic with a real-valued output. The discriminator maximizes and the generator minimizes it, but the generator’s loss contains only the second term: the penalty is computed on interpolates involving , and only the discriminator is trained on it.
Neither nor mentions the input: both are satisfied by any realistic image of domain , whatever was. This is the problem CycleGAN met with the adversarial loss alone, and StarGAN uses the same remedy, a cycle-consistency loss (Choi et al., 2018, Eq. 4):
The way back is the same generator with the original label . There is no second network to undo the first: inverting a translation is just another translation. StarGAN has no identity loss.
The full objective#
The two players minimize different objectives (Choi et al., 2018, Eqs. 5–6):
with and in all experiments. This is not one minimax value: each player has its own classification term, and each minimizes it. Training uses Adam with learning rate and betas , five critic updates per generator update as in WGAN-GP, batch size 16 and random horizontal flips. On CelebA (202,599 faces with 40 binary attributes, cropped from to and resized to , 2,000 held out) the learning rate is constant for 10 epochs and decays linearly to zero over 10 more (Sections 5.2–5.3). 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 (Choi, 2018).
One StarGAN training step: translate, fool D, return
- Translate. The generator receives the real image and a random target label , tiled into constant maps and concatenated with the colour channels, and produces .
- Fool the discriminator. The critic scores the fake, and the generator wants a high score. The classifier must assign the fake to , which is .
- Return. The same generator maps the fake back with the original label , and asks for the input.
- Train the discriminator. On real images and fakes, the critic learns to score real above fake under the gradient penalty; on real images only, the classifier learns the original label , which is .
Training on several datasets: the mask vector#
CelebA labels hair colour, gender and age; RaFD (4,824 images of 67 people, each showing eight expressions) 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 (Choi et al., 2018, Section 3.2).
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 (Eq. 7):
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 generator is unchanged apart from its input width. 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 jointly trained model gives CelebA faces sharper expressions than a model trained on RaFD alone, which never sees a CelebA image (Section 5.6, Figure 6). The mask is used as intended. Given an expression label with the wrong mask , the model ignores the expression, reads the all-zero CelebA part as “not young”, and ages the face (Figure 7).
What one generator buys#
- Quality. In a user study on CelebA against DIAT, CycleGAN and IcGAN, StarGAN received 66.2% of the votes for the best translation of hair colour, 39.1% for gender and 70.6% for age (Choi et al., 2018, Table 1).
- Correct domains. On RaFD, translating neutral faces to the seven other expressions, a ResNet-18 expression classifier that errs on 0.45% of real test images errs on 2.12% of StarGAN’s translations, against 5.99% for CycleGAN, 4.10% for DIAT and 8.07% for IcGAN (Table 3).
- Size. The same seven translations take 53.2 million parameters with StarGAN and with CycleGAN, about 14 times more (Table 3). The architecture tables give 8.43 million for the generator and 44.76 million for the discriminator, 53.19 million in total.
The authors attribute the quality to multi-task learning: a model trained on two RaFD expressions sees about 1,000 images, StarGAN about 4,000, and the features it learns serve all directions (Section 5.5).
One output per image and domain#
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. Its authors put it plainly in the follow-up paper: the generator receives a fixed label and “inevitably produces the same output per each domain, given a source image” (Choi et al., 2020, Section 1). On CelebA-HQ, StarGAN changes the gender of a face only locally, by applying makeup (Section 3.1, Figure 3a).
The obvious fix is to give the generator noise as well. StarGAN v2 tried it: feed a latent code to next to the domain, and ask an encoder to recover it, . Outputs became varied, but the codes had no way to separate domains, so they learned variations shared by all domains, such as colour, rather than domain-specific ones, such as hairstyle (Section 3.1). The fix that worked maps the noise to a style of a particular domain first.
StarGAN v2: a style code instead of a label#
Four networks#
StarGAN v2 has four networks (Choi et al., 2020, Section 2.1 and Appendix E):
- the generator translates to an image that reflects the style code . It receives no domain label: the style code already belongs to a domain;
- the mapping network turns a latent code , , into a style code of a chosen domain, . It is an MLP with four shared linear layers of width 512 and, for each of the domains, a branch of four more;
- the style encoder extracts the style code of an image of domain , : six shared residual blocks and one linear output head per domain;
- the discriminator has the same trunk and one output per domain. says whether is a real image of domain .
The two style sources give two ways to translate. Latent-guided: sample and use ; each gives another output. Reference-guided: take a real image of the target domain and use ; the output takes on the reference’s style. A new domain costs one branch of (0.82 million parameters), a linear head of (32,832) and a head of (513).
How the style enters: AdaIN#
For channel of a feature map , let and be its mean and variance over the spatial positions of one image, and a small constant ( in PyTorch’s InstanceNorm2d) that keeps the division finite for a constant channel. Adaptive instance normalization replaces them with values computed from the style:
where and are learned affine functions of , one per AdaIN layer. The scale is a learned number, not a standard deviation: nothing keeps it positive, and a negative value flips the channel, giving an output standard deviation of . All their biases start at 0 except that of the scale , which starts at 1 (Choi et al., 2020, Appendices B and E); the official code writes the scale as with a zero bias, which is equivalent (Clova AI Research, 2020). AdaIN comes from style transfer, where and were the standard deviation and the mean of the features of a style image (Huang and Belongie, 2017, Eq. 8); StyleGAN computed them from a learned style vector instead (Karras, Laine and Aila, 2019). In the StarGAN v2 generator, the downsampling blocks and the first two bottleneck blocks use plain instance normalization; the last two bottleneck blocks and all upsampling blocks use AdaIN, and the style code enters every one of them.
Worked example (toy numbers). A channel holds over four positions. Its mean is and its standard deviation , so the normalized values are ; changes them only in the sixth decimal. A style that gives and turns them into , whose mean is exactly and standard deviation exactly . The input would give the same normalized values: AdaIN discards each channel’s incoming mean and scale and imposes the ones chosen by the style. The style acts after the normalization, so, unlike the concatenated label of StarGAN, it cannot be normalized away.
One discriminator head per domain#
StarGAN’s discriminator answered two questions with two heads: is the image real, and which domain is it. StarGAN v2’s 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. This multi-task discriminator (Mescheder, Geiger and Nowozin, 2018; Liu et al., 2019) lets the generator change the global structure of an image, not only local details (Choi et al., 2020, Section 3.1).
Losses#
Let be a real image of domain , and draw a target domain and a latent code , with the target style (Choi et al., 2020, Section 2.2). The paper writes target quantities with a tilde, ; we keep the tilde only on the domain.
Adversarial loss. The head of the target domain judges the translation (Eq. 1):
where here denotes a probability, a sigmoid of the head’s logit. In practice the generator uses the non-saturating loss (Appendix B).
Style reconstruction. The generator must use the style code, so the style encoder must be able to read it back from the output (Eq. 2):
Style diversification. A second latent code gives a second style code , and the generator is rewarded for making the two outputs differ (Eq. 3):
This term is maximized. Earlier versions divided it by the distance between the latent codes, ; when the two codes are close, the ratio becomes large and its gradients unstable, so StarGAN v2 drops the denominator (Section 2.2). The term has no upper bound, and its weight is decreased linearly to zero during training (Appendix B).
Cycle consistency. The output must lead back to the input when the generator is given the input’s own style (Eq. 4):
In StarGAN the way back used the original label , which says only which domain to return to. Here it uses the estimated style of itself, so the return must restore the particular appearance of the input.
Full objective. The generator, the mapping network and the style encoder play against the discriminator (Eq. 5):
with and on CelebA-HQ, on AFHQ (Appendix B). 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
- Style. The mapping network turns into a style code for a random target domain ; in reference-guided steps the style encoder reads it from a reference image instead.
- Translate and judge. The generator injects through AdaIN and produces ; the head judges whether it is a real image of domain .
- Style reconstruction. The style encoder must recover from the output: .
- Cycle. With the input’s own style , the generator must map the output back to : .
- Diversity. A second code gives a second output, and rewards the distance between the two.
R1: a penalty on real images only#
StarGAN used WGAN-GP. StarGAN v2 uses the non-saturating loss and adds the R1 penalty to the discriminator’s loss (Mescheder, Geiger and Nowozin, 2018, Section 4.1, Eq. 9; Choi et al., 2020, Appendix B):
where is the logit of the discriminator (for StarGAN v2, of the head for a real image of domain ). The penalty acts on real images only and pulls the gradient towards zero, not towards norm 1 on interpolates as in WGAN-GP. The reasoning concerns the end of training. When the generator reproduces the data and the discriminator is constant on it, a discriminator with a non-zero gradient across the data could still push the generator off the data; R1 makes such a gradient cost something. Mescheder et al. prove that GAN training with zero-centered penalties of this kind converges locally, where unregularized training need not.
Worked example. A discriminator with a linear logit and has and . WGAN-GP’s penalty is : the slope is exactly what WGAN-GP asks for. R1 with is : it asks for a discriminator that is flat at the data.
Training. Batch size 8, 100,000 iterations (about three days on one V100), images at . Adam with betas ; learning rate for , and , and for . decays linearly to zero over the 100,000 iterations. Evaluation uses an exponential moving average of the weights of every network except (Choi et al., 2020, Appendix B).
What each piece buys#
StarGAN v2 was built by adding one change at a time to StarGAN and measuring each step on CelebA-HQ, split into the domains male and female (Choi et al., 2020, Table 1). FID measures how far the translations are from real images of the target domain (lower is better); LPIPS here is the average distance between outputs generated for the same input (higher means more diverse).
| Configuration | FID | LPIPS |
|---|---|---|
| A. StarGAN: WGAN-GP, auxiliary classifier, concatenated label | 98.4 | – |
| B. + multi-task discriminator | 91.4 | – |
| C. + R1 penalty, AdaIN instead of concatenation | 80.5 | – |
| D. + latent code fed to , latent reconstruction | 32.3 | 0.312 |
| E. + mapping network and style code, style reconstruction | 17.1 | 0.405 |
| F. + style diversification: StarGAN v2 | 13.7 | 0.452 |
Configurations A to C give one output per input, so they have no diversity score. The drop from C to D is not a better model: D produces ten outputs per input where C produces one, an FID computed from more generated images is lower, and the authors attribute the whole C–D gap to the number of samples (Section 3.1). This is the sample-size bias of FID in practice. Between configurations with the same number of outputs, the style code (D to E) and the diversity term (E to F) both lower FID and raise LPIPS. For reference, the FID between the training and the test images of CelebA-HQ is 14.8. It is not a floor: the test set is small, and StarGAN v2’s 13.7 lies below it (Table 2).
Against two-domain models that already produced diverse outputs, MUNIT, DRIT and MSGAN, each trained once per pair of domains, StarGAN v2 has the lowest FID in latent-guided translation: 13.7 against 31.4–52.1 on CelebA-HQ and 16.2 against 41.5–95.6 on AFHQ, a dataset of 15,000 cat, dog and wildlife faces (Table 2). Its LPIPS is the highest on CelebA-HQ (0.452), but on AFHQ MUNIT (0.511) and MSGAN (0.517) score higher than StarGAN v2 (0.450). The authors attribute this to spurious artifacts in those outputs: artifacts make outputs differ, and a diversity score counts them as diversity.
How translation models are measured#
The comparisons above follow a fixed protocol (Choi et al., 2020, Appendix C):
- FID 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. This is repeated for every pair of domains and averaged. Reference-guided synthesis uses 10 random reference images of the target domain instead.
- LPIPS for diversity. Every test image gets 10 outputs from 10 random latent codes. The LPIPS distances of all pairs of outputs are averaged, and the result is averaged over the test images.
Each number answers one question and misses others. FID never looks at the input, so a generator that ignores and produces good faces scores well. Diversity rewards differences of any kind, artifacts included. Two further checks cover what these miss. One is the domain: does a classifier trained on real images recognize the target domain in the output, as in StarGAN’s RaFD experiment? The other is faithfulness: does the output keep what should not change, such as the pose and identity of the input? A distance between an output and its own input, such as LPIPS, measures how much the translation changed the image, which is neither realism nor diversity. The identity map scores 0, a perfect value, while translating nothing. A fair report gives realism, diversity, domain accuracy and faithfulness together.
What counts as a domain#
StarGAN and StarGAN v2 read labels differently. StarGAN takes a vector of attributes and can request any combination of them, such as blond, male and old at once. StarGAN v2 needs each image to belong to exactly one domain: the training pairs are with a single , and , and are heads indexed by it. Overlapping attributes are therefore not domains. In CelebA one face is male, young and smiling at once, so its 40 attributes cannot serve as 40 domains of StarGAN v2. A usable partition takes one binary attribute (male and female, as for CelebA-HQ in the paper), mutually exclusive values of one attribute (black, blond and brown hair, after dropping images with none or several), or the products of a few attributes (male or female times young or old, four domains).
Exploring the style space#
The mapping network makes the style space of each domain a learned transformation of a Gaussian, following StyleGAN, rather than the Gaussian itself, which the authors see as one reason for the diversity of the outputs (Choi et al., 2020, Section 4). The space can be probed directly. Interpolating between two style codes and , or between the codes of two reference images, and translating one input with every intermediate code shows whether the outputs change smoothly and which attributes change; the authors show such interpolations in a video (Appendix D). Interpolating in and in gives different paths, because is nonlinear. Style codes extracted from many real images can be projected to two dimensions, for example with PCA, to see whether the domains separate and whether directions correspond to visible attributes. Swapping references within a domain separates what the style code controls (hairstyle, makeup, beard, age on CelebA-HQ) from what the input keeps (pose, identity).
What changed#
| CycleGAN | StarGAN | StarGAN v2 | |
|---|---|---|---|
| Networks for domains | generators and discriminators | one , one | one each of , , , |
| Condition | none: one direction per generator | target label, tiled and concatenated | style code through AdaIN, from or |
| Discriminator | one per domain, PatchGAN | shared: critic and classifier | one real/fake head per domain |
| Adversarial loss | least squares | WGAN-GP, | non-saturating, R1 with |
| Tie to the input | two cycles and an identity loss | one cycle with the original label , | cycle with the input’s style , |
| Outputs per input and target | one | one | many |
| Labels | none: two unlabelled collections | any combination of attributes; several datasets via a mask vector | one domain per image |
Limitations#
- StarGAN is deterministic. One output per image and label; and the label, entering by concatenation, tends to produce local edits rather than changes of structure.
- StarGAN v2 needs a partition. Every image must belong to one domain, and combinations of attributes must be enumerated as domains of their own. The mask vector of StarGAN, which joins datasets with different label sets, has no counterpart.
- The cycle checks recoverability, not meaning. As in CycleGAN, a translation can be invertible and still wrong, or hide what it needs for the return in signals invisible to a human.
- Diversity has to be managed. The diversity term has no upper bound and is switched off gradually during training. Diversity scores also reward artifacts, as the AFHQ results show.
- Style and content are split by the losses, not by definition. What ends up in the style code depends on the data: breed and fur on AFHQ; hairstyle, makeup, beard and even age on CelebA-HQ.
- The numbers depend on the protocol. FID depends on the number of generated images, and a distance to the input rewards doing nothing; comparisons are fair only under the same protocol.
Summary#
- One generator conditioned on a target label replaces the generators of pairwise models, and learns from the images of all domains.
- StarGAN’s discriminator has a WGAN-GP critic and an auxiliary domain classifier . The classifier is trained on real images (), and the generator must make fakes it assigns to the target (). A reconstruction with the original label, , ties the output to the input; , , .
- A mask vector marks which dataset’s labels are known, so one model trains on CelebA and RaFD together.
- StarGAN is deterministic. StarGAN v2 replaces the label with a style code from a mapping network () or a style encoder (), injects it with AdaIN, judges outputs with one discriminator head per domain, and trains with style reconstruction, style diversification, a cycle with the input’s own style and the R1 penalty.
- Translation models are measured by FID for realism and pairwise LPIPS among outputs for diversity; domain accuracy and faithfulness to the input need checks of their own.
Notebook#
Two notebooks of the HSE-LAMBDA Deep Generative Models repository cover this note: the StarGAN notebook, which presents the theory of both versions, and the task notebook 02_hw_StarGAN.ipynb, which asks the reader to implement, train and evaluate StarGAN on CelebA (cell numbers count every cell, markdown included, from 1). The README of the folder lists the StarGAN and StarGAN v2 papers next to the CycleGAN material.
The StarGAN notebook contains no code; its only code cell (cell 6) is empty.
- Cells 1–2: introduction. Pix2Pix and CycleGAN translate between two domains; StarGAN extends this to many domains, and StarGAN v2 adds diverse outputs.
- Cell 3: StarGAN. The count, , the two-headed discriminator, , and , a combined objective, how StarGAN extends CycleGAN, and the limitation to one output per input and label. Figures 2 and 3 of the StarGAN paper are embedded.
- Cell 4: StarGAN v2. , , and ; latent-guided and reference-guided styles; the adversarial, style-reconstruction, diversity and cycle losses and the full objective; improvements over StarGAN. Figure 2 of the StarGAN v2 paper is embedded.
- Cell 5: conclusions.
Issues in the StarGAN notebook:
- How the label enters. Cell 3 says the label is injected “via channel-wise concatenation or AdaIN-like normalization in some variations”. StarGAN tiles the label spatially and concatenates it with the input image; AdaIN is the StarGAN v2 mechanism.
- The adversarial loss and the weights. Cell 3 calls the adversarial loss “WGAN-GP or similar” and gives no weights. StarGAN uses WGAN-GP with , and , .
- One objective instead of two. Cell 3 writes . The discriminator minimizes rather than maximizing it, so the paper writes two objectives, and , as in The full objective.
- The StarGAN v2 discriminator. Cell 4 describes it as classifying “real vs. fake and the domain label”. It has one binary real/fake output per domain and no classification loss; replacing the auxiliary classifier is configuration B of the ablation.
- The adversarial loss of StarGAN v2. Cell 4 introduces “the non-saturating GAN loss” but writes the minimax value, and uses both for the domain of the real image and for the target. In the paper the real term uses the original domain and the fake term the target ; the non-saturating version changes only the generator’s term to .
- The diversity loss. Cell 4 says it is “possibly normalized by ”. StarGAN v2 removes that denominator for stability and decays linearly to zero.
- The cycle loss. Cell 4 uses a style "". It is the same style encoder, read at the head of the original domain: .
- Missing pieces. Cell 4 omits the R1 penalty and the loss weights (, or ).
The task notebook has three stages: data, model and training, evaluation and analysis. It loads CelebA through CelebADataset from the repository’s seminars/utils/datasets/celeba.py.
- Cells 1–2: title and the goal, generating faces with given attributes from CelebA.
- Cell 3: setup. Installs
lpips, adds../../seminars/utils/tosys.path, imports torch,LPIPS, torchvision transforms,Munch,trange, matplotlib, numpy andCelebADataset, and picks the device (CUDA, Apple MPS or CPU). - Cell 4: data.
args.img_size = 256; images are resized so that the shorter side is 256, center-cropped to and normalized with the ImageNet mean and standard deviation. The dataset root is../../seminars/data/celeba, which the helper fills by downloading CelebA and its attribute file from Google Drive if missing. Batch size 32, shuffled;num_workersis 0 on CUDA and 2 otherwise,pin_memoryonly on CUDA. - Cell 5:
args.num_domains = len(dataset.header), the number of attribute names in the annotation file, 40. - Cells 6–7: a grid of random images, each min-max rescaled for display.
- Cells 8–10: the model. The reader chooses StarGAN or StarGAN v2. Cell 10 prepares a
Munchcontainer with commented slotsgenerator,mapping_network,style_encoderanddiscriminator, the four networks of StarGAN v2 under the names of the authors’ implementation. - Cells 11–12: training, an empty cell for the reader’s code.
- Cells 13–16: evaluation.
LPIPS()with its default AlexNet backbone. Cell 16 runs 100 iterations; each draws three fresh batchesx_real,x_ref,x_ref2and uniformly randomy_trgandy_orgovernum_domains, leaves the translationx_faketo the reader (aNotImplementedErrorplaceholder), appends the LPIPS betweenx_fakeandx_real, prints the mean and asserts that it is below 1.3. - Cells 17–18: one output. Under
no_grad, a latent-guided translation, of sizeargs.latent_dim,s_trg = mapping_network(z_trg, y_trg),x_fake = generator(x_real, s_trg)(the reference-guidedstyle_encoder(x_ref2, y_trg)is commented out), and a plot of the second image of the batch. - Cell 19: analysis. The reader is asked to analyse the latent space and to write a review of possible improvements; Exploring the style space and Limitations cover both.
Neither notebook has saved outputs, so they document procedures rather than results; cells 4 and 12 of the task notebook carry execution counts but no output.
Issues in the task notebook and its helper:
- Paths. The notebook sits in
homeworks/, one level below the repository root, but cells 3 and 4 go up two levels, to../../seminars/utils/and../../seminars/data/celeba. Fromhomeworks/the paths are../seminars/utils/and../seminars/data/celeba. - Imports.
sys.path.appendputs the helper folder last, so an installed Hugging Facedatasetspackage shadows the helper’sdatasetsfolder andfrom datasets.celeba import CelebADatasetfails;sys.path.insert(0, ...)avoids this. The helper also needsgdown, and cell 3 installs neither it normunch. - 40 attributes are not 40 domains. CelebA’s attributes overlap, so they cannot be the domains of StarGAN v2, which needs one domain per image (see What counts as a domain); StarGAN would instead use a binary vector of a few attributes. The helper returns, for each image, a dictionary whose
attributesentry holds all 40 values as , the encoding oflist_attr_celeba.txt; the labels must be converted to or to a domain index before training. Cell 16 discards them (x_real, _ = ...) and drawsy_orgat random, so the “source domain” is not the domain ofx_real. - Pixel range. The ImageNet normalization maps pixels to about , not . LPIPS expects inputs in ; the StarGAN generator ends in and cannot produce the normalized range; and cell 18 plots the generator output without undoing the normalization, so
imshowclips it.Normalize([0.5] * 3, [0.5] * 3)gives . - Resolution. The aligned CelebA images are , so resizing the shorter side to 256 upsamples them by a factor of about 1.44. StarGAN used crops resized to ; StarGAN v2 trains at on CelebA-HQ.
- The LPIPS line fails.
lpips(x_fake, x_real)returns one value per image, shape ;.squeeze().item()raises an error for the batch size of 32. Use.mean().item(). The loop also runs withouttorch.no_grad()oreval(). - What the metric measures. The LPIPS between a translation and its own input measures how much the translation changed the image, not realism and not diversity; returning the input unchanged scores 0 and passes the assertion. StarGAN v2’s LPIPS is the average over pairs of outputs for the same input (see How translation models are measured).
- Cell 18 depends on cell 16. It uses
batch_size,y_trgandx_realfrom the last iteration of cell 16, andargs.latent_dim, which no cell sets (16 in StarGAN v2). - Data loading.
next(iter(dataloader))builds a new iterator three times per evaluation step, which is slow with worker processes, and the choice of 0 workers on CUDA but 2 elsewhere looks inverted.
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 (pix2pixHD, SPADE), and adding plausible detail to a small image (SRGAN, ESRGAN).
Where to see the images#
These models are best judged by their samples. Each line says where to find them; figure numbers refer to the linked arXiv version.
- StarGAN (Choi et al., 2018; arXiv v3): CelebA faces given RaFD expressions by one generator in Figure 1; single and multiple attribute transfer on CelebA in Figure 4, with more in Figures 9 and 10; RaFD expressions in Figures 5 and 11; 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, with more in Figures 9 and 10; comparisons with the baselines in Figures 5 and 6; teaser video and 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. Section numbers refer to arXiv v3: the losses in Section 3.1 (Eqs. 1–6), the mask vector in Section 3.2 (Eq. 7), WGAN-GP and the networks in Section 4 (Eq. 8) and Appendix 7.2 (Tables 4–5), datasets and training in Sections 5.2–5.3, results in Tables 1–3 and Figures 6–7.
- Y. Choi, Y. Uh, J. Yoo, J.-W. Ha. StarGAN v2: Diverse image synthesis for multiple domains. CVPR, 2020. Section numbers refer to arXiv v2: the networks and losses in Section 2 (Eqs. 1–5), the ablation in Section 3.1 and Table 1, comparisons in Tables 2–3, training details in Appendix B, the evaluation protocol in Appendix C, the architectures in Appendix E.
- A. Odena, C. Olah, J. Shlens. Conditional image synthesis with auxiliary classifier GANs. ICML, 2017. The auxiliary classifier.
- I. Gulrajani, F. Ahmed, M. Arjovsky, V. Dumoulin, A. Courville. Improved training of Wasserstein GANs. NeurIPS, 2017.
- L. Mescheder, A. Geiger, S. Nowozin. Which training methods for GANs do actually converge?. ICML, 2018. The R1 penalty in Section 4.1, Eq. 9; its implementation is also the origin of the multi-task discriminator used by StarGAN v2.
- M.-Y. Liu, X. Huang, A. Mallya, T. Karras, T. Aila, J. Lehtinen, J. Kautz. Few-shot unsupervised image-to-image translation. ICCV, 2019. FUNIT; the multi-task adversarial discriminator in Section 3.2.
- X. Huang, S. Belongie. Arbitrary style transfer in real-time with adaptive instance normalization. ICCV, 2017. AdaIN, Eq. 8.
- T. Karras, S. Laine, T. Aila. A style-based generator architecture for generative adversarial networks. CVPR, 2019.
- J.-Y. Zhu, T. Park, P. Isola, A. A. Efros. Unpaired image-to-image translation using cycle-consistent adversarial networks. ICCV, 2017.
- Y. Choi. StarGAN, official PyTorch implementation, 2018.
model.py: label tiling and the remark on padding and instance normalization;solver.py: binary cross-entropy for CelebA labels, target labels by shuffling the batch, the mask vector. - Clova AI Research. StarGAN v2, official PyTorch implementation, 2020.
core/model.py: AdaIN with scale , the mapping network;core/solver.py: the losses and the R1 penalty. - HSE-LAMBDA, Deep Generative Models course repository:
StarGAN.ipynb,02_hw_StarGAN.ipynb, and the dataset helperceleba.py.