Semantic Image Inpainting with Perceptual and Contextual Losses

Raymond A. Yeh, Chen Chen, Teck-Yian Lim, Mark Hasegawa-Johnson, Minh N. Do

TL;DR

This paper introduces a novel semantic image inpainting method that leverages a pre-trained Deep Convolutional Generative Adversarial Network (DCGAN) to fill in large, arbitrarily shaped missing regions in images. By defining a combination of importance-weighted context loss and adversarial prior loss, the method searches the latent space of the generator to find the closest realistic image match. Crucially, this approach requires no knowledge of the corruption mask during training, allowing it to handle arbitrary holes during inference.

What Problem Does This Paper Address?

Traditional image inpainting methods focus on restoring small corruptions or textures by borrowing pixels from elsewhere in the same image. However, they fail at semantic inpainting—the task of filling large missing regions that require high-level contextual understanding (e.g., generating a missing eye or nose on a face).

While deep learning approaches like Context Encoders (CE) can infer these semantic structures, they are typically trained on fixed mask shapes (like a central square block). If the missing region at test time has an arbitrary or unexpected shape, these models produce blurry, artifact-laden results. The authors aim to create a flexible, mask-agnostic semantic inpainting framework that achieves pixel-level photorealism.

Methods

Instead of training a network to directly output the missing pixels, the authors treat inpainting as a constrained image generation problem using a pre-trained DCGAN. The weights of the GAN are kept fixed, and the model searches for the optimal latent vector $\hat{z}$ that minimizes a two-part loss function:

  1. Importance-Weighted Context Loss: Measures the $\ell_1$-norm difference between the generated image and the uncorrupted parts of the input image. Crucially, it applies a spatial weight so that uncorrupted pixels closer to the missing holes contribute more to the loss than distant background pixels.
  2. Prior Loss: Utilizes the GAN's discriminator loss to penalize unrealistic images. This forces the optimization process to stay within the manifold of natural images, ensuring the generated content looks authentic.

The optimal latent vector $\hat{z}$ is found iteratively via backpropagation to the input data space using the Adam optimizer. Once the optimal image is generated, Poisson blending is applied as a post-processing step to seamlessly integrate the newly generated details with the original uncorrupted pixels, eliminating visible seams.

The proposed framework maps the corrupted image to the closest latent encoding via iterative backpropagation.

Datasets

The method was evaluated across four different mask types (central block, random patterns with ~25% missing, 80% completely random missing, and half missing) on three benchmark datasets:

  • CelebFaces Attributes Dataset (CelebA): 202,599 face images cropped and resized to $64 \times 64$.
  • Street View House Numbers (SVHN): 99,289 images of cropped house numbers.
  • Stanford Cars Dataset: 16,185 images spanning 196 classes of cars.

Key Insights and Ideas

  • Decoupling the Mask from Training: By utilizing a generative model trained entirely on clean, uncorrupted data, the network learns the true underlying data distribution. The actual shape of the missing region is only introduced during inference, making the model incredibly robust to arbitrary corruptions.
  • The Importance of the Prior Loss: Without the adversarial prior loss, the latent space optimization can easily converge to perceptually implausible results that technically satisfy the pixel-matching criteria but look unnatural to humans.
  • The PSNR Paradox: The authors highlight a common issue in generative tasks: standard metrics like Peak Signal-to-Noise Ratio (PSNR) directly minimize mean squared error and favor blurry, averaged results. Because semantic inpainting has multiple valid realistic solutions (e.g., generating a different but plausible hairstyle), the proposed method occasionally scores lower on PSNR despite being visually superior and more photorealistic than baseline methods.

Key Results

The proposed method significantly outperforms classical techniques like Total Variation (TV), Low Rank (LR) approximation, and PatchMatch (PM) when dealing with large missing regions.

Figure 1. Semantic inpainting results by TV, LR, PM and our method. Holes are marked by black color.

Quantitatively, when evaluated against Context Encoders on random masks, the proposed method achieved substantial gains in PSNR (e.g., 33.0 dB vs. 24.1 dB on the SVHN dataset). Visually, the results are much sharper, maintaining structural coherence and fine details where other methods produce severe blurring or checkerboard artifacts.

Limitations and Future Work

The primary limitation of this approach is its heavy reliance on the capacity of the underlying generative model. The DCGAN architecture used in the paper performs exceptionally well on highly structured domains (like faces or cars) but is too constrained to represent complex, diverse real-world scenes. The authors note that as generative models continue to improve, their inpainting framework will naturally inherit those improvements to handle more complex images.

Summary updated

BibTeX
@inproceedings{yeh2017semantic,
  author = {Raymond A. Yeh and Chen Chen and Teck-Yian Lim and Mark Hasegawa-Johnson and Minh N. Do},
  title = {{Semantic Image Inpainting with Perceptual and Contextual Losses}},
  booktitle = {IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  year = {2017},
  doi = {10.1109/cvpr.2017.728},
}
Figure