腾讯最强3D生成模型,详述搭建环境和使用过程

1,343 阅读6分钟

介绍

这两天腾讯终于放出了大招了,其中有一款框架,可以直接文生3d模型,或者图片生3d模型,下面是它的一个原理图

image.png

  • 模型采用两阶段生成方法,在保证质量和可控的基础上,仅需10秒即可生成3D资产。
  • 在第一阶段,我们采用了一种多视角扩散模型,轻量版模型能够在大约4秒内高效生成多视角图像,这些多视角图像从不同的视角捕捉了3D资产的丰富的纹理和几何先验,将任务从单视角重建松弛到多视角重建。
  • 在第二阶段,我们引入了一种前馈重建模型,利用上一阶段生成的多视角图像。该模型能够在大约3秒内快速而准确地重建3D资产。重建模型学习处理多视角扩散引入的噪声和不一致性,并利用条件图像中的可用信息高效恢复3D结构。最终,该模型可以实现输入任意单视角实现三维生成。

效果展示

Prompt:一个卡通风格的阿拉伯人穿着白色长袍和头巾,双手叉腰,背景为白色。

part_2-4.gif

Prompt:这是一座卡通风格的小房子,由木材和纸板制成,外观呈现棕黄色,背景为白色。

part_2-5.gif

可以看出效果还是很惊人的,下面我们开始搭建本地的环境吧。

将仓库拉到本地

我将 hunyuan3D 仓库 拉下来直接放到了 D 盘下面,也就是 D:\Hunyuan3D-1 。

下载模型

hunyuan3d模型所有的文件都下载下来。在 D:\Hunyuan3D-1 中创建文件夹 weights ,然后将下载的模型都放进去。

HunyuanDiT-v1.1-Diffusers-Distilled所有文件都下载下来,在 D:\Hunyuan3D-1\weights 下面新建文件夹 hunyuanDiT ,然后将下载下来的模型文件都放进去。

创建虚拟环境

首先创建 3.10 的 python 虚拟环境

conda create -n hunyuan3d python=3.10
conda activate hunyuan3d

因为我的 cuda 是 12.1 把那本,使用下面命令安装 torch 等相关库

pip install torch==2.2.0 torchvision==0.17.0 torchaudio==2.2.0 --index-url https://download.pytorch.org/whl/cu121

进入 D:\Hunyuan3D-1 目录下面,修改 requirements.txt 文件如下,安装下面的库。

diffusers==0.31.0
numpy==1.26.4
transformers==4.46.2
rembg==2.0.59
tqdm==4.67.0
omegaconf==2.3.0
matplotlib==3.9.2
opencv-python==4.10.0.84
imageio==2.36.0
jaxtyping==0.2.34
einops==0.8.0
sentencepiece==0.2.0
accelerate==1.1.1
trimesh==4.5.2
PyMCubes==0.1.6
xatlas==0.0.9
libigl==2.5.1
# pytorch3d==0.7.6
git+https://github.com/facebookresearch/pytorch3d@stable
# nvdiffrast==0.3.3
git+https://github.com/NVlabs/nvdiffrast
open3d==0.18.0
ninja==1.11.1.1

文生3D模型测试

在 D:\Hunyuan3D-1 目录下面使用下面的命令进行生成。

 python main.py  --text_prompt "一颗红色的柳树"  --save_folder ./outputs/liushu/  --max_faces_num 90000  --do_texture_mapping  --do_render

如果报错 from pytorch3d import _C 请直接跳过看最后一节。

如果正常运行,日志如下:

(hunyuan3d) PS D:\Hunyuan3D-1>  python main.py  --text_prompt "一颗红色的柳树"  --save_folder ./outputs/liushu/  --max_faces_num 90000  --do_texture_mapping  --do_render
Loading pipeline components...: 100%|████████████████████████████████████████████████████| 7/7 [00:00<00:00, 15.47it/s]
image2views unet model {'Total': 2567463684, 'Trainable': 0}
None pretrained model for dinov2 encoder ...
DEFAULT_RENDERING_KWARGS
{'ray_start': 'auto', 'ray_end': 'auto', 'box_warp': 1.2, 'white_back': True, 'disparity_space_sampling': False, 'clamp_mode': 'softplus', 'sampler_bbox_min': -0.6, 'sampler_bbox_max': 0.6}
SVRMModel has 458.69 M params.
Load model successfully
=====> mv23d model init time: 3.292725086212158
view2mesh model {'Total': 458688965, 'Trainable': 0}
Loading checkpoint shards: 100%|█████████████████████████████████████████████████████████| 2/2 [00:01<00:00,  1.22it/s]
Loading pipeline components...: 100%|████████████████████████████████████████████████████| 7/7 [00:06<00:00,  1.13it/s]
You have disabled the safety checker for <class 'diffusers.pipelines.pag.pipeline_pag_hunyuandit.HunyuanDiTPAGPipeline'> by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .
text2image transformer model {'Total': 1516534048, 'Trainable': 0}
prompt is: 一颗红色的柳树
100%|██████████████████████████████████████████████████████████████████████████████████| 25/25 [00:18<00:00,  1.34it/s]
[HunYuan3D]-[text to image], cost time: 20.4802s
[HunYuan3D]-[remove background], cost time: 0.4009s
100%|██████████████████████████████████████████████████████████████████████████████████| 50/50 [00:13<00:00,  3.57it/s]
[HunYuan3D]-[image to views], cost time: 19.2197s
./outputs/liushu/
=====> Triplane forward time: 228.2751338481903
reduce face: 181869 -> 90000
=====> generate mesh with vertex shading time: 3.9938809871673584
Using xatlas to perform UV unwrapping, may take a while ...
=====> generate mesh with texture shading time: 16.907540321350098
[HunYuan3D]-[views to mesh], cost time: 249.4986s
[HunYuan3D]-[gif render], cost time: 9.6563s

总共显存消耗23.5G ,耗时 197s 左右。在 D:\Hunyuan3D-1\outputs\liushu 目录下面会有如下的目录:

点击 output.gif 可以看到一棵红色的柳树,尽管有点丑,后面需要调整 prompt 即可,起码是可以成功生成的。

图生3D模型测试

我用了一张实景三维的照片 building.png ,如下:

在 D:\Hunyuan3D-1 目录下面使用下面的命令进行生成,

python main.py  --image_prompt ./demos/building.png  --save_folder ./outputs/test/  --max_faces_num 90000 --do_texture   --do_render

如果报错 from pytorch3d import _C 可以跳过,请看最后一节解决方案。

如果正常运行,日志如下:

(hunyuan3d) PS D:\Hunyuan3D-1> python main.py  --image_prompt ./demos/building.png  --save_folder ./outputs/test/  --max_faces_num 90000 --do_texture   --do_render
Downloading data from 'https://github.com/danielgatis/rembg/releases/download/v0.0.0/u2net.onnx' to file 'C:\Users\13900K.u2net\u2net.onnx'.
100%|###############################################| 176M/176M [00:00<?, ?B/s]
Loading pipeline components...: 100%|████████████████████████████████████████████████████| 7/7 [00:00<00:00,  9.86it/s]
image2views unet model {'Total': 2567463684, 'Trainable': 0}
None pretrained model for dinov2 encoder ...
DEFAULT_RENDERING_KWARGS
{'ray_start': 'auto', 'ray_end': 'auto', 'box_warp': 1.2, 'white_back': True, 'disparity_space_sampling': False, 'clamp_mode': 'softplus', 'sampler_bbox_min': -0.6, 'sampler_bbox_max': 0.6}
SVRMModel has 458.69 M params.
Load model successfully
=====> mv23d model init time: 4.093821048736572
view2mesh model {'Total': 458688965, 'Trainable': 0}
[HunYuan3D]-[remove background], cost time: 0.2740s
100%|██████████████████████████████████████████████████████████████████████████████████| 50/50 [00:13<00:00,  3.65it/s]
[HunYuan3D]-[image to views], cost time: 14.5492s
./outputs/test/
=====> Triplane forward time: 76.13665747642517
reduce face: 116886 -> 90000
D:\Hunyuan3D-1\svrm\ldm\models\svrm.py:198: RuntimeWarning: invalid value encountered in power
  color = [color[0]**color_ratio, color[1]**color_ratio, color[2]**color_ratio]
=====> generate mesh with vertex shading time: 2.1429805755615234
Using xatlas to perform UV unwrapping, may take a while ...
=====> generate mesh with texture shading time: 38.4321174621582
[HunYuan3D]-[views to mesh], cost time: 116.7878s
[HunYuan3D]-[gif render], cost time: 7.3994s

总共显存消耗23.4G ,耗时 135s 左右。在 D:\Hunyuan3D-1\outputs\test 目录下面会有如下的目录

点击 mesh.obj 进行查看,是不是很牛X,可以自动生成一幢楼的 3d 模型。

Window 安装 pytorch3d教程

一般在执行命令的时候会报错 from pytorch3d import _C 报错 ,一般要先保证 cuda 和 pytorch 的版本是否版本正确并适配,另外就是因为 pytorch3d 本身的版本太高,降低一下即可。直接按照 二、pytorch3D安装 这一章节模仿一步一步做即可。

1、从官网下载 pytorch3d 0.7.6 的源码,解压之后将文件夹改成 pytorch3d,整个放入虚拟环境的库目录之下,我的是 D:\anaconda\envs\hunyuan3d\Lib\site-packages\

2、D:\anaconda\envs\hunyuan3d\Lib\site-packages\pytorch3d\setup.py 中的第 52 行代码改成如下:

extra_compile_args = {"cxx": []}

3、我这里只有 x64 Native Tools Command Prompt for VS 2022 , 使用这个终端也可以成功安装。使用管理员身份打开,进入你自己的虚拟环境,我这里是 hunyuan3d ,然后依次设置如下两个命令:

set DISTUTILS_USE_SDK=1
set PYTORCH3D_NO_NINJA=1

然后进行安装

python setup.py install

如果正常安装成功会打印如下日志:

查看安装的版本如下:

image.png

已经安装成功。返回上一节重新执行生成3d模型的命令即可。