Consistency Models
Last updated
Last updated
Consistency Models were proposed in by Yang Song, Prafulla Dhariwal, Mark Chen, and Ilya Sutskever.
The abstract from the paper is:
Diffusion models have significantly advanced the fields of image, audio, and video generation, but they depend on an iterative sampling process that causes slow generation. To overcome this limitation, we propose consistency models, a new family of models that generate high quality samples by directly mapping noise to data. They support fast one-step generation by design, while still allowing multistep sampling to trade compute for sample quality. They also support zero-shot data editing, such as image inpainting, colorization, and super-resolution, without requiring explicit training on these tasks. Consistency models can be trained either by distilling pre-trained diffusion models, or as standalone generative models altogether. Through extensive experiments, we demonstrate that they outperform existing distillation techniques for diffusion models in one- and few-step sampling, achieving the new state-of-the-art FID of 3.55 on CIFAR-10 and 6.20 on ImageNet 64x64 for one-step generation. When trained in isolation, consistency models become a new family of generative models that can outperform existing one-step, non-adversarial generative models on standard benchmarks such as CIFAR-10, ImageNet 64x64 and LSUN 256x256.
The original codebase can be found at , and additional checkpoints are available at .
The pipeline was contributed by and . ❤️
For an additional speed-up, use torch.compile
to generate multiple images in <1 second:
Copied
( unet: UNet2DModelscheduler: CMStochasticIterativeScheduler )
Parameters
Pipeline for unconditional or class-conditional image generation.
__call__
Parameters
batch_size (int
, optional, defaults to 1) — The number of images to generate.
class_labels (torch.Tensor
or List[int]
or int
, optional) — Optional class labels for conditioning class-conditional consistency models. Not used if the model is not class-conditional.
num_inference_steps (int
, optional, defaults to 1) — The number of denoising steps. More denoising steps usually lead to a higher quality image at the expense of slower inference.
timesteps (List[int]
, optional) — Custom timesteps to use for the denoising process. If not defined, equal spaced num_inference_steps
timesteps are used. Must be in descending order.
latents (torch.FloatTensor
, optional) — Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image generation. Can be used to tweak the same generation with different prompts. If not provided, a latents tensor is generated by sampling using the supplied random generator
.
output_type (str
, optional, defaults to "pil"
) — The output format of the generated image. Choose between PIL.Image
or np.array
.
callback (Callable
, optional) — A function that calls every callback_steps
steps during inference. The function is called with the following arguments: callback(step: int, timestep: int, latents: torch.FloatTensor)
.
callback_steps (int
, optional, defaults to 1) — The frequency at which the callback
function is called. If not specified, the callback is called at every step.
Returns
Examples:
Copied
( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] )
Parameters
images (List[PIL.Image.Image]
or np.ndarray
) — List of denoised PIL images of length batch_size
or NumPy array of shape (batch_size, height, width, num_channels)
.
Output class for image pipelines.
unet () — A UNet2DModel
to denoise the encoded image latents.
scheduler () — A scheduler to be used in combination with unet
to denoise the encoded image latents. Currently only compatible with .
This model inherits from . Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).
( batch_size: int = 1class_labels: typing.Union[torch.Tensor, typing.List[int], int, NoneType] = Nonenum_inference_steps: int = 1timesteps: typing.List[int] = Nonegenerator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = Nonelatents: typing.Optional[torch.FloatTensor] = Noneoutput_type: typing.Optional[str] = 'pil'return_dict: bool = Truecallback: typing.Union[typing.Callable[[int, int, torch.FloatTensor], NoneType], NoneType] = Nonecallback_steps: int = 1 ) → or tuple
generator (torch.Generator
, optional) — A to make generation deterministic.
return_dict (bool
, optional, defaults to True
) — Whether or not to return a instead of a plain tuple.
or tuple
If return_dict
is True
, is returned, otherwise a tuple
is returned where the first element is a list with the generated images.