3D 機器學習課程文件

實戰

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

實戰

Open In Colab

本次實戰的目標是使用 LGM(大型高斯模型)作為示例,構建一個文字到點雲管線。

這包括生成 3D 管線的兩個部分:

  1. 多檢視擴散
  2. ML 友好的 3D(高斯濺射)

設定

開啟上面連結的 Colab notebook。點選 Runtime -> Change runtime type 並選擇 GPU 作為硬體加速器。

然後,開始安裝必要的依賴項。

!pip install -r https://huggingface.co/spaces/dylanebert/LGM-mini/raw/main/requirements.txt
!pip install https://huggingface.co/spaces/dylanebert/LGM-mini/resolve/main/wheel/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl

和之前一樣,如果 notebook 要求您重新啟動會話,請照做,然後重新執行程式碼塊。

載入模型

就像在多檢視擴散 notebook 中一樣,載入預訓練的多檢視擴散模型。

import torch
from diffusers import DiffusionPipeline

image_pipeline = DiffusionPipeline.from_pretrained(
    "dylanebert/multi-view-diffusion",
    custom_pipeline="dylanebert/multi-view-diffusion",
    torch_dtype=torch.float16,
    trust_remote_code=True,
).to("cuda")

這是因為多檢視擴散是 LGM 管線的第一步。

然後,載入生成式高斯濺射模型,這是 LGM 的主要貢獻。

splat_pipeline = DiffusionPipeline.from_pretrained(
    "dylanebert/LGM",
    custom_pipeline="dylanebert/LGM",
    torch_dtype=torch.float16,
    trust_remote_code=True,
).to("cuda")

載入圖片

和之前一樣,載入著名的貓雕像圖片。

import requests
from PIL import Image
from io import BytesIO

image_url = "https://huggingface.co/datasets/dylanebert/3d-arena/resolve/main/inputs/images/a_cat_statue.jpg"
response = requests.get(image_url)
image = Image.open(BytesIO(response.content))
image

執行管線

最後,將圖片透過兩個管線。輸出將是一個點雲資料矩陣,可以使用 splat_pipeline.save_ply() 儲存。

import numpy as np
from google.colab import files

input_image = np.array(image, dtype=np.float32) / 255.
multi_view_images = image_pipeline("", input_image, guidance_scale=5, num_inference_steps=30, elevation=0)

Multi-view Cats

splat = splat_pipeline(multi_view_images)

output_path = "/tmp/output.ply"
splat_pipeline.save_ply(splat, output_path)
files.download(output_path)

這包括 files.download(),用於在 Colab 中執行 notebook 時將檔案下載到本地機器。如果您在本地執行 notebook,可以刪除此行。

恭喜!您已成功執行 LGM 管線。

Gradio 演示

現在,讓我們建立一個 Gradio 演示,透過易於使用的介面端到端執行模型。

import gradio as gr

def run(image):
    input_image = image.astype("float32") / 255.0
    images = image_pipeline("", input_image, guidance_scale=5, num_inference_steps=30, elevation=0)
    splat = splat_pipeline(images)
    output_path = "/tmp/output.ply"
    splat_pipeline.save_ply(splat, output_path)
    return output_path

demo = gr.Interface(fn=run, inputs="image", outputs=gr.Model3D())
demo.launch()

這將建立一個 Gradio 演示,它以影像作為輸入並輸出 3D 點雲。

< > 在 GitHub 上更新

© . This site is unofficial and not affiliated with Hugging Face, Inc.