3D 機器學習課程文件

在 Notebook 中執行

Hugging Face's logo
加入 Hugging Face 社群

並獲得增強的文件體驗

開始使用

在 Notebook 中執行

Open In Colab

第一種方式是在 Colab Notebook 中執行。這是快速驗證程式碼最簡單的方法。

設定

  1. 點選上方的“在 Colab 中開啟”按鈕。
  2. 將執行時型別更改為 GPU。
  3. 向下滾動到在此 Notebook 中執行部分。

執行演示

首先安裝依賴項。

!pip install -r https://huggingface.co/spaces/dylanebert/LGM-tiny/raw/main/requirements.txt

然後,執行演示程式碼。這與 Space app.py 中的程式碼完全相同。為確保您的模型按預期工作,請將所有 dylanebert/LGM-full 例項替換為您的 {username}/{model_name}。然後,執行程式碼。

import shlex
import subprocess

import gradio as gr
import numpy as np
import torch
from diffusers import DiffusionPipeline

subprocess.run(
    shlex.split(
        "pip install https://huggingface.co/spaces/dylanebert/LGM-mini/resolve/main/wheel/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl"
    )
)

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


def run(image):
    input_image = np.array(image, dtype=np.float32) / 255.0
    splat = pipeline(
        "", input_image, guidance_scale=5, num_inference_steps=30, elevation=0
    )
    splat_file = "/tmp/output.ply"
    pipeline.save_ply(splat, splat_file)
    return splat_file


demo = gr.Interface(
    fn=run,
    title="LGM Tiny",
    description="An extremely simplified version of [LGM](https://huggingface.co/ashawkey/LGM). Intended as resource for the [ML for 3D Course](https://huggingface.co/learn/ml-for-3d-course/unit0/introduction).",
    inputs="image",
    outputs=gr.Model3D(),
    examples=[
        "https://huggingface.co/datasets/dylanebert/iso3d/resolve/main/jpg@512/a_cat_statue.jpg"
    ],
    cache_examples=True,
    allow_duplication=True,
)
demo.queue().launch()

演示細分

讓我們細分演示程式碼。

匯入依賴項

匯入所需的庫。

import shlex
import subprocess

import gradio as gr
import numpy as np
import spaces
import torch
from diffusers import DiffusionPipeline

安裝 diff-gaussian-rasterization

對於 LGM 的高斯潑濺步驟,我們需要安裝一個自定義的 wheel 檔案。這是為了讓 Space 在 ZeroGPU 上執行的變通方法。

subprocess.run(
    shlex.split(
        "pip install https://huggingface.co/spaces/dylanebert/LGM-mini/resolve/main/wheel/diff_gaussian_rasterization-0.0.0-cp310-cp310-linux_x86_64.whl"
    )
)

構建管道

構建 LGM 管道。將 dylanebert/LGM-full 替換為您的 {username}/{model_name}

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

定義執行函式

定義執行函式,該函式接收影像並返回 ply 檔案。

  1. 將影像轉換為 numpy 陣列並將其歸一化到 [0, 1] 範圍。
  2. 使用預設引數執行管道。
  3. 將 ply 檔案儲存到 /tmp/output.ply
  4. 返回 ply 檔案。
@spaces.GPU
def run(image):
    input_image = np.array(image, dtype=np.float32) / 255.0
    splat = pipeline(
        "", input_image, guidance_scale=5, num_inference_steps=30, elevation=0
    )
    splat_file = "/tmp/output.ply"
    pipeline.save_ply(splat, splat_file)
    return splat_file

建立演示

使用 Gradio 建立演示,它為我們處理 UI。

demo = gr.Interface(
    fn=run,
    title="LGM Tiny",
    description="An extremely simplified version of [LGM](https://huggingface.co/ashawkey/LGM). Intended as resource for the [ML for 3D Course](https://huggingface.co/learn/ml-for-3d-course/unit0/introduction).",
    inputs="image",
    outputs=gr.Model3D(),
    examples=[
        "https://huggingface.co/datasets/dylanebert/iso3d/resolve/main/jpg@512/a_cat_statue.jpg"
    ],
    cache_examples=True,
    allow_duplication=True,
)
demo.queue().launch()

LGM Tiny Demo

< > 在 GitHub 上更新

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