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 上更新