Diffusers 文档
潜在一致性模型
并获得增强的文档体验
开始使用
潜在一致性模型
潜在一致性模型 (LCM) 是由 Simian Luo、Yiqin Tan、Longbo Huang、Jian Li 和 Hang Zhao 在《潜在一致性模型:用少量步骤推理合成高分辨率图像》中提出的。
论文摘要如下:
潜在扩散模型 (LDM) 在合成高分辨率图像方面取得了显著成果。然而,迭代采样过程计算量大,导致生成速度慢。受一致性模型 (song et al.) 的启发,我们提出了潜在一致性模型 (LCM),可以在任何预训练 LDM(包括 Stable Diffusion (rombach et al))上实现以最少步骤进行快速推理。将引导反向扩散过程视为求解增强概率流 ODE (PF-ODE),LCM 旨在直接预测潜在空间中此类 ODE 的解,从而减少大量迭代的需要,并实现快速、高保真采样。从预训练的无分类器引导扩散模型中高效提取,一个高质量的 768 x 768 2~4 步 LCM 仅需 32 A100 GPU 小时进行训练。此外,我们引入了潜在一致性微调 (LCF),这是一种专为在定制图像数据集上微调 LCM 而量身定制的新颖方法。在 LAION-5B-Aesthetics 数据集上的评估表明,LCM 在少量步骤推理下实现了最先进的文本到图像生成性能。项目页面:this https URL。
SimianLuo/LCM_Dreamshaper_v7 检查点的演示可以在此处找到。
该管道由luosiallen、nagolinc 和dg845贡献。
LatentConsistencyModelPipeline
类 diffusers.LatentConsistencyModelPipeline
< 来源 >( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: LCMScheduler safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: typing.Optional[transformers.models.clip.modeling_clip.CLIPVisionModelWithProjection] = None requires_safety_checker: bool = True )
参数
- vae (AutoencoderKL) — 用于将图像编码和解码为潜在表示的变分自动编码器 (VAE) 模型。
- text_encoder (CLIPTextModel) — 冻结的文本编码器(clip-vit-large-patch14)。
- tokenizer (CLIPTokenizer) — 用于对文本进行标记的
CLIPTokenizer
。 - unet (UNet2DConditionModel) — 用于对编码图像潜在表示进行去噪的
UNet2DConditionModel
。 - scheduler (SchedulerMixin) — 与
unet
结合使用,用于对编码图像潜在表示进行去噪的调度器。目前仅支持 LCMScheduler。 - safety_checker (
StableDiffusionSafetyChecker
) — 用于评估生成图像是否可能被视为冒犯性或有害的分类模块。有关模型潜在危害的更多详细信息,请参阅模型卡片。 - feature_extractor (CLIPImageProcessor) — 用于从生成图像中提取特征的
CLIPImageProcessor
;用作safety_checker
的输入。 - requires_safety_checker (
bool
, 可选, 默认为True
) — 管道是否需要安全检查器组件。
用于使用潜在一致性模型进行文本到图像生成的管道。
此模型继承自DiffusionPipeline。有关所有管道实现的通用方法(下载、保存、在特定设备上运行等),请参阅超类文档。
该管道还继承了以下加载方法
- load_textual_inversion() 用于加载文本反演嵌入
- load_lora_weights() 用于加载 LoRA 权重
- save_lora_weights() 用于保存 LoRA 权重
- from_single_file() 用于加载
.ckpt
文件 - load_ip_adapter() 用于加载 IP 适配器
__call__
< 来源 >( prompt: typing.Union[str, typing.List[str]] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 4 original_inference_steps: int = None timesteps: typing.List[int] = None guidance_scale: float = 8.5 num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None ip_adapter_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor], NoneType] = None ip_adapter_image_embeds: typing.Optional[typing.List[torch.Tensor]] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None clip_skip: typing.Optional[int] = None callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) → StableDiffusionPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词或提示词列表。如果未定义,则需要传入prompt_embeds
。 - height (
int
, 可选, 默认为self.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的高度(像素)。 - width (
int
, 可选, 默认为self.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的宽度(像素)。 - num_inference_steps (
int
, 可选, 默认为 50) — 去噪步骤数。更多的去噪步骤通常会带来更高质量的图像,但推理速度会变慢。 - original_inference_steps (
int
, 可选) — 用于生成线性间隔时间步长的原始推理步骤数,我们将从中均匀间隔抽取num_inference_steps
个时间步长作为最终的时间步长调度,遵循论文中的跳步方法(参见 4.3 节)。如果未设置,则默认为调度器的original_inference_steps
属性。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步长。如果未定义,将使用原始 LCM 训练/蒸馏时间步长调度中均匀间隔的num_inference_steps
个时间步长。必须按降序排列。 - guidance_scale (
float
, 可选, 默认为 7.5) — 较高的引导比例值鼓励模型生成与文本prompt
紧密相关的图像,但会以较低的图像质量为代价。当guidance_scale > 1
时启用引导比例。请注意,原始潜在一致性模型论文使用不同的 CFG 公式,其中引导比例减少 1(因此在论文公式中,当guidance_scale > 0
时启用 CFG)。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示词生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 用于使生成具有确定性的torch.Generator
。 - latents (
torch.Tensor
, 可选) — 从高斯分布采样的预生成噪声潜在表示,用作图像生成的输入。可用于使用不同的提示词调整相同的生成。如果未提供,则使用提供的随机generator
进行采样生成一个潜在张量。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示词加权)。如果未提供,则从prompt
输入参数生成文本嵌入。 - ip_adapter_image — (
PipelineImageInput
, 可选): 用于 IP 适配器的可选图像输入。 - ip_adapter_image_embeds (
List[torch.Tensor]
, 可选) — 预生成的用于 IP-Adapter 的图像嵌入。它应该是一个长度与 IP 适配器数量相同的列表。每个元素应该是一个形状为(batch_size, num_images, emb_dim)
的张量。如果do_classifier_free_guidance
设置为True
,它应该包含负图像嵌入。如果未提供,则从ip_adapter_image
输入参数计算嵌入。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,则传递给self.processor
中定义的AttentionProcessor
。 - clip_skip (
int
, 可选) — 计算提示词嵌入时要跳过的 CLIP 层数。值为 1 表示将使用倒数第二层的输出计算提示词嵌入。 - callback_on_step_end (
Callable
, 可选) — 在推理过程中每个去噪步骤结束时调用的函数。该函数将使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类的._callback_tensor_inputs
属性中列出的变量。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 StableDiffusionPipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表,第二个元素是布尔值列表,指示相应的生成图像是否包含“不适合工作”(nsfw) 内容。
用于生成的管道的调用函数。
示例
>>> from diffusers import DiffusionPipeline
>>> import torch
>>> pipe = DiffusionPipeline.from_pretrained("SimianLuo/LCM_Dreamshaper_v7")
>>> # To save GPU memory, torch.float16 can be used, but it may compromise image quality.
>>> pipe.to(torch_device="cuda", torch_dtype=torch.float32)
>>> prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"
>>> # Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps.
>>> num_inference_steps = 4
>>> images = pipe(prompt=prompt, num_inference_steps=num_inference_steps, guidance_scale=8.0).images
>>> images[0].save("image.png")
启用 FreeU
< source >( s1: float s2: float b1: float b2: float )
启用FreeU机制,详见 https://huggingface.ac.cn/papers/2309.11497。
缩放因子后面的后缀表示它们应用的阶段。
有关Stable Diffusion v1、v2和Stable Diffusion XL等不同管道的已知良好值组合,请参阅官方仓库。
如果FreeU机制已启用,则禁用它。
启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。
禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing
,此方法将返回一步计算解码。
启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。
禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling
,此方法将恢复一步计算解码。
encode_prompt
< source >( prompt: str 或 List[str] device: torch.device num_images_per_prompt: int do_classifier_free_guidance: bool negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )
参数
- prompt (
str
或List[str]
, 可选) — 要编码的提示词 - device — (
torch.device
):torch 设备 - num_images_per_prompt (
int
) — 每个提示词应生成的图像数量 - do_classifier_free_guidance (
bool
) — 是否使用分类器无关引导 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示词。如果未定义,则必须传入negative_prompt_embeds
。当不使用引导时(即,如果guidance_scale
小于1
时),此参数将被忽略。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,将根据prompt
输入参数生成文本嵌入。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,负文本嵌入将根据negative_prompt
输入参数生成。 - lora_scale (
float
, 可选) — 如果加载了LoRA层,则应用于文本编码器所有LoRA层的LoRA缩放因子。 - clip_skip (
int
, 可选) — 计算提示词嵌入时要跳过CLIP的层数。值为1表示将使用倒数第二层的输出计算提示词嵌入。
将提示编码为文本编码器隐藏状态。
get_guidance_scale_embedding
< source >( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
LatentConsistencyModelImg2ImgPipeline
class diffusers.LatentConsistencyModelImg2ImgPipeline
< source >( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: LCMScheduler safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: typing.Optional[transformers.models.clip.modeling_clip.CLIPVisionModelWithProjection] = None requires_safety_checker: bool = True )
参数
- vae (AutoencoderKL) — 变分自编码器(VAE)模型,用于将图像编码和解码为潜在表示。
- text_encoder (CLIPTextModel) — 冻结的文本编码器(clip-vit-large-patch14)。
- tokenizer (CLIPTokenizer) — 一个用于标记化文本的
CLIPTokenizer
。 - unet (UNet2DConditionModel) — 一个
UNet2DConditionModel
模型,用于对编码图像的潜在表示进行去噪。 - scheduler (SchedulerMixin) — 一个与
unet
结合使用的调度器,用于对编码图像的潜在表示进行去噪。目前仅支持 LCMScheduler。 - safety_checker (
StableDiffusionSafetyChecker
) — 用于评估生成图像是否可能具有冒犯性或有害性的分类模块。有关模型潜在危害的更多详细信息,请参阅模型卡。 - feature_extractor (CLIPImageProcessor) — 一个用于从生成的图像中提取特征的
CLIPImageProcessor
;用作safety_checker
的输入。 - requires_safety_checker (
bool
, 可选, 默认为True
) — 管道是否需要安全检查器组件。
用于图像到图像生成的潜在一致性模型管道。
此模型继承自DiffusionPipeline。有关所有管道实现的通用方法(下载、保存、在特定设备上运行等),请参阅超类文档。
该管道还继承了以下加载方法
- load_textual_inversion() 用于加载文本反演嵌入
- load_lora_weights() 用于加载 LoRA 权重
- save_lora_weights() 用于保存 LoRA 权重
- from_single_file() 用于加载
.ckpt
文件 - load_ip_adapter() 用于加载 IP 适配器
__call__
< source >( prompt: typing.Union[str, typing.List[str]] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None num_inference_steps: int = 4 strength: float = 0.8 original_inference_steps: int = None timesteps: typing.List[int] = None guidance_scale: float = 8.5 num_images_per_prompt: typing.Optional[int] = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None prompt_embeds: typing.Optional[torch.Tensor] = None ip_adapter_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor], NoneType] = None ip_adapter_image_embeds: typing.Optional[typing.List[torch.Tensor]] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None clip_skip: typing.Optional[int] = None callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs: Any ) → StableDiffusionPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
, 可选) — 用于引导图像生成的提示词。如果未定义,需要传入prompt_embeds
。 - height (
int
, 可选, 默认为self.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的像素高度。 - width (
int
, 可选, 默认为self.unet.config.sample_size * self.vae_scale_factor
) — 生成图像的像素宽度。 - num_inference_steps (
int
, 可选, 默认为50) — 去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度会变慢。 - original_inference_steps (
int
, 可选) — 用于生成线性间隔时间步计划的原始推理步数,我们将从中抽取num_inference_steps
个均匀间隔的时间步作为最终时间步计划,遵循论文中的跳过步长法(参见第4.3节)。如果未设置,这将默认为调度器的original_inference_steps
属性。 - timesteps (
List[int]
, 可选) — 用于去噪过程的自定义时间步。如果未定义,则使用原始LCM训练/蒸馏时间步计划上等距的num_inference_steps
时间步。必须按降序排列。 - guidance_scale (
float
, 可选, 默认为7.5) — 更高的引导尺度值鼓励模型生成与文本prompt
紧密相关的图像,但图像质量会降低。当guidance_scale > 1
时启用引导尺度。请注意,原始潜在一致性模型论文中使用了不同的CFG公式,其中引导尺度减少了1(因此在论文公式中,当guidance_scale > 0
时启用CFG)。 - num_images_per_prompt (
int
, 可选, 默认为1) — 每个提示词要生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个torch.Generator
,用于使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 从高斯分布中采样的预生成噪声潜在表示,用作图像生成的输入。可用于使用不同提示词调整相同的生成。如果未提供,则使用提供的随机generator
进行采样生成一个潜在表示张量。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(提示词权重)。如果未提供,文本嵌入将根据prompt
输入参数生成。 - ip_adapter_image — (
PipelineImageInput
, 可选):与IP适配器一起使用的可选图像输入。 - ip_adapter_image_embeds (
List[torch.Tensor]
, 可选) — IP-Adapter的预生成图像嵌入。它应该是一个长度与IP适配器数量相同的列表。每个元素都应该是一个形状为(batch_size, num_images, emb_dim)
的张量。如果do_classifier_free_guidance
设置为True
,它应该包含负图像嵌入。如果未提供,嵌入将从ip_adapter_image
输入参数计算。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在PIL.Image
或np.array
之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。 - cross_attention_kwargs (
dict
, 可选) — 一个 kwargs 字典,如果指定,将传递给self.processor
中定义的AttentionProcessor
。 - clip_skip (
int
, 可选) — 计算提示词嵌入时要跳过CLIP的层数。值为1表示将使用倒数第二层的输出计算提示词嵌入。 - callback_on_step_end (
Callable
, 可选) — 在推理过程中每个去噪步骤结束时调用的函数。该函数将使用以下参数调用:callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
。callback_kwargs
将包含callback_on_step_end_tensor_inputs
指定的所有张量列表。 - callback_on_step_end_tensor_inputs (
List
, 可选) —callback_on_step_end
函数的张量输入列表。列表中指定的张量将作为callback_kwargs
参数传递。您只能包含管道类._callback_tensor_inputs
属性中列出的变量。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 StableDiffusionPipelineOutput,否则返回一个 tuple
,其中第一个元素是生成的图像列表,第二个元素是布尔值列表,指示相应的生成图像是否包含“不适合工作”(nsfw) 内容。
用于生成的管道的调用函数。
示例
>>> from diffusers import AutoPipelineForImage2Image
>>> import torch
>>> import PIL
>>> pipe = AutoPipelineForImage2Image.from_pretrained("SimianLuo/LCM_Dreamshaper_v7")
>>> # To save GPU memory, torch.float16 can be used, but it may compromise image quality.
>>> pipe.to(torch_device="cuda", torch_dtype=torch.float32)
>>> prompt = "High altitude snowy mountains"
>>> image = PIL.Image.open("./snowy_mountains.png")
>>> # Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps.
>>> num_inference_steps = 4
>>> images = pipe(
... prompt=prompt, image=image, num_inference_steps=num_inference_steps, guidance_scale=8.0
... ).images
>>> images[0].save("image.png")
启用 FreeU
< source >( s1: float s2: float b1: float b2: float )
启用FreeU机制,详见 https://huggingface.ac.cn/papers/2309.11497。
缩放因子后面的后缀表示它们应用的阶段。
有关Stable Diffusion v1、v2和Stable Diffusion XL等不同管道的已知良好值组合,请参阅官方仓库。
如果FreeU机制已启用,则禁用它。
启用切片 VAE 解码。启用此选项后,VAE 会将输入张量分片,分步计算解码。这有助于节省一些内存并允许更大的批次大小。
禁用切片 VAE 解码。如果之前启用了 enable_vae_slicing
,此方法将返回一步计算解码。
启用平铺 VAE 解码。启用此选项后,VAE 将把输入张量分割成瓦片,分多步计算编码和解码。这对于节省大量内存和处理更大的图像非常有用。
禁用平铺 VAE 解码。如果之前启用了 enable_vae_tiling
,此方法将恢复一步计算解码。
encode_prompt
< source >( prompt: str 或 List[str] device: torch.device num_images_per_prompt: int do_classifier_free_guidance: bool negative_prompt = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )
参数
- prompt (
str
或List[str]
, 可选) — 待编码的提示词 - device — (
torch.device
): torch 设备 - num_images_per_prompt (
int
) — 每个提示词应生成的图像数量 - do_classifier_free_guidance (
bool
) — 是否使用分类器自由引导 - negative_prompt (
str
或List[str]
, 可选) — 不用于引导图像生成的提示词。如果未定义,则必须传递negative_prompt_embeds
。在不使用引导时忽略(即,如果guidance_scale
小于1
则忽略)。 - prompt_embeds (
torch.Tensor
, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,文本嵌入将从prompt
输入参数生成。 - negative_prompt_embeds (
torch.Tensor
, 可选) — 预生成的负向文本嵌入。可用于轻松调整文本输入,例如提示词权重。如果未提供,negative_prompt_embeds
将从negative_prompt
输入参数生成。 - lora_scale (
float
, 可选) — 将应用于文本编码器所有 LoRA 层的 LoRA 缩放因子(如果已加载 LoRA 层)。 - clip_skip (
int
, 可选) — 从 CLIP 跳过的层数,用于计算提示词嵌入。值为 1 表示将使用倒数第二层的输出计算提示词嵌入。
将提示编码为文本编码器隐藏状态。
get_guidance_scale_embedding
< source >( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
StableDiffusionPipelineOutput
class diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput
< source >( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )
Stable Diffusion 管道的输出类。