DDIMScheduler controls diffusion timing: it schedules step sizes, adds time-dependent noise to images, and drives the reverse denoising sequence. It supports deterministic inference from random noise to clean samples. section 15.2.2 section 15.4.2
Its main workflow has two complementary parts:
- Use add_noise(clean_images, noise, timesteps) to create transitional noisy images. Increasing t reduces the clean-image contribution and increases the noise contribution. section 15.2.2
- Use generate() to begin with random noise, select inference timesteps, call the denoising U-Net, and repeatedly apply step() until reaching t = 0. section 15.4.2
- Choose the inference count independently for generation; the example uses 50 equally spaced steps across T = 1,000. section 15.4.2
Generating flower images
A trained denoising U-Net generates a batch of 10 flower images from random noise with num_inference_steps=50, eta=1.0, and use_clipped_model_output=True. The scheduler returns generated samples and intermediate images, allowing the transition from noisy states to clean flowers to be plotted. section 15.4.2
Listing 15.7 Defining a generate() method in the DDIMScheduler() class
@torch.no_grad()
def generate(self,model,device,batch_size=1,generator=None,
eta=1.0,use_clipped_model_output=True,num_inference_steps=50):
imgs=[]
image=torch.randn((batch_size,model.in_channels,model.sample_size,
model.sample_size),
generator=generator).to(device) #1
self.set_timesteps(num_inference_steps)
for t in tqdm(self.timesteps): #2
model_output = model(image, t)["sample"] #3
image = self.step(model_output,t,image,eta,
use_clipped_model_output=\
use_clipped_model_output) #4
img = unnormalize_to_zero_to_one(image)
img = img.cpu().permute(0, 2, 3, 1).numpy()
imgs.append(img) #5
image = unnormalize_to_zero_to_one(image)
image = image.cpu().permute(0, 2, 3, 1).numpy()
return {"sample": image}, imgs
Inspect the generate() implementation to follow initialization, timestep selection, model prediction, scheduler stepping, and intermediate-image capture. section 15.4.2
