# Free-Form Image Inpainting with Gated Convolution

Gated convolutions let inpainting handle free-form holes with learned masks

Jiahui Yu, Zhe Lin, Jimei Yang, Xiaohui Shen, Xin Lu, Thomas Huang · arXiv · 2018-6-10

- **Problem** Vanilla convolutions treat hole pixels as valid, and partial convolution uses a fixed rule-based mask that cannot track which features are missing. Free-form holes make these errors show up as colour discrepancy and blur near boundaries.
- **Method** Gated convolution learns a soft gate for each channel and location, multiplying a feature map by a sigmoid gating map. The model pairs this with SN-PatchGAN, a patch-based GAN loss that works for holes of any shape.
- **Results** On Places2 with free-form masks, the model reaches 9.1% mean l1 error. That compares with 10.4% for PartialConv\* trained in the same framework.

## Why ordinary convolutions struggle with holes

Inpainting fills a missing region so that the filled content looks realistic and fits its surroundings, and the networks that do this are stacks of convolutions. A vanilla convolution applies the same filters at every location and treats every input value as a valid observation, so real pixels and hole pixels enter the computation on equal terms.

Near a hole boundary a filter sees a mixture of valid and invalid values, and that mixture produces colour discrepancy, blur and edge responses around the hole when free-form masks are used.

Partial convolution responds by renormalizing each output over valid pixels only and then updating the binary mask with a fixed rule after each layer. The authors list three limits of that rule. One mask is shared by all channels, so it cannot differ between features. The mask saturates to ones in deep layers, which removes the signal of which positions are inside the hole.

A sketch placed inside a hole has no obvious valid or invalid status.

## Gated convolution as a learned soft mask

A gated convolution computes two feature maps from the same input with two different convolution filters, one producing a feature map and one producing a gating map. The feature map is passed through an activation φ, the gating map through a sigmoid σ, and the output is their elementwise product. \\\[ O\_{y,x} = \\phi(\\mathrm{Feature}\_{y,x}) \\odot \\sigma(\\mathrm{Gating}\_{y,x}) \\\] Because the sigmoid bounds each gate between zero and one, every channel at every spatial location receives its own soft weight, and the weights are learned from data rather than set by a rule.

The activation φ can be ReLU, ELU or LeakyReLU, and the two filter banks are not shared. The authors inspected the gating values and report that they depend on background, mask and sketch regions, and that in some channels they follow foreground and background semantic segmentation. This evidence is a visual inspection of intermediate values, not a measured quantity.

Applying a mask before or after a stacked convolution gives the same result, because the masked input is already filled with zeros, so the difference lies in the learned gate and not in where the mask is applied.

## SN-PatchGAN: a discriminator over patches

Global and local GANs were designed for one rectangular hole, so they are not applicable when holes can appear anywhere with any shape. SN-PatchGAN instead outputs a three-dimensional map, and every element of that map is treated as its own real-or-fake decision. The discriminator is a fully convolutional network with six strided convolutions of kernel size 5 and stride 2, taking the image, the mask and any guidance channels as input.

Each output neuron's receptive field covers the entire input in this setting, so the authors do not add a separate global discriminator.

Spectral normalization is applied to the discriminator through the fast approximation from SN-GANs, to stabilize GAN training. The generator and discriminator losses use the hinge form: \\\[ L\_G = -\\mathbb{E}\_{z\\sim P\_z(z)}\[D\_{sn}(G(z))\] \\\] \\\[ L\_{D\_{sn}} = \\mathbb{E}\_{x\\sim P\_{data}(x)}\[\\mathrm{ReLU}(1-D\_{sn}(x))\] + \\mathbb{E}\_{z\\sim P\_z(z)}\[\\mathrm{ReLU}(1+D\_{sn}(G(z)))\] \\\] Here G is the inpainting network applied to the incomplete image z, and D\_{sn} is the spectral-normalized discriminator.

The discriminator term pushes real outputs above one and generated outputs below minus one, while the generator term raises the discriminator score of its outputs.

The authors report that training with SN-PatchGAN is faster and more stable than with their baseline model, and they attribute the stability to the spectral normalization together with the patch-level outputs.

## Generator, objective and training masks

The generator has a coarse network and a refinement network. The coarse network uses gated convolutions, some of them dilated, and the refinement network has two branches, one of which includes a contextual attention module. Every vanilla convolution is replaced by a gated convolution, and the encoder-decoder design is kept in place of a U-Net.

The authors argue that U-Net skip connections add little for masks that are not narrow, because near the centre of a hole their inputs are almost zero and carry no colour or texture information to the decoder. Their encoder-decoder with gated convolutions is enough to give seamless hole boundaries.

The objective has only two terms: a pixel-wise l1 reconstruction loss and the SN-PatchGAN loss, with a 1:1 balance between them. A perceptual loss is not used, because the patch-level information it would add is already encoded by SN-PatchGAN. The authors contrast this with PartialConv, which uses 6 different loss terms and balancing hyper-parameters.

Gated convolutions add parameters, so the authors cut the model width by 25% to keep the efficiency of their baseline, and they report no obvious performance drop. The full model has 4.1M parameters, and the network is fully convolutional, so it accepts different input resolutions at inference.

Training masks are not read from a fixed set. The method draws random strokes with random vertices, angles, lengths and brush widths, adds circles at the joints to keep strokes smooth, and generates the masks on the fly during training. Earlier work used a fixed set of irregular masks from video occlusion estimates, which the authors judge less diverse and less controllable.

## Adding a sketch channel for user guidance

A user sketch is passed to the network as an extra input channel, so the guided model sees the RGB channels, the mask channel and the sketch channel, five channels in total. For faces, the sketch is made by detecting landmarks and connecting related ones. For natural scenes, the HED edge detector produces edge maps, and values above 0.6 are set to one.

The authors note that a separate constraint loss could enforce the guidance, but they found that the same pixel-wise reconstruction loss and GAN loss, with the sketch channel fed to the discriminator as well, produce results that respect the guidance. A extra pixel-wise loss on HED output features was also tried, and the authors found the inpainting quality similar.

The guided model is trained separately from the automatic model.

## Mean error on Places2 masks

The evaluation uses Places2 validation images with both rectangular and free-form masks, and it reports mean l1 and l2 errors, where lower values are better. Neither the PartialConv\* baseline nor the comparisons use edge guidance, and PartialConv\* is the authors' own implementation within the same framework. The authors note that image inpainting lacks good quantitative metrics, which is why these two error measures are their main numbers.

With free-form masks, the model reaches 9.1% mean l1 error and 1.6% mean l2 error. The comparison values are 10.4% and 1.9% for PartialConv\*, 11.3% and 2.4% for PatchMatch, 17.2% and 4.7% for ContextAttention and 21.6% and 7.1% for Global&Local. The authors attribute the weaker result of partial convolution within the same framework to its un-learnable rule-based gating.

With rectangular masks, the model reaches 8.6% mean l1 error and 2.0% mean l2 error. ContextAttention has 8.6% and 2.1% on the same masks, Global&Local has 9.3% and 2.2%, PartialConv\* has 9.8% and 2.3%, and PatchMatch has 16.1% and 3.9%. On rectangular masks the model matches ContextAttention on the l1 error, and the l2 error is lower for the model.

Every learning-based method beats PatchMatch on both measures in this table.

## User study and visual comparisons

The user study used 30 Places2 validation images with holes and no sketches, and 104 users completed it. Raters scored each result for naturalness from 1 to 10, with higher being better, and also made pairwise choices between the model and the official PartialConv.

Naturalness scores were 9.89 for ground truth, 7.72 for the model, 7.07 for the re-implemented PartialConv and 6.54 for the official PartialConv. In pairwise comparison, 79.4% of choices favoured the model over the official PartialConv, against 20.6% for PartialConv.

In the visual comparisons, vanilla-convolution methods show clear artefacts and edge responses in and around holes, and PartialConv gives better results with a remaining colour discrepancy. The gated model gives results without noticeable colour inconsistency, and the authors identify uniform regions as hard cases for all learning-based methods.

Given a sparse sketch, the user-guided results are described as realistic with seamless boundaries, but these are visual observations, not scored results.

In an object removal case, Photoshop's Content-Aware Fill copies half of a face from the left side of the hole, which the authors read as the effect of a method that ignores image semantics. The ContextAttention model shows artefacts near the hole boundary.

## What the SN-PatchGAN ablation shows

The ablation places the original image, the masked input, the output of one global GAN and the output of SN-PatchGAN side by side, so the comparison is visual. The authors report that SN-PatchGAN gives visibly better results than the single global discriminator, and no metric is given for this comparison in the main text.

The comparison pairs a single vanilla global discriminator with SN-PatchGAN, which changes both the discriminator layout and the use of spectral normalization, so the main text does not isolate the effect of each change.

## Run time on GPU and CPU

For 512×512 images, inference takes 0.21 seconds per image on a single NVIDIA Tesla V100 GPU and 1.9 seconds on an Intel Xeon CPU at 2.00GHz, with the authors stating that the time does not depend on hole size. Training used TensorFlow v1.8, CUDNN v7.0 and CUDA v9.0.

## Limitations

Quantitative error results are reported on Places2 only, while CelebA-HQ faces are evaluated visually. The quantitative table excludes edge guidance, so the user-guided variant is assessed only by visual comparison and by the sketch examples.

The user study covers 30 images, and the official PartialConv is compared only in that study and in the visual comparisons, not in the error table. The authors' PartialConv\* is their own implementation, since no official implementation was available for the error table.

The model width was reduced by 25% to offset the extra parameters of gated convolutions, and the authors' judgement that performance did not drop rests on their own quantitative and qualitative checks. The SN-PatchGAN ablation is qualitative, and the runtime was measured on one GPU and one CPU.

## Paper info

- **Authors**: Jiahui Yu, Zhe Lin, Jimei Yang, Xiaohui Shen, Xin Lu, Thomas Huang
- **Venue**: arXiv · Preprint
- **First published**: 2018-6-10
- **Added**: 2026-11-12
- **Updated**: 2019-10-22
- **Version**: v2
- **arXiv**: 1806.03589
- **Category**: cs.CV · cs.GR · cs.LG
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/1806.03589v2) · [PDF ↗](https://arxiv.org/pdf/1806.03589v2)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Jiahui Yu, Zhe Lin, Jimei Yang, Xiaohui Shen, Xin Lu, Thomas Huang (2018). Free-Form Image Inpainting with Gated Convolution. arXiv:1806.03589v2. https://arxiv.org/abs/1806.03589v2

**BibTeX**

```
@misc{yu2018free,
  title = {{Free-Form Image Inpainting with Gated Convolution}},
  author = {Jiahui Yu and Zhe Lin and Jimei Yang and Xiaohui Shen and Xin Lu and Thomas Huang},
  year = {2018},
  eprint = {1806.03589},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/1806.03589v2}
}
```

## Related papers

- [Generative Image Inpainting with Contextual Attention](https://refivu.com/en/papers/aad66e29-0d9a-49ff-bbf1-91b1b036c9eb-generative-image-inpainting-with-contextual-attention.md)
- [Spectral Normalization for Generative Adversarial Networks](https://refivu.com/en/papers/a69a23bf-3dd6-4b32-9fb1-444f7f67a6e8-spectral-normalization-for-generative-adversarial-networks.md)
- [Self-Attention Generative Adversarial Networks](https://refivu.com/en/papers/819d5b11-1213-4c83-93af-58db3c44b12f-self-attention-generative-adversarial-networks.md)
- [DU-GAN: Generative Adversarial Networks with Dual-Domain U-Net Based Discriminators for Low-Dose CT Denoising](https://refivu.com/en/papers/46030b78-75d0-4156-80ef-e92602d09b95-du-gan-generative-adversarial-networks-with-dual-domain.md)
- [Attention Gated Networks: Learning to Leverage Salient Regions in Medical Images](https://refivu.com/en/papers/63b212b7-3fc0-42f4-b6d8-1724e918c784-attention-gated-networks-learning-to-leverage-salient.md)
- [Attention U-Net: Learning Where to Look for the Pancreas](https://refivu.com/en/papers/b480e08e-027d-4c11-b1c5-0d3a4a63ead9-attention-u-net-learning-where-to-look-for.md)

## Other paper commentary

- [Focal and Efficient IOU Loss for Accurate Bounding Box Regression](https://refivu.com/en/papers/cc28bb37-8cb7-49b2-84aa-5bd885a3d4e6-focal-and-efficient-iou-loss-for-accurate-bounding.md)
- [NAS-FPN: Learning Scalable Feature Pyramid Architecture for Object Detection](https://refivu.com/en/papers/52a19738-611e-4c4e-b54f-2e9b88114304-nas-fpn-learning-scalable-feature-pyramid-architecture-for.md)
- [Understanding Convolution for Semantic Segmentation](https://refivu.com/en/papers/c32b12c2-d4e5-4e10-b7c6-b643713776fc-understanding-convolution-for-semantic-segmentation.md)
- [Unsupervised Image-to-Image Translation Networks](https://refivu.com/en/papers/23eee1aa-7651-4535-93bf-d37262ee5dff-unsupervised-image-to-image-translation-networks.md)
- [Methods of Hierarchical Clustering](https://refivu.com/en/papers/b67289cf-e579-4f78-a7ff-bdea1acaa376-methods-of-hierarchical-clustering.md)
- [TSM: Temporal Shift Module for Efficient Video Understanding](https://refivu.com/en/papers/e7e3764b-d00b-4555-9a23-62f113a3fdfe-tsm-temporal-shift-module-for-efficient-video-understanding.md)
- [Person Transfer GAN to Bridge Domain Gap for Person Re-Identification](https://refivu.com/en/papers/68ab1f6a-e5fc-4ab0-aa97-7c531e651f1d-person-transfer-gan-to-bridge-domain-gap-for.md)
- [Generalized Random Forests](https://refivu.com/en/papers/0e375cb5-e9bd-4631-b3f2-4cdd0973c2a8-generalized-random-forests.md)
- [Learning Bayesian Networks with the bnlearn R Package](https://refivu.com/en/papers/bdeeb8df-cd3a-419f-af61-f58819af852d-learning-bayesian-networks-with-the-bnlearn-r-package.md)
- [A Survey on In-context Learning](https://refivu.com/en/papers/e3dcb216-0b54-4a23-8e0d-f077c493f505-a-survey-on-in-context-learning.md)
- [Probabilistic Latent Semantic Analysis](https://refivu.com/en/papers/2bb41f8f-b6cc-410c-9347-fd84aed3b2b4-probabilistic-latent-semantic-analysis.md)
- [Deep Learning for Sentiment Analysis : A Survey](https://refivu.com/en/papers/423bc94d-8ef5-42e2-a68f-87cb54c7636d-deep-learning-for-sentiment-analysis-a-survey.md)

## Terms in this paper

- [u-net](https://refivu.com/en/terms/f49a93d54ad5-u-net)
- [CelebA-HQ](https://refivu.com/en/terms/55be90c6f5a7-celeba-hq)
- [spectral normalization](https://refivu.com/en/terms/bf3f626bf932-spectral-normalization)
- [hinge loss](https://refivu.com/en/terms/81cbdf4a8c8a-hinge-loss)
- [Places2](https://refivu.com/en/terms/1e69ee0dc4d0-places2)
- [PatchMatch](https://refivu.com/en/terms/37cf9d703cf5-patchmatch)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/c438682f-8d73-4981-a1ac-035e6f376b93-free-form-image-inpainting-with-gated-convolution)
