Diffusers 文件

Stable Diffusion XL

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

Stable Diffusion XL

LoRA MPS

Stable Diffusion XL (SDXL) 由 Dustin Podell, Zion English, Kyle Lacey, Andreas Blattmann, Tim Dockhorn, Jonas Müller, Joe Penna 和 Robin Rombach 在 SDXL: Improving Latent Diffusion Models for High-Resolution Image Synthesis 中提出。

論文摘要如下:

我們提出了 SDXL,一個用於文字到影像合成的潛在擴散模型。與之前的 Stable Diffusion 版本相比,SDXL 利用了三倍大的 UNet 骨幹網路:模型引數的增加主要是由於更多的注意力塊和更大的交叉注意力上下文,因為 SDXL 使用了第二個文字編碼器。我們設計了多種新穎的條件方案,並在多種寬高比上訓練了 SDXL。我們還引入了一個最佳化模型,用於通過後處理影像到影像技術來提高 SDXL 生成樣本的視覺保真度。我們證明了 SDXL 與之前的 Stable Diffusion 版本相比,效能顯著提高,並取得了與黑盒最先進影像生成器相當的結果。

提示

  • 已知使用 DPM++ 排程器少於 50 步的 SDXL 會產生 視覺偽影,因為求解器變得數值不穩定。為了解決這個問題,請檢視此 PR,其中建議對於 ODE/SDE 求解器
    • 設定 use_karras_sigmas=Truelu_lambdas=True 以提高影像質量
    • 如果您使用統一步長的求解器(DPM++2M 或 DPM++2M SDE),請設定 euler_at_final=True
  • 大多數 SDXL 檢查點在 1024x1024 的影像尺寸下效果最佳。也支援 768x768 和 512x512 的影像尺寸,但效果不佳。不建議使用 512x512 以下的任何尺寸,對於預設檢查點(如 stabilityai/stable-diffusion-xl-base-1.0)可能不會有好的效果。
  • SDXL 可以為它訓練的每個文字編碼器傳遞不同的提示。我們甚至可以將相同提示的不同部分傳遞給文字編碼器。
  • 透過在影像到影像設定中使用最佳化模型,可以改進 SDXL 輸出影像。
  • SDXL 提供 negative_original_sizenegative_crops_coords_top_leftnegative_target_size,以便在影像解析度和裁剪引數上對模型進行負面條件處理。

要了解如何將 SDXL 用於各種任務、如何最佳化效能以及其他使用示例,請查閱 Stable Diffusion XL 指南。

請檢視 Stability AI Hub 組織,獲取官方基礎模型和最佳化模型檢查點!

StableDiffusionXLPipeline

class diffusers.StableDiffusionXLPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None force_zeros_for_empty_prompt: bool = True add_watermarker: typing.Optional[bool] = None )

引數

  • vae (AutoencoderKL) — 用於將影像編碼和解碼為潛在表示的變分自編碼器 (VAE) 模型。
  • text_encoder (CLIPTextModel) — 凍結的文字編碼器。Stable Diffusion XL 使用 CLIP 的文字部分,特別是 clip-vit-large-patch14 變體。
  • text_encoder_2 ( CLIPTextModelWithProjection) — 第二個凍結的文字編碼器。Stable Diffusion XL 使用 CLIP 的文字和池化部分,特別是 laion/CLIP-ViT-bigG-14-laion2B-39B-b160k 變體。
  • tokenizer (CLIPTokenizer) — 型別為 CLIPTokenizer 的分詞器。
  • tokenizer_2 (CLIPTokenizer) — 型別為 CLIPTokenizer 的第二個分詞器。
  • unet (UNet2DConditionModel) — 用於對編碼影像潛在表示進行去噪的條件 U-Net 架構。
  • scheduler (SchedulerMixin) — 與 unet 結合使用的排程器,用於對編碼影像潛在表示進行去噪。可以是 DDIMSchedulerLMSDiscreteSchedulerPNDMScheduler 之一。
  • force_zeros_for_empty_prompt (bool, 可選, 預設為 "True") — 是否強制負面提示詞嵌入始終設定為 0。另請參閱 stabilityai/stable-diffusion-xl-base-1-0 的配置。
  • add_watermarker (bool, 可選) — 是否使用 invisible_watermark 庫 為輸出影像新增水印。如果未定義,如果已安裝該包,則預設為 True,否則不使用水印。

用於使用 Stable Diffusion XL 進行文字到影像生成的管道。

此模型繼承自 DiffusionPipeline。請查閱父類文件,瞭解庫為所有管道實現的通用方法(例如下載或儲存、在特定裝置上執行等)。

該管道還繼承了以下載入方法

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None prompt_2: typing.Union[str, typing.List[str], NoneType] = None height: typing.Optional[int] = None width: typing.Optional[int] = None num_inference_steps: int = 50 timesteps: typing.List[int] = None sigmas: typing.List[float] = None denoising_end: typing.Optional[float] = None guidance_scale: float = 5.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt_2: 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 pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_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 original_size: typing.Optional[typing.Tuple[int, int]] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) target_size: typing.Optional[typing.Tuple[int, int]] = None negative_original_size: typing.Optional[typing.Tuple[int, int]] = None negative_crops_coords_top_left: typing.Tuple[int, int] = (0, 0) negative_target_size: typing.Optional[typing.Tuple[int, int]] = None clip_skip: typing.Optional[int] = None callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutputtuple

引數

  • prompt (strList[str], 可選) — 用於引導影像生成的提示詞或提示詞列表。如果未定義,則必須傳遞 prompt_embeds
  • prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的提示詞或提示詞列表。如果未定義,則 prompt 將在兩個文字編碼器中使用。
  • height (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的畫素高度。為獲得最佳結果,預設設定為 1024。小於 512 畫素的任何尺寸都不能很好地用於 stabilityai/stable-diffusion-xl-base-1.0 和未專門針對低解析度進行微調的檢查點。
  • width (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的畫素寬度。為獲得最佳結果,預設設定為 1024。小於 512 畫素的任何尺寸都不能很好地用於 stabilityai/stable-diffusion-xl-base-1.0 和未專門針對低解析度進行微調的檢查點。
  • num_inference_steps (int, optional, 預設為 50) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但推理速度會變慢。
  • timesteps (List[int], optional) — 用於去噪過程的自定義時間步,適用於其 set_timesteps 方法支援 timesteps 引數的排程器。如果未定義,將使用傳入 num_inference_steps 時的預設行為。必須按降序排列。
  • sigmas (List[float], optional) — 用於去噪過程的自定義sigmas,適用於其 set_timesteps 方法支援 sigmas 引數的排程器。如果未定義,將使用傳入 num_inference_steps 時的預設行為。
  • denoising_end (float, optional) — 指定時,確定在故意提前終止去噪過程之前要完成的總去噪過程的分數(介於 0.0 和 1.0 之間)。因此,返回的樣本仍將保留由排程器選擇的離散時間步所確定的相當數量的噪聲。denoising_end 引數最好在將此管道作為“去噪器混合”多管道設定的一部分時使用,如最佳化影像輸出中所詳述。
  • guidance_scale (float, optional, 預設為 5.0) — 如無分類器擴散引導中所定義的引導比例。guidance_scale 定義為Imagen 論文中公式 2 的 w。透過設定 guidance_scale > 1 來啟用引導比例。更高的引導比例會促使生成與文字 prompt 緊密相關的影像,通常以犧牲較低影像質量為代價。
  • negative_prompt (strList[str], optional) — 不引導影像生成的提示詞。如果未定義,則必須傳入 negative_prompt_embeds。當不使用引導時(即 guidance_scale 小於 1 時),此引數將被忽略。
  • negative_prompt_2 (strList[str], optional) — 不引導影像生成要傳送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,則 negative_prompt 將用於兩個文字編碼器。
  • num_images_per_prompt (int, optional, 預設為 1) — 每個提示要生成的影像數量。
  • eta (float, optional, 預設為 0.0) — 對應 DDIM 論文中的引數 eta (η): https://huggingface.co/papers/2010.02502。僅適用於 schedulers.DDIMScheduler,對其他排程器將被忽略。
  • generator (torch.GeneratorList[torch.Generator], optional) — 一個或多個 torch 生成器,用於使生成具有確定性。
  • latents (torch.Tensor, optional) — 預先生成的噪聲潛像,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同的提示調整相同的生成。如果未提供,將使用提供的隨機 generator 取樣生成一個潛像張量。
  • prompt_embeds (torch.Tensor, optional) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,文字嵌入將從 prompt 輸入引數生成。
  • negative_prompt_embeds (torch.Tensor, optional) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,負面提示嵌入將從 negative_prompt 輸入引數生成。
  • pooled_prompt_embeds (torch.Tensor, optional) — 預先生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,池化文字嵌入將從 prompt 輸入引數生成。
  • negative_pooled_prompt_embeds (torch.Tensor, optional) — 預先生成的負面池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,池化負面提示嵌入將從 negative_prompt 輸入引數生成。
  • ip_adapter_image — (PipelineImageInput, optional): 用於 IP 介面卡的可選影像輸入。
  • ip_adapter_image_embeds (List[torch.Tensor], optional) — 用於 IP-Adapter 的預先生成的影像嵌入。它應該是一個列表,長度與 IP-介面卡數量相同。每個元素都應該是一個形狀為 (batch_size, num_images, emb_dim) 的張量。如果 do_classifier_free_guidance 設定為 True,它應該包含負面影像嵌入。如果未提供,嵌入將從 ip_adapter_image 輸入引數計算。
  • output_type (str, optional, 預設為 "pil") — 生成影像的輸出格式。選擇 PIL: PIL.Image.Imagenp.array
  • return_dict (bool, optional, 預設為 True) — 是否返回 ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput 而不是普通元組。
  • cross_attention_kwargs (dict, optional) — 如果指定,則一個 kwargs 字典將傳遞給 AttentionProcessor,其定義在 diffusers.models.attention_processor 中的 self.processor 下。
  • guidance_rescale (float, optional, 預設為 0.0) — 常見擴散噪聲排程和取樣步驟存在缺陷 中提出的引導重新縮放因子。guidance_scale 定義為 常見擴散噪聲排程和取樣步驟存在缺陷 中方程 16 的 φ。引導重新縮放因子應解決使用零終端 SNR 時的過曝問題。
  • original_size (Tuple[int], optional, 預設為 (1024, 1024)) — 如果 original_sizetarget_size 不同,影像將顯示為縮小或放大。如果未指定,original_size 預設為 (height, width)。作為 SDXL 微調的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。
  • crops_coords_top_left (Tuple[int], optional, 預設為 (0, 0)) — crops_coords_top_left 可用於生成從 crops_coords_top_left 位置向下“裁剪”的影像。通常透過將 crops_coords_top_left 設定為 (0, 0) 來獲得有利的、居中的影像。作為 SDXL 微調的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。
  • target_size (Tuple[int], optional, 預設為 (1024, 1024)) — 在大多數情況下,target_size 應設定為生成影像的所需高度和寬度。如果未指定,它將預設為 (height, width)。作為 SDXL 微調的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。
  • negative_original_size (Tuple[int], optional, 預設為 (1024, 1024)) — 根據特定影像解析度對生成過程進行負面條件限制。作為 SDXL 微調的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。有關更多資訊,請參閱此問題討論串: https://github.com/huggingface/diffusers/issues/4208
  • negative_crops_coords_top_left (Tuple[int], optional, 預設為 (0, 0)) — 根據特定裁剪座標對生成過程進行負面條件限制。作為 SDXL 微調的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。有關更多資訊,請參閱此問題討論串: https://github.com/huggingface/diffusers/issues/4208
  • negative_target_size (Tuple[int], optional, 預設為 (1024, 1024)) — 根據目標影像解析度對生成過程進行負面條件限制。在大多數情況下,它應與 target_size 相同。作為 SDXL 微調的一部分,如 https://huggingface.co/papers/2307.01952 的第 2.2 節所述。有關更多資訊,請參閱此問題討論串: https://github.com/huggingface/diffusers/issues/4208
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, optional) — 一個函式或 PipelineCallbackMultiPipelineCallbacks 的子類,在推理過程中每個去噪步驟結束時呼叫,引數如下: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, optional) — callback_on_step_end 函式的張量輸入列表。列表中指定的張量將作為 callback_kwargs 引數傳遞。您只能包含管道類 ._callback_tensor_inputs 屬性中列出的變數。

返回

~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutputtuple

如果 return_dict 為 True,則返回 ~pipelines.stable_diffusion_xl.StableDiffusionXLPipelineOutput,否則返回 tuple。返回元組時,第一個元素是包含生成影像的列表。

呼叫管道進行生成時呼叫的函式。

示例

>>> import torch
>>> from diffusers import StableDiffusionXLPipeline

>>> pipe = StableDiffusionXLPipeline.from_pretrained(
...     "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")

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

encode_prompt

< >

( prompt: str prompt_2: typing.Optional[str] = None device: typing.Optional[torch.device] = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: typing.Optional[str] = None negative_prompt_2: typing.Optional[str] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )

引數

  • prompt (strList[str], optional) — 要編碼的提示詞。
  • prompt_2 (strList[str], optional) — 要傳送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,則 prompt 將用於兩個文字編碼器。
  • device — (torch.device): torch 裝置
  • num_images_per_prompt (int) — 每個提示應該生成的影像數量。
  • do_classifier_free_guidance (bool) — 是否使用無分類器引導。
  • negative_prompt (strList[str], optional) — 不引導影像生成的提示詞。如果未定義,則必須傳入 negative_prompt_embeds。當不使用引導時(即 guidance_scale 小於 1 時),此引數將被忽略。
  • negative_prompt_2 (strList[str], optional) — 不引導影像生成要傳送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,則 negative_prompt 將用於兩個文字編碼器。
  • prompt_embeds (torch.Tensor, optional) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,文字嵌入將從 prompt 輸入引數生成。
  • negative_prompt_embeds (torch.Tensor, optional) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,負面提示嵌入將從 negative_prompt 輸入引數生成。
  • pooled_prompt_embeds (torch.Tensor, optional) — 預先生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,池化文字嵌入將從 prompt 輸入引數生成。
  • negative_pooled_prompt_embeds (torch.Tensor, optional) — 預先生成的負面池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞加權。如果未提供,池化負面提示嵌入將從 negative_prompt 輸入引數生成。
  • lora_scale (float, optional) — 一個 LoRA 縮放因子,如果載入了 LoRA 層,它將應用於文字編碼器的所有 LoRA 層。
  • clip_skip (int, optional) — 計算提示詞嵌入時要跳過的 CLIP 層數。值為 1 表示將使用倒數第二層的輸出計算提示詞嵌入。

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

get_guidance_scale_embedding

< >

( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) torch.Tensor

引數

  • w (torch.Tensor) — 生成指定引導比例的嵌入向量,以便後續豐富時間步嵌入。
  • embedding_dim (int, optional, 預設為 512) — 要生成的嵌入的維度。
  • dtype (torch.dtype, optional, 預設為 torch.float32) — 生成嵌入的資料型別。

返回

torch.Tensor

形狀為 (len(w), embedding_dim) 的嵌入向量。

請參閱 https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298

StableDiffusionXLImg2ImgPipeline

class diffusers.StableDiffusionXLImg2ImgPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None requires_aesthetics_score: bool = False force_zeros_for_empty_prompt: bool = True add_watermarker: typing.Optional[bool] = None )

引數

  • vae (AutoencoderKL) — 用於將影像編碼和解碼為潛在表示的變分自編碼器(VAE)模型。
  • text_encoder (CLIPTextModel) — 凍結的文字編碼器。Stable Diffusion XL 使用 CLIP 的文字部分,特別是 clip-vit-large-patch14 變體。
  • text_encoder_2 ( CLIPTextModelWithProjection) — 第二個凍結的文字編碼器。Stable Diffusion XL 使用 CLIP 的文字和池化部分,特別是 laion/CLIP-ViT-bigG-14-laion2B-39B-b160k 變體。
  • tokenizer (CLIPTokenizer) — 類別為 CLIPTokenizer 的分詞器。
  • tokenizer_2 (CLIPTokenizer) — 類別為 CLIPTokenizer 的第二個分詞器。
  • unet (UNet2DConditionModel) — 用於對編碼影像潛在表示進行去噪的條件 U-Net 架構。
  • scheduler (SchedulerMixin) — 用於與 unet 結合以對編碼影像潛在表示進行去噪的排程器。可以是 DDIMSchedulerLMSDiscreteSchedulerPNDMScheduler 中的一個。
  • requires_aesthetics_score (bool, 可選, 預設為 "False") — unet 在推理過程中是否需要傳遞 aesthetic_score 條件。另請參閱 stabilityai/stable-diffusion-xl-refiner-1-0 的配置。
  • force_zeros_for_empty_prompt (bool, 可選, 預設為 "True") — 是否強制將負面提示嵌入設定為 0。另請參閱 stabilityai/stable-diffusion-xl-base-1-0 的配置。
  • add_watermarker (bool, 可選) — 是否使用 invisible_watermark 庫 為輸出影像新增水印。如果未定義,則在安裝了該包的情況下預設為 True,否則不使用水印。

用於使用 Stable Diffusion XL 進行文字到影像生成的管道。

此模型繼承自 DiffusionPipeline。請查閱父類文件,瞭解庫為所有管道實現的通用方法(例如下載或儲存、在特定裝置上執行等)。

該管道還繼承了以下載入方法

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None prompt_2: typing.Union[str, typing.List[str], NoneType] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None strength: float = 0.3 num_inference_steps: int = 50 timesteps: typing.List[int] = None sigmas: typing.List[float] = None denoising_start: typing.Optional[float] = None denoising_end: typing.Optional[float] = None guidance_scale: float = 5.0 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt_2: 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 pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_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 original_size: typing.Tuple[int, int] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) target_size: typing.Tuple[int, int] = None negative_original_size: typing.Optional[typing.Tuple[int, int]] = None negative_crops_coords_top_left: typing.Tuple[int, int] = (0, 0) negative_target_size: typing.Optional[typing.Tuple[int, int]] = None aesthetic_score: float = 6.0 negative_aesthetic_score: float = 2.5 clip_skip: typing.Optional[int] = None callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutputtuple

引數

  • prompt (strList[str], 可選) — 用於引導影像生成的提示詞或提示詞列表。如果未定義,則必須傳遞 prompt_embeds
  • prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的提示詞或提示詞列表。如果未定義,則在兩個文字編碼器中都使用 prompt
  • image (torch.TensorPIL.Image.Imagenp.ndarrayList[torch.Tensor]List[PIL.Image.Image]List[np.ndarray]) — 要使用管道修改的影像。
  • strength (float, 可選, 預設為 0.3) — 從概念上講,表示要轉換參考 image 的程度。必須在 0 到 1 之間。image 將作為起點,strength 越大,新增的噪聲就越多。去噪步驟的數量取決於最初新增的噪聲量。當 strength 為 1 時,新增的噪聲將最大,去噪過程將執行 num_inference_steps 中指定的完整迭代次數。因此,值為 1 實際上忽略了 image。請注意,如果將 denoising_start 宣告為整數,則將忽略 strength 的值。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步驟的數量。更多的去噪步驟通常會導致更高質量的影像,但會以較慢的推理速度為代價。
  • timesteps (List[int], 可選) — 用於去噪過程的自定義時間步,適用於其 set_timesteps 方法支援 timesteps 引數的排程器。如果未定義,則使用傳遞 num_inference_steps 時的預設行為。必須按降序排列。
  • sigmas (List[float], 可選) — 用於去噪過程的自定義 sigmas,適用於其 set_timesteps 方法支援 sigmas 引數的排程器。如果未定義,則使用傳遞 num_inference_steps 時的預設行為。
  • denoising_start (float, 可選) — 指定時,表示在去噪過程開始之前要跳過的總去噪過程的分數(介於 0.0 和 1.0 之間)。因此,去噪過程的初始部分被跳過,並且假定傳遞的 image 是部分去噪的影像。請注意,當指定此引數時,強度將被忽略。denoising_start 引數在將此管道整合到“去噪器混合”多管道設定中時特別有用,詳見 Refine Image Quality
  • denoising_end (float, 可選) — 指定時,確定在有意提前終止之前要完成的總去噪過程的分數(介於 0.0 和 1.0 之間)。因此,返回的樣本仍將保留大量噪聲(大約需要最後 20% 的時間步),並且應由將 denoising_start 設定為 0.8 的後續管道進行去噪,以便它僅對排程器的最後 20% 進行去噪。denoising_end 引數應在將此管道作為“去噪器混合”多管道設定的一部分時使用,詳見 Refine Image Quality
  • guidance_scale (float, 可選, 預設為 7.5) — 無分類器擴散引導中定義的引導比例。guidance_scale 定義為 Imagen Paper 方程 2 中的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例會鼓勵生成與文字 prompt 緊密相關的影像,但通常以犧牲影像質量為代價。
  • negative_prompt (strList[str], 可選) — 不引導影像生成的提示詞或提示詞列表。如果未定義,則必須傳遞 negative_prompt_embeds。在使用非引導時(即如果 guidance_scale 小於 1 時),此引數將被忽略。
  • negative_prompt_2 (strList[str], 可選) — 不引導影像生成的提示詞或提示詞列表,將傳送到 tokenizer_2text_encoder_2。如果未定義,則在兩個文字編碼器中都使用 negative_prompt
  • num_images_per_prompt (int, 可選, 預設為 1) — 每個提示詞要生成的影像數量。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η): https://huggingface.co/papers/2010.02502。僅適用於 schedulers.DDIMScheduler,對其他排程器將被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可選) — 一個或多個 torch 生成器,用於使生成具有確定性。
  • latents (torch.Tensor, 可選) — 預生成的帶噪潛在表示,從高斯分佈中取樣,用作影像生成的輸入。可用於使用不同的提示詞調整相同的生成。如果未提供,則將透過使用提供的隨機 generator 進行取樣來生成一個潛在張量。
  • prompt_embeds (torch.Tensor, 可選) — 預生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,則將從 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,負面提示詞嵌入將從 negative_prompt 輸入引數生成。
  • pooled_prompt_embeds (torch.Tensor, 可選) — 預生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,池化文字嵌入將從 prompt 輸入引數生成。
  • negative_pooled_prompt_embeds (torch.Tensor, 可選) — 預生成的負面池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,池化負面提示詞嵌入將從 negative_prompt 輸入引數生成。
  • ip_adapter_image — (PipelineImageInput, 可選): 用於 IP Adapters 的可選影像輸入。
  • ip_adapter_image_embeds (List[torch.Tensor], 可選) — IP-Adapter 的預生成影像嵌入。它應該是一個列表,長度與 IP-adapter 數量相同。每個元素都應該是一個形狀為 (batch_size, num_images, emb_dim) 的張量。如果 do_classifier_free_guidance 設定為 True,它應該包含負面影像嵌入。如果未提供,嵌入將從 ip_adapter_image 輸入引數計算。
  • output_type (str, 可選, 預設為 "pil") — 生成影像的輸出格式。在 PIL: PIL.Image.Imagenp.array 之間選擇。
  • return_dict (bool, 可選, 預設為 True) — 是否返回 ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput 而不是普通元組。
  • cross_attention_kwargs (dict, 可選) — 一個 kwargs 字典,如果指定,將作為引數傳遞給 diffusers.models.attention_processorself.processor 下定義的 AttentionProcessor
  • guidance_rescale (float, 可選, 預設為 0.0) — 常見擴散噪聲排程和取樣步驟存在缺陷中提出的引導重縮放因子。guidance_scale常見擴散噪聲排程和取樣步驟存在缺陷的方程 16 中定義為 φ。引導重縮放因子應在零終端 SNR 下修復過度曝光。
  • original_size (Tuple[int], 可選, 預設為 (1024, 1024)) — 如果 original_sizetarget_size 不同,影像將顯示為下采樣或上取樣。如果未指定,original_size 預設為 (height, width)。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。
  • crops_coords_top_left (Tuple[int], 可選, 預設為 (0, 0)) — crops_coords_top_left 可用於生成一張從 crops_coords_top_left 位置向下“裁剪”的影像。通常透過將 crops_coords_top_left 設定為 (0, 0) 來獲得令人滿意且居中的影像。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。
  • target_size (Tuple[int], 可選, 預設為 (1024, 1024)) — 在大多數情況下,target_size 應設定為生成影像的所需高度和寬度。如果未指定,它將預設為 (height, width)。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。
  • negative_original_size (Tuple[int], 可選, 預設為 (1024, 1024)) — 用於根據特定影像解析度對生成過程進行負面調節。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。有關更多資訊,請參閱此問題討論串:https://github.com/huggingface/diffusers/issues/4208
  • negative_crops_coords_top_left (Tuple[int], 可選, 預設為 (0, 0)) — 用於根據特定裁剪座標對生成過程進行負面調節。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。有關更多資訊,請參閱此問題討論串:https://github.com/huggingface/diffusers/issues/4208
  • negative_target_size (Tuple[int], 可選, 預設為 (1024, 1024)) — 用於根據目標影像解析度對生成過程進行負面調節。在大多數情況下,它應與 target_size 相同。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。有關更多資訊,請參閱此問題討論串:https://github.com/huggingface/diffusers/issues/4208
  • aesthetic_score (float, 可選, 預設為 6.0) — 透過影響正面文字條件來模擬生成影像的美學分數。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。
  • negative_aesthetic_score (float, 可選, 預設為 2.5) — SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 的 2.2 節所述。可用於透過影響負面文字條件來模擬生成影像的美學分數。
  • clip_skip (int, 可選) — 在計算提示詞嵌入時要跳過的 CLIP 層數。值為 1 表示將使用倒數第二層的輸出計算提示詞嵌入。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, 可選) — 一個函式或 PipelineCallbackMultiPipelineCallbacks 的子類,在推理過程中每個去噪步驟結束時呼叫,引數如下: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 屬性中列出的變數。

返回

~pipelines.stable_diffusion.StableDiffusionXLPipelineOutputtuple

如果 return_dict 為 True,則返回 ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput,否則返回 `tuple`。返回元組時,第一個元素是包含生成影像的列表。

呼叫管道進行生成時呼叫的函式。

示例

>>> import torch
>>> from diffusers import StableDiffusionXLImg2ImgPipeline
>>> from diffusers.utils import load_image

>>> pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
...     "stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16
... )
>>> pipe = pipe.to("cuda")
>>> url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"

>>> init_image = load_image(url).convert("RGB")
>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> image = pipe(prompt, image=init_image).images[0]

encode_prompt

< >

( prompt: str prompt_2: typing.Optional[str] = None device: typing.Optional[torch.device] = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: typing.Optional[str] = None negative_prompt_2: typing.Optional[str] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )

引數

  • prompt (strList[str], 可選) — 要編碼的提示詞。
  • prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,則在兩個文字編碼器中都使用 prompt
  • device — (torch.device): torch 裝置
  • num_images_per_prompt (int) — 每個提示詞應生成的影像數量
  • do_classifier_free_guidance (bool) — 是否使用分類器自由引導。
  • negative_prompt (strList[str], 可選) — 不引導影像生成的提示詞。如果未定義,則必須傳遞 negative_prompt_embeds。在不使用引導時(即如果 guidance_scale 小於 1),此引數將被忽略。
  • negative_prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的不引導影像生成的提示詞。如果未定義,則在兩個文字編碼器中都使用 negative_prompt
  • prompt_embeds (torch.Tensor, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 negative_prompt 輸入引數生成 negative_prompt_embeds。
  • pooled_prompt_embeds (torch.Tensor, 可選) — 預先生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 prompt 輸入引數生成池化文字嵌入。
  • negative_pooled_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

StableDiffusionXLInpaintPipeline

class diffusers.StableDiffusionXLInpaintPipeline

< >

( vae: AutoencoderKL text_encoder: CLIPTextModel text_encoder_2: CLIPTextModelWithProjection tokenizer: CLIPTokenizer tokenizer_2: CLIPTokenizer unet: UNet2DConditionModel scheduler: KarrasDiffusionSchedulers image_encoder: CLIPVisionModelWithProjection = None feature_extractor: CLIPImageProcessor = None requires_aesthetics_score: bool = False force_zeros_for_empty_prompt: bool = True add_watermarker: typing.Optional[bool] = None )

引數

  • vae (AutoencoderKL) — 變分自動編碼器 (VAE) 模型,用於將影像編碼和解碼為潛在表示。
  • text_encoder (CLIPTextModel) — 凍結文字編碼器。Stable Diffusion XL 使用 CLIP 的文字部分,特別是 clip-vit-large-patch14 變體。
  • text_encoder_2 ( CLIPTextModelWithProjection) — 第二個凍結文字編碼器。Stable Diffusion XL 使用 CLIP 的文字和池化部分,特別是 laion/CLIP-ViT-bigG-14-laion2B-39B-b160k 變體。
  • tokenizer (CLIPTokenizer) — CLIPTokenizer 類的分詞器。
  • tokenizer_2 (CLIPTokenizer) — 第二個 CLIPTokenizer 類的分詞器。
  • unet (UNet2DConditionModel) — 用於對編碼影像潛像進行去噪的條件 U-Net 架構。
  • scheduler (SchedulerMixin) — 一個排程器,與 unet 結合使用,對編碼影像潛像進行去噪。可以是 DDIMSchedulerLMSDiscreteSchedulerPNDMScheduler 之一。
  • requires_aesthetics_score (bool, 可選, 預設為 "False") — unet 是否需要在推理過程中傳遞美學分數條件。另請參閱 stabilityai/stable-diffusion-xl-refiner-1-0 的配置。
  • force_zeros_for_empty_prompt (bool, 可選, 預設為 "True") — 是否強制將負面提示嵌入始終設定為 0。另請參閱 stabilityai/stable-diffusion-xl-base-1-0 的配置。
  • add_watermarker (bool, 可選) — 是否使用 invisible_watermark 庫 對輸出影像新增水印。如果未定義,則如果已安裝該包,預設為 True,否則不使用水印。

用於使用 Stable Diffusion XL 進行文字到影像生成的管道。

此模型繼承自 DiffusionPipeline。請查閱父類文件,瞭解庫為所有管道實現的通用方法(例如下載或儲存、在特定裝置上執行等)。

該管道還繼承了以下載入方法

__call__

< >

( prompt: typing.Union[str, typing.List[str]] = None prompt_2: typing.Union[str, typing.List[str], NoneType] = None image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None mask_image: typing.Union[PIL.Image.Image, numpy.ndarray, torch.Tensor, typing.List[PIL.Image.Image], typing.List[numpy.ndarray], typing.List[torch.Tensor]] = None masked_image_latents: Tensor = None height: typing.Optional[int] = None width: typing.Optional[int] = None padding_mask_crop: typing.Optional[int] = None strength: float = 0.9999 num_inference_steps: int = 50 timesteps: typing.List[int] = None sigmas: typing.List[float] = None denoising_start: typing.Optional[float] = None denoising_end: typing.Optional[float] = None guidance_scale: float = 7.5 negative_prompt: typing.Union[str, typing.List[str], NoneType] = None negative_prompt_2: 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 pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_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 original_size: typing.Tuple[int, int] = None crops_coords_top_left: typing.Tuple[int, int] = (0, 0) target_size: typing.Tuple[int, int] = None negative_original_size: typing.Optional[typing.Tuple[int, int]] = None negative_crops_coords_top_left: typing.Tuple[int, int] = (0, 0) negative_target_size: typing.Optional[typing.Tuple[int, int]] = None aesthetic_score: float = 6.0 negative_aesthetic_score: float = 2.5 clip_skip: typing.Optional[int] = None callback_on_step_end: typing.Union[typing.Callable[[int, int, typing.Dict], NoneType], diffusers.callbacks.PipelineCallback, diffusers.callbacks.MultiPipelineCallbacks, NoneType] = None callback_on_step_end_tensor_inputs: typing.List[str] = ['latents'] **kwargs ) ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutputtuple

引數

  • prompt (strList[str], 可選) — 用於引導影像生成的提示詞。如果未定義,則必須傳遞 prompt_embeds
  • prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的提示詞。如果未定義,則在兩個文字編碼器中都使用 prompt
  • image (PIL.Image.Image) — Image,或表示影像批次的張量,將進行修復,即影像的某些部分將使用 mask_image 遮罩,並根據 prompt 重新繪製。
  • mask_image (PIL.Image.Image) — Image,或表示影像批次的張量,用於遮罩 image。遮罩中的白色畫素將被重新繪製,而黑色畫素將被保留。如果 mask_image 是 PIL 影像,它在使用前將轉換為單通道(亮度)。如果它是張量,它應該包含一個顏色通道(L)而不是 3,所以預期形狀將是 (B, H, W, 1)
  • height (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的高度(畫素)。為獲得最佳效果,預設為 1024。對於 stabilityai/stable-diffusion-xl-base-1.0 和未專門在低解析度上微調的檢查點,低於 512 畫素的任何值都將無法正常工作。
  • width (int, 可選, 預設為 self.unet.config.sample_size * self.vae_scale_factor) — 生成影像的寬度(畫素)。為獲得最佳效果,預設為 1024。對於 stabilityai/stable-diffusion-xl-base-1.0 和未專門在低解析度上微調的檢查點,低於 512 畫素的任何值都將無法正常工作。
  • padding_mask_crop (int, 可選, 預設為 None) — 應用於影像和遮罩裁剪的邊距大小。如果為 None,則不對影像和 mask_image 應用裁剪。如果 padding_mask_crop 不為 None,它將首先找到一個與影像長寬比相同幷包含所有遮罩區域的矩形區域,然後根據 padding_mask_crop 擴充套件該區域。然後影像和 mask_image 將根據擴充套件區域進行裁剪,然後調整大小以用於影像修復。當遮罩區域很小而影像很大幷包含與影像修復無關的資訊(如背景)時,這很有用。
  • strength (float, 可選, 預設為 0.9999) — 概念上,表示參考 image 的遮罩部分要轉換的程度。必須介於 0 和 1 之間。image 將作為起點,strength 越大,新增的噪聲越多。去噪步驟的數量取決於最初新增的噪聲量。當 strength 為 1 時,新增的噪聲將達到最大值,去噪過程將執行 num_inference_steps 中指定的全部迭代次數。因此,值為 1 基本上會忽略參考 image 的遮罩部分。請注意,如果 denoising_start 被宣告為整數,則 strength 的值將被忽略。
  • num_inference_steps (int, 可選, 預設為 50) — 去噪步驟的數量。更多的去噪步驟通常會帶來更高質量的影像,但推理速度會變慢。
  • timesteps (List[int], 可選) — 用於去噪過程的自定義時間步,適用於其 set_timesteps 方法支援 timesteps 引數的排程器。如果未定義,將使用傳遞 num_inference_steps 時的預設行為。必須按降序排列。
  • sigmas (List[float], 可選) — 用於去噪過程的自定義 sigma,適用於其 set_timesteps 方法支援 sigmas 引數的排程器。如果未定義,將使用傳遞 num_inference_steps 時的預設行為。
  • denoising_start (float, 可選) — 指定後,表示去噪過程(介於 0.0 和 1.0 之間)在啟動前要繞過的總分數。因此,去噪過程的初始部分被跳過,並假定傳遞的 image 是部分去噪的影像。請注意,指定此引數時,strength 將被忽略。當此管道整合到“去噪器混合”多管道設定中時,denoising_start 引數特別有用,詳細資訊請參閱 最佳化影像輸出
  • denoising_end (float, 可選) — 指定後,確定去噪過程(介於 0.0 和 1.0 之間)在有意提前終止之前要完成的百分比。因此,返回的樣本仍將保留大量噪聲(約需最後 20% 的時間步),應由設定 denoising_start 為 0.8 的後續管道進行去噪,以便它僅對排程器中最後 20% 的時間步進行去噪。當此管道作為“去噪器混合”多管道設定的一部分時,應理想地使用 denoising_end 引數,詳細資訊請參閱 最佳化影像輸出
  • guidance_scale (float, 可選, 預設為 7.5) — 無分類器擴散引導 中定義的引導比例。guidance_scale 定義為 Imagen 論文 中公式 2 的 w。透過設定 guidance_scale > 1 啟用引導比例。更高的引導比例鼓勵生成與文字 prompt 緊密相關的影像,通常以犧牲較低影像質量為代價。
  • negative_prompt (strList[str], 可選) — 不引導影像生成的提示詞。如果未定義,則必須傳遞 negative_prompt_embeds。在不使用引導時(即如果 guidance_scale 小於 1),此引數將被忽略。
  • negative_prompt_2 (strList[str], 可選) — 要傳送到 tokenizer_2text_encoder_2 的不引導影像生成的提示詞。如果未定義,則在兩個文字編碼器中都使用 negative_prompt
  • prompt_embeds (torch.Tensor, 可選) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, 可選) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 negative_prompt 輸入引數生成 negative_prompt_embeds。
  • pooled_prompt_embeds (torch.Tensor, 可選) — 預先生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 prompt 輸入引數生成池化文字嵌入。
  • negative_pooled_prompt_embeds (torch.Tensor, 可選) — 預先生成的負面池化文字嵌入。可用於輕鬆調整文字輸入,例如提示詞權重。如果未提供,將根據 negative_prompt 輸入引數生成池化 negative_prompt_embeds。
  • ip_adapter_image — (PipelineImageInput, 可選): 與 IP 介面卡配合使用的可選影像輸入。
  • ip_adapter_image_embeds (List[torch.Tensor], 可選) — 預先生成的 IP-Adapter 影像嵌入。它應該是一個列表,長度與 IP-adapter 的數量相同。每個元素都應該是一個形狀為 (batch_size, num_images, emb_dim) 的張量。如果 do_classifier_free_guidance 設定為 True,則它應該包含負影像嵌入。如果未提供,將根據 ip_adapter_image 輸入引數計算嵌入。
  • num_images_per_prompt (int, 可選, 預設為 1) — 每個提示詞要生成的影像數量。
  • eta (float, 可選, 預設為 0.0) — 對應於 DDIM 論文中的引數 eta (η): https://huggingface.co/papers/2010.02502。僅適用於 schedulers.DDIMScheduler,對其他排程器將被忽略。
  • generator (torch.Generator, optional) — 一個或多個 torch generator(s),用於使生成具有確定性。
  • latents (torch.Tensor, optional) — 預先生成的噪聲潛像(從高斯分佈中取樣),用作影像生成的輸入。可用於使用不同提示調整相同的生成。如果未提供,將使用提供的隨機 generator 取樣生成一個潛像張量。
  • output_type (str, optional, 預設為 "pil") — 生成影像的輸出格式。在 PIL: PIL.Image.Imagenp.array 之間選擇。
  • return_dict (bool, optional, 預設為 True) — 是否返回 StableDiffusionPipelineOutput 而不是普通元組。
  • cross_attention_kwargs (dict, optional) — 一個 kwargs 字典,如果指定,將作為引數傳遞給 diffusers.models.attention_processor 中定義的 self.processorAttentionProcessor
  • original_size (Tuple[int], optional, 預設為 (1024, 1024)) — 如果 original_sizetarget_size 不同,影像將顯示為下采樣或上取樣。如果未指定,original_size 預設為 (height, width)。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。
  • crops_coords_top_left (Tuple[int], optional, 預設為 (0, 0)) — crops_coords_top_left 可用於生成從 crops_coords_top_left 位置向下“裁剪”的影像。透過將 crops_coords_top_left 設定為 (0, 0) 通常可以獲得良好居中的影像。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。
  • target_size (Tuple[int], optional, 預設為 (1024, 1024)) — 對於大多數情況,target_size 應設定為生成影像的所需高度和寬度。如果未指定,它將預設為 (height, width)。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。
  • negative_original_size (Tuple[int], optional, 預設為 (1024, 1024)) — 基於特定影像解析度對生成過程進行負面條件反射。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。更多資訊請參閱此問題帖:https://github.com/huggingface/diffusers/issues/4208
  • negative_crops_coords_top_left (Tuple[int], optional, 預設為 (0, 0)) — 基於特定的裁剪座標對生成過程進行負面條件反射。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。更多資訊請參閱此問題帖:https://github.com/huggingface/diffusers/issues/4208
  • negative_target_size (Tuple[int], optional, 預設為 (1024, 1024)) — 基於目標影像解析度對生成過程進行負面條件反射。在大多數情況下,它應與 target_size 相同。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。更多資訊請參閱此問題帖:https://github.com/huggingface/diffusers/issues/4208
  • aesthetic_score (float, optional, 預設為 6.0) — 用於透過影響正文字條件來模擬生成影像的美學分數。SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。
  • negative_aesthetic_score (float, optional, 預設為 2.5) — SDXL 微條件的一部分,如 https://huggingface.co/papers/2307.01952 第 2.2 節所述。可用於透過影響負文字條件來模擬生成影像的美學分數。
  • clip_skip (int, optional) — 在計算提示嵌入時從 CLIP 中跳過的層數。值為 1 表示將使用倒數第二層的輸出計算提示嵌入。
  • callback_on_step_end (Callable, PipelineCallback, MultiPipelineCallbacks, optional) — 一個函式或 PipelineCallbackMultiPipelineCallbacks 的子類,在推理過程中每個去噪步驟結束時呼叫,引數如下: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, optional) — callback_on_step_end 函式的張量輸入列表。列表中指定的張量將作為 callback_kwargs 引數傳遞。您只能包含管道類 ._callback_tensor_inputs 屬性中列出的變數。

返回

~pipelines.stable_diffusion.StableDiffusionXLPipelineOutputtuple

如果 return_dict 為 True,則為 ~pipelines.stable_diffusion.StableDiffusionXLPipelineOutput,否則為 tuple。返回元組時,第一個元素是生成的影像列表。

呼叫管道進行生成時呼叫的函式。

示例

>>> import torch
>>> from diffusers import StableDiffusionXLInpaintPipeline
>>> from diffusers.utils import load_image

>>> pipe = StableDiffusionXLInpaintPipeline.from_pretrained(
...     "stabilityai/stable-diffusion-xl-base-1.0",
...     torch_dtype=torch.float16,
...     variant="fp16",
...     use_safetensors=True,
... )
>>> pipe.to("cuda")

>>> img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
>>> mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"

>>> init_image = load_image(img_url).convert("RGB")
>>> mask_image = load_image(mask_url).convert("RGB")

>>> prompt = "A majestic tiger sitting on a bench"
>>> image = pipe(
...     prompt=prompt, image=init_image, mask_image=mask_image, num_inference_steps=50, strength=0.80
... ).images[0]

encode_prompt

< >

( prompt: str prompt_2: typing.Optional[str] = None device: typing.Optional[torch.device] = None num_images_per_prompt: int = 1 do_classifier_free_guidance: bool = True negative_prompt: typing.Optional[str] = None negative_prompt_2: typing.Optional[str] = None prompt_embeds: typing.Optional[torch.Tensor] = None negative_prompt_embeds: typing.Optional[torch.Tensor] = None pooled_prompt_embeds: typing.Optional[torch.Tensor] = None negative_pooled_prompt_embeds: typing.Optional[torch.Tensor] = None lora_scale: typing.Optional[float] = None clip_skip: typing.Optional[int] = None )

引數

  • prompt (strList[str], optional) — 要編碼的提示
  • prompt_2 (strList[str], optional) — 要傳送到 tokenizer_2text_encoder_2 的提示。如果未定義,prompt 將用於兩個文字編碼器。
  • device — (torch.device): torch 裝置
  • num_images_per_prompt (int) — 每個提示應生成的影像數量
  • do_classifier_free_guidance (bool) — 是否使用分類器自由引導
  • negative_prompt (strList[str], optional) — 不用於引導影像生成的提示。如果未定義,則必須傳遞 negative_prompt_embeds。當不使用引導時(即,如果 guidance_scale 小於 1),則忽略。
  • negative_prompt_2 (strList[str], optional) — 不用於引導影像生成併發送到 tokenizer_2text_encoder_2 的提示。如果未定義,則 negative_prompt 將用於兩個文字編碼器。
  • prompt_embeds (torch.Tensor, optional) — 預先生成的文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,將從 prompt 輸入引數生成文字嵌入。
  • negative_prompt_embeds (torch.Tensor, optional) — 預先生成的負面文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,負面提示嵌入將從 negative_prompt 輸入引數生成。
  • pooled_prompt_embeds (torch.Tensor, optional) — 預先生成的池化文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,池化文字嵌入將從 prompt 輸入引數生成。
  • negative_pooled_prompt_embeds (torch.Tensor, optional) — 預先生成的負面池化文字嵌入。可用於輕鬆調整文字輸入,例如提示權重。如果未提供,池化負面提示嵌入將從 negative_prompt 輸入引數生成。
  • lora_scale (float, optional) — 一個 LoRA 比例,如果載入了 LoRA 層,它將應用於文字編碼器的所有 LoRA 層。
  • clip_skip (int, optional) — 在計算提示嵌入時從 CLIP 中跳過的層數。值為 1 表示將使用倒數第二層的輸出計算提示嵌入。

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

get_guidance_scale_embedding

< >

( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) torch.Tensor

引數

  • w (torch.Tensor) — 生成具有指定引導比例的嵌入向量,以隨後豐富時間步嵌入。
  • embedding_dim (int, optional, 預設為 512) — 要生成的嵌入維度。
  • dtype (torch.dtype, optional, 預設為 torch.float32) — 生成嵌入的資料型別。

返回

torch.Tensor

形狀為 (len(w), embedding_dim) 的嵌入向量。

請參閱 https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298

< > 在 GitHub 上更新

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