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 , 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 , , , .
Start with the easy case, where every training example is a pair : an edge map and the photo it was drawn from. A generator maps to an image, and a loss compares with .
A pixel loss alone produces blur. When several outputs are plausible for one input, the expected loss is minimized by their per-pixel mean, and the expected 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 or and decides whether the output is a real photo for this input:
Here is the sum of absolute pixel differences. The adversarial term makes the output sharp, and the 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 and with data distributions and , and a generator . Only the adversarial signal is left: a discriminator tells real from translated .
That loss depends on only through the distribution of its outputs. Every that pushes onto 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 and "zebras" . Three maps produce outputs with exactly the zebra distribution:
The first is an increasing line, the second a decreasing one. The third is a sampler that draws fresh noise and ignores altogether.
Even an optimal discriminator cannot tell the three apart: it sees only outputs, and the outputs have the same distribution. Only is the translation we want. 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 and a second discriminator that tells real from . If and translate in opposite directions, each should undo the other: , the forward cycle, and , the backward cycle. The cycle-consistency loss measures both:
What does it rule out? A zero forward-cycle loss means for every . Then is injective: if two inputs shared an output, 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 , so learns nothing about from it. Its best constant guess under the loss is the median of , which is zero, and the cycle error stays positive:
The two lines, by contrast, can be undone exactly. Each has an inverse map , and with that the cycle error is zero.
What the cycle leaves open
What does the cycle not rule out? The decreasing line reverses the order of its inputs, the smallest horse becoming the largest zebra. It is just as invertible as , 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 , where is a small stack of convolutions. A block whose 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. maps to through an image of , 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 , 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:
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:
with and .
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 images it is, in order: a convolution to 64 channels; two stride-2 convolutions down to ; nine residual blocks at that size, each with two convolutions; two transposed convolutions back to ; and a convolution to 3 channels with . Instance normalization follows the convolutions, the natural choice with one image per batch (see Batch normalization, gradients and convolutions).
The discriminator is a PatchGAN: four convolutions with 64, 128, 256 and 512 channels and strides 2, 2, 2, 1, then a convolution to one channel. On a image it outputs a map of scores. Each score sees a window of the input, its receptive field. For a stack of convolutions with kernel sizes and strides , that size is
With and this gives .
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. minimizes , and minimizes . 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 , 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 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).
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Answers are saved on the server and tied to this browser until you sign in.