Diffusers 文档

Stable Cascade

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Stable Cascade

此模型基于 Würstchen 架构构建,它与其他模型(如 Stable Diffusion)的主要区别在于它在更小的潜在空间中工作。为什么这很重要?潜在空间越小,您可以运行推理的速度就越快,并且训练变得越便宜。潜在空间有多小?Stable Diffusion 使用 8 的压缩因子,导致 1024x1024 图像被编码为 128x128。Stable Cascade 实现了 42 的压缩因子,这意味着可以将 1024x1024 图像编码为 24x24,同时保持清晰的重建。然后,在高度压缩的潜在空间中训练文本条件模型。此架构的先前版本实现了比 Stable Diffusion 1.5 低 16 倍的成本降低。

因此,这种模型非常适合效率很重要的用例。此外,所有已知的扩展,如微调、LoRA、ControlNet、IP-Adapter、LCM 等,也都可以使用此方法。

原始代码库可以在 Stability-AI/StableCascade 找到。

模型概述

Stable Cascade 由三个模型组成:Stage A、Stage B 和 Stage C,代表生成图像的级联,因此得名“Stable Cascade”。

Stage A 和 Stage B 用于压缩图像,类似于 Stable Diffusion 中 VAE 的工作。但是,使用此设置,可以实现更高的图像压缩率。虽然 Stable Diffusion 模型使用 8 的空间压缩因子,将 1024 x 1024 分辨率的图像编码为 128 x 128,但 Stable Cascade 实现了 42 的压缩因子。这可以将 1024 x 1024 图像编码为 24 x 24,同时能够准确解码图像。这带来了更便宜的训练和推理的巨大好处。此外,Stage C 负责生成给定文本提示的 24 x 24 小潜在空间。

Stage C 模型在 24 x 24 小潜在空间上运行,并对以文本提示为条件的潜在空间进行去噪。该模型也是 Cascade pipeline 中最大的组件,旨在与 StableCascadePriorPipeline 一起使用

Stage B 和 Stage A 模型与 StableCascadeDecoderPipeline 一起使用,负责生成给定 24 x 24 小潜在空间的最终图像。

可以与 Stable Cascade 模型一起使用的数据类型有一些限制。StableCascadePriorPipeline 的官方检查点不支持 torch.float16 数据类型。请改用 torch.bfloat16

为了将 torch.bfloat16 数据类型与 StableCascadeDecoderPipeline 一起使用,您需要安装 PyTorch 2.2.0 或更高版本。 这也意味着将 StableCascadeCombinedPipelinetorch.bfloat16 一起使用也需要 PyTorch 2.2.0 或更高版本,因为它在内部调用了 StableCascadeDecoderPipeline

如果您的环境中无法安装 PyTorch 2.2.0 或更高版本,则 StableCascadeDecoderPipeline 可以单独与 torch.float16 数据类型一起使用。 您可以下载完整精度或 bf16 变体权重的 pipeline,并将权重转换为 torch.float16

使用示例

import torch
from diffusers import StableCascadeDecoderPipeline, StableCascadePriorPipeline

prompt = "an image of a shiba inu, donning a spacesuit and helmet"
negative_prompt = ""

prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", variant="bf16", torch_dtype=torch.bfloat16)
decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", variant="bf16", torch_dtype=torch.float16)

prior.enable_model_cpu_offload()
prior_output = prior(
    prompt=prompt,
    height=1024,
    width=1024,
    negative_prompt=negative_prompt,
    guidance_scale=4.0,
    num_images_per_prompt=1,
    num_inference_steps=20
)

decoder.enable_model_cpu_offload()
decoder_output = decoder(
    image_embeddings=prior_output.image_embeddings.to(torch.float16),
    prompt=prompt,
    negative_prompt=negative_prompt,
    guidance_scale=0.0,
    output_type="pil",
    num_inference_steps=10
).images[0]
decoder_output.save("cascade.png")

使用 Stage B 和 Stage C 模型的 Lite 版本

import torch
from diffusers import (
    StableCascadeDecoderPipeline,
    StableCascadePriorPipeline,
    StableCascadeUNet,
)

prompt = "an image of a shiba inu, donning a spacesuit and helmet"
negative_prompt = ""

prior_unet = StableCascadeUNet.from_pretrained("stabilityai/stable-cascade-prior", subfolder="prior_lite")
decoder_unet = StableCascadeUNet.from_pretrained("stabilityai/stable-cascade", subfolder="decoder_lite")

prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", prior=prior_unet)
decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", decoder=decoder_unet)

prior.enable_model_cpu_offload()
prior_output = prior(
    prompt=prompt,
    height=1024,
    width=1024,
    negative_prompt=negative_prompt,
    guidance_scale=4.0,
    num_images_per_prompt=1,
    num_inference_steps=20
)

decoder.enable_model_cpu_offload()
decoder_output = decoder(
    image_embeddings=prior_output.image_embeddings,
    prompt=prompt,
    negative_prompt=negative_prompt,
    guidance_scale=0.0,
    output_type="pil",
    num_inference_steps=10
).images[0]
decoder_output.save("cascade.png")

使用 from_single_file 加载原始检查点

StableCascadeUNet 中的 from_single_file 方法支持加载原始格式的检查点。

import torch
from diffusers import (
    StableCascadeDecoderPipeline,
    StableCascadePriorPipeline,
    StableCascadeUNet,
)

prompt = "an image of a shiba inu, donning a spacesuit and helmet"
negative_prompt = ""

prior_unet = StableCascadeUNet.from_single_file(
    "https://huggingface.co/stabilityai/stable-cascade/resolve/main/stage_c_bf16.safetensors",
    torch_dtype=torch.bfloat16
)
decoder_unet = StableCascadeUNet.from_single_file(
    "https://huggingface.co/stabilityai/stable-cascade/blob/main/stage_b_bf16.safetensors",
    torch_dtype=torch.bfloat16
)

prior = StableCascadePriorPipeline.from_pretrained("stabilityai/stable-cascade-prior", prior=prior_unet, torch_dtype=torch.bfloat16)
decoder = StableCascadeDecoderPipeline.from_pretrained("stabilityai/stable-cascade", decoder=decoder_unet, torch_dtype=torch.bfloat16)

prior.enable_model_cpu_offload()
prior_output = prior(
    prompt=prompt,
    height=1024,
    width=1024,
    negative_prompt=negative_prompt,
    guidance_scale=4.0,
    num_images_per_prompt=1,
    num_inference_steps=20
)

decoder.enable_model_cpu_offload()
decoder_output = decoder(
    image_embeddings=prior_output.image_embeddings,
    prompt=prompt,
    negative_prompt=negative_prompt,
    guidance_scale=0.0,
    output_type="pil",
    num_inference_steps=10
).images[0]
decoder_output.save("cascade-single-file.png")

用途

直接使用

该模型目前仅用于研究目的。 可能的研究领域和任务包括

  • 生成模型的研究。
  • 对可能生成有害内容的模型进行安全部署。
  • 探测和理解生成模型的局限性和偏差。
  • 艺术品的生成以及在设计和其他艺术过程中的使用。
  • 在教育或创意工具中的应用。

排除的用途如下所述。

超出范围的使用

该模型并非旨在真实或准确地表示人物或事件,因此,使用该模型生成此类内容超出了该模型的能力范围。 该模型不应用于任何违反 Stability AI 可接受使用政策 的方式。

局限性和偏见

局限性

  • 可能无法正确生成面部和人物。
  • 模型的自动编码部分是有损的。

StableCascadeCombinedPipeline

class diffusers.StableCascadeCombinedPipeline

< >

( tokenizer: CLIPTokenizer text_encoder: CLIPTextModel decoder: StableCascadeUNet scheduler: DDPMWuerstchenScheduler vqgan: PaellaVQModel prior_prior: StableCascadeUNet prior_text_encoder: CLIPTextModel prior_tokenizer: CLIPTokenizer prior_scheduler: DDPMWuerstchenScheduler prior_feature_extractor: Optional = None prior_image_encoder: Optional = None )

参数

  • tokenizer (CLIPTokenizer) — 用于文本输入的解码器 tokenizer。
  • text_encoder (CLIPTextModel) — 用于文本输入的解码器文本编码器。
  • decoder (StableCascadeUNet) — 用于解码器图像生成 pipeline 的解码器模型。
  • scheduler (DDPMWuerstchenScheduler) — 用于解码器图像生成 pipeline 的 scheduler。
  • vqgan (PaellaVQModel) — 用于解码器图像生成 pipeline 的 VQGAN 模型。
  • feature_extractor (CLIPImageProcessor) — 从生成的图像中提取特征的模型,用作 image_encoder 的输入。
  • image_encoder (CLIPVisionModelWithProjection) — 冻结的 CLIP 图像编码器 (clip-vit-large-patch14)。
  • prior_prior (StableCascadeUNet) — 用于 prior pipeline 的 prior 模型。
  • prior_scheduler (DDPMWuerstchenScheduler) — 用于 prior pipeline 的 scheduler。

用于使用 Stable Cascade 进行文本到图像生成的组合 Pipeline。

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

__call__

< >

( prompt: Union = None images: Union = None height: int = 512 width: int = 512 prior_num_inference_steps: int = 60 prior_guidance_scale: float = 4.0 num_inference_steps: int = 12 decoder_guidance_scale: float = 0.0 negative_prompt: Union = None prompt_embeds: Optional = None prompt_embeds_pooled: Optional = None negative_prompt_embeds: Optional = None negative_prompt_embeds_pooled: Optional = None num_images_per_prompt: int = 1 generator: Union = None latents: Optional = None output_type: Optional = 'pil' return_dict: bool = True prior_callback_on_step_end: Optional = None prior_callback_on_step_end_tensor_inputs: List = ['latents'] callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] )

参数

  • prompt (strList[str]) — 用于引导 prior 和解码器图像生成的 prompt 或 prompts。
  • images (torch.Tensor, PIL.Image.Image, List[torch.Tensor], List[PIL.Image.Image], 可选) — 用于引导 prior 图像生成的图像。
  • negative_prompt (strList[str], 可选) — 不用于引导图像生成的 prompt 或 prompts。 当不使用 guidance 时忽略(即,如果 guidance_scale 小于 1,则忽略)。
  • prompt_embeds (torch.Tensor, 可选) — 先验模型的预生成文本嵌入 (Pre-generated text embeddings for the prior)。 可用于轻松调整文本输入,例如,提示词权重 (prompt weighting)。 如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • prompt_embeds_pooled (torch.Tensor, 可选) — 先验模型的预生成文本嵌入 (Pre-generated text embeddings for the prior)。 可用于轻松调整文本输入,例如,提示词权重 (prompt weighting)。 如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • negative_prompt_embeds (torch.Tensor, 可选) — 先验模型的预生成负面文本嵌入 (Pre-generated negative text embeddings for the prior)。 可用于轻松调整文本输入,例如,提示词权重 (prompt weighting)。 如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • negative_prompt_embeds_pooled (torch.Tensor, 可选) — 先验模型的预生成负面文本嵌入 (Pre-generated negative text embeddings for the prior)。 可用于轻松调整文本输入,例如,提示词权重 (prompt weighting)。 如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个提示词生成的图像数量。
  • height (int, 可选, 默认为 512) — 生成图像的高度,单位为像素。
  • width (int, 可选, 默认为 512) — 生成图像的宽度,单位为像素。
  • prior_guidance_scale (float, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导缩放 (Guidance scale)。 prior_guidance_scale 定义为 Imagen Paper 公式 2 中的 w。 通过设置 prior_guidance_scale > 1 启用引导缩放。 较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • prior_num_inference_steps (Union[int, Dict[float, int]], 可选, 默认为 60) — 先验模型的去噪步骤 (denoising steps) 数量。 更多的去噪步骤通常会带来更高的图像质量,但会牺牲推理速度。 对于更具体的时间步长间隔 (timestep spacing),您可以传递自定义的 prior_timesteps
  • num_inference_steps (int, 可选, 默认为 12) — 解码器去噪步骤 (decoder denoising steps) 数量。 更多的去噪步骤通常会带来更高的图像质量,但会牺牲推理速度。 对于更具体的时间步长间隔 (timestep spacing),您可以传递自定义的 timesteps
  • decoder_guidance_scale (float, 可选, 默认为 0.0) — Classifier-Free Diffusion Guidance 中定义的引导缩放 (Guidance scale)。 guidance_scale 定义为 Imagen Paper 公式 2 中的 w。 通过设置 guidance_scale > 1 启用引导缩放。 较高的引导缩放鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 一个或一组 torch generator(s),用于使生成过程具有确定性 (deterministic)。
  • latents (torch.Tensor, 可选) — 预生成的噪声潜变量 (noisy latents),从高斯分布中采样,用作图像生成的输入。 可用于使用不同的提示词 (prompts) 调整相同的生成结果。 如果未提供,将通过使用提供的随机 generator 采样来生成潜变量张量 (latents tensor)。
  • output_type (str, 可选, 默认为 "pil") — 生成图像的输出格式。 从以下选项中选择: "pil" (PIL.Image.Image), "np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通的元组 (tuple)。
  • prior_callback_on_step_end (Callable, 可选) — 一个函数,在推理 (inference) 期间的每个去噪步骤结束时调用。 该函数使用以下参数调用: prior_callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)
  • prior_callback_on_step_end_tensor_inputs (List, 可选) — prior_callback_on_step_end 函数的张量输入列表。 列表中指定的张量将作为 callback_kwargs 参数传递。 您将只能包含管道类 (pipeline class) 的 ._callback_tensor_inputs 属性中列出的变量。
  • callback_on_step_end (Callable, 可选) — 一个函数,在推理 (inference) 期间的每个去噪步骤结束时调用。 该函数使用以下参数调用: 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 参数传递。 您将只能包含管道类 (pipeline class) 的 ._callback_tensor_inputs 属性中列出的变量。

调用管道进行生成时调用的函数。

示例

>>> import torch
>>> from diffusers import StableCascadeCombinedPipeline

>>> pipe = StableCascadeCombinedPipeline.from_pretrained(
...     "stabilityai/stable-cascade", variant="bf16", torch_dtype=torch.bfloat16
... )
>>> pipe.enable_model_cpu_offload()
>>> prompt = "an image of a shiba inu, donning a spacesuit and helmet"
>>> images = pipe(prompt=prompt)

enable_model_cpu_offload

< >

( gpu_id: Optional = None device: Union = 'cuda' )

使用 accelerate 将所有模型卸载到 CPU,从而减少内存使用量,且对性能的影响很小。 与 enable_sequential_cpu_offload 相比,此方法在调用模型的 forward 方法时,一次将一个完整模型移动到 GPU,并且该模型在 GPU 中保持运行状态,直到下一个模型运行。 内存节省低于 enable_sequential_cpu_offload,但由于 unet 的迭代执行,性能要好得多。

enable_sequential_cpu_offload

< >

( gpu_id: Optional = None device: Union = 'cuda' )

使用 🤗 Accelerate 将所有模型 (unet, text_encoder, vae, 和 safety checker 状态字典) 卸载到 CPU,从而显著减少内存使用量。 模型被移动到 torch.device('meta'),并且仅在其特定子模块的 forward 方法被调用时才加载到 GPU 上。 卸载在子模块级别进行。 内存节省高于使用 enable_model_cpu_offload,但性能较低。

StableCascadePriorPipeline

class diffusers.StableCascadePriorPipeline

< >

( tokenizer: CLIPTokenizer text_encoder: CLIPTextModelWithProjection prior: StableCascadeUNet scheduler: DDPMWuerstchenScheduler resolution_multiple: float = 42.67 feature_extractor: Optional = None image_encoder: Optional = None )

参数

  • prior (StableCascadeUNet) — Stable Cascade 先验模型,用于从文本和/或图像嵌入 (embedding) 中近似图像嵌入。
  • text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器 (laion/CLIP-ViT-bigG-14-laion2B-39B-b160k)。
  • feature_extractor (CLIPImageProcessor) — 从生成的图像中提取特征的模型,用作 image_encoder 的输入。
  • image_encoder (CLIPVisionModelWithProjection) — 冻结的 CLIP 图像编码器 (clip-vit-large-patch14)。
  • tokenizer (CLIPTokenizer) — CLIPTokenizer 类的分词器 (Tokenizer)。
  • 调度器 (DDPMWuerstchenScheduler) — 与 prior 结合使用的调度器,用于生成图像嵌入。
  • resolution_multiple (‘float’, 可选的, 默认为 42.67) — 生成多张图像的默认分辨率倍数。

用于为 Stable Cascade 生成图像先验的 Pipeline。

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

__call__

< >

( prompt: Union = None images: Union = None height: int = 1024 width: int = 1024 num_inference_steps: int = 20 timesteps: List = None guidance_scale: float = 4.0 negative_prompt: Union = None prompt_embeds: Optional = None prompt_embeds_pooled: Optional = None negative_prompt_embeds: Optional = None negative_prompt_embeds_pooled: Optional = None image_embeds: Optional = None num_images_per_prompt: Optional = 1 generator: Union = None latents: Optional = None output_type: Optional = 'pt' return_dict: bool = True callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] )

参数

  • prompt (strList[str]) — 用于引导图像生成的提示或提示列表。
  • height (int, 可选的, 默认为 1024) — 生成图像的高度像素值。
  • width (int, 可选的, 默认为 1024) — 生成图像的宽度像素值。
  • num_inference_steps (int, 可选的, 默认为 60) — 去噪步骤的数量。更多的去噪步骤通常会以较慢的推理速度为代价, menghasilkan 更高质量的图像。
  • guidance_scale (float, 可选的, 默认为 8.0) — 无分类器扩散引导中定义的引导尺度。 decoder_guidance_scale 定义为 Imagen Paper 等式 2 中的 w。通过设置 decoder_guidance_scale > 1 启用引导尺度。更高的引导尺度鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • negative_prompt (strList[str], 可选的) — 不用于引导图像生成的提示或提示列表。当不使用引导时忽略(即,如果 decoder_guidance_scale 小于 1 则忽略)。
  • prompt_embeds (torch.Tensor, 可选的) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从 prompt 输入参数生成文本嵌入。
  • prompt_embeds_pooled (torch.Tensor, 可选的) — 预生成的池化文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从 prompt 输入参数生成池化文本嵌入。
  • negative_prompt_embeds (torch.Tensor, 可选的) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • negative_prompt_embeds_pooled (torch.Tensor, 可选的) — 预生成的负面池化文本嵌入。可用于轻松调整文本输入,例如 提示词权重。如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds_pooled。
  • image_embeds (torch.Tensor, 可选的) — 预生成的图像嵌入。可用于轻松调整图像输入,例如 提示词权重。如果未提供,并且存在 image 输入参数,则将从该参数生成图像嵌入。
  • num_images_per_prompt (int, 可选的, 默认为 1) — 每个提示词要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], 可选的) — 用于使生成具有确定性的一个或多个 torch 生成器
  • latents (torch.Tensor, 可选的) — 预生成的噪声潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示词调整相同的生成结果。如果未提供,将通过使用提供的随机 generator 采样来生成潜在变量张量。
  • output_type (str, 可选的, 默认为 "pil") — 生成图像的输出格式。 从以下选项中选择: "pil" (PIL.Image.Image), "np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, 可选的, 默认为 True) — 是否返回 ImagePipelineOutput 而不是纯元组。
  • 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 属性中列出的变量。

调用管道进行生成时调用的函数。

示例

>>> import torch
>>> from diffusers import StableCascadePriorPipeline

>>> prior_pipe = StableCascadePriorPipeline.from_pretrained(
...     "stabilityai/stable-cascade-prior", torch_dtype=torch.bfloat16
... ).to("cuda")

>>> prompt = "an image of a shiba inu, donning a spacesuit and helmet"
>>> prior_output = pipe(prompt)

StableCascadePriorPipelineOutput

class diffusers.pipelines.stable_cascade.pipeline_stable_cascade_prior.StableCascadePriorPipelineOutput

< >

( image_embeddings: Union prompt_embeds: Union prompt_embeds_pooled: Union negative_prompt_embeds: Union negative_prompt_embeds_pooled: Union )

参数

  • image_embeddings (torch.Tensornp.ndarray) — 文本提示的先验图像嵌入
  • prompt_embeds (torch.Tensor) — 提示的文本嵌入。
  • negative_prompt_embeds (torch.Tensor) — 负面提示的文本嵌入。

WuerstchenPriorPipeline 的输出类。

StableCascadeDecoderPipeline

class diffusers.StableCascadeDecoderPipeline

< >

( decoder: StableCascadeUNet tokenizer: CLIPTokenizer text_encoder: CLIPTextModel scheduler: DDPMWuerstchenScheduler vqgan: PaellaVQModel latent_dim_scale: float = 10.67 )

参数

  • tokenizer (CLIPTokenizer) — CLIP tokenizer。
  • text_encoder (CLIPTextModel) — CLIP 文本编码器。
  • decoder (StableCascadeUNet) — Stable Cascade 解码器 unet。
  • vqgan (PaellaVQModel) — VQGAN 模型。
  • scheduler (DDPMWuerstchenScheduler) — 一个调度器,与 prior 结合使用以生成图像嵌入。
  • latent_dim_scale (float, optional, 默认为 10.67) — 从图像嵌入确定 VQ 潜在空间大小的乘数。如果图像嵌入的高度=24,宽度=24,则 VQ 潜在形状需要高度=int(2410.67)=256,宽度=int(2410.67)=256,以匹配训练条件。

用于从 Stable Cascade 模型生成图像的 Pipeline。

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

__call__

< >

( image_embeddings: Union prompt: Union = None num_inference_steps: int = 10 guidance_scale: float = 0.0 negative_prompt: Union = None prompt_embeds: Optional = None prompt_embeds_pooled: Optional = None negative_prompt_embeds: Optional = None negative_prompt_embeds_pooled: Optional = None num_images_per_prompt: int = 1 generator: Union = None latents: Optional = None output_type: Optional = 'pil' return_dict: bool = True callback_on_step_end: Optional = None callback_on_step_end_tensor_inputs: List = ['latents'] )

参数

  • image_embedding (torch.TensorList[torch.Tensor]) — 从图像中提取或由先验模型生成的图像嵌入。
  • prompt (strList[str]) — 用于引导图像生成的提示或提示列表。
  • num_inference_steps (int, optional, 默认为 12) — 去噪步骤的数量。更多的去噪步骤通常会带来更高的图像质量,但会牺牲推理速度。
  • guidance_scale (float, optional, 默认为 0.0) — Classifier-Free Diffusion Guidance 中定义的 Guidance scale。 decoder_guidance_scale 定义为 Imagen Paper 的公式 2 中的 w。通过设置 decoder_guidance_scale > 1 启用 Guidance scale。较高的 guidance scale 鼓励生成与文本 prompt 紧密相关的图像,但通常以降低图像质量为代价。
  • negative_prompt (strList[str], optional) — 不用于引导图像生成的提示或提示列表。当不使用 guidance 时忽略(即,如果 decoder_guidance_scale 小于 1 则忽略)。
  • prompt_embeds (torch.Tensor, optional) — 预生成的文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,将从 prompt 输入参数生成文本嵌入。
  • prompt_embeds_pooled (torch.Tensor, optional) — 预生成的池化文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,将从 prompt 输入参数生成池化的文本嵌入。
  • negative_prompt_embeds (torch.Tensor, optional) — 预生成的负面文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • negative_prompt_embeds_pooled (torch.Tensor, optional) — 预生成的负面池化文本嵌入。可用于轻松调整文本输入,例如 提示权重。如果未提供,将从 negative_prompt 输入参数生成 negative_prompt_embeds_pooled。
  • num_images_per_prompt (int, optional, 默认为 1) — 每个提示要生成的图像数量。
  • generator (torch.GeneratorList[torch.Generator], optional) — 用于使生成具有确定性的一个或多个 torch 生成器
  • latents (torch.Tensor, optional) — 预生成的噪声 latents,从高斯分布中采样,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,将通过使用提供的随机 generator 进行采样来生成 latents 张量。
  • output_type (str, optional, 默认为 "pil") — 生成图像的输出格式。在以下选项中选择:"pil" (PIL.Image.Image)、"np" (np.array) 或 "pt" (torch.Tensor)。
  • return_dict (bool, optional, 默认为 True) — 是否返回 ImagePipelineOutput 而不是普通元组。
  • 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 参数传递。您将只能包含在 pipeline 类的 ._callback_tensor_inputs 属性中列出的变量。

调用管道进行生成时调用的函数。

示例

>>> import torch
>>> from diffusers import StableCascadePriorPipeline, StableCascadeDecoderPipeline

>>> prior_pipe = StableCascadePriorPipeline.from_pretrained(
...     "stabilityai/stable-cascade-prior", torch_dtype=torch.bfloat16
... ).to("cuda")
>>> gen_pipe = StableCascadeDecoderPipeline.from_pretrain(
...     "stabilityai/stable-cascade", torch_dtype=torch.float16
... ).to("cuda")

>>> prompt = "an image of a shiba inu, donning a spacesuit and helmet"
>>> prior_output = pipe(prompt)
>>> images = gen_pipe(prior_output.image_embeddings, prompt=prompt)
< > 在 GitHub 上更新