ai papers
Image Manipulation with GANs: Image-to-Image Translation and GAN Inversion
Junyoung Park · 2022-12-12 · 9 min
GANs support many kinds of image synthesis:
- Texture synthesis: PSGAN, TextureGAN, and Texture Mixer
- Super-resolution: ProGAN, Progressive Face Super-Resolution, BigGAN, and StyleGAN
- Image inpainting, which completes missing or damaged images: DeepFill v1, ExGAN, DeepFill v2, EdgeConnect, and PEN-Net
- Face synthesis: ELEGANT, STGAN, SCGAN, Example-Guided Image Synthesis, SGGAN, and MaskGAN
- Human image synthesis for pose and body shape: Text-Guided, Progressive Pose Attention, Coordinate-Based, and Semantic Parsing
This article focuses instead on changing an existing image:
- Image-to-image translation: CycleGAN, MUNIT, DRIT, TransGaGa, and RelGAN
- Image editing: SC-FEGAN, FE-GAN, Mask-Guided, and FaceShapeGene
- Cartoon generation: CartoonGAN, PI-REC, Internal Representation Collaging, U-GAT-IT, and Landmark-Assisted CycleGAN
Image Translation
The long list shows how broadly GANs can be used. Our real aim is to understand the early ideas behind image manipulation.
Image-to-image translation generates an output image related to an input. It includes semantic labeling and boundary detection in computer vision,
as well as colorization and super-resolution in computer graphics and computational photography.
Supervision maps a source image from domain into a target image in domain . Every such task reduces to the same outline:
- Define objective .
- Prepare training pairs .
- Train network .
- Use translation .
Applying an ordinary GAN creates two problems. First, it cannot control the generation mode: we need a translated version of the input, not merely any plausible target-domain image. Second, early generators produced low-resolution output. Three representative solutions are pix2pix, CycleGAN, and pix2pixHD. We will focus on the first two.
pix2pix: Image-to-Image Translation with Conditional Adversarial Nets
pix2pix is the representative GAN approach to paired image translation. Its best-known demonstration turns a sketch into a realistic photograph.
A generator can make a plausible bag from a sketch while still ignoring the sketch's exact structure. In the upper row, the generated image follows the outline; in the lower row, it resembles a bag but has poor correspondence with the input.
The ordinary GAN objective cannot express this distinction:
It merely asks whether the generated image looks as though it came from the real target distribution. Supplying sketch to the generator gives
but an unconditional discriminator still tells only whether the output looks real, not whether it corresponds to .
pix2pix uses a conditional GAN objective and adds mean absolute error between output and paired ground truth:
Conditioning discriminator on requires the result to be plausible for that source. The L1 term further preserves content and reduces the blurry or structurally unrelated solution; pix2pix uses a large weight around .
The generator uses a U-Net, whose skip connections preserve spatial structure. In practice, pix2pix does not supply an explicit latent in addition to ; dropout in the decoder supplies stochasticity. Experiments found that an explicit latent was largely ignored.
The figure isolates the losses. Without L1, output loses the target's broad structure. Without the conditional GAN, it becomes blurry. The discriminator uses Leaky ReLU with slope and, as in DCGAN, omits batch normalization from the first layer.
pix2pix can solve many translation tasks whenever paired datasets are available. Its limitation is precisely the pair requirement. Sketch-to-image learning needs a photograph corresponding to every sketch; depth generation needs a depth map for every scene. Such pairs are expensive and sometimes impossible to collect.
CycleGAN starts from this limitation. Suppose we want to convert photographs into Monet's style. We cannot photograph the exact scenes Monet painted under the same weather, landscaping, and historical conditions. Nor can we resurrect Monet and ask him to paint a new paired dataset.
pix2pix applies only to pairs like those on the left, not unrelated collections like those on the right.
CycleGAN: Unpaired Image-to-Image Translation with Cycle Consistency
Think of generation as language translation. Translate English into Korean and then translate the Korean back into English; the result should reproduce the original sentence. In CycleGAN, generators are the translators.
For horse-to-zebra translation, converting a horse to a zebra and then back to a horse should reconstruct the original horse. This constraint preserves content even without paired examples.
Let and be two domains. Generator maps to , while maps to .
The forward adversarial loss is
where discriminator distinguishes real examples from outputs of . This trains and but says nothing about the reverse mapping, so CycleGAN adds
where and compete over domain .
The central intuition—returning should recover the original—is expressed by an L1 cycle-consistency loss:
The implementation resembles pix2pix, but the original CycleGAN uses instance normalization and a modified ResNet generator.
One training step is:
- Sample and independently.
- Generate and .
- Train on real versus fake , and on real versus fake .
- Reconstruct and .
- Calculate cycle L1 losses between and .
- Combine adversarial and cycle terms to update the generators.
The method works across many domains using only two unpaired collections. Its limitation is that a GAN has no explicit object understanding. In horse-to-zebra translation, it can paint stripes onto the background or even onto a rider. If riders are common and balanced in training, the model may learn not to alter them; if they are rare, inference produces these artifacts.
GAN Inversion
GAN inversion approaches image manipulation from the opposite direction.
For a trained generator , latent point produces fake image . Latents are commonly sampled from . Inversion instead starts with a real image and searches for the latent whose generated image best matches it:
If an ideal reconstructs , then moving that latent through meaningful directions edits the real image. The concept is simple once the generator is understood.
Many pretrained StyleGANs expose useful domains for manipulation; see this collection of pretrained StyleGAN models.
Image2StyleGAN
StyleGAN contains an MLP mapping into intermediate space . Image2StyleGAN performs inversion in an expanded space. A normal latent is copied 18 times and supplied to all 18 style layers. A code instead has shape , allowing a different row vector at every style layer and therefore a more detailed reconstruction.
Rather than accept an arbitrary StyleGAN sample, optimize a latent to reproduce a reference image, then edit through that latent. Optimization proceeds as follows:
- Initialize latent code .
- Generate with a pretrained StyleGAN.
- Compare with reference using loss .
- Update by gradient descent while keeping fixed.
- Repeat for several iterations.
Because the generator is fixed, two choices dominate performance: the latent space and initialization, and the loss that identifies a faithful code within that space.
Random initialization is possible, but StyleGAN samples poorly in low-density regions. If the initial point lies where feasible gradient directions lead away from supported images, optimization may not reconstruct the target. Starting from the mean latent—the “mean face”—is safer.
Pixel-wise MSE alone also fails to reflect perceptual content and makes high-quality inversion difficult. A perceptual loss adds similarity between deep features:
Perceptual loss uses feature maps from an ImageNet-pretrained VGG-16 to compare hidden representations of the two images.
The original perceptual-loss paper optimized style through VGG features, demonstrating that pretrained activations can guide content- and style-aware reconstruction:
Applications
Morphing
Morphing gradually changes one image into another. If images and invert to codes and , interpolate their latents:
The morphed image is .
Expression Transfer
Expression transfer uses latent arithmetic. Suppose:
- represents a neutral cat image .
- represents a neutral dog image .
- represents a smiling dog image .
Then
should produce a smiling cat. The difference represents a direction associated with the expression, which can transfer to another identity or category.
Style Transfer
This resembles StyleGAN's original style mixing but operates in :
- Invert into .
- Invert into .
- Use early rows from one and later rows from the other, concatenating coarse styles from one source with fine styles from the second.
Because inversion supplies style codes for real images, this transfer can work even across seemingly unrelated source domains, extending StyleGAN's original face-synthesis demonstration.
Image2StyleGAN++ expands the approach further with mask-based style transfer, image inpainting, local editing, and other applications.