What the inpainting model does
Inpainting fills a chosen region of a photo with new content that fits what is around it. There are two ways to make a text-to-image model do it. One leaves the model as it is and constrains its sampling: start from the photo, or keep putting the photo back outside the hole. The other trains the model to read the photo and the mask as inputs, so that at every step it sees what it has to fit. Stable Diffusion's inpainting model takes the second way; this tab records it and sets it beside SD 1.5's img2img, a version of the first.
The model is stable-diffusion-v1-5/stable-diffusion-inpainting: a U-Net of SD 1.5's design trained further to inpaint, with five extra input channels, and SD 1.5's VAE and text encoder (the same files). It repaints the dog in the beagle photo as a ginger cat sitting in a garden, photo at 384 × 384 pixels (a 4 × 48 × 48 latent), with 20 DDIM steps, guidance 7.5 and seed 0.
The mask
The Photo and mask panel starts from SAM's mask of the dog, the one the Transformer Explainer's SAM tab predicts for a box around it, grown by 4 pixels on every side (a 9 × 9 maximum filter) so that the dog's outline is repainted too. White is repainted: mask values of 0.5 and above become 1, the rest 0. The masked photo is the photo in times , where is the mask, so the hole becomes 0: mid-grey, not black.
In latent space shows what the U-Net receives. The mask is shrunk to 48 × 48 by nearest neighbour, so each latent cell is the top-left pixel of its 8 × 8 block. The masked photo goes through SD 1.5's VAE encoder, and a sample of its latent distribution, times 0.18215, gives four channels on the same scale as the noisy latent. Its decode shows what that latent keeps of the photo and of the grey hole.
Nine input channels
At every step the U-Net reads the noisy latent, the mask and the masked photo's latent stacked on channels, and still predicts four channels of noise:
That makes 4 + 1 + 4 = 9 input channels. Only the first layer changes shape: conv_in maps 9 channels to 320 instead of 4, and everything after it is SD 1.5's U-Net. This is latent diffusion's general way to condition on anything aligned with the image, which Rombach et al. describe as concatenating it to the input of .
The model card says how this model was trained: from Stable Diffusion 1.2's weights, 595k steps of ordinary training, then 440k steps of inpainting at 512 × 512 on LAION-Aesthetics v2 5+, with the text dropped 10 % of the time for guidance. The weights on the five new channels started at zero, so inpainting training began from the text-to-image model's own behaviour, the idea behind ControlNet's zero convolutions too (the ControlNet tab). Masks were synthetic, and in 25 % of examples everything was masked, which keeps the model able to draw without any photo.
Because a convolution is linear, conv_in's output splits exactly into what each group of channels contributes, . The Nine input channels panel draws the three parts at the chosen step on one colour scale, with bars for the size of the weights on each input channel; below it, the U-Net shows every block's output in the prompt pass.
Guidance and sampling
Both passes of classifier-free guidance get the same mask and masked latent, so the difference that guidance amplifies is the prompt's alone:
The Guidance panel shows both predictions and the result. The Sampling loop is DDIM from pure noise, as on the SD 1.5 tab: is noise everywhere, outside the hole too, and the U-Net has to draw the kept region again from the masked latent beside it. Nothing is pasted back at the end, so outside the mask the result is the VAE's decode of what the model drew, not the photo's own pixels; the Inpainting vs img2img panel gives each result's mean difference from the photo there.
Img2img, with the photo pasted back
SD 1.5's own U-Net has four input channels and no way to read a mask. Its img2img pipeline is SDEdit (Meng et al.): encode the photo to , noise it part of the way, and denoise from there with the prompt. Strength 0.8, diffusers' default, keeps the last of the 20 DDIM steps, so the run starts at from
Meng et al. describe the strength as a trade between realism and faithfulness to the input: the more noise, the freer the model, and the less of the input survives. Here the start carries the whole photo under the noise, dog included, and the model regenerates every pixel without being told where the hole is. Afterwards the photo is pasted back outside the mask, so there the result is the photo exactly and only the inside is new.
The Inpainting vs img2img panel shows the inpainting result, img2img as generated and img2img pasted. The Inpainting U-Net vs SD 1.5 img2img card sets at every step of both runs side by side; img2img's 16 steps start later, so its row aligns with the other by step number, and the last column holds each run's final image, img2img's after pasting.
In this run the two differ as their inputs predict. The inpainting U-Net draws a ginger cat sitting where the dog was, in the photo's light and among its garden hose, and outside the mask its image differs from the photo by 6.6 of 255 on average: the VAE's redraw. Img2img as generated differs there by 40.0, because it drew a new scene, a cat lying on a green cushion; pasting brings that to 0, but the cat and the cushion were drawn without knowing where the hole was, and they meet the kept photo at visible seams.
Where inpainting came from
Before diffusion, inpainting was the work of dedicated networks, often GANs, trained only to fill holes. RePaint (Lugmayr et al.) showed that an unconditional diffusion model can inpaint without being trained for it: at every step the known region is replaced by the photo noised to that step, and steps are repeated so that the two parts can agree, at the cost of many more steps. Rombach et al. trained latent diffusion models to inpaint with the masked image as an input, and Stable Diffusion's inpainting model is that recipe at scale, with text.
The Pix2Pix tab uses the same channel concatenation with the whole photo and a written instruction instead of a mask. The IP-Adapter tab takes a picture in through cross-attention instead, as content to borrow rather than a photo to keep.
Papers
- High-Resolution Image Synthesis with Latent Diffusion Models Latent diffusion, with spatially aligned conditions concatenated to the U-Net's input, including the inpainting experiments that the Stable Diffusion inpainting model scales up.
- Stable Diffusion Inpainting model card How the recorded model was trained: five extra input channels started at zero, synthetic masks, and a quarter of the examples fully masked.
- Segment Anything SAM, whose mask of the dog (from the SAM tab) marks what is repainted.
- SDEdit: Guided Image Synthesis and Editing with Stochastic Differential Equations Editing by noising an image part of the way and denoising it again: SD 1.5's img2img in the comparison.
- RePaint: Inpainting using Denoising Diffusion Probabilistic Models Inpainting with an unconditional model and no training for it: the known region is replaced by the noised photo at every step.
- Classifier-Free Diffusion Guidance The guidance mix, here with the mask and the masked latent in both passes.
- Denoising Diffusion Implicit Models DDIM, the sampler of both runs.