Pix2Pix
Image-to-image translation maps an input image to an output image of the same size. Examples are labels to street scenes, labels to facades, edges to photos, grayscale to color, aerial photos to maps, and day to night. Pix2pix (Isola et al., 2017), short for “pixels to pixels”, learns such a mapping from paired examples . It uses a conditional GAN, in which both networks see the input .
The generator is a 6.4. U-Net, an encoder-decoder whose skip connections carry fine detail from the input to the output. The discriminator judges a pair of images. A real pair is from the training set, and a fake pair is . In the lecture’s convention outputs the probability that its input is fake, and the conditional GAN objective is
pushes toward and toward , while pushes toward . Pix2pix adds an L1 term to the ground truth:
where sums absolute pixel differences and sets the weight ( in the paper). The adversarial part is the GAN game with applied to pairs instead of single images.From ‘s perspective, is a learned loss function. It replaces a hand-designed loss such as the classification loss built for colorization, and the same recipe works across tasks. Pix2pix needs pixel-aligned training pairs. When only two unpaired collections exist, 8.5. CycleGAN replaces the L1 term with a cycle-consistency loss.
Which Output of Means "Fake"
The lecture’s outputs the probability that its input is fake, so the objective contains and . The pix2pix paper and most GAN texts let output the probability of real and write . Replacing by turns one form into the other, so the game and its solution are the same. Check the convention before reading a number such as .
Intuition
Many vision tasks take an image and return an image. Each one used to need its own loss, and colorization shows how hard a good loss is to design by hand. Pix2pix trains the loss instead. A discriminator learns to tell real outputs from generated ones, and the generator learns to fool it. Any visible flaw, such as dull colors, blur or missing texture, is something the discriminator can learn to detect.
The discriminator must see the input. An unconditional discriminator checks only realism. In the lecture’s example, a generator turns a grayscale photo of bottles into a photo of a cat. An unconditional answers “real!”, because the cat is a convincing photo. Shown the pair (bottles, cat), sees at once that the output does not belong to the input. With pairs, judges whether the output is a realistic translation of this particular input. The figure shows the two pairs that compares and the L1 term.
\usepackage{tikz}
\begin{document}
\definecolor{fake}{HTML}{D9483B}
\definecolor{real}{HTML}{2F80ED}
\definecolor{mid}{HTML}{8A8A8A}
\begin{tikzpicture}[>=latex, img/.style={draw, minimum width=1.1cm, minimum height=1.1cm}, net/.style={draw, fill=mid!12, minimum width=1.0cm, minimum height=1.4cm}]
\node[img] (x) at (0,0) {$x$};
\node[net] (G) at (1.8,0) {$G$};
\node[img, fake] (gx) at (3.6,0) {$G(x)$};
\draw[->, thick] (x) -- (G);
\draw[->, thick] (G) -- (gx);
\node[mid] at (1.8,1.05) {\small U-Net};
\node[img] (x1) at (6.0,1.4) {$x$};
\node[img, fake] (g1) at (7.15,1.4) {$G(x)$};
\node[img] (x2) at (6.0,-1.4) {$x$};
\node[img, real] (y2) at (7.15,-1.4) {$y$};
\node[net] (D1) at (9.0,1.4) {$D$};
\node[net] (D2) at (9.0,-1.4) {$D$};
\draw[->, thick, fake] (gx.north) |- (7.15,2.4) -- (g1.north);
\draw[->, thick] (7.75,1.4) -- (D1);
\draw[->, thick] (7.75,-1.4) -- (D2);
\node[fake, anchor=west] at (9.6,1.4) {fake pair: $D \to 1$};
\node[real, anchor=west] at (9.6,-1.4) {real pair: $D \to 0$};
\draw[dashed, thick, mid] (gx.south) |- (7.15,-2.5) -- (y2.south);
\node at (5.4,-2.8) {\small L1 term: $\lambda\,\|y - G(x)\|_1$};
\node[mid] at (6.575,0) {\small pairs (input, output)};
\end{tikzpicture}
\end{document}Properties
(L1 Plus GAN) The L1 term ties the output to the ground truth at coarse scales, and the GAN term supplies sharp detail.
A per-pixel loss such as L1 compares each pixel on its own. When the exact position or color of a detail is uncertain, it rewards a smeared compromise, as L2 does in colorization. The paper reports blurry results with L1 alone. The GAN term alone gives sharp outputs, but nothing ties them to the ground truth, and the paper reports more visual artifacts. The sum takes the low frequencies from L1 and realistic high-frequency detail from the discriminator.
(PatchGAN Discriminator) The paper’s classifies each patch as real or fake and averages the answers over the image.
The L1 term already enforces the low frequencies, so only needs to judge local structure. A patch discriminator treats the image as a texture of patches and assumes that pixels farther apart than a patch are independent. This is the same assumption as the texture models that ignore long-range position. The patch discriminator has fewer parameters than a whole-image one, runs faster, and applies to images of any size.
(A Loss That Adapts) The discriminator is retrained alongside the generator, so the loss it defines changes during training.
Training alternates a gradient step on with a gradient step on . When removes one telltale flaw, can learn to spot the next one. A hand-designed loss stays fixed and cannot do this. This is the sense in which the lecture calls a learned loss function.
(One Answer per Input) Pix2pix gives nearly the same output each time it sees a given input.
A conditional GAN could produce many outputs for one input by also taking random noise. The paper found that its generator ignored an input noise vector. It supplies randomness only through dropout, which stays on at test time, and the outputs still vary little. Pix2pix therefore returns one plausible translation. It does not sample the full range of plausible translations.
(Where Pairs Come From) Pix2pix applies when aligned pairs exist or can be computed from ordinary photos.
Some pairs are free. Desaturating color photos gives grayscale-to-color pairs (ImageNet data, Russakovsky et al. 2015). Running an edge detector on photos gives edges-to-photo pairs (edges from Xie & Tu 2015). Other pairs come from aligned datasets: map tiles and aerial photos from maps.google.com, labeled facades (Tylecek 2013), and fixed webcams for day to night (Laffont et al. 2014). A model trained on edges also accepts human sketches (sketch data from Eitz, Hays, Alexa 2012), which the edges2cats demo (Christopher Hesse) made popular. Photo to painting or horse to zebra has no aligned pairs at all.
Examples
(Easy) Reading the Discriminator's Outputs
Use the lecture’s convention, where outputs the probability of “fake”. A grayscale photo of bottles on a shelf has the true color photo . Early in training, and .
Evaluate the bracket inside . How does it change when improves until ? What value does it take when outputs for every pair? What happens to a generator that outputs a realistic cat? The bracket is . This is close to its maximum of , so is winning. When drops to , the bracket becomes . The generator's own term fell from to , which is what asks for. When cannot tell the pairs apart and outputs , the bracket is .
A cat output forms the pair . A conditional has only ever seen bottles paired with colored bottles, so it assigns this pair a probability of fake near . The generator’s term stays near , its worst value, and the cat earns no credit.
(Medium) When Pix2Pix Applies
Four projects are proposed: (a) colorizing old family photos, (b) turning sketches of handbags into product photos, (c) turning landscape photos into Monet paintings, and (d) turning daytime frames of a street webcam into night frames.
For each project, decide whether pix2pix applies and where the training pairs come from. 8.3. Image Colorization, and the GAN term counters that.
(a) Pix2pix applies. Desaturating any color photo gives an aligned pair, so the data is free. An L1 term alone would give dull colors for the reasons in
(b) Pix2pix applies. Running an edge detector on handbag product photos gives aligned edge maps. The trained model then accepts a human sketch, as in the lecture’s edges-to-photos results.
(c) Pix2pix does not apply. Monet never painted the exact scene of each photo, so no aligned pairs exist. Unpaired training with 8.5. CycleGAN fits this case.
(d) Pix2pix applies if the webcam does not move. Frames of the same view by day and by night are aligned pairs, which is how the day-to-night data (Laffont et al. 2014) was built.