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 的輸出類。