Diffusers 文档
文本到 (RGB, 深度)
并获取增强的文档体验
开始使用
文本到 (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
< source >( 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
结合使用以对编码后的图像潜在空间进行去噪。可以是 DDIMScheduler、 LMSDiscreteScheduler 或 PNDMScheduler 之一。 - 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
- load_textual_inversion() for loading textual inversion embeddings
- load_lora_weights() for loading LoRA weights
- save_lora_weights() for saving LoRA weights
- from_single_file() for loading
.ckpt
files - load_ip_adapter() for loading IP Adapters
__call__
< source >( 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 (
str
或List[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 (
str
或List[str]
, 可选) — 用于引导图像生成中不应包含的内容的提示或提示列表。如果未定义,则需要传递negative_prompt_embeds
代替。当不使用 guidance 时(guidance_scale < 1
),将被忽略。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个 prompt 生成的图像数量。 - eta (
float
, 可选, 默认为 0.0) — 对应于 DDIM 论文中的参数 eta (η)。仅适用于 DDIMScheduler,在其他调度器中将被忽略。 - generator (
torch.Generator
或List[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.Image
或np.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
属性中列出的变量。
返回
StableDiffusionPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 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
< source >( 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 (
str
或List[str]
, 可选) — 要编码的提示 - device — (
torch.device
): torch 设备 - num_images_per_prompt (
int
) — 每个提示应生成的图像数量 - do_classifier_free_guidance (
bool
) — 是否使用无分类器引导 - negative_prompt (
str
或List[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
< source >( w: Tensor embedding_dim: int = 512 dtype: dtype = torch.float32 ) → torch.Tensor
LDM3DPipelineOutput
class diffusers.pipelines.stable_diffusion_ldm3d.pipeline_stable_diffusion_ldm3d.LDM3DPipelineOutput
< source >( 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 管道的输出类。
Upscaler
LDM3D-VR 是 LDM3D 的扩展版本。
该论文的摘要是:潜在扩散模型已被证明在视觉输出的创建和操纵方面处于最先进水平。然而,据我们所知,RGB 与深度图的联合生成仍然有限。我们介绍了 LDM3D-VR,一套针对虚拟现实开发的扩散模型,其中包括 LDM3D-pano 和 LDM3D-SR。这些模型分别支持基于文本提示生成全景 RGBD,以及将低分辨率输入放大到高分辨率 RGBD。我们的模型是在包含全景/高分辨率 RGB 图像、深度图和 captions 的数据集上,从现有的预训练模型微调而来。与现有的相关方法相比,对这两个模型都进行了评估
有两个检查点可供使用
- ldm3d-pano。此检查点支持生成全景图像,并且需要使用 StableDiffusionLDM3DPipeline 管道。
- ldm3d-sr。此检查点支持 RGB 和深度图像的放大。可以在原始 LDM3D 管道之后级联使用,方法是使用 communauty 管道中的 StableDiffusionUpscaleLDM3DPipeline。