Diffusers 文件

自注意力引導

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

自注意力引導

利用自注意力引導改進擴散模型樣本質量由Susung Hong等人完成。

論文摘要如下:

去噪擴散模型 (DDMs) 因其出色的生成質量和多樣性而受到關注。這一成功主要歸因於類條件或文字條件擴散引導方法的使用,例如分類器和無分類器引導。本文提出了一個超越傳統引導方法的更全面的視角。從這個廣義視角出發,我們引入了新穎的無條件和無訓練策略來提升生成影像的質量。作為一個簡單的解決方案,模糊引導提高了中間樣本對其精細資訊和結構的適用性,使擴散模型能夠以適度的引導尺度生成更高質量的樣本。在此基礎上,自注意力引導 (SAG) 利用擴散模型的中間自注意力圖來增強其穩定性和效率。具體來說,SAG 在每次迭代中只對擴散模型關注的區域進行對抗性模糊,並據此進行引導。我們的實驗結果表明,SAG 改進了包括 ADM、IDDPM、Stable Diffusion 和 DiT 在內的各種擴散模型的效能。此外,SAG 與傳統引導方法相結合可以帶來進一步的改進。

您可以在專案頁面原始程式碼庫找到有關自注意力引導的更多資訊,並在演示筆記本中試用。

請務必檢視排程器指南,瞭解如何在排程器速度和質量之間進行權衡,並參閱跨管道複用元件部分,瞭解如何高效地將相同元件載入到多個管道中。

StableDiffusionSAGPipeline

diffusers.StableDiffusionSAGPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers safety_checker: StableDiffusionSafetyChecker feature_extractor: CLIPImageProcessor image_encoder: typing.Optional[transformers.models.clip.modeling_clip.CLIPVisionModelWithProjection] = None requires_safety_checker: bool = True )

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 guidance_scale: float = 7.5 sag_scale: float = 0.75 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None num_images_per_prompt: typing.Optional[int] = 1 eta: float = 0.0 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 negative_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 callback: typing.Optional[typing.Callable[[int, int, torch.Tensor], NoneType]] = None callback_steps: typing.Optional[int] = 1 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None clip_skip: typing.Optional[int] = None ) 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) — 去噪步數。更多的去噪步數通常能生成更高質量的影像,但推理速度會變慢。
  • guidance_scale (float, 可選, 預設為 7.5) — 較高的引導尺度值鼓勵模型生成與文字 prompt 密切相關的影像,但影像質量會降低。當 guidance_scale > 1 時啟用引導尺度。
  • sag_scale (float, 可選, 預設為 0.75) — 介於 [0, 1.0] 之間,以獲得更好的質量。
  • negative_prompt (strList[str], 可選) — 用於引導影像生成中不包含內容的提示詞或提示詞列表。如果未定義,則需要傳入 negative_prompt_embeds。當不使用引導時(guidance_scale < 1),此引數將被忽略。
  • num_images_per_prompt (int, 可選, 預設為 1) — 每個提示詞生成的影像數量。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η)。僅適用於 DDIMScheduler,在其他排程器中將被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個 torch.Generator 用於使生成具有確定性。
  • latents (torch.Tensor, 可選) — 從高斯分佈中取樣的預生成噪聲潛在變數,用作影像生成的輸入。可用於使用不同提示詞調整同一生成。如果未提供,將使用提供的隨機 generator 取樣生成一個潛在變數張量。
  • prompt_embeds (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入(提示詞權重)。如果未提供,文字嵌入將從 prompt 輸入引數生成。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入(提示詞權重)。如果未提供,negative_prompt_embeds 將從 negative_prompt 輸入引數生成。
  • ip_adapter_image — (PipelineImageInput, 可選): 與 IP 介面卡配合使用的可選影像輸入。
  • ip_adapter_image_embeds (List[torch.Tensor], 可選) — IP-Adapter 的預生成影像嵌入。如果未提供,嵌入將從 ip_adapter_image 輸入引數計算。
  • output_type (str, 可選, 預設為 "pil") — 生成影像的輸出格式。選擇 PIL.Imagenp.array
  • return_dict (bool, 可選, 預設為 True) — 是否返回 StableDiffusionPipelineOutput,否則返回一個 tuple,其中第一個元素是生成的影像列表,第二個元素是布林值列表,指示相應生成的影像是否包含“不適合工作”(nsfw) 內容。
  • callback (Callable, 可選) — 在推理過程中每 callback_steps 步呼叫的函式。該函式將使用以下引數呼叫:callback(step: int, timestep: int, latents: torch.Tensor)
  • callback_steps (int, 可選, 預設為 1) — 呼叫 callback 函式的頻率。如果未指定,回撥將在每個步驟呼叫。
  • cross_attention_kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,將作為引數傳遞給 self.processor 中定義的 AttentionProcessor
  • clip_skip (int, 可選) — 計算提示詞嵌入時要跳過的 CLIP 層數。值為 1 表示將使用倒數第二層的輸出計算提示詞嵌入。

返回

StableDiffusionPipelineOutputtuple

如果 return_dictTrue,則返回 StableDiffusionPipelineOutput,否則返回一個 tuple,其中第一個元素是生成的影像列表,第二個元素是布林值列表,指示相應生成的影像是否包含“不適合工作”(nsfw) 內容。

用於生成的管道的呼叫函式。

示例

>>> import torch
>>> from diffusers import StableDiffusionSAGPipeline

>>> pipe = StableDiffusionSAGPipeline.from_pretrained(
...     "stable-diffusion-v1-5/stable-diffusion-v1-5", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")

>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt, sag_scale=0.75).images[0]

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_embeds 將從 negative_prompt 輸入引數生成。
  • lora_scale (float, 可選) — 如果載入了 LoRA 層,則將應用於文字編碼器所有 LoRA 層的 LoRA 縮放因子。
  • clip_skip (int, 可選) — 計算提示詞嵌入時要跳過的 CLIP 層數。值為 1 表示將使用倒數第二層的輸出計算提示詞嵌入。

將提示編碼為文字編碼器隱藏狀態。

StableDiffusionOutput

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 上更新

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