Filling the Hole

Diffusion ModelsMedium+20 XP+20

Filling the Hole

Inpainting means filling in a missing part of an image so that it fits what is around it. A diffusion model trained only to make whole images can already do this. Nothing is retrained; one extra move is added to sampling.

Ordinary sampling starts from pure noise xTx_TxT​ and applies the trained denoising step again and again: xT→xT−1→⋯→x0x_T \to x_{T-1} \to \dots \to x_0xT​→xT−1​→⋯→x0​. When you already know some pixels of the answer, add this after every step: overwrite each known pixel with its true value noised to the level the image has just reached, using the forward formula

αˉs  y+1−αˉs  n\sqrt{\bar\alpha_s}\;y + \sqrt{1-\bar\alpha_s}\;nαˉs​​y+1−αˉs​​n

where sss is that level, yyy is the pixel's true value and nnn is a noise value for that pixel. Unknown pixels keep whatever the denoiser produced. The denoiser therefore always sees known pixels at the same noise level as the rest of the image, and at every step it shapes the unknown region to agree with them.

In full: set x=xTx = x_Tx=xT​. Then for t=T,T−1,…,1t = T, T-1, \dots, 1t=T,T−1,…,1:

  1. x←x \leftarrowx← denoise_step(x, t), which takes the whole image from level ttt to level t−1t-1t−1.
  2. Overwrite every known pixel using the formula with s=t−1s = t - 1s=t−1.

Level 0 means no noise at all (αˉ0=1\bar\alpha_0 = 1αˉ0​=1), so after the last step the known pixels hold exactly their true values.

Task: write inpaint(known, mask, x_T, known_noise, alpha_bars, denoise_step).

  • known: the true image. Only the positions where mask is 1 matter; the others hold placeholder zeros.
  • mask: 1 for a known pixel, 0 for a pixel to fill in.
  • x_T: the starting noise, one value per pixel.
  • known_noise: the value nnn for each pixel, reused at every level.
  • alpha_bars: [αˉ1,αˉ2,…,αˉT][\bar\alpha_1, \bar\alpha_2, \dots, \bar\alpha_T][αˉ1​,αˉ2​,…,αˉT​], so its length is TTT.
  • denoise_step: the trained denoising step, as a function. denoise_step(x, t) takes the whole image as a list and returns a new list.

Return the final image x0x_0x0​, every value rounded to 4 decimal places.

A real sampler draws fresh noise for the known pixels at every step; one fixed draw is used here so the result can be checked. The test denoisers are small stand-ins, such as a step that pulls every pixel part of the way toward the image's average. Like a real denoiser, they let pixels influence one another, which is what lets the known region steer the rest.

Examples

inpaint([0.8, 0.8, 0.0, 0.0], [1, 1, 0, 0], [0.5, -1.0, 1.2, -0.3], [0.4, -0.6, 0.0, 0.0], [0.9, 0.6, 0.3], lambda x, t: [0.5 * v + 0.5 * sum(x) / len(x) for v in x])
returns [0.8, 0.8, 0.5346, 0.3471]
inpaint([0.5, 0.0, 0.0, 0.0, 0.5], [1, 0, 0, 0, 1], [1.1, -0.7, 0.3, 0.9, -1.4], [-0.5, 0.0, 0.0, 0.0, 0.8], [0.95, 0.8, 0.5, 0.2], lambda x, t: [(x[max(i - 1, 0)] + x[i] + x[min(i + 1, len(x) - 1)]) / 3 for i in range(len(x))])
returns [0.5, 0.2421, 0.2565, 0.4265, 0.5]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run