text
stringlengths
0
5.54k
... generator=generator,
... device=torch_device,
... ) Denoise the image Start by scaling the input with the initial noise distribution, sigma, the noise scale value, which is required for improved schedulers like UniPCMultistepScheduler: Copied >>> latents = latents * scheduler.init_noise_sigma The last step is to create the denoising loop that’ll progressively t...
>>> scheduler.set_timesteps(num_inference_steps)
>>> for t in tqdm(scheduler.timesteps):
... # expand the latents if we are doing classifier-free guidance to avoid doing two forward passes.
... latent_model_input = torch.cat([latents] * 2)
... latent_model_input = scheduler.scale_model_input(latent_model_input, timestep=t)
... # predict the noise residual
... with torch.no_grad():
... noise_pred = unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample
... # perform guidance
... noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
... noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
... # compute the previous noisy sample x_t -> x_t-1
... latents = scheduler.step(noise_pred, t, latents).prev_sample Decode the image The final step is to use the vae to decode the latent representation into an image and get the decoded output with sample: Copied # scale and decode the image latents with vae
latents = 1 / 0.18215 * latents
with torch.no_grad():
image = vae.decode(latents).sample Lastly, convert the image to a PIL.Image to see your generated image! Copied >>> image = (image / 2 + 0.5).clamp(0, 1).squeeze()
>>> image = (image.permute(1, 2, 0) * 255).to(torch.uint8).cpu().numpy()
>>> images = (image * 255).round().astype("uint8")
>>> image = Image.fromarray(image)
>>> image Next steps From basic to complex pipelines, you’ve seen that all you really need to write your own diffusion system is a denoising loop. The loop should set the scheduler’s timesteps, iterate over them, and alternate between calling the UNet model to predict the noise residual and passing it to the schedule...
Pipelines
Pipelines provide a simple way to run state-of-the-art diffusion models in inference.
Most diffusion systems consist of multiple independently-trained models and highly adaptable scheduler
components - all of which are needed to have a functioning end-to-end diffusion system.
As an example, Stable Diffusion has three independently trained models:
Autoencoder
Conditional Unet
CLIP text encoder
a scheduler component, scheduler,
a CLIPImageProcessor,
as well as a safety checker.
All of these components are necessary to run stable diffusion in inference even though they were trained
or created independently from each other.
To that end, we strive to offer all open-sourced, state-of-the-art diffusion system under a unified API.
More specifically, we strive to provide pipelines that
can load the officially published weights and yield 1-to-1 the same outputs as the original implementation according to the corresponding paper (e.g. LDMTextToImagePipeline, uses the officially released weights of High-Resolution Image Synthesis with Latent Diffusion Models),
have a simple user interface to run the model in inference (see the Pipelines API section),
are easy to understand with code that is self-explanatory and can be read along-side the official paper (see Pipelines summary),
can easily be contributed by the community (see the Contribution section).
Note that pipelines do not (and should not) offer any training functionality.
If you are looking for official training examples, please have a look at examples.
🧨 Diffusers Summary
The following table summarizes all officially supported pipelines, their corresponding paper, and if
available a colab notebook to directly try them out.
Pipeline
Paper
Tasks
Colab
alt_diffusion
AltDiffusion
Image-to-Image Text-Guided Generation
-
audio_diffusion
Audio Diffusion
Unconditional Audio Generation
controlnet
ControlNet with Stable Diffusion
Image-to-Image Text-Guided Generation
cycle_diffusion
Cycle Diffusion
Image-to-Image Text-Guided Generation
dance_diffusion
Dance Diffusion
Unconditional Audio Generation
ddpm
Denoising Diffusion Probabilistic Models
Unconditional Image Generation
ddim
Denoising Diffusion Implicit Models
Unconditional Image Generation
if
IF
Image Generation
if_img2img
IF
Image-to-Image Generation
if_inpainting
IF
Image-to-Image Generation