Skip to content
BIASED

Beyond CycleGAN: MUNIT, CUT and SPADE

Other ways to keep a translation faithful to its input: content and style instead of a cycle (MUNIT), a contrastive loss between patches (CUT), a segmentation mask inside every normalization layer (SPADE), and super-resolution, where the best answer by pixels is a blurry average (SRGAN).

Content and style

StarGAN v2 gave one input many outputs (see StarGAN: one generator for many domains). A style code entered the generator through AdaIN, and a cycle carried the image back with its own style. Three requests are still open. Translate without a second pass through a cycle. Render a photo from a map of labels, "sky here, road there". Make a small image sharp at four times the size. This topic covers one answer to each, and starts with an older route to many outputs.

MUNIT (Huang et al., 2018) assumes that an image is made from two codes. The content code cc is shared by both domains: the spatial structure, such as a cat's pose and layout. The style code sis_i belongs to domain ii: the rendering, such as fur and colour. Here cc is content, not StarGAN's label.

Each domain ii gets a content encoder EicE^c_i, a style encoder EisE^s_i and a decoder GiG_i. To translate from domain 1 to domain 2, keep the content and swap the decoder:

x1→2=G2(E1c(x1), s2),s2∼N(0,I).x_{1 \to 2} = G_2\big(E^c_1(x_1),\, s_2\big), \qquad s_2 \sim \mathcal N(0, I).

A new s2s_2 gives a new output. Taking s2=E2s(x2)s_2 = E^s_2(x_2) from a real image of the second domain gives example-guided translation: the output copies the style of the example.

The shapes of the two codes do much of the separating. For a 256×256256 \times 256 image, the content is a 64×64×25664 \times 64 \times 256 feature map computed with instance normalization. That normalization removes each channel's mean and spread, statistics that carry much of the style. The style is a vector of just 8 numbers from global average pooling, so it has no layout at all. The decoder injects it through AdaIN, as in StyleGAN: a style-based generator, with the scales and shifts computed from ss by a small MLP.

Reconstruction instead of a cycle

MUNIT trains its encoders and decoders to be inverses in both directions. Take a real x1x_1, its content c1=E1c(x1)c_1 = E^c_1(x_1) and a random style s2∼N(0,I)s_2 \sim \mathcal N(0, I). Three losses tie everything together:

Lreconx1=E ∥G1(E1c(x1),E1s(x1))−x1∥1,Lreconc1=E ∥E2c(G2(c1,s2))−c1∥1,Lrecons2=E ∥E2s(G2(c1,s2))−s2∥1.\begin{aligned} \mathcal L^{x_1}_{\mathrm{recon}} &= \mathbb E\, \big\|G_1\big(E^c_1(x_1), E^s_1(x_1)\big) - x_1\big\|_1, \\ \mathcal L^{c_1}_{\mathrm{recon}} &= \mathbb E\, \big\|E^c_2\big(G_2(c_1, s_2)\big) - c_1\big\|_1, \\ \mathcal L^{s_2}_{\mathrm{recon}} &= \mathbb E\, \big\|E^s_2\big(G_2(c_1, s_2)\big) - s_2\big\|_1 . \end{aligned}

The same three terms run with the domains swapped. Least-squares adversarial losses make translations look like real images of the target domain. The weights are 1010 for the image terms and 11 for the content and style terms.

Image reconstruction makes each pair (Ei,Gi)(E_i, G_i) an autoencoder of its own domain. Content reconstruction ties the output to the input: the target domain's encoder must read c1c_1 back from the translation. Style reconstruction forces the decoder to use s2s_2. Without it, a decoder learns to ignore a random input, as the pix2pix generator ignored its noise.

Why not simply add CycleGAN's cycle? Huang et al. (2018, Proposition 5) show that it fights the randomness. Suppose translation is random in both directions, its outputs look like the other domain, and both cycles hold exactly: every translation of x1x_1, translated back, returns x1x_1, and every translation of x2x_2, translated back, returns x2x_2. Let aa and bb be two translations of one input x1∗x^*_1. By the forward cycle, every way back from aa lands on x1∗x^*_1. Now apply the backward cycle to aa and see what it demands of x1∗x^*_1.

The two cycles together leave each input a single translation: CycleGAN's one output per input, now as a theorem. MUNIT keeps a weaker cycle instead. Translate x1x_1 with a random s2s_2, then translate back with the original's own style E1s(x1)E^s_1(x_1), and you get x1x_1. This style-augmented cycle holds at the optimum of the three reconstruction losses. StarGAN v2's cycle with the input's style is the same idea.

One ablation is a warning about measuring "many outputs". Without image reconstruction, outputs of the same input differ about twice as much as MUNIT's, by a learned perceptual distance (LPIPS, which the course defines later). Human raters prefer them in only 6% of comparisons. Artifacts make outputs differ, and a diversity score counts them. A diversity number means something only next to a quality number.

A contrastive loss instead of a cycle

The cycle assumes that the two domains correspond one to one, and many do not: a horse can become many zebras, and a photo holds more than its label map. It also costs a second generator and a second discriminator, which exist only to check the first. CUT (Park et al., 2020) keeps a single generator, y^=G(x)\hat y = G(x), and asks for a different tie. A patch of the output should tell us which patch of the input it came from. A zebra's forehead should point back to the horse's forehead, and to nothing else in the photo.

The tool is InfoNCE (van den Oord, Li and Vinyals, 2018). It turns "is v+v^+ related to vv?" into a classification. A query vv, its positive v+v^+ and NN negatives v1−,…,vN−v^-_1, \dots, v^-_N are unit vectors, and a softmax over their dot products must pick the positive:

ℓ(v,v+,v−)=−log⁡ev⋅v+/τev⋅v+/τ+∑n=1Nev⋅vn−/τ.\ell(v, v^+, v^-) = -\log \frac{e^{v \cdot v^+ / \tau}}{e^{v \cdot v^+ / \tau} + \sum_{n=1}^{N} e^{v \cdot v^-_n / \tau}} .

This is the cross-entropy of an (N+1)(N + 1)-way classification with temperature τ\tau; CUT uses τ=0.07\tau = 0.07.

Why does a low loss mean that the output remembers the input? If the candidates carried no information about the query, the positive would be as likely as any negative. The best classifier could then give it only 1/(N+1)1/(N + 1), and the loss would sit at the chance level log⁡(N+1)\log(N + 1). Van den Oord et al. turn this into a bound on the mutual information, the KL divergence between the joint distribution of vv and v+v^+ and the product of their marginals: I(v;v+)≥log⁡(N+1)−E ℓI(v; v^+) \ge \log(N + 1) - \mathbb E\,\ell. A loss far below chance requires dependence.

The temperature decides how much small differences count. Let a query have cosine 0.50.5 with its positive and 0.450.45, 0.20.2 and −0.1-0.1 with three negatives. With τ=1\tau = 1 the positive gets probability 0.3090.309, a loss of 1.1761.176, close to chance, log⁡4=1.386\log 4 = 1.386. With τ=0.07\tau = 0.07 the gap of 0.050.05 to the nearest negative becomes 0.710.71 in the logits. The positive gets 0.6650.665, and the loss falls to 0.4080.408. A small temperature makes the loss sensitive to small differences, and the hardest negative dominates the gradient.

PatchNCE: each patch finds its source

CUT applies InfoNCE to patches at several scales. The encoder half of the generator, GencG_{\mathrm{enc}}, computes a stack of feature maps; each position of the map at layer ll describes a patch of the input, and deeper layers describe larger patches. CUT takes L=5L = 5 layers, from the pixels to the fifth residual block. A small two-layer MLP HlH_l per layer maps each feature vector to a unit vector.

The same encoder embeds the input and the output: vl=Hl(Gencl(x))v_l = H_l\big(G^l_{\mathrm{enc}}(x)\big) and v^l=Hl(Gencl(y^))\hat v_l = H_l\big(G^l_{\mathrm{enc}}(\hat y)\big). At Sl=256S_l = 256 random positions ss per layer, the output feature v^ls\hat v^s_l is the query, the input feature at the same position vlsv^s_l is the positive, and the input features at the other 255 positions, vlS∖sv^{S \setminus s}_l, are the negatives:

LPatchNCE=Ex∑l=1L∑s=1Slℓ(v^ls, vls, vlS∖s).\mathcal L_{\mathrm{PatchNCE}} = \mathbb E_{x} \sum_{l=1}^{L} \sum_{s=1}^{S_l} \ell\big(\hat v^s_l,\, v^s_l,\, v^{S \setminus s}_l\big).

The negatives come from the same image. A zebra leg in the output must be closer to the horse leg at that position than to the horse's back, the sky or the grass of the same photo. Negatives from other images worked worse: the encoder then has to tell images apart by things the translation does not care about, such as white against brown horses. An output that kept no trace of its input would score about log⁡256=5.545\log 256 = 5.545 per query. And the encoder learns what "corresponding" means across domains, shapes and parts rather than texture, so stripes themselves cost nothing.

The generator and the MLPs minimize the adversarial loss plus PatchNCE with weight 11 on the pairs (x,G(x))(x, G(x)) and weight 11 on the pairs (y,G(y))(y, G(y)) for real target images yy, a contrastive version of CycleGAN's identity loss. On horse → zebra, FID (a distance between image distributions that the course defines later; lower is better) falls from 77.2 for CycleGAN to 45.5, at 0.24 s per iteration instead of 0.40 (Park et al., 2020).

Without a cycle, CUT is free to change more of the image, and it uses that freedom to match the target's statistics. Zebras cover 36.8% of the pixels of the training zebra photos and horses 17.9% of the horse photos. CUT's zebras cover 30.8%, CycleGAN's 19.1%. That helps cat → dog, which needs a change of shape, and hurts when stripes grow onto the background.

Label maps and normalization

Semantic image synthesis turns a segmentation mask mm, a class label for every pixel, into a photo. Training data are pairs of a photo and its mask, so the problem is supervised, as for pix2pix. Two things are new. Street-scene masks come at 2048×10242048 \times 1024. And a mask is mostly large uniform regions, which the normalization layers of a standard generator erase.

pix2pixHD (Wang et al., 2018a) handles the resolution. Its generator works coarse to fine, and three identical 70×7070 \times 70 PatchGANs judge the image downsampled by 1, 2 and 4: the same window covers a wider region of a coarser image, so one discriminator judges global layout and another fine detail. The generator must also match the discriminators' internal features on the real photo and on its synthesis, a feature-matching loss that needs the pairs.

Now the erasing. pix2pixHD feeds the one-hot mask to its first layer and follows every convolution with instance normalization, which subtracts each channel's mean over positions and divides by its standard deviation. Take a mask with one label everywhere. A convolution of a constant is a constant (away from the borders, and everywhere with reflection padding): channel cc holds one value aca_c at every position, its standard deviation is 00, and normalization returns

ac−ac0+ε=0\frac{a_c - a_c}{\sqrt{0 + \varepsilon}} = 0

whatever the label was. An all-grass mask and an all-sky mask give the same activations.

Two labels fare little better. Let sky cover a fraction pp of the pixels and grass the rest, and let a 1×11 \times 1 convolution give a channel the values asky>agrassa_{\mathrm{sky}} > a_{\mathrm{grass}}. The channel's mean and standard deviation are

μ=p asky+(1−p) agrass,σ=p(1−p) (asky−agrass).\mu = p\,a_{\mathrm{sky}} + (1 - p)\,a_{\mathrm{grass}}, \qquad \sigma = \sqrt{p(1 - p)}\,\big(a_{\mathrm{sky}} - a_{\mathrm{grass}}\big).

For p=14p = \tfrac14, normalization gives 1.7321.732 on the sky and −0.577-0.577 on the grass, for the weights (2,−1)(2, -1), (0.3,0.1)(0.3, 0.1) or (5,4)(5, 4) alike. The layout survives: where the boundary runs and which region is larger. Which labels they were, and how different, is gone. Feeding the mask again at every layer does not help, since each copy is convolved and normalized in turn.

SPADE: the mask after the normalization

SPADE (spatially-adaptive denormalization; Park et al., 2019) lets the mask in after the normalization, where nothing can remove it. A layer's activations hh are normalized without learned parameters, with the per-channel mean μc\mu_c and standard deviation σc\sigma_c. Then maps computed from the mask scale and shift them, position by position:

SPADE(h,m)c,y,x=γc,y,x(m) hc,y,x−μcσc+βc,y,x(m).\mathrm{SPADE}(h, m)_{c, y, x} = \gamma_{c, y, x}(m)\, \frac{h_{c, y, x} - \mu_c}{\sigma_c} + \beta_{c, y, x}(m).

Compare the three relatives. Batch normalization learns one γc\gamma_c and one βc\beta_c per channel. AdaIN computes γc(s)\gamma_c(s) and βc(s)\beta_c(s) from a style vector, still one number per channel. SPADE's scale and shift have spatial dimensions: a different value at every pixel, set by the labels around it. A small convolutional network computes them from the mask, resized to the layer's resolution: a 3×33 \times 3 convolution with ReLU, then two 3×33 \times 3 convolutions, one for γ\gamma and one for β\beta.

A toy layer shows the arithmetic. One channel on a 4×44 \times 4 map has μ=2.25\mu = 2.25 and σ=1.392\sigma = 1.392. The mask network gives the sky γ=1.5\gamma = 1.5, β=0.8\beta = 0.8 and the grass γ=0.5\gamma = 0.5, β=−0.6\beta = -0.6. A sky pixel with h=5h = 5 is normalized to 1.9761.976 and leaves the layer as 1.5⋅1.976+0.8=3.761.5 \cdot 1.976 + 0.8 = 3.76.

A uniform mask now gives a uniform but label-specific γ\gamma and β\beta, so sky and grass differ. Since the mask enters every layer, the generator needs no encoder for it: it upsamples a random vector through residual blocks with SPADE, and that vector makes the synthesis multimodal.

The ablation isolates the idea. With the same decoder, a segmentation network run on the generated COCO-Stuff photos agrees with the input masks at a mean IoU of 35.2 with SPADE and 31.9 with the mask concatenated at every layer (Park et al., 2019). What matters is where the mask enters.

Super-resolution: the average is blurry

Super-resolution recovers a high-resolution image xHRx^{\mathrm{HR}} from a downsampled xLRx^{\mathrm{LR}}, usually four times along each side, so 16 pixels per input pixel. Pairs are free: downsample any photo. The difficulty is the loss. One small image fits many large ones, which differ exactly in the fine texture that downsampling removed.

The output with the smallest expected squared error is the conditional mean, μ=E[xHR∣xLR]\mu = \mathbb E[x^{\mathrm{HR}} \mid x^{\mathrm{LR}}], and its error is the total conditional variance tr⁡Cov⁡(xHR∣xLR)\operatorname{tr}\operatorname{Cov}(x^{\mathrm{HR}} \mid x^{\mathrm{LR}}). The standard score, PSNR, is 10log⁡10(peak2/MSE)10 \log_{10}(\text{peak}^2/\mathrm{MSE}) decibels, so it is maximized by the same mean. When plausible answers differ in texture, their mean is a smooth image that matches none of them. This is the VAE's blur again (see Latent variables and the ELBO).

Even a perfect generator cannot win on this score. Let x′x' be a sample from the true conditional distribution, independent of xHRx^{\mathrm{HR}} given xLRx^{\mathrm{LR}}. Write both as deviations from μ\mu; the cross term vanishes, because the deviations are independent with mean zero:

E ∥xHR−x′∥2=E ∥xHR−μ∥2+E ∥x′−μ∥2=2tr⁡Cov⁡(xHR∣xLR).\mathbb E\,\|x^{\mathrm{HR}} - x'\|^2 = \mathbb E\,\|x^{\mathrm{HR}} - \mu\|^2 + \mathbb E\,\|x' - \mu\|^2 = 2 \operatorname{tr}\operatorname{Cov}(x^{\mathrm{HR}} \mid x^{\mathrm{LR}}) .

An ideal sampler has twice the MSE of the mean, on any data. In a toy, four stripe patterns shifted by 0 to 3 pixels all shrink to the same flat grey input: their mean, a flat grey, has MSE 0.250.25, and a sharp stripe sample has 0.500.50.

SRGAN (Ledig et al., 2017) replaces the squared error by a perceptual loss: a squared error between feature maps Φ\Phi of a pretrained VGG19 network, plus 10−310^{-3} times the non-saturating adversarial loss. Deep features tolerate texture that differs in detail, and the discriminator pushes the output towards natural images, where an average of textures does not lie. On the BSD100 test set at 4×4\times, raters scored images from 1 to 5:

bicubic MSE-trained network SRGAN original
PSNR, dB 25.94 27.58 25.16 ∞
mean opinion score 1.47 2.29 3.56 4.46

ESRGAN (Wang et al., 2018b) refines each part: a deeper generator without batch normalization, VGG features taken before the ReLU, a relativistic discriminator that asks whether a real image looks more real than the fakes on average, and blending of the weights of a PSNR-trained and a GAN-trained network to trade one for the other. The added texture is plausible but invented. That is fine for a holiday photo and wrong wherever details are evidence, as in medical images.

What keeps the output faithful

The translation models of this extension differ mostly in what keeps the output faithful to its input:

Model Input → output Tie to the input Data Outputs
pix2pix image → image L1L_1 to the target paired one
CycleGAN image → image cycle through a second generator unpaired one
StarGAN v2 image + style → image cycle with the input's style unpaired, labelled many
MUNIT image + style → image content and style reconstruction unpaired many
CUT image → image PatchNCE between patches unpaired one
SPADE label map → photo mask in every normalization layer paired many
SRGAN small → large image VGG features of the target paired one

Every unpaired model here needed an extra loss to make its input recoverable from its output, because its generators are not invertible. Cycles, reconstructions and contrastive patches are all ways of buying back that lost information. And no GAN, of any architecture, can say how likely an image is.

A generator that is invertible by construction would settle both. Its inverse is exact, so a cycle through it costs nothing and holds by design, and the change of variables turns it into an exact density. That is where the main line resumes: The change of variables, normalizing flows.

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.