Diffusers 文件
Shap-E
並獲得增強的文件體驗
開始使用
Shap-E
Shap-E 是一個用於生成 3D 資產的條件模型,可用於影片遊戲開發、室內設計和建築。它在一個大型 3D 資產資料集上進行訓練,並經過後處理以渲染每個物件的更多檢視,並生成 16K 而非 4K 點雲。Shap-E 模型分兩步進行訓練
- 一個編碼器接受 3D 資產的點雲和渲染檢視,並輸出表示該資產的隱式函式的引數
- 擴散模型在編碼器生成的潛在空間上進行訓練,以生成神經輻射場(NeRFs)或紋理 3D 網格,從而使其在下游應用中更易於渲染和使用 3D 資產
本指南將向您展示如何使用 Shap-E 開始生成您自己的 3D 資產!
開始之前,請確保已安裝以下庫:
# uncomment to install the necessary libraries in Colab
#!pip install -q diffusers transformers accelerate trimesh
文字到 3D
要生成 3D 物件的 gif,請將文字提示傳遞給 ShapEPipeline。管道生成一個影像幀列表,用於建立 3D 物件。
import torch
from diffusers import ShapEPipeline
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
pipe = ShapEPipeline.from_pretrained("openai/shap-e", torch_dtype=torch.float16, variant="fp16")
pipe = pipe.to(device)
guidance_scale = 15.0
prompt = ["A firecracker", "A birthday cupcake"]
images = pipe(
prompt,
guidance_scale=guidance_scale,
num_inference_steps=64,
frame_size=256,
).images
現在,使用 export_to_gif() 函式將影像幀列表轉換為 3D 物件的 gif。
from diffusers.utils import export_to_gif
export_to_gif(images[0], "firecracker_3d.gif")
export_to_gif(images[1], "cake_3d.gif")


影像到 3D
要從另一張影像生成 3D 物件,請使用 ShapEImg2ImgPipeline。您可以使用現有影像或生成一個全新影像。讓我們使用 Kandinsky 2.1 模型生成一個新影像。
from diffusers import DiffusionPipeline
import torch
prior_pipeline = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1-prior", torch_dtype=torch.float16, use_safetensors=True).to("cuda")
pipeline = DiffusionPipeline.from_pretrained("kandinsky-community/kandinsky-2-1", torch_dtype=torch.float16, use_safetensors=True).to("cuda")
prompt = "A cheeseburger, white background"
image_embeds, negative_image_embeds = prior_pipeline(prompt, guidance_scale=1.0).to_tuple()
image = pipeline(
prompt,
image_embeds=image_embeds,
negative_image_embeds=negative_image_embeds,
).images[0]
image.save("burger.png")
將芝士漢堡傳遞給 ShapEImg2ImgPipeline 以生成其 3D 表示。
from PIL import Image
from diffusers import ShapEImg2ImgPipeline
from diffusers.utils import export_to_gif
pipe = ShapEImg2ImgPipeline.from_pretrained("openai/shap-e-img2img", torch_dtype=torch.float16, variant="fp16").to("cuda")
guidance_scale = 3.0
image = Image.open("burger.png").resize((256, 256))
images = pipe(
image,
guidance_scale=guidance_scale,
num_inference_steps=64,
frame_size=256,
).images
gif_path = export_to_gif(images[0], "burger_3d.gif")


生成網格
Shap-E 是一個靈活的模型,也可以生成帶紋理的網格輸出,以便在下游應用中渲染。在此示例中,您將輸出轉換為 `glb` 檔案,因為 🤗 Datasets 庫支援 `glb` 檔案的網格視覺化,可以透過 Dataset viewer 進行渲染。
透過將 `output_type` 引數指定為 `"mesh"`,您可以為 ShapEPipeline 和 ShapEImg2ImgPipeline 生成網格輸出
import torch
from diffusers import ShapEPipeline
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
pipe = ShapEPipeline.from_pretrained("openai/shap-e", torch_dtype=torch.float16, variant="fp16")
pipe = pipe.to(device)
guidance_scale = 15.0
prompt = "A birthday cupcake"
images = pipe(prompt, guidance_scale=guidance_scale, num_inference_steps=64, frame_size=256, output_type="mesh").images
使用 `export_to_ply()` 函式將網格輸出儲存為 `ply` 檔案
您可以選擇使用 `export_to_obj()` 函式將網格輸出儲存為 `obj` 檔案。能夠以各種格式儲存網格輸出使其在下游使用中更加靈活!
from diffusers.utils import export_to_ply
ply_path = export_to_ply(images[0], "3d_cake.ply")
print(f"Saved to folder: {ply_path}")
然後,您可以使用 trimesh 庫將 `ply` 檔案轉換為 `glb` 檔案
import trimesh
mesh = trimesh.load("3d_cake.ply")
mesh_export = mesh.export("3d_cake.glb", file_type="glb")
預設情況下,網格輸出的焦點是從底部視角,但您可以透過應用旋轉變換來更改預設視角
import trimesh
import numpy as np
mesh = trimesh.load("3d_cake.ply")
rot = trimesh.transformations.rotation_matrix(-np.pi / 2, [1, 0, 0])
mesh = mesh.apply_transform(rot)
mesh_export = mesh.export("3d_cake.glb", file_type="glb")
將網格檔案上傳到您的資料集儲存庫,以便在資料集檢視器中視覺化!
