Diffusers 文件

MultiDiffusion

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

MultiDiffusion

LoRA

MultiDiffusion: 融合擴散路徑以實現受控影像生成 由 Omer Bar-Tal、Lior Yariv、Yaron Lipman 和 Tali Dekel 撰寫。

論文摘要如下:

文字到影像生成與擴散模型的最新進展展示了影像質量的變革效能力。然而,使用者對生成影像的可控性以及對新任務的快速適應仍然是一個開放的挑戰,目前主要透過昂貴且漫長的再訓練和微調或針對特定影像生成任務的臨時適應來解決。在這項工作中,我們提出了 MultiDiffusion,一個統一的框架,它能夠使用預訓練的文字到影像擴散模型,無需任何進一步的訓練或微調,實現多功能和可控的影像生成。我們方法的核心是一種新的生成過程,它基於一個最佳化任務,該任務將多個擴散生成過程與一組共享引數或約束相結合。我們展示了 MultiDiffusion 可以輕鬆應用於生成高質量和多樣化的影像,這些影像符合使用者提供的控制,例如所需的縱橫比(例如,全景圖)和空間引導訊號,範圍從緊密的分割掩碼到邊界框。

您可以在專案頁面原始程式碼庫上找到有關 MultiDiffusion 的更多資訊,並可以在演示中試用。

提示

呼叫 StableDiffusionPanoramaPipeline 時,可以將 view_batch_size 引數設定為 > 1。對於某些高效能 GPU,這可以加速生成過程並增加 VRAM 使用量。

要生成全景式影像,請確保相應地傳遞 width 引數。我們建議寬度值為 2048,這是預設值。

當處理全景圖時,應用迴圈填充以確保沒有拼接偽影,從而實現從最右側到最左側的無縫過渡。透過啟用迴圈填充(設定 circular_padding=True),該操作會在影像最右側之後應用額外的裁剪,從而使模型能夠“看到”從最右側到最左側的過渡。這有助於保持 360 度視覺一致性,並建立可以使用 360 度全景檢視器檢視的正確“全景圖”。在 Stable Diffusion 中解碼潛在變數時,應用迴圈填充以確保解碼後的潛在變數在 RGB 空間中匹配。

例如,沒有迴圈填充時,存在拼接偽影(預設): img

但有了迴圈填充,右側和左側部分匹配(circular_padding=True): img

請務必檢視排程器指南,瞭解如何探索排程器速度和質量之間的權衡,並檢視跨管道重用元件部分,瞭解如何有效地將相同元件載入到多個管道中。

StableDiffusionPanoramaPipeline

diffusers.StableDiffusionPanoramaPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel tokenizer: CLIPTokenizer unet: UNet2DConditionModel scheduler: DDIMScheduler 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] = 512 width: typing.Optional[int] = 2048 num_inference_steps: int = 50 timesteps: typing.List[int] = None guidance_scale: float = 7.5 view_batch_size: int = 1 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 cross_attention_kwargs: typing.Optional[typing.Dict[str, typing.Any]] = None guidance_rescale: float = 0.0 circular_padding: bool = False 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: typing.Any ) StableDiffusionPipelineOutputtuple

引數

  • prompt (strList[str], 可選) — 用於引導影像生成的提示或提示列表。如果未定義,則需要傳遞 prompt_embeds
  • height (int, 可選, 預設為 512) — 生成影像的高度(畫素)。
  • width (int, 可選, 預設為 2048) — 生成影像的寬度(畫素)。由於管道旨在生成全景式影像,因此寬度保持較高。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但推理速度會變慢。
  • timesteps (List[int], 可選) — 生成影像的時間步。如果未指定,則使用排程器的預設時間步間隔策略。
  • guidance_scale (float, 可選, 預設為 7.5) — 較高的指導比例值會鼓勵模型生成與文字 prompt 緊密相關的影像,但會以較低的影像質量為代價。當 guidance_scale > 1 時啟用指導比例。
  • view_batch_size (int, 可選, 預設為 1) — 去噪拆分檢視的批處理大小。對於某些高效能 GPU,更高的檢視批處理大小可以加速生成並增加 VRAM 使用量。
  • 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 輸入引數生成 negative_prompt_embeds
  • 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 的引數傳遞給 diffusers.models.attention_processor
  • guidance_rescale (float, 可選, 預設為 0.0) — 指導嵌入的重新縮放因子。值為 0.0 表示不應用重新縮放。
  • circular_padding (bool, 可選, 預設為 False) — 如果設定為 True,則應用迴圈填充以確保沒有拼接偽影。迴圈填充允許模型無縫地生成從影像最右側到最左側的過渡,從而保持 360 度的一致性。
  • 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[str], 可選) — callback_on_step_end 函式的張量輸入列表。列表中指定的張量將作為 callback_kwargs 引數傳遞。您只能包含管道類 ._callback_tensor_inputs 屬性中列出的變數。

返回

StableDiffusionPipelineOutputtuple

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

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

示例

>>> import torch
>>> from diffusers import StableDiffusionPanoramaPipeline, DDIMScheduler

>>> model_ckpt = "stabilityai/stable-diffusion-2-base"
>>> scheduler = DDIMScheduler.from_pretrained(model_ckpt, subfolder="scheduler")
>>> pipe = StableDiffusionPanoramaPipeline.from_pretrained(
...     model_ckpt, scheduler=scheduler, torch_dtype=torch.float16
... )

>>> pipe = pipe.to("cuda")

>>> prompt = "a photo of the dolomites"
>>> image = pipe(prompt).images[0]

帶填充的解碼潛在變數

< >

( latents: Tensor padding: int = 8 ) torch.Tensor

引數

  • latents (torch.Tensor) — 要解碼的輸入潛變數。
  • padding (int, optional) — 為迴圈推理在每側新增的潛變數數量。預設為8。

返回

torch.Tensor

已移除填充的解碼影像。

使用填充解碼給定潛變數以進行迴圈推理。

備註

  • 新增填充是為了消除邊界偽影並提高輸出質量。
  • 這會略微增加記憶體使用。
  • 然後從解碼影像中移除填充畫素。

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

get_views

< >

( panorama_height: int panorama_width: int window_size: int = 64 stride: int = 8 circular_padding: bool = False ) List[Tuple[int, int, int, int]]

引數

  • panorama_height (int) — 全景圖的高度。
  • panorama_width (int) — 全景圖的寬度。
  • window_size (int, optional) — 視窗大小。預設為64。
  • stride (int, optional) — 步長值。預設為8。
  • circular_padding (bool, optional) — 是否應用迴圈填充。預設為False。

返回

List[Tuple[int, int, int, int]]

表示檢視的元組列表。每個元組包含四個整數,表示視窗在全景圖中的起始和結束座標。

根據給定引數生成檢視列表。在這裡,我們定義對映 F_i (參見 MultiDiffusion 論文 https://huggingface.co/papers/2302.08113 中的公式7)。如果全景圖的高度/寬度 < 視窗大小,則高度/寬度應返回1個塊。

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

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