ML for 3D 课程文档

在 notebook 中运行

Hugging Face's logo
加入 Hugging Face 社区

并获取增强的文档体验

开始使用

在 notebook 中运行

Open In Colab

首先,可以在 Colab notebook 中运行。这是快速验证代码的最简单方法。

设置

  1. 点击上方的 “在 Colab 中打开” 按钮。
  2. 将运行时类型更改为 GPU。
  3. 向下滚动到 Run in this notebook section 部分。

运行演示

首先安装依赖项。

!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"
    )
)

构建 pipeline

构建 LGM pipeline。将 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")

定义 run 函数

定义 run 函数,该函数接受图像并返回 ply 文件。

  1. 将图像转换为 numpy 数组并将其归一化到 [0, 1]。
  2. 使用默认参数运行 pipeline。
  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 上更新