Diffusers 文档
Stable Cascade
并获得增强的文档体验
开始使用
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 和 B 用于压缩图像,类似于 VAE 在 Stable Diffusion 中的作用。然而,通过这种设置,可以实现图像的更高压缩率。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 管道中最大的组件,旨在与 `StableCascadePriorPipeline` 一起使用。
Stage B 和 Stage A 模型与 `StableCascadeDecoderPipeline` 一起使用,负责根据小的 24 x 24 潜在空间生成最终图像。
Stable Cascade 模型对可使用的数据类型有一些限制。`StableCascadePriorPipeline` 的官方检查点不支持 `torch.float16` 数据类型。请改用 `torch.bfloat16`。
要在 `StableCascadeDecoderPipeline` 中使用 `torch.bfloat16` 数据类型,您需要安装 PyTorch 2.2.0 或更高版本。这也意味着使用 `StableCascadeCombinedPipeline` 和 `torch.bfloat16` 需要 PyTorch 2.2.0 或更高版本,因为它在内部调用 `StableCascadeDecoderPipeline`。
如果无法在您的环境中安装 PyTorch 2.2.0 或更高版本,则可以将 `StableCascadeDecoderPipeline` 独立用于 `torch.float16` 数据类型。您可以下载该管道的完整精度或 `bf16` 变体权重,并将权重转换为 `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 模型的精简版
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
< 源 >( 分词器: CLIPTokenizer 文本编码器: CLIPTextModelWithProjection 解码器: StableCascadeUNet 调度器: DDPMWuerstchenScheduler vqgan: PaellaVQModel 先验: StableCascadeUNet 先验文本编码器: CLIPTextModelWithProjection 先验分词器: CLIPTokenizer 先验调度器: DDPMWuerstchenScheduler 先验特征提取器: typing.Optional[transformers.models.clip.image_processing_clip.CLIPImageProcessor] = None 先验图像编码器: typing.Optional[transformers.models.clip.modeling_clip.CLIPVisionModelWithProjection] = None )
参数
- **分词器** (`CLIPTokenizer`) — 用于文本输入的解码器分词器。
- **文本编码器** (`CLIPTextModelWithProjection`) — 用于文本输入的解码器文本编码器。
- **解码器** (`StableCascadeUNet`) — 用于解码器图像生成管道的解码器模型。
- **调度器** (`DDPMWuerstchenScheduler`) — 用于解码器图像生成管道的调度器。
- **vqgan** (`PaellaVQModel`) — 用于解码器图像生成管道的 VQGAN 模型。
- **prior_prior** (`StableCascadeUNet`) — 用于先验管道的先验模型。
- **prior_text_encoder** (`CLIPTextModelWithProjection`) — 用于文本输入的先验文本编码器。
- **prior_tokenizer** (`CLIPTokenizer`) — 用于文本输入的先验分词器。
- **prior_scheduler** (`DDPMWuerstchenScheduler`) — 用于先验管道的调度器。
- **prior_feature_extractor** (CLIPImageProcessor) — 从生成的图像中提取特征以用作 `image_encoder` 输入的模型。
- **prior_image_encoder** (`CLIPVisionModelWithProjection`) — 冻结的 CLIP 图像编码器 (clip-vit-large-patch14)。
用于文本到图像生成的 Stable Cascade 组合管道。
此模型继承自DiffusionPipeline。有关库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< 源 >( 提示: typing.Union[str, typing.List[str], NoneType] = None 图像: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] = None 高度: int = 512 宽度: int = 512 先验推理步数: int = 60 先验引导比例: float = 4.0 推理步数: int = 12 解码器引导比例: float = 0.0 负面提示: typing.Union[str, typing.List[str], NoneType] = None 提示嵌入: typing.Optional[torch.Tensor] = None 池化提示嵌入: typing.Optional[torch.Tensor] = None 负面提示嵌入: typing.Optional[torch.Tensor] = None 池化负面提示嵌入: typing.Optional[torch.Tensor] = None 每提示图像数量: int = 1 生成器: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None 潜在值: typing.Optional[torch.Tensor] = None 输出类型: typing.Optional[str] = 'pil' 返回字典: bool = True 每步结束先验回调: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None 每步结束先验回调张量输入: typing.List[str] = ['latents'] 每步结束回调: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None 每步结束回调张量输入: typing.List[str] = ['latents'] )
参数
- **prompt** (`str` 或 `List[str]`) — 用于引导先验和解码器图像生成的提示或多个提示。
- **images** (`torch.Tensor`, `PIL.Image.Image`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, _可选_) — 用于引导先验图像生成的图像。
- **negative_prompt** (`str` 或 `List[str]`, _可选_) — 不用于引导图像生成的提示或多个提示。当不使用引导时(即,如果 `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
。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 为每个提示生成的图像数量。 - height (
int
, 可选, 默认为 512) — 生成图像的像素高度。 - width (
int
, 可选, 默认为 512) — 生成图像的像素宽度。 - prior_guidance_scale (
float
, 可选, 默认为 4.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。prior_guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过设置prior_guidance_scale > 1
启用引导尺度。更高的引导尺度鼓励生成与文本prompt
紧密相关的图像,通常以牺牲较低图像质量为代价。 - prior_num_inference_steps (
Union[int, Dict[float, int]]
, 可选, 默认为 60) — 先验去噪步数。更多的去噪步数通常会带来更高的图像质量,但推理速度会变慢。对于更具体的时间步间隔,您可以传递自定义的prior_timesteps
。 - num_inference_steps (
int
, 可选, 默认为 12) — 解码器去噪步数。更多的去噪步数通常会带来更高的图像质量,但推理速度会变慢。对于更具体的时间步间隔,您可以传递自定义的timesteps
。 - decoder_guidance_scale (
float
, 可选, 默认为 0.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过设置guidance_scale > 1
启用引导尺度。更高的引导尺度鼓励生成与文本prompt
紧密相关的图像,通常以牺牲较低图像质量为代价。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch generator(s) 以使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的带噪潜在变量,从高斯分布采样,用作图像生成的输入。可用于使用不同提示调整相同的生成。如果未提供,将使用提供的随机generator
采样生成潜在变量张量。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。可选择:"pil"
(PIL.Image.Image
)、"np"
(np.array
) 或"pt"
(torch.Tensor
)。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ImagePipelineOutput 而非普通元组。 - prior_callback_on_step_end (
Callable
, 可选) — 在推理过程中,每个去噪步骤结束时调用的函数。该函数以以下参数调用: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
参数传递。您只能包含管道类._callback_tensor_inputs
属性中列出的变量。 - 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 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)
启用模型 CPU 卸载
< 源 >( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )
使用 accelerate 卸载所有模型到 CPU,在对性能影响很小的情况下减少内存使用。与 enable_sequential_cpu_offload
相比,此方法在调用模型的 forward
方法时一次将整个模型移动到 GPU,并且模型一直保留在 GPU 中直到下一个模型运行。内存节省低于 enable_sequential_cpu_offload
,但由于 unet
的迭代执行,性能要好得多。
启用顺序 CPU 卸载
< 源 >( gpu_id: typing.Optional[int] = None device: typing.Union[torch.device, str] = None )
使用 🤗 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: typing.Optional[transformers.models.clip.image_processing_clip.CLIPImageProcessor] = None image_encoder: typing.Optional[transformers.models.clip.modeling_clip.CLIPVisionModelWithProjection] = None )
参数
- prior (
StableCascadeUNet
) — 用于从文本和/或图像嵌入近似图像嵌入的 Stable Cascade 先验。 - 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 类的分词器。 - scheduler (
DDPMWuerstchenScheduler
) — 用于与prior
结合生成图像嵌入的调度器。 - resolution_multiple (
float
, 可选, 默认为 42.67) — 生成多张图像的默认分辨率。
用于生成 Stable Cascade 图像先验的管道。
此模型继承自DiffusionPipeline。有关库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< 源 >( prompt: typing.Union[str, typing.List[str], NoneType] = None images: typing.Union[torch.Tensor, PIL.Image.Image, typing.List[torch.Tensor], typing.List[PIL.Image.Image]] = None height: int = 1024 width: int = 1024 num_inference_steps: int = 20 timesteps: typing.List[float] = None guidance_scale: float = 4.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_embeds_pooled: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds_pooled: typing.Optional[torch.Tensor] = None image_embeds: typing.Optional[torch.Tensor] = None 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 output_type: typing.Optional[str] = 'pt' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] )
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的提示词或提示词列表。 - height (
int
, 可选, 默认为 1024) — 生成图像的像素高度。 - width (
int
, 可选, 默认为 1024) — 生成图像的像素宽度。 - num_inference_steps (
int
, 可选, 默认为 60) — 去噪步数。更多的去噪步数通常会带来更高的图像质量,但推理速度会变慢。 - guidance_scale (
float
, 可选, 默认为 8.0) — Classifier-Free Diffusion Guidance 中定义的引导尺度。decoder_guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过设置decoder_guidance_scale > 1
启用引导尺度。更高的引导尺度鼓励生成与文本prompt
紧密相关的图像,通常以牺牲较低图像质量为代价。 - negative_prompt (
str
或List[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
输入参数,则会从image
输入参数生成图像嵌入。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 为每个提示生成的图像数量。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个或多个 torch generator(s) 以使生成具有确定性。 - 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
< source >( image_embeddings: typing.Union[torch.Tensor, numpy.ndarray] prompt_embeds: typing.Union[torch.Tensor, numpy.ndarray] prompt_embeds_pooled: typing.Union[torch.Tensor, numpy.ndarray] negative_prompt_embeds: typing.Union[torch.Tensor, numpy.ndarray] negative_prompt_embeds_pooled: typing.Union[torch.Tensor, numpy.ndarray] )
WuerstchenPriorPipeline 的输出类。
StableCascadeDecoderPipeline
class diffusers.StableCascadeDecoderPipeline
< source >( decoder: StableCascadeUNet tokenizer: CLIPTokenizer text_encoder: CLIPTextModelWithProjection scheduler: DDPMWuerstchenScheduler vqgan: PaellaVQModel latent_dim_scale: float = 10.67 )
参数
- tokenizer (
CLIPTokenizer
) — CLIP 分词器。 - text_encoder (
CLIPTextModelWithProjection
) — CLIP 文本编码器。 - decoder (
StableCascadeUNet
) — Stable Cascade 解码器 UNet。 - vqgan (
PaellaVQModel
) — VQGAN 模型。 - scheduler (
DDPMWuerstchenScheduler
) — 用于与prior
结合生成图像嵌入的调度器。 - latent_dim_scale (float,
optional
, defaults to 10.67) — 用于根据图像嵌入确定 VQ 潜在空间大小的乘数。如果图像嵌入的高度为 24 且宽度为 24,则 VQ 潜在形状需要为高度 = int(24*10.67)=256 且宽度 = int(24*10.67)=256,以匹配训练条件。
用于从 Stable Cascade 模型生成图像的管道。
此模型继承自DiffusionPipeline。有关库为所有管道实现的通用方法(例如下载或保存、在特定设备上运行等),请查看超类文档。
__call__
< source >( image_embeddings: typing.Union[torch.Tensor, typing.List[torch.Tensor]] prompt: typing.Union[str, typing.List[str]] = None num_inference_steps: int = 10 guidance_scale: float = 0.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None prompt_embeds: typing.Optional[torch.Tensor] = None prompt_embeds_pooled: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds_pooled: typing.Optional[torch.Tensor] = None num_images_per_prompt: int = 1 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None output_type: typing.Optional[str] = 'pil' return_dict: bool = True callback_on_step_end: typing.Optional[typing.Callable[[int, int, typing.Dict], NoneType]] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] )
参数
- image_embedding (
torch.Tensor
或List[torch.Tensor]
) — 从图像中提取或由先验模型生成的图像嵌入。 - prompt (
str
或List[str]
) — 用于指导图像生成的提示。 - num_inference_steps (
int
, 可选, 默认为 12) — 去噪步数。更多去噪步数通常会带来更高质量的图像,但推理速度较慢。 - guidance_scale (
float
, 可选, 默认为 0.0) — Classifier-Free Diffusion Guidance 中定义的引导比例。decoder_guidance_scale
定义为 Imagen Paper 中公式 2 的w
。通过将decoder_guidance_scale > 1
启用引导比例。更高的引导比例鼓励生成与文本prompt
密切相关的图像,通常以牺牲图像质量为代价。 - negative_prompt (
str
或List[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。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的图像数量。 - generator (
torch.Generator
或List[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, 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)