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 和 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.GeneratorList[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 解除安裝所有模型(unettext_encodervaesafety 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 (strList[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 (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 輸入引數,則會從 image 輸入引數生成影像嵌入。
  • num_images_per_prompt (int, 可選, 預設為 1) — 為每個提示生成的影像數量。
  • generator (torch.GeneratorList[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

< >

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

引數

  • 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: 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__

< >

( 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.TensorList[torch.Tensor]) — 從影像中提取或由先驗模型生成的影像嵌入。
  • prompt (strList[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 (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。
  • 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, 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 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.