The problem
To generate an image, a diffusion model runs the noising process backwards: it starts from pure noise and, at each step from down to 1, asks the network for a prediction of the noise in and removes a bit of it, getting . You will write one reverse step, the clean-image estimate, and the whole loop (Ho et al., 2020, Algorithm 2).
One reverse step
Given the current sample and the network's noise prediction :
where:
- and come from the schedule of the previous exercise;
- the first part removes the predicted noise from ;
- , and is fresh standard Gaussian noise for , but at the last step, : the final image has no noise added.
Example. Schedule , so . Step with , prediction and fresh noise :
- first part: ;
- noise: ;
- .
Step with the same and : no noise term, and (with ).
The clean-image estimate
From the noise prediction one can estimate the clean image directly, inverting the forward formula:
Example. The forward example: and noise 1 gave . If the network predicts the noise exactly (): . A wrong prediction gives a wrong estimate.
What to implement
With betas and abar indexed from 0 as in the previous exercise (step is index ):
predict_x0(x_t, eps_hat, t, abar): the clean-image estimate.reverse_step(x_t, eps_hat, t, betas, abar, z): .zis given (an array shaped likex_t), so the step is deterministic; ignore it when .sample(eps_model, x_T, betas, abar, rng): the full loop from down to 1, callingeps_model(x, t)for the prediction and drawingz = rng.standard_normal(x.shape)at every step with (and none at ).
Requirements
- .
- Draw the random numbers exactly as described so that the tests can reproduce the samples.