Diffusers 文件

文字到(RGB,深度)

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

文字到(RGB,深度)

LoRA

LDM3D 由 Gabriela Ben Melech Stan、Diana Wofk、Scottie Fox、Alex Redden、Will Saxton、Jean Yu、Estelle Aflalo、Shao-Yen Tseng、Fabio Nonato、Matthias Muller 和 Vasudev Lal 在LDM3D: Latent Diffusion Model for 3D 中提出。LDM3D 從給定的文字提示生成影像和深度圖,與現有的僅生成影像的文字到影像擴散模型(如Stable Diffusion)不同。LDM3D 以幾乎相同的引數數量成功建立了一個可以同時壓縮 RGB 影像和深度圖的潛在空間。

兩個檢查點可供使用

  • ldm3d-original。論文中使用的原始檢查點
  • ldm3d-4c。新版本的 LDM3D 使用 4 通道輸入而不是 6 通道輸入,並在更高解析度的影像上進行了微調。

論文摘要如下:

這篇研究論文提出了一種用於 3D 的潛在擴散模型(LDM3D),它可以根據給定的文字提示生成影像和深度圖資料,從而允許使用者從文字提示生成 RGBD 影像。LDM3D 模型在包含 RGB 影像、深度圖和標題的元組資料集上進行了微調,並透過大量實驗進行了驗證。我們還開發了一個名為 DepthFusion 的應用程式,它使用生成的 RGB 影像和深度圖,透過 TouchDesigner 建立沉浸式和互動式的 360 度視角體驗。這項技術有望改變娛樂和遊戲、建築和設計等廣泛行業。總而言之,這篇論文對生成式 AI 和計算機視覺領域做出了重大貢獻,並展示了 LDM3D 和 DepthFusion 徹底改變內容建立和數字體驗的潛力。總結該方法的簡短影片可以在此 URL 找到。

務必檢視 Stable Diffusion 的 提示 部分,瞭解如何探索排程器速度和質量之間的權衡,以及如何高效地重用管道元件!

StableDiffusionLDM3DPipeline

class diffusers.StableDiffusionLDM3DPipeline

< >

( 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] 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 = 49 timesteps: typing.List[int] = None sigmas: typing.List[float] = None guidance_scale: float = 5.0 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 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 ) 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) — 去噪步數。更多的去噪步數通常會帶來更高質量的影像,但推理速度會變慢。
  • timesteps (List[int], 可選) — 用於去噪過程的自定義時間步,適用於支援 timesteps 引數的排程器的 set_timesteps 方法。如果未定義,將使用傳入 num_inference_steps 時的預設行為。必須按降序排列。
  • sigmas (List[float], 可選) — 用於去噪過程的自定義 sigmas,適用於支援 sigmas 引數的排程器的 set_timesteps 方法。如果未定義,將使用傳入 num_inference_steps 時的預設行為。
  • guidance_scale (float, 可選, 預設為 5.0) — 較高的引導尺度值鼓勵模型生成與文字 prompt 緊密相關的影像,但會降低影像質量。當 guidance_scale > 1 時啟用引導尺度。
  • 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 數量相同的列表。每個元素都應該是一個形狀為 (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
  • 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, 可選) — callback_on_step_end 函式的張量輸入列表。列表中指定的張量將作為 callback_kwargs 引數傳遞。您只能包含管道類的 ._callback_tensor_inputs 屬性中列出的變數。

返回

StableDiffusionPipelineOutputtuple

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

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

示例

>>> from diffusers import StableDiffusionLDM3DPipeline

>>> pipe = StableDiffusionLDM3DPipeline.from_pretrained("Intel/ldm3d-4c")
>>> pipe = pipe.to("cuda")

>>> prompt = "a photo of an astronaut riding a horse on mars"
>>> output = pipe(prompt)
>>> rgb_image, depth_image = output.rgb, output.depth
>>> rgb_image[0].save("astronaut_ldm3d_rgb.jpg")
>>> depth_image[0].save("astronaut_ldm3d_depth.png")

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

LDM3DPipelineOutput

diffusers.pipelines.stable_diffusion_ldm3d.pipeline_stable_diffusion_ldm3d.LDM3DPipelineOutput

< >

( rgb: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] depth: typing.Union[typing.List[PIL.Image.Image], numpy.ndarray] nsfw_content_detected: typing.Optional[typing.List[bool]] )

引數

  • rgb (List[PIL.Image.Image]np.ndarray) — 長度為 batch_size 的去噪 PIL 影像列表或形狀為 (batch_size, height, width, num_channels) 的 NumPy 陣列。
  • depth (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 管道的輸出類。

__call__

( *args **kwargs )

將自身作為函式呼叫。

升頻器

LDM3D-VR 是 LDM3D 的擴充套件版本。

該論文摘要如下:潛在擴散模型已被證明在建立和操縱視覺輸出方面處於最先進水平。然而,據我們所知,同時生成深度圖和 RGB 仍然受到限制。我們引入 LDM3D-VR,一套針對虛擬現實開發的擴散模型,其中包括 LDM3D-pano 和 LDM3D-SR。這些模型分別能夠基於文字提示生成全景 RGBD,以及將低解析度輸入升級到高解析度 RGBD。我們的模型是在現有預訓練模型的基礎上,針對包含全景/高解析度 RGB 影像、深度圖和標題的資料集進行微調的。這兩個模型都與現有相關方法進行了比較評估。

兩個檢查點可供使用

  • ldm3d-pano。此檢查點支援全景影像的生成,需要使用 StableDiffusionLDM3DPipeline 管道。
  • ldm3d-sr。此檢查點支援 RGB 和深度影像的升頻。可在原始 LDM3D 管道之後使用 communauty 管道中的 StableDiffusionUpscaleLDM3DPipeline 進行級聯使用。
< > 在 GitHub 上更新

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