Diffusers 文档

潜在一致性模型

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

潜在一致性模型

潜在一致性模型 (LCMs) 在 Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference 这篇论文中被提出,作者是 Simian Luo, Yiqin Tan, Longbo Huang, Jian Li, 和 Hang Zhao。

该论文的摘要如下:

潜在扩散模型 (LDMs) 在合成高分辨率图像方面取得了显著成果。然而,迭代采样过程计算密集,导致生成速度缓慢。受到一致性模型(Consistency Models)(song et al.)的启发,我们提出了潜在一致性模型 (LCMs),可以在任何预训练的 LDM(包括 Stable Diffusion (rombach et al))上实现最少步骤的快速推理。将引导式逆扩散过程视为求解增强概率流 ODE (PF-ODE),LCMs 被设计为直接预测潜在空间中此类 ODE 的解,从而减少了对大量迭代的需求,并允许快速、高保真度的采样。从预训练的无分类器引导扩散模型中高效提炼,高质量的 768 x 768 2~4 步 LCM 仅需 32 个 A100 GPU 小时进行训练。此外,我们还介绍了潜在一致性微调 (LCF),这是一种为在定制图像数据集上微调 LCM 而量身定制的新方法。在 LAION-5B-Aesthetics 数据集上的评估表明,LCM 在少步推理的情况下实现了最先进的文本到图像生成性能。项目页面:this https URL

可以在 SimianLuo/LCM_Dreamshaper_v7 检查点的演示中找到 这里

这些 pipelines 由 luosiallen, nagolinc, 和 dg845 贡献。

LatentConsistencyModelPipeline

class 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, optional, defaults to True) — 管道是否需要安全检查器组件。

使用潜在一致性模型进行文本到图像生成的管道。

此模型继承自 DiffusionPipeline。查看超类文档,了解为所有管道实现的通用方法(下载、保存、在特定设备上运行等)。

该管道还继承了以下加载方法

__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 ) StableDiffusionPipelineOutputtuple

参数

  • prompt (strList[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) — 更高的 guidance scale 值会鼓励模型生成与文本 prompt 紧密相关的图像,但会降低图像质量。当 guidance_scale > 1 时,guidance scale 生效。请注意,原始的潜在一致性模型论文使用了不同的 CFG 公式,其中 guidance scale 减小了 1(因此在论文公式中,当 guidance_scale > 0 时,CFG 生效)。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成具有确定性的 torch.Generator
  • latents (torch.Tensor, 可选) — 从高斯分布中采样的预生成噪声潜在变量,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则通过使用提供的随机 generator 进行采样来生成潜在张量。
  • prompt_embeds (torch.Tensor, optional) — 预生成的文本嵌入 (text embeddings)。 可以用于轻松调整文本输入(提示词权重)。 如果未提供,则会从 prompt 输入参数生成文本嵌入。
  • ip_adapter_image — (PipelineImageInput, optional): 可选的图像输入,用于 IP Adapters。
  • ip_adapter_image_embeds (List[torch.Tensor], optional) — IP-Adapter 的预生成图像嵌入。 它应该是一个列表,长度与 IP-adapters 的数量相同。 每个元素都应该是一个形状为 (batch_size, num_images, emb_dim) 的张量。 如果 do_classifier_free_guidance 设置为 True,则应包含负图像嵌入。 如果未提供,则会从 ip_adapter_image 输入参数计算嵌入。
  • output_type (str, optional, defaults to "pil") — 生成图像的输出格式。 在 PIL.Imagenp.array 之间选择。
  • return_dict (bool, optional, defaults to True) — 是否返回 StableDiffusionPipelineOutput 而不是普通元组。
  • cross_attention_kwargs (dict, optional) — 如果指定,则传递给 self.processor 中定义的 AttentionProcessor 的 kwargs 字典。
  • clip_skip (int, optional) — 在计算提示词嵌入时,要从 CLIP 跳过的层数。 值为 1 表示预最终层的输出将用于计算提示词嵌入。
  • callback_on_step_end (Callable, optional) — 在推理期间的每个去噪步骤结束时调用的函数。 该函数使用以下参数调用: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, optional) — callback_on_step_end 函数的张量输入列表。 列表中指定的张量将作为 callback_kwargs 参数传递。 您将只能包含在管道类的 ._callback_tensor_inputs 属性中列出的变量。

返回

StableDiffusionPipelineOutputtuple

如果 return_dictTrue,则返回 StableDiffusionPipelineOutput,否则返回 tuple,其中第一个元素是包含生成图像的列表,第二个元素是 bool 列表,指示相应的生成图像是否包含“不适合工作场所观看”(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")

enable_freeu

< >

( s1: float s2: float b1: float b2: float )

参数

  • s1 (float) — 阶段 1 的缩放因子,用于衰减跳过特征的贡献。 这样做是为了减轻增强去噪过程中的“过度平滑效应”。
  • s2 (float) — 阶段 2 的缩放因子,用于衰减跳过特征的贡献。 这样做是为了减轻增强去噪过程中的“过度平滑效应”。
  • b1 (float) — 阶段 1 的缩放因子,用于放大骨干特征的贡献。
  • b2 (float) — 阶段 2 的缩放因子,用于放大骨干特征的贡献。

启用 FreeU 机制,如 https://arxiv.org/abs/2309.11497 中所述。

缩放因子后的后缀表示应用它们的阶段。

请参阅 官方存储库,了解已知适用于不同管道(如 Stable Diffusion v1、v2 和 Stable Diffusion XL)的值组合。

disable_freeu

< >

( )

如果已启用,则禁用 FreeU 机制。

enable_vae_slicing

< >

( )

启用切片 VAE 解码。 启用此选项后,VAE 将输入张量拆分为切片,以分步计算解码。 这对于节省一些内存并允许更大的批量大小很有用。

disable_vae_slicing

< >

( )

禁用切片 VAE 解码。 如果之前启用了 enable_vae_slicing,则此方法将恢复为一步计算解码。

enable_vae_tiling

< >

( )

启用平铺 VAE 解码。 启用此选项后,VAE 会将输入张量拆分为平铺,以分步计算解码和编码。 这对于节省大量内存并允许处理更大的图像很有用。

disable_vae_tiling

< >

( )

禁用平铺 VAE 解码。 如果之前启用了 enable_vae_tiling,则此方法将恢复为一步计算解码。

encode_prompt

< >

( prompt device num_images_per_prompt do_classifier_free_guidance 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 or List[str], optional) — 要编码的提示词
  • device — (torch.device): torch 设备
  • num_images_per_prompt (int) — 每个提示应生成的图像数量
  • do_classifier_free_guidance (bool) — 是否使用无分类器引导(classifier free guidance)。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示或提示列表。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • lora_scale (float, 可选) — 如果加载了 LoRA 层,则将应用于文本编码器所有 LoRA 层的 LoRA 缩放比例。
  • clip_skip (int, 可选) — 从 CLIP 跳过的层数,用于计算提示嵌入。值为 1 表示预最终层的输出将用于计算提示嵌入。

将提示编码为文本编码器隐藏状态。

get_guidance_scale_embedding

< >

( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) torch.Tensor

参数

  • w (torch.Tensor) — 生成具有指定引导比例的嵌入向量,以便后续丰富时间步长嵌入。
  • embedding_dim (int, 可选, 默认为 512) — 要生成的嵌入的维度。
  • dtype (torch.dtype, 可选, 默认为 torch.float32) — 生成的嵌入的数据类型。

返回

torch.Tensor

形状为 (len(w), embedding_dim) 的嵌入向量。

请参阅 https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298

LatentConsistencyModelImg2ImgPipeline

class diffusers.LatentConsistencyModelImg2ImgPipeline

< >

( 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。查看超类文档,了解为所有管道实现的通用方法(下载、保存、在特定设备上运行等)。

该管道还继承了以下加载方法

__call__

< >

( 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 ) StableDiffusionPipelineOutputtuple

参数

  • prompt (strList[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.GeneratorList[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.Imagenp.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 属性中列出的变量。

返回

StableDiffusionPipelineOutputtuple

如果 return_dictTrue,则返回 StableDiffusionPipelineOutput,否则返回 tuple,其中第一个元素是包含生成图像的列表,第二个元素是 bool 列表,指示相应的生成图像是否包含“不适合工作场所观看”(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")

enable_freeu

< >

( s1: float s2: float b1: float b2: float )

参数

  • s1 (float) — 阶段 1 的缩放因子,用于衰减跳跃连接特征的贡献。这样做是为了减轻增强去噪过程中的“过度平滑效应”。
  • s2 (float) — 阶段 2 的缩放因子,用于衰减跳跃连接特征的贡献。这样做是为了减轻增强去噪过程中的“过度平滑效应”。
  • b1 (float) — 阶段 1 的缩放因子,用于放大骨干网络特征的贡献。
  • b2 (float) — 阶段 2 的缩放因子,用于放大骨干网络特征的贡献。

启用 FreeU 机制,如 https://arxiv.org/abs/2309.11497 中所述。

缩放因子后的后缀表示应用它们的阶段。

请参阅 官方存储库,了解已知适用于不同管道(如 Stable Diffusion v1、v2 和 Stable Diffusion XL)的值组合。

disable_freeu

< >

( )

如果已启用,则禁用 FreeU 机制。

enable_vae_slicing

< >

( )

启用切片 VAE 解码。 启用此选项后,VAE 将输入张量拆分为切片,以分步计算解码。 这对于节省一些内存并允许更大的批量大小很有用。

disable_vae_slicing

< >

( )

禁用切片 VAE 解码。 如果之前启用了 enable_vae_slicing,则此方法将恢复为一步计算解码。

enable_vae_tiling

< >

( )

启用平铺 VAE 解码。 启用此选项后,VAE 会将输入张量拆分为平铺,以分步计算解码和编码。 这对于节省大量内存并允许处理更大的图像很有用。

disable_vae_tiling

< >

( )

禁用平铺 VAE 解码。 如果之前启用了 enable_vae_tiling,则此方法将恢复为一步计算解码。

encode_prompt

< >

( prompt device num_images_per_prompt do_classifier_free_guidance 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 (strList[str], 可选) — 要编码的提示
  • device — (torch.device): torch 设备
  • num_images_per_prompt (int) — 每个提示应生成的图像数量
  • do_classifier_free_guidance (bool) — 是否使用无分类器引导
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的提示。如果未定义,则必须传递 negative_prompt_embeds。当不使用引导时忽略(即,如果 guidance_scale 小于 1 则忽略)。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 negative_prompt 输入参数生成 `negative_prompt_embeds`。
  • lora_scale (float, 可选) — 如果加载了 LoRA 层,则将应用于文本编码器所有 LoRA 层的 LoRA 比例。
  • clip_skip (int, 可选) — 在计算提示嵌入时,从 CLIP 跳过的层数。值为 1 表示将使用倒数第二层的输出计算提示嵌入。

将提示编码为文本编码器隐藏状态。

get_guidance_scale_embedding

< >

( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) torch.Tensor

参数

  • w (torch.Tensor) — 生成具有指定引导尺度的嵌入向量,以随后丰富时间步嵌入。
  • embedding_dim (int, 可选, 默认为 512) — 要生成的嵌入的维度。
  • dtype (torch.dtype, 可选, 默认为 torch.float32) — 生成的嵌入的数据类型。

返回

torch.Tensor

形状为 (len(w), embedding_dim) 的嵌入向量。

请参阅 https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298

StableDiffusionPipelineOutput

class diffusers.pipelines.stable_diffusion.StableDiffusionPipelineOutput

< >

( images: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )

参数

  • images (List[PIL.Image.Image]np.ndarray) — 长度为 batch_size 的去噪 PIL 图像列表,或形状为 (batch_size, height, width, num_channels) 的 NumPy 数组。
  • nsfw_content_detected (List[bool]) — 列表,指示相应的生成图像是否包含“不适合工作场所”(nsfw)内容;如果无法执行安全检查,则为 None

Stable Diffusion 管道的输出类。

< > 在 GitHub 上更新