Skip to content
BIASED

CycleGAN: unpaired image-to-image translation

Translating images without paired examples — why an adversarial loss alone does not tie an output to its input, how the cycle and identity losses do, and where the cycle can be cheated.

Translation with pairs: pix2pix

So far a GAN has turned noise into images. The conditional GAN from GANs as a two-player game also took a label cc, and the previous extension, Autoencoding GANs: AAE and α-GAN, mapped images to codes and back. Now the input is an image and so is the output.

You have a thousand photos of horses and a thousand photos of zebras. You want a network that repaints a given horse as a zebra: same pose, same field, stripes added. This task is image-to-image translation. With four networks in play, we drop the parameter subscripts and write GG, FF, DXD_X, DYD_Y.

Start with the easy case, where every training example is a pair (x,y)(x, y): an edge map xx and the photo yy it was drawn from. A generator GG maps xx to an image, and a loss compares G(x)G(x) with yy.

A pixel loss alone produces blur. When several outputs are plausible for one input, the expected L2L_2 loss is minimized by their per-pixel mean, and the expected L1L_1 loss by their per-pixel median. Neither looks like any of the plausible outputs.

pix2pix (Isola et al., 2017) adds a conditional adversarial loss. Its discriminator sees the pair (x,y)(x, y) or (x,G(x))(x, G(x)) and decides whether the output is a real photo for this input:

G∗=arg⁡min⁡Gmax⁡D  LcGAN(G,D)+λ Ex,y ∥y−G(x)∥1,λ=100.G^* = \arg\min_G \max_D\; \mathcal L_{\text{cGAN}}(G, D) + \lambda\, \mathbb E_{x, y}\, \|y - G(x)\|_1 , \qquad \lambda = 100 .

Here ∥a−b∥1\|a - b\|_1 is the sum of absolute pixel differences. The adversarial term makes the output sharp, and the L1L_1 term keeps it near the true target. The discriminator is a PatchGAN: it outputs a grid of verdicts, one per overlapping image patch, and the loss averages them.

Without pairs the adversarial loss is not enough

Now drop the pairs. We have two domains XX and YY with data distributions PXP_X and PYP_Y, and a generator G:X→YG: X \to Y. Only the adversarial signal is left: a discriminator DYD_Y tells real y∼PYy \sim P_Y from translated G(x)G(x).

That loss depends on GG only through the distribution of its outputs. Every GG that pushes PXP_X onto PYP_Y is optimal, whatever it does to an individual image. A network with enough capacity can send the horse photos to any permutation of the zebra photos. In practice the adversarial loss alone also tends to collapse, mapping all inputs to the same few outputs (Zhu et al., 2017).

A one-dimensional model makes this concrete. Let "horses" be x∼N(0,1)x \sim \mathcal N(0, 1) and "zebras" y∼N(3,0.52)y \sim \mathcal N(3, 0.5^2). Three maps produce outputs with exactly the zebra distribution:

Ga(x)=3+0.5x,Gb(x)=3−0.5x,Gc(x,ε)=3+0.5 ε.G_a(x) = 3 + 0.5x, \qquad G_b(x) = 3 - 0.5x, \qquad G_c(x, \varepsilon) = 3 + 0.5\,\varepsilon .

The first is an increasing line, the second a decreasing one. The third is a sampler that draws fresh noise ε∼N(0,1)\varepsilon \sim \mathcal N(0, 1) and ignores xx altogether.

Even an optimal discriminator cannot tell the three apart: it sees only outputs, and the outputs have the same distribution. Only GaG_a is the translation we want. GcG_c has thrown away everything about its input. A deterministic network cannot ignore its input this way, but it can forget it too, by sending many inputs to one output.

Cycle consistency

CycleGAN (Zhu et al., 2017) adds one idea: translating an image to the other domain and back should return the original.

So add a second generator F:Y→XF: Y \to X and a second discriminator DXD_X that tells real xx from F(y)F(y). If GG and FF translate in opposite directions, each should undo the other: F(G(x))≈xF(G(x)) \approx x, the forward cycle, and G(F(y))≈yG(F(y)) \approx y, the backward cycle. The cycle-consistency loss measures both:

Lcyc(G,F)=Ex∼PX ∥F(G(x))−x∥1+Ey∼PY ∥G(F(y))−y∥1.\mathcal L_{\text{cyc}}(G, F) = \mathbb E_{x \sim P_X}\, \|F(G(x)) - x\|_1 + \mathbb E_{y \sim P_Y}\, \|G(F(y)) - y\|_1 .

What does it rule out? A zero forward-cycle loss means F(G(x))=xF(G(x)) = x for every xx. Then GG is injective: if two inputs shared an output, FF could return only one of them. A translation that forgets its input cannot be undone.

In the toy, the sampler's output is independent of xx, so FF learns nothing about xx from it. Its best constant guess under the L1L_1 loss is the median of xx, which is zero, and the cycle error stays positive:

E ∣x−0∣=2/π≈0.80.\mathbb E\,|x - 0| = \sqrt{2/\pi} \approx 0.80 .

The two lines, by contrast, can be undone exactly. Each has an inverse map FF, and with that FF the cycle error is zero.

What the cycle leaves open

What does the cycle not rule out? The decreasing line GbG_b reverses the order of its inputs, the smallest horse becoming the largest zebra. It is just as invertible as GaG_a, so it is just as cycle-consistent. The cycle demands that the translation keep enough information to be undone. It does not say which invertible correspondence is the right one.

For images, the architecture makes that choice. CycleGAN's generator is built from residual blocks. Each computes h+r(h)h + r(h), where rr is a small stack of convolutions. A block whose rr is near zero passes its input through unchanged, so the network represents the identity easily and prefers small, local changes. The solution it reaches keeps the layout and changes texture and colour: stripes on the same horse in the same pose.

When two domains admit several such correspondences, the network can pick a wrong one. On photo → label maps, CycleGAN sometimes swaps the labels of trees and buildings (Zhu et al., 2017).

A second view links this to Autoencoders. F∘GF \circ G maps XX to XX through an image of YY, so CycleGAN trains two autoencoders whose codes are images of the other domain. The cycle loss is their reconstruction loss. The adversarial loss makes the codes look like real images of YY, much as the code discriminator of an adversarial autoencoder matches the codes to a prior.

The identity loss and the full objective

Painting → photo translation exposed one more freedom. The generators changed the tint of an image when nothing asked for it: a daytime Monet often came out as a sunset photo, and both the adversarial and the cycle loss were satisfied.

The identity loss (Taigman, Polyak and Wolf, 2017) asks each generator to leave an image of its own target domain unchanged:

Lid(G,F)=Ey∼PY ∥G(y)−y∥1+Ex∼PX ∥F(x)−x∥1.\mathcal L_{\text{id}}(G, F) = \mathbb E_{y \sim P_Y}\, \|G(y) - y\|_1 + \mathbb E_{x \sim P_X}\, \|F(x) - x\|_1 .

A zebra fed to the horse → zebra generator is already a zebra, so the output should be the input. This removes global colour shifts that neither domain asks for. The paper used the term for paintings and photo enhancement; common implementations use it everywhere.

All the losses combine into one objective, minimized over the generators and maximized over the discriminators:

L(G,F,DX,DY)=LGAN(G,DY)+LGAN(F,DX)+λcyc Lcyc(G,F)+λid Lid(G,F),\mathcal L(G, F, D_X, D_Y) = \mathcal L_{\text{GAN}}(G, D_Y) + \mathcal L_{\text{GAN}}(F, D_X) + \lambda_{\text{cyc}}\, \mathcal L_{\text{cyc}}(G, F) + \lambda_{\text{id}}\, \mathcal L_{\text{id}}(G, F),

with λcyc=10\lambda_{\text{cyc}} = 10 and λid=0.5 λcyc=5\lambda_{\text{id}} = 0.5\,\lambda_{\text{cyc}} = 5.

Both cycles are needed. With only the forward cycle or only the backward one, training was often unstable and collapsed in the direction whose cycle had been removed (Zhu et al., 2017).

A residual generator and a patch discriminator

The generator follows the style-transfer network of Johnson, Alahi and Fei-Fei (2016). For 256×256256 \times 256 images it is, in order: a 7×77 \times 7 convolution to 64 channels; two stride-2 convolutions down to 64×64×25664 \times 64 \times 256; nine residual blocks at that size, each with two 3×33 \times 3 convolutions; two transposed convolutions back to 256×256×64256 \times 256 \times 64; and a 7×77 \times 7 convolution to 3 channels with tanh⁡\tanh. Instance normalization follows the convolutions, the natural choice with one image per batch (see Batch normalization, gradients and convolutions).

The discriminator is a 70×7070 \times 70 PatchGAN: four 4×44 \times 4 convolutions with 64, 128, 256 and 512 channels and strides 2, 2, 2, 1, then a 4×44 \times 4 convolution to one channel. On a 256×256256 \times 256 image it outputs a 30×3030 \times 30 map of scores. Each score sees a 70×7070 \times 70 window of the input, its receptive field. For a stack of convolutions with kernel sizes kℓk_\ell and strides sℓs_\ell, that size is

r=1+∑ℓ(kℓ−1)∏m<ℓsm.r = 1 + \sum_\ell (k_\ell - 1) \prod_{m < \ell} s_m .

With kℓ=4k_\ell = 4 and s=(2,2,2,1,1)s = (2, 2, 2, 1, 1) this gives r=1+3 (1+2+4+8+8)=70r = 1 + 3\,(1 + 2 + 4 + 8 + 8) = 70.

A patch discriminator judges local texture, which is what most translations change, and it runs on images of any size. Global structure is left mostly to the generator and the cycle loss.

Training and results

The adversarial terms use the least-squares loss of Mao et al. (2017) instead of cross-entropy. The discriminator outputs a raw score, with no sigmoid. DYD_Y minimizes E (DY(y)−1)2+E DY(G(x))2\mathbb E\,(D_Y(y) - 1)^2 + \mathbb E\,D_Y(G(x))^2, and GG minimizes E (DY(G(x))−1)2\mathbb E\,(D_Y(G(x)) - 1)^2. A sigmoid cross-entropy goes flat once the discriminator is sure a fake is real, even if that fake is still far from the data. The squared loss pulls every score towards exactly 11, and Mao et al. report steadier training.

Each discriminator trains on fakes drawn from a buffer of the 50 most recent generated images (Shrivastava et al., 2017), the replay buffer from GANs as a two-player game. A discriminator that sees only the current generator can chase it in circles; one that remembers earlier generators damps the oscillation. The optimizer is Adam with learning rate 2⋅10−42 \cdot 10^{-4} and batch size 1: 100 epochs at a constant rate, then 100 more with the rate decaying linearly to zero.

Two evaluations from the paper show what the cycle buys. In a perceptual study on maps ↔ aerial photos, raters took CycleGAN's map → photo outputs for real in 26.8% of trials; earlier unpaired methods fooled them at most 2.1% of the time.

Faithfulness was measured on Cityscapes labels → photo. A pretrained segmentation network runs on the generated photos, and its output is compared with the input labels. CycleGAN reaches a class IoU of 0.11, against 0.18 for pix2pix trained on pairs. The adversarial loss alone gives 0.08, and the cycle loss alone 0.02 (Zhu et al., 2017, Tables 2 and 4).

Where the cycle is cheated

The cycle asks that xx be recoverable from G(x)G(x). It does not ask that the information be visible. When one domain carries more detail than the other, the generator must store the surplus somewhere, and it hides it.

Photo → map translation shows this. A map draws a roof as a flat grey area; the aerial photo shows vents and shadows on it. Many photos share one map, so a faithful map cannot be undone. The forward cycle demands it anyway, and Chu, Zhmoginov and Sandler (2017) found how GG complies: the generated maps carry a faint, high-frequency signal, invisible until the contrast is stretched, and FF rebuilds the roof details from it.

A toy shows why such a solution is both invisible and fragile. Pixels lie in [0,1][0, 1], the roof is grey, 0.50.5, on the true map, and the photo carries one detail d∈[−1,1]d \in [-1, 1]. The generator writes the detail at a small amplitude aa, and FF reads it back:

G(x)=0.5+a d,d^=(G(x)−0.5)/a=d.G(x) = 0.5 + a\, d, \qquad \hat d = \big(G(x) - 0.5\big) / a = d .

With a=1/256a = 1/256, one 8-bit grey level, the cycle loss is zero and the map differs from a real one by at most one grey level. Now add noise ε\varepsilon of standard deviation σ\sigma to the map, as JPEG compression would. The decoded detail becomes d+ε/ad + \varepsilon / a, with an error of standard deviation σ/a\sigma / a. Chu et al. saw the reconstruction change almost completely at σ≈3/256\sigma \approx 3/256: in the toy, an error of standard deviation 3 on a detail whose whole range is 2.

Other failures come from the same objective. Changes of shape mostly fail: on dog → cat the learned map barely changes its input. A model trained on horses without riders paints stripes on the rider too. And both generators are deterministic, so a horse has exactly one zebra version.

The last limit is bookkeeping. Each CycleGAN links two domains, so kk domains need k(k−1)/2k(k-1)/2 of them. Ten hair colours would take 45 CycleGANs and 90 generators, each trained on the images of only two colours. The next extension, StarGAN: one generator for many domains, replaces them with one generator that receives the target domain as an input.

The main line goes elsewhere. CycleGAN learns two maps that only approximately invert each other, and like every GAN it still cannot score an image. A generator that is exactly invertible by construction gets an exact likelihood in return. That is the next main-line topic, 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.