Diffusers 文档
Shap-E
并获得增强的文档体验
开始使用
Shap-E
Shap-E 模型由 OpenAI 的 Alex Nichol 和 Heewoo Jun 在论文 Shap-E: 生成条件 3D 隐式函数 中提出。
论文摘要如下:
我们提出了 Shap-E,一个用于 3D 资产的条件生成模型。与近期生成单个输出表示的 3D 生成模型不同,Shap-E 直接生成隐式函数的参数,这些函数可以被渲染为带纹理的网格和神经辐射场。我们分两个阶段训练 Shap-E:首先,我们训练一个编码器,将 3D 资产确定性地映射到隐式函数的参数;其次,我们对编码器的输出训练一个条件扩散模型。在大量配对的 3D 和文本数据上训练后,我们得到的模型能够在几秒钟内生成复杂多样的 3D 资产。与 Point-E(一个基于点云的显式生成模型)相比,Shap-E 收敛速度更快,并达到相似或更好的样本质量,尽管它建模的是更高维度的多表示输出空间。
原始代码库可在 openai/shap-e 找到。
请参阅跨管道重用组件部分,了解如何有效地将相同组件加载到多个管道中。
ShapEPipeline
class diffusers.ShapEPipeline
< 来源 >( prior: PriorTransformer text_encoder: CLIPTextModelWithProjection tokenizer: CLIPTokenizer scheduler: HeunDiscreteScheduler shap_e_renderer: ShapERenderer )
参数
- prior (PriorTransformer) — 用于从文本嵌入近似图像嵌入的规范 unCLIP 先验模型。
- text_encoder (CLIPTextModelWithProjection) — 冻结的文本编码器。
- tokenizer (CLIPTokenizer) — 一个用于文本分词的
CLIPTokenizer
。 - scheduler (HeunDiscreteScheduler) — 与
prior
模型结合使用以生成图像嵌入的调度器。 - shap_e_renderer (
ShapERenderer
) — Shap-E 渲染器将生成的潜在表示投影到 MLP 的参数中,以使用 NeRF 渲染方法创建 3D 对象。
用于生成 3D 资产的潜在表示并使用 NeRF 方法进行渲染的管道。
此模型继承自 DiffusionPipeline。请查看超类文档,了解所有管道实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< 来源 >( prompt: str num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None guidance_scale: float = 4.0 frame_size: int = 64 output_type: typing.Optional[str] = 'pil' return_dict: bool = True ) → ShapEPipelineOutput 或 tuple
参数
- prompt (
str
或List[str]
) — 用于引导图像生成的提示或提示列表。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的图像数量。 - num_inference_steps (
int
, 可选, 默认为 25) — 去噪步骤的数量。更多的去噪步骤通常会导致更高质量的图像,但推理速度会变慢。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个torch.Generator
用于使生成过程确定化。 - latents (
torch.Tensor
, 可选) — 从高斯分布采样的预生成噪声潜在表示,用作图像生成的输入。可用于使用不同提示调整相同的生成。如果未提供,则使用提供的随机generator
进行采样以生成潜在张量。 - guidance_scale (
float
, 可选, 默认为 4.0) — 更高的引导尺度值鼓励模型生成与文本prompt
紧密相关的图像,但会降低图像质量。当guidance_scale > 1
时启用引导尺度。 - frame_size (
int
, 可选, 默认为 64) — 生成的 3D 输出中每个图像帧的宽度和高度。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。可在"pil"
(PIL.Image.Image
)、"np"
(np.array
)、"latent"
(torch.Tensor
) 或网格 (MeshDecoderOutput
) 之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ShapEPipelineOutput 而不是纯元组。
返回
ShapEPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ShapEPipelineOutput,否则返回一个 tuple
,其中第一个元素是包含生成图像的列表。
用于生成的管道的调用函数。
示例
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from diffusers.utils import export_to_gif
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> repo = "openai/shap-e"
>>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)
>>> pipe = pipe.to(device)
>>> guidance_scale = 15.0
>>> prompt = "a shark"
>>> images = pipe(
... prompt,
... guidance_scale=guidance_scale,
... num_inference_steps=64,
... frame_size=256,
... ).images
>>> gif_path = export_to_gif(images[0], "shark_3d.gif")
ShapEImg2ImgPipeline
class diffusers.ShapEImg2ImgPipeline
< 来源 >( prior: PriorTransformer image_encoder: CLIPVisionModel image_processor: CLIPImageProcessor scheduler: HeunDiscreteScheduler shap_e_renderer: ShapERenderer )
参数
- prior (PriorTransformer) — 用于从图像嵌入近似图像嵌入的规范 unCLIP 先验模型。
- image_encoder (CLIPVisionModel) — 冻结的图像编码器。
- image_processor (CLIPImageProcessor) — 一个用于处理图像的
CLIPImageProcessor
。 - scheduler (HeunDiscreteScheduler) — 与
prior
模型结合使用以生成图像嵌入的调度器。 - shap_e_renderer (
ShapERenderer
) — Shap-E 渲染器将生成的潜在表示投影到 MLP 的参数中,以使用 NeRF 渲染方法创建 3D 对象。
从图像生成 3D 资产的潜在表示并使用 NeRF 方法进行渲染的管道。
此模型继承自 DiffusionPipeline。请查看超类文档,了解所有管道实现的通用方法(下载、保存、在特定设备上运行等)。
__call__
< source >( image: typing.Union[PIL.Image.Image, typing.List[PIL.Image.Image]] num_images_per_prompt: int = 1 num_inference_steps: int = 25 generator: typing.Union[torch._C.Generator, typing.List[torch._C.Generator], NoneType] = None latents: typing.Optional[torch.Tensor] = None guidance_scale: float = 4.0 frame_size: int = 64 output_type: typing.Optional[str] = 'pil' return_dict: bool = True ) → ShapEPipelineOutput or tuple
参数
- image (
torch.Tensor
,PIL.Image.Image
,np.ndarray
,List[torch.Tensor]
,List[PIL.Image.Image]
或List[np.ndarray]
) — 用作起点的Image
或表示图像批次的张量。也可以接受图像潜在表示作为图像,但如果直接传递潜在表示,则不会再次编码。 - num_images_per_prompt (
int
, 可选, 默认为 1) — 每个提示要生成的图像数量。 - num_inference_steps (
int
, 可选, 默认为 25) — 去噪步数。更多的去噪步数通常会产生更高质量的图像,但会牺牲推理速度。 - generator (
torch.Generator
或List[torch.Generator]
, 可选) — 一个torch.Generator
用于使生成具有确定性。 - latents (
torch.Tensor
, 可选) — 预生成的从高斯分布采样的噪声潜在表示,用作图像生成的输入。可用于使用不同的提示调整相同的生成。如果未提供,则使用提供的随机generator
采样生成一个潜在张量。 - guidance_scale (
float
, 可选, 默认为 4.0) — 较高的引导比例值鼓励模型生成与文本prompt
紧密相关的图像,但会牺牲图像质量。当guidance_scale > 1
时启用引导比例。 - frame_size (
int
, 可选, 默认为 64) — 生成的 3D 输出的每个图像帧的宽度和高度。 - output_type (
str
, 可选, 默认为"pil"
) — 生成图像的输出格式。在"pil"
(PIL.Image.Image
)、"np"
(np.array
)、"latent"
(torch.Tensor
) 或网格 (MeshDecoderOutput
) 之间选择。 - return_dict (
bool
, 可选, 默认为True
) — 是否返回 ShapEPipelineOutput 而不是普通的元组。
返回
ShapEPipelineOutput 或 tuple
如果 return_dict
为 True
,则返回 ShapEPipelineOutput,否则返回一个 tuple
,其中第一个元素是包含生成图像的列表。
用于生成的管道的调用函数。
示例
>>> from PIL import Image
>>> import torch
>>> from diffusers import DiffusionPipeline
>>> from diffusers.utils import export_to_gif, load_image
>>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
>>> repo = "openai/shap-e-img2img"
>>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)
>>> pipe = pipe.to(device)
>>> guidance_scale = 3.0
>>> image_url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/shap-e/corgi.png"
>>> image = load_image(image_url).convert("RGB")
>>> images = pipe(
... image,
... guidance_scale=guidance_scale,
... num_inference_steps=64,
... frame_size=256,
... ).images
>>> gif_path = export_to_gif(images[0], "corgi_3d.gif")
ShapEPipelineOutput
class diffusers.pipelines.shap_e.pipeline_shap_e.ShapEPipelineOutput
< source >( images: typing.Union[typing.List[typing.List[PIL.Image.Image]], typing.List[typing.List[numpy.ndarray]]] )
用于 ShapEPipeline 和 ShapEImg2ImgPipeline 的输出类。