Diffusers 文档

文本到 (RGB, 深度)

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始使用

文本到 (RGB, 深度)

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 中被提出。与现有的文本到图像扩散模型(如 Stable Diffusion,仅生成图像)不同,LDM3D 从给定的文本提示生成图像和深度图。LDM3D 以几乎相同的参数数量,实现了创建一个可以压缩 RGB 图像和深度图的潜在空间。

有两个检查点可供使用

  • ldm3d-original。论文中使用的原始检查点
  • ldm3d-4c。LDM3D 的新版本,使用 4 通道输入代替 6 通道输入,并在更高分辨率的图像上进行了微调。

论文摘要如下:

本研究论文提出了一个用于 3D 的潜在扩散模型 (LDM3D),它可以从给定的文本提示生成图像和深度图数据,允许用户从文本提示生成 RGBD 图像。LDM3D 模型在一个包含 RGB 图像、深度图和标题的元组数据集上进行了微调,并通过广泛的实验进行了验证。我们还开发了一个名为 DepthFusion 的应用程序,它使用生成的 RGB 图像和深度图,利用 TouchDesigner 创建沉浸式和交互式的 360 度视角体验。这项技术有潜力改变广泛的行业,从娱乐和游戏到建筑和设计。总的来说,本文对生成式 AI 和计算机视觉领域做出了重大贡献,并展示了 LDM3D 和 DepthFusion 在革新内容创作和数字体验方面的潜力。关于该方法的简短视频摘要可以在 此链接 中找到。

请务必查看 Stable Diffusion 技巧 部分,了解如何探索 scheduler 速度和质量之间的权衡,以及如何高效地重用 pipeline 组件!

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 )

Parameters

  • vae (AutoencoderKL) — 变分自动编码器 (VAE) 模型,用于将图像编码和解码为潜在表示形式。
  • text_encoder (CLIPTextModel) — 冻结的文本编码器 (clip-vit-large-patch14)。
  • tokenizer (CLIPTokenizer) — 用于标记文本的 CLIPTokenizer
  • unet (UNet2DConditionModel) — 用于对编码后的图像潜在空间进行去噪的 UNet2DConditionModel
  • scheduler (SchedulerMixin) — 调度器,与 unet 结合使用以对编码后的图像潜在空间进行去噪。可以是 DDIMSchedulerLMSDiscreteSchedulerPNDMScheduler 之一。
  • safety_checker (StableDiffusionSafetyChecker) — 分类模块,用于评估生成的图像是否可能被认为具有攻击性或有害。有关模型潜在危害的更多详细信息,请参阅 模型卡
  • feature_extractor (CLIPImageProcessor) — CLIPImageProcessor,用于从生成的图像中提取特征;用作 safety_checker 的输入。

Pipeline for text-to-image and 3D generation using LDM3D.

This model inherits from DiffusionPipeline. Check the superclass documentation for the generic methods implemented for all pipelines (downloading, saving, running on a particular device, etc.).

The pipeline also inherits the following loading methods

__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 ) StableDiffusionPipelineOutput or tuple

Parameters

  • 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], 可选) — 自定义时间步长,用于支持在其 set_timesteps 方法中使用 timesteps 参数的调度器进行去噪过程。如果未定义,将使用传递 num_inference_steps 时的默认行为。必须以降序排列。
  • sigmas (List[float], 可选) — 自定义 sigma 值,用于支持在其 set_timesteps 方法中使用 sigmas 参数的调度器进行去噪过程。如果未定义,将使用传递 num_inference_steps 时的默认行为。
  • guidance_scale (float, 可选, 默认为 5.0) — 更高的 guidance scale 值会鼓励模型生成与文本 prompt 紧密相关的图像,但会以降低图像质量为代价。当 guidance_scale > 1 时,guidance scale 启用。
  • negative_prompt (strList[str], 可选) — 用于引导图像生成中不应包含的内容的提示或提示列表。如果未定义,则需要传递 negative_prompt_embeds 代替。当不使用 guidance 时(guidance_scale < 1),将被忽略。
  • num_images_per_prompt (int, 可选, 默认为 1) — 每个 prompt 生成的图像数量。
  • eta (float, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中将被忽略。
  • generator (torch.GeneratorList[torch.Generator], 可选) — 用于使生成确定性的 torch.Generator
  • latents (torch.Tensor, 可选) — 预生成的噪声潜在变量,从高斯分布中采样,用作图像生成的输入。可用于使用不同的 prompt 微调相同的生成。如果未提供,则潜在张量将通过使用提供的随机 generator 进行采样来生成。
  • prompt_embeds (torch.Tensor, 可选) — 预生成的文本嵌入。可用于轻松调整文本输入(prompt 加权)。如果未提供,则文本嵌入将从 prompt 输入参数生成。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负文本嵌入。可用于轻松调整文本输入(prompt 加权)。如果未提供,则 negative_prompt_embeds 将从 negative_prompt 输入参数生成。
  • ip_adapter_image — (PipelineImageInput, 可选): 可选的图像输入,用于与 IP Adapters 一起使用。
  • ip_adapter_image_embeds (List[torch.Tensor], 可选) — IP-Adapter 的预生成图像 embeddings。它应该是一个列表,长度与 IP-Adapter 的数量相同。每个元素应该是一个形状为 (batch_size, num_images, emb_dim) 的张量。如果 do_classifier_free_guidance 设置为 True,则应包含负图像 embedding。如果未提供,则 embeddings 从 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 中跳过的层数,用于计算提示 embeddings。值为 1 表示预最终层的输出将用于计算提示 embeddings。
  • 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,其中第一个元素是包含生成图像的列表,第二个元素是 bool 列表,指示相应的生成图像是否包含“不适合工作场所观看”(nsfw)内容。

用于生成的管道调用函数。

示例

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

Parameters

  • 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, 可选) — 预生成的文本 embeddings。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 prompt 输入参数生成文本 embeddings。
  • negative_prompt_embeds (torch.Tensor, 可选) — 预生成的负文本 embeddings。可用于轻松调整文本输入,例如 提示权重。如果未提供,则将从 negative_prompt 输入参数生成 negative_prompt_embeds。
  • lora_scale (float, 可选) — 如果加载了 LoRA 层,则将应用于文本编码器所有 LoRA 层的 LoRA 比例。
  • clip_skip (int, 可选) — 从 CLIP 中跳过的层数,用于计算提示 embeddings。值为 1 表示预最终层的输出将用于计算提示 embeddings。

将提示编码为文本编码器隐藏状态。

get_guidance_scale_embedding

< >

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

Parameters

  • w (torch.Tensor) — 生成具有指定引导比例的 embedding 向量,以随后丰富时间步 embeddings。
  • embedding_dim (int, 可选, 默认为 512) — 要生成的 embeddings 的维度。
  • dtype (torch.dtype, 可选, 默认为 torch.float32) — 生成的 embeddings 的数据类型。

返回

torch.Tensor

形状为 (len(w), embedding_dim) 的 Embedding 向量。

参见 https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298

LDM3DPipelineOutput

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

Parameters

  • 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 )

将自身作为函数调用。

Upscaler

LDM3D-VR 是 LDM3D 的扩展版本。

该论文的摘要是:潜在扩散模型已被证明在视觉输出的创建和操纵方面处于最先进水平。然而,据我们所知,RGB 与深度图的联合生成仍然有限。我们介绍了 LDM3D-VR,一套针对虚拟现实开发的扩散模型,其中包括 LDM3D-pano 和 LDM3D-SR。这些模型分别支持基于文本提示生成全景 RGBD,以及将低分辨率输入放大到高分辨率 RGBD。我们的模型是在包含全景/高分辨率 RGB 图像、深度图和 captions 的数据集上,从现有的预训练模型微调而来。与现有的相关方法相比,对这两个模型都进行了评估

有两个检查点可供使用

  • ldm3d-pano。此检查点支持生成全景图像,并且需要使用 StableDiffusionLDM3DPipeline 管道。
  • ldm3d-sr。此检查点支持 RGB 和深度图像的放大。可以在原始 LDM3D 管道之后级联使用,方法是使用 communauty 管道中的 StableDiffusionUpscaleLDM3DPipeline。
< > 在 GitHub 上更新