Skip to content
BIASED

StarGAN: one generator for many domains

One generator translates images between many domains: the target label as extra channels, a discriminator with a classifier head, a cycle run by the same generator, a mask vector for several datasets, and StarGAN v2 with style codes instead of labels.

From two domains to many

CycleGAN: unpaired image-to-image translation learned to move images between two domains without paired examples. It used two generators, one per direction, two discriminators, and a cycle-consistency loss that sends an image there and back and asks it to return unchanged. That covers one pair of domains.

A domain here is a set of images that share a label: black hair, blond hair, smiling faces, angry faces. A face dataset such as CelebA carries many such labels, and we want to move a face between any two of them. Pairwise CycleGAN needs a separate generator for every ordered pair of domains: black → blond is one network, blond → black another.

Each of those generators sees only the images of its own two domains. Yet they all have to learn the same things about faces: where the eyes are, how skin looks under light, what must not change when the hair does.

StarGAN (Choi et al., 2018) trains one generator for all directions. It takes an image of any domain and the label cc of the domain we want:

x  ↦  G(x,c).x \;\mapsto\; G(x, c).

The output should look like a real image of domain cc and keep everything about xx that the label does not mention. The name comes from the picture: the domains sit at the tips of a star, with one generator in the middle.

How the generator reads the label

The label is a short vector. On CelebA StarGAN uses five attributes: black, blond and brown hair (exactly one of the three is on), male, and young. So c∈{0,1}5c \in \{0, 1\}^5.

A convolutional network takes images, not vectors, so the label is turned into one. Each entry cic_i becomes a constant map of size H×WH \times W filled with cic_i, and these maps are stacked onto the three colour channels. For nn label entries the generator's input has 3+n3 + n channels.

The rest is the CycleGAN generator, in its 128×128128 \times 128 version: two stride-2 convolutions down, six residual blocks instead of nine, two transposed convolutions up, and instance normalization after the convolutions. Instance normalization subtracts each channel's mean over the positions of one image and divides by its standard deviation (see Batch normalization, gradients and convolutions).

One detail decides whether the label gets through at all. The first layer is a 7×77 \times 7 convolution followed by instance normalization, and its input needs 3 pixels of padding on each side. CycleGAN pads by reflecting the image at its border. The official StarGAN code pads with zeros, and that is not a matter of taste.

A discriminator with two heads

A translation has two jobs: look real, and belong to domain cc. StarGAN's discriminator checks both with one shared trunk and two outputs. Dsrc(x)D_{\text{src}}(x) scores how real xx looks. Dcls(c∣x)D_{\text{cls}}(c \mid x) is the probability the network assigns to label cc for image xx: an auxiliary classifier, as in AC-GAN (Odena et al., 2017).

For binary attributes the classifier has one sigmoid output did_i per attribute, and the loss for a label cc is a sum of binary cross-entropies:

−log⁡Dcls(c∣x)=−∑i=1n[cilog⁡di+(1−ci)log⁡(1−di)].-\log D_{\text{cls}}(c \mid x) = -\sum_{i=1}^{n} \Big[c_i \log d_i + (1 - c_i)\log(1 - d_i)\Big].

For a categorical label, such as one of eight facial expressions, it is the usual softmax cross-entropy.

The two players use this loss on different images. The discriminator learns to classify real images, with their true labels c′c':

Lclsr=Ex,c′[−log⁡Dcls(c′∣x)].\mathcal L^{r}_{\text{cls}} = \mathbb E_{x, c'}\big[-\log D_{\text{cls}}(c' \mid x)\big].

The generator is rewarded when its translation is classified as the target label:

Lclsf=Ex,c[−log⁡Dcls(c∣G(x,c))].\mathcal L^{f}_{\text{cls}} = \mathbb E_{x, c}\big[-\log D_{\text{cls}}\big(c \mid G(x, c)\big)\big].

The classifier never trains on fakes.

Adversarial loss and the way back

Realism comes from the WGAN-GP loss of Wasserstein GANs and Lipschitz constraints, with DsrcD_{\text{src}} as the critic:

Ladv=Ex[Dsrc(x)]−Ex,c[Dsrc(G(x,c))]−λgp Ex^[(∥∇x^Dsrc(x^)∥2−1)2],\mathcal L_{\text{adv}} = \mathbb E_{x}\big[D_{\text{src}}(x)\big] - \mathbb E_{x, c}\big[D_{\text{src}}(G(x, c))\big] - \lambda_{\text{gp}}\,\mathbb E_{\hat x}\Big[\big(\lVert\nabla_{\hat x} D_{\text{src}}(\hat x)\rVert_2 - 1\big)^2\Big],

with λgp=10\lambda_{\text{gp}} = 10 and x^\hat x on segments between real and translated images.

So far nothing ties the output to the input: any convincing blond face would satisfy both losses. The fix is CycleGAN's cycle, now run by the same generator in both directions. Translate xx to cc, translate back to its original label c′c', and compare:

Lrec=Ex,c,c′[∥x−G(G(x,c), c′)∥1].\mathcal L_{\text{rec}} = \mathbb E_{x, c, c'}\Big[\big\lVert x - G\big(G(x, c),\, c'\big)\big\rVert_1\Big].

The full objectives are

LD=−Ladv+λcls Lclsr,LG=Ladv+λcls Lclsf+λrec Lrec,\mathcal L_D = -\mathcal L_{\text{adv}} + \lambda_{\text{cls}}\,\mathcal L^{r}_{\text{cls}}, \qquad \mathcal L_G = \mathcal L_{\text{adv}} + \lambda_{\text{cls}}\,\mathcal L^{f}_{\text{cls}} + \lambda_{\text{rec}}\,\mathcal L_{\text{rec}},

with λcls=1\lambda_{\text{cls}} = 1 and λrec=10\lambda_{\text{rec}} = 10. In LG\mathcal L_G only the fake term of Ladv\mathcal L_{\text{adv}} depends on the generator.

Training follows WGAN-GP habits: Adam with learning rate 10−410^{-4}, β1=0.5\beta_1 = 0.5, β2=0.999\beta_2 = 0.999, five discriminator steps per generator step, batches of 16. Target labels are drawn by shuffling the labels of the batch, so targets appear as often as they do in the data.

Several datasets: the mask vector

Labels come from datasets, and datasets label different things. CelebA has about 200,000 celebrity faces with 40 binary attributes but no expressions. RaFD has 4,824 studio photos of 67 people with eight expressions but none of CelebA's attributes. RaFD alone is small; CelebA could teach the generator a lot about faces in general.

StarGAN trains on both at once with one label vector. It concatenates the labels of all datasets and appends a mask vector mm, a one-hot vector that says which dataset the current image comes from:

c~=[ cCelebA,  cRaFD,  m ].\tilde c = \big[\,c_{\text{CelebA}},\; c_{\text{RaFD}},\; m\,\big].

The part of the label that belongs to the other dataset is unknown, so it is set to zero. The discriminator's classification loss covers only the known part, and training alternates between batches from the two datasets. The generator learns to read the mask and ignore the zeros, and at test time it can be asked for a RaFD expression on a CelebA face.

How well does a translation hit its target? One measure is to train a classifier on real images and run it on translations. On RaFD expressions alone, its error on StarGAN translations was 2.12%, against 5.99% for pairwise CycleGANs and 0.45% on real test photos.

StarGAN v2: a style code instead of a label

StarGAN gives one output per image and domain: G(x,c)G(x, c) is deterministic. Every face becomes blond in the same way, although a domain holds many styles: shades of blond, hairstyles, breeds of dog.

StarGAN v2 (Choi et al., 2020) replaces the label by a style code s∈R64s \in \mathbb R^{64} that belongs to a domain. Write yy for the domain of the input and y~\tilde y for the target. The code comes from one of two new networks:

  • the mapping network FF turns noise z∈R16z \in \mathbb R^{16}, z∼N(0,I)z \sim \mathcal N(0, I), into a style of the target domain, s~=Fy~(z)\tilde s = F_{\tilde y}(z); its layers are shared, with one output branch per domain;
  • the style encoder EE reads the style off a reference image of domain yy, s=Ey(xref)s = E_y(x_{\text{ref}}), with one output head per domain.

The generator G(x,s)G(x, s) takes no label at all: the domain is carried by ss. The style enters through adaptive instance normalization (AdaIN; see StyleGAN: a style-based generator). It normalizes each channel of a feature map hh and then rescales and shifts it with affine functions of the style; StarGAN v2 writes the scale as 1+γ(s)1 + \gamma(s), so a zero output leaves the normalized features as they are:

AdaIN⁡(h,s)=(1+γ(s))⊙h−μ(h)σ(h)+β(s).\operatorname{AdaIN}(h, s) = \big(1 + \gamma(s)\big) \odot \frac{h - \mu(h)}{\sigma(h)} + \beta(s).

The downsampling blocks use plain instance normalization; the last two bottleneck blocks and all upsampling blocks use AdaIN. The discriminator has one real/fake output per domain, Dy(x)D_y(x): "is this a real image of domain yy?". The classifier head is gone.

So there are two ways to translate. Latent-guided: draw zz and use FF, getting a new style each time. Reference-guided: point at an image and copy its style with EE. The paper shows both on CelebA-HQ (female, male) and AFHQ (cat, dog, wildlife) at 256×256256 \times 256.

Training StarGAN v2

StarGAN v2 trains GG, FF and EE against DD with four terms. The adversarial term is the value function of GANs as a two-player game, played by the head of the relevant domain, with s~=Fy~(z)\tilde s = F_{\tilde y}(z); as usual, the generator side is trained with the non-saturating version:

Ladv=Ex,y[log⁡Dy(x)]+Ex,y~,z[log⁡(1−Dy~(G(x,s~)))].\mathcal L_{\text{adv}} = \mathbb E_{x, y}\big[\log D_y(x)\big] + \mathbb E_{x, \tilde y, z}\Big[\log\big(1 - D_{\tilde y}(G(x, \tilde s))\big)\Big].

Style reconstruction makes the generator use the code: the style encoder must recover it from the output.

Lsty=E[∥s~−Ey~(G(x,s~))∥1].\mathcal L_{\text{sty}} = \mathbb E\Big[\big\lVert \tilde s - E_{\tilde y}\big(G(x, \tilde s)\big)\big\rVert_1\Big].

Style diversification pushes two styles s~1,s~2\tilde s_1, \tilde s_2 from two noise draws to give different images. It is maximized:

Lds=E[∥G(x,s~1)−G(x,s~2)∥1].\mathcal L_{\text{ds}} = \mathbb E\Big[\big\lVert G(x, \tilde s_1) - G(x, \tilde s_2)\big\rVert_1\Big].

The cycle keeps the content, now with the input's own style s^=Ey(x)\hat s = E_y(x) for the way back:

Lcyc=E[∥x−G(G(x,s~), s^)∥1].\mathcal L_{\text{cyc}} = \mathbb E\Big[\big\lVert x - G\big(G(x, \tilde s),\, \hat s\big)\big\rVert_1\Big].

Together:

min⁡G,F,E  max⁡D    Ladv+λsty Lsty−λds Lds+λcyc Lcyc,\min_{G, F, E}\;\max_{D}\;\; \mathcal L_{\text{adv}} + \lambda_{\text{sty}}\,\mathcal L_{\text{sty}} - \lambda_{\text{ds}}\,\mathcal L_{\text{ds}} + \lambda_{\text{cyc}}\,\mathcal L_{\text{cyc}},

with λsty=λcyc=1\lambda_{\text{sty}} = \lambda_{\text{cyc}} = 1 and λds=1\lambda_{\text{ds}} = 1 on CelebA-HQ, 22 on AFHQ. The weight λds\lambda_{\text{ds}} is decreased linearly to zero during training.

The gradient penalty changes too. Instead of WGAN-GP, the discriminator pays the R1 penalty (Mescheder et al., 2018) on real images only:

R1=λR12 Ex∼P[∥∇xDy(x)∥22],λR1=1.R_1 = \frac{\lambda_{R1}}{2}\,\mathbb E_{x \sim P}\Big[\big\lVert \nabla_x D_y(x) \big\rVert_2^2\Big], \qquad \lambda_{R1} = 1.

WGAN-GP pulls the gradient norm towards 1 at interpolates. R1 pulls it towards 0 on the data. At the equilibrium the generator matches the data and the ideal discriminator is flat there; Mescheder et al. showed on a one-point toy GAN that without such a penalty the alternating updates circle around that equilibrium, and with it they converge.

What is left open

Take stock. Pairwise CycleGAN needs a generator per ordered pair of domains. StarGAN needs one, conditioned on a label tiled over the image, and gives one output per image and domain. StarGAN v2 keeps one generator, adds a mapping network, a style encoder and per-domain heads, and gives many outputs per image and domain.

Some limits remain. In StarGAN v2 every image belongs to exactly one domain: male or female, cat or dog. Overlapping attributes such as CelebA's "blond" and "young" cannot serve as v2 domains as they are. Both versions still lean on the cycle, which needs a backward translation for every forward one and favours maps that can be undone, so they keep the input's layout. And comparing translators fairly needs scores for realism and diversity; the Evaluation module of the course builds them.

The next extension, Beyond CycleGAN: MUNIT, CUT and SPADE, starts with an older route to many outputs, an explicit split of an image into content and style, and then answers three open requests: translation without a cycle, a photo rendered from a map of labels, and a small image made sharp at four times the size. The main line continues with The change of variables: normalizing flows, where the generator finally gets an exact likelihood.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.