Shape-E 教程:如何设置和使用 Shap-E 模型

1,800 阅读4分钟

Shap-E是OpenAI开发的一种创新模型,它可以使用文本或图像作为输入来生成一系列3D对象,从而改变了3D应用领域。这项非凡的技术可以在GitHub上免费访问,用户可以在自己的计算机上无缝运行它,不需要OpenAI API密钥或互联网连接。Shap-E的多功能性也是其与众不同之处,用户可以将生成的3D对象在Microsoft Paint 3D等软件中打开,甚至可以转换成STL文件进行3D打印。这项技术正在重新定义我们处理文本到3D和图像到3D生成的方式,以及人工智能应用程序可以从中产生哪些创造性的可能性。

在本教程中,我们将学习如何在Google Colab中创建笔记本,设置和使用OpenAI的Shap-E模型生成3D模型,并使用Blender Studio对其进行自定义。

首先,您需要前往blender.org并下载与您的操作系统兼容的Blender Studio。

接下来,转到Google Colab并创建一个新的笔记本。! image.png 在 Google Colab 中创建新笔记本


现在,我们需要将 Shap-E 存储库克隆到我们的 Google Colab Notebook。

!git clone https://github.com/openai/shap-e

进入目录并安装要求。

%cd shap-e!pip install -e .

添加新的code cell.

在这里,我们将导入所有必要的库。

import torchfrom shap_e.diffusion.sample import sample_latentsfrom shap_e.diffusion.gaussian_diffusion import diffusion_from_configfrom shap_e.models.download import load_model, load_configfrom shap_e.util.notebooks import create_pan_cameras, decode_latent_images, gif_widget

请点击“Run”按钮或按下“CMD/CTRL + Enter”键来运行单个代码块。

现在,我们将设置设备为CUDA(如果可用),否则设置为CPU。

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

单击RunCMD/CTRL + Enter

添加新的code cell.

在这里,我们将加载模型和权重。

xm = load_model('transmitter', device=device) model = load_model('text300M', device=device) diffusion = diffusion_from_config(load_config('diffusion'))

请点击“Run”或按下“CMD/CTRL + Enter”键。

请耐心等待,加载模型和权重需要一些时间。对我来说,这大约花费了5分钟的时间。但是,这取决于您的互联网连接速度。

接下来,我们将生成一个3D模型。

batch_size = 1 # this is the size of the models, higher values take longer to generate.guidance_scale = 15.0 # this is the scale of the guidance, higher values make the model look more like the prompt.prompt = "a donut" # this is the prompt, you can change this to anything you want.latents = sample_latents(
    batch_size=batch_size,
    model=model,
    diffusion=diffusion,
    guidance_scale=guidance_scale,
    model_kwargs=dict(texts=[prompt] * batch_size),
    progress=True,
    clip_denoised=True,
    use_fp16=True,
    use_karras=True,
    karras_steps=64,
    sigma_min=1E-3,
    sigma_max=160,
    s_churn=0,)

单击RunCMD/CTRL + Enter

生成 3D 模型需要一些时间,根据您的batch_size更高batch_size将需要更长的时间来生成 3D 模型。对我来说,生成 3D 模型大约需要 22 秒batch_size=1

添加新的code cell.

这里我们将渲染 3D 模型,使用render_mode = 'nerf' Neural Radiance Fields (NeRF)来渲染 3D 模型。您可以将其更改为使用样式传递函数 (STF)render_mode = 'stf' 渲染模式渲染 3D 模型。

render_mode = 'nerf' # you can change this to 'stf'size = 64 # this is the size of the renders, higher values take longer to render.cameras = create_pan_cameras(size, device)for i, latent in enumerate(latents):
    images = decode_latent_images(xm, latent, cameras, rendering_mode=render_mode)
    display(gif_widget(images))

请点击“Run”或按下“CMD/CTRL + Enter”键。

你看到模型旋转了吗?很酷,是吗?

接下来,我们将把3D模型保存为.ply和.obj文件。

请注意:.obj文件将在稍后在Blender Studio中用于自定义。

# Example of saving the latents as meshes.from shap_e.util.notebooks import decode_latent_meshfor i, latent in enumerate(latents):
    t = decode_latent_mesh(xm, latent).tri_mesh()
    with open(f'example_mesh_{i}.ply''wb'as f: # this is three-dimensional geometric data of model.
        t.write_ply(f)
    with open(f'example_mesh_{i}.obj''w'as f: # we will use this file to customize in Blender Studio later.
        t.write_obj(f)

单击RunCMD/CTRL + Enter

将选项卡切换到Files并点击刷新。您将看到example_mesh_0.plyexample_mesh_0.obj文件。谷歌 Colab 文件

谷歌 Colab 文件

将文件下载 .obj到本地计算机。


打开 Blender Studio 并创建新项目。搅拌机工作室

搅拌机工作室

删除默认多维数据集。删除默认立方体

删除默认立方体

点击FileImportWavefront (.obj) 。选择 .obj您从 Google Colab 下载的文件。导入 .obj 文件

导入 .obj 文件

您应该会在中心看到 3D 模型。3D模型

3D模型

它本身看起来很牛,顺便说一句,Shap-E 做得很好。

您可以根据需要自定义 3D 模型。

出于演示目的,我简单地使用粉红色的彩色浇头和棕色的甜甜圈。定制3D模型

定制3D模型Midjourney 的 AI 生成艺术


Midjourney 的 AI 生成艺术

在这个教程中,我们学习了如何使用Google Colab来设置和使用Shape-E模型。此外,我们还研究了Blender Studio并尝试了自定义生成的3D模型。