2D画像から3Dモデルを生成!TRELLISによる画像→3D再構成の試み on Paperspace Gradient

はじめに

ChatGPTで生成した1枚の2D画像から、3次元の立体モデルを自動生成できたら…そんな夢のような技術が、実は深層学習と3Dコンピュータビジョンの進化によって、実現可能になりつつあります。
本記事では、Microsoft Researchの「TRELLIS(Text-Reconstructed 3D Latent Scene)」を用いて、1枚の画像から3Dモデルを生成するまでの流れを解説します。

TRELLIS: https://github.com/microsoft/TRELLIS

最近のChatGPTは飛躍的に進化しており,以下のような画像や,

以下のような漫画が作れます.

このキャラクターが気に入ったので、今回はこのキャラを3Dモデル化していきます。


環境のセットアップ

まずは、GPU環境と必要ライブラリの準備です。なお,GPUはPaperspace GradientのA6000(VRAM 48GB)を使っています。

!nvidia-smi
!nvcc -V
!pip list

CUDAやPyTorchのバージョンを確認して、適切なバージョンで実行可能かを確認します。

また、Import関連のエラーが出たら上記のコードを実行して、どこが想定通りにインストールされていないかを確認します。

CUDA環境の構築

以下のようにCUDAツールキットをバージョン12.4に変更します.

# CUDAをバージョン12.4に変更
!sudo apt-get --purge remove "cuda" -y
!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
!sudo dpkg -y -i cuda-keyring_1.1-1_all.deb
!sudo apt-get update
!sudo apt-get -y install cuda-toolkit-12-4

インストールが終わったらカーネルをリスタートします.


依存ライブラリのインストール

以下のようにTrellisのリポジトリのコピーや、依存ライブラリ(Open3D, PyTorch, Gradio など)を一括で整備しています。

# Trellisのリポジトリをcloneする
!git clone --recurse-submodules https://github.com/microsoft/TRELLIS.git
%cd /notebooks/TRELLIS

# 関係パッケージをインストール
!pip install --ignore-installed open3d
!pip install --upgrade pip setuptools wheel
!pip install torch==2.5.1 torchvision --index-url=https://download.pytorch.org/whl/cu124
!pip install xformers==0.0.28.post3 --index-url=https://download.pytorch.org/whl/cu124
!pip install pillow imageio imageio-ffmpeg tqdm easydict opencv-python-headless scipy ninja rembg onnxruntime trimesh xatlas pyvista pymeshfix igraph transformers
!pip install git+https://github.com/EasternJournalist/utils3d.git@9a4eb15e4021b67b12c460c7057d642626897ec8
!pip install https://github.com/Dao-AILab/flash-attention/releases/download/v2.7.1.post1/flash_attn-2.7.1.post1+cu12torch2.5cxx11abiFALSE-cp311-cp311-linux_x86_64.whl
!pip install kaolin -f https://nvidia-kaolin.s3.us-east-2.amazonaws.com/torch-2.5.1_cu124.html

!git clone https://github.com/NVlabs/nvdiffrast.git ./tmp/extensions/nvdiffrast
!pip install ./tmp/extensions/nvdiffrast

!git clone --recurse-submodules https://github.com/JeffreyXiang/diffoctreerast.git ./tmp/extensions/diffoctreerast
!pip install ./tmp/extensions/diffoctreerast

!git clone https://github.com/autonomousvision/mip-splatting.git ./tmp/extensions/mip-splatting
!pip install ./tmp/extensions/mip-splatting/submodules/diff-gaussian-rasterization/

%cp -r ./extensions/vox2seq ./tmp/extensions/vox2seq
!pip install ./tmp/extensions/vox2seq

!pip install spconv-cu120
!pip install gradio==4.44.1 gradio_litmodel3d==0.0.1

!pip install --upgrade packaging==23.1
!pip install matplotlib==3.6.3

!set ATTN_BACKEND=flash-attn
!set SPCONV_ALGO=native

TRELLISのリポジトリはMicrosoftによって公開されており、3D表現の構築とレンダリングを含むパイプラインが詰め込まれています。


画像→3Dパイプラインの実行

以下が本ノートブックの核となる部分です。まずは先ほどの画像(以下ではtest.pngとリネーム済み)を「/notebooks/TRELLIS」にアップロードしておきます。その後、以下のコードを実行します。

import os

# Can be 'flash-attn' or 'xformers', default is 'flash-attn'
# os.environ['ATTN_BACKEND'] = 'xformers'

# Can be 'native' or 'auto', default is 'auto'. 'auto' is faster but will do benchmarking at the beginning. Recommended to set to 'native' if run only once.

os.environ['SPCONV_ALGO'] = 'native'

import imageio
from PIL import Image
from trellis.pipelines import TrellisImageTo3DPipeline
from trellis.utils import render_utils, postprocessing_utils

# パイプラインの初期化(学習済みモデルを読み込む)
pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
pipeline.cuda()

# 入力画像の読み込み
image = Image.open("/notebooks/TRELLIS/test.png")

# パイプラインの実行

outputs = pipeline.run(
image,
seed=1,
# Optional parameters
# sparse_structure_sampler_params={
# "steps": 12,
# "cfg_strength": 7.5,
# },
# slat_sampler_params={
# "steps": 12,
# "cfg_strength": 3,
# },
)
# outputs is a dictionary containing generated 3D assets in different formats:
# - outputs['gaussian']: a list of 3D Gaussians
# - outputs['radiance_field']: a list of radiance fields
# - outputs['mesh']: a list of meshes

ここでは、画像ファイル(test.png)を読み込んだあと、TrellisImageTo3DPipelineを通じて、画像から以下の3つの3D情報を生成します.

  • Gaussians(点群的な3D表現)
  • Radiance Field(放射場ベースの表現)
  • Mesh(ポリゴンメッシュ)

5: 結果のレンダリングと可視化

生成された3D出力を回転表示させて、動画として保存しています。

video = render_utils.render_video(outputs['gaussian'][0])['color']
imageio.mimsave("sample_gs.mp4", video, fps=30)

video = render_utils.render_video(outputs['radiance_field'][0])['color']
imageio.mimsave("sample_rf.mp4", video, fps=30)

video = render_utils.render_video(outputs['mesh'][0])['normal']
imageio.mimsave("sample_mesh.mp4", video, fps=30)

これにより、生成された3Dモデルを視覚的に確認できます。


3Dファイルのエクスポート(GLB/PLY)

GLB形式やPLY形式で保存することで、外部ソフトでも利用可能になります。

# GLB形式への変換と保存
glb = postprocessing_utils.to_glb(
outputs['gaussian'][0],
outputs['mesh'][0],
# Optional parameters
simplify=0.95, # Ratio of triangles to remove in the simplification process
texture_size=1024, # Size of the texture used for the GLB
)
glb.export("sample.glb")

# PLY形式での保存
outputs['gaussian'][0].save_ply("sample.ply")

GLB形式はWebでも利用可能な3Dファイル形式で、SketchfabやThree.jsなどとの連携にも向いています。

結果

以下の3Dモデルが得られました。


まとめ

このノートブックでは、TRELLISモデルを使って、2D画像を3Dに再構成する一連の流れを体験しました。
このような技術は以下のような応用が期待されています:

  • バーチャル試着、ARアバター生成
  • ゲーム開発における3Dアセット自動生成
  • 建築・製造業でのデジタルツイン構築

なお,このブログ記事はソースコードのみをChatGPTに読み込ませ、生成させたものを少し修正したものです。


【後日】モデルを3Dプリントしてみた

Blenderを用いてGLBファイルをOBJ形式に変換し、スライサーソフトでスライス後に印刷してみた。
小さくて足が折れたが、当然印刷はできる。

コメント

タイトルとURLをコピーしました