TRELLIS による画像から3Dアセットへの変換(ソースコードと実行結果)

TRELLIS による画像から3Dアセットへの変換

概要

TRELLISは、Microsoftが開発したテキストや画像プロンプトから高品質な3Dアセットを生成する3Dアセット生成モデルである。Structured LATent (SLAT:構造化潜在表現)という統一された表現を使用して、Radiance Fields(輝度場)、3D Gaussians(3次元ガウシアン)、メッシュ(三角形網)など様々な形式で出力することができる。

主な特徴

インストール

IgorAherne氏によるTRELLISのフォーク版「trellis-stable-projectorz」を使用する。

このフォークの特徴

管理者権限でコマンドプロンプトを起動(手順:Windowsキーまたはスタートメニュー > cmd と入力 > 右クリック > 「管理者として実行」)し、以下を実行する。管理者権限は、wingetの--scope machineオプションでシステム全体にソフトウェアをインストールするために必要である。


curl -L -o v40_py311_trellis_stableprojectorz.zip https://github.com/IgorAherne/trellis-stable-projectorz/releases/download/latest/v40_py311_trellis_stableprojectorz.zip
mkdir C:\TRELLIS
tar -xf v40_py311_trellis_stableprojectorz.zip -C C:\TRELLIS
cd C:\TRELLIS
run-gradio-fp16.bat

WebUIの使用

http://127.0.0.1:7860 でGradioインターフェースにアクセスする。

参考文献

  1. 公式論文: Xiang, J., et al. "Structured 3D Latents for Scalable and Versatile 3D Generation" (2024) - arXiv:2412.01506
  2. Microsoft公式リポジトリ: https://github.com/microsoft/TRELLIS
  3. Microsoft公式プロジェクトページ: https://microsoft.github.io/TRELLIS/
  4. Hugging Face公式モデル: https://huggingface.co/JeffreyXiang/TRELLIS-image-large
  5. IgorAherne フォーク版: https://github.com/IgorAherne/trellis-stable-projectorz

TRELLIS による画像から3Dアセットへの変換プログラム

実行手順

環境のセットアップとコードの実行

cd C:\TRELLIS
code\venv\Scripts\activate.bat
python a.py

出力フォーマット

TRELLISは以下の形式で3Dアセットを生成する:

ソースコード


import os
import sys
import torch
from PIL import Image

# Set environment for Windows
os.environ['SPCONV_ALGO'] = 'native'
os.environ['ATTN_BACKEND'] = 'xformers'

# TRELLIS Parameters
SEED = 42
SPARSE_STRUCTURE_SAMPLER_PARAMS = {
    "steps": 12,
    "cfg_strength": 7.5,
}
SLAT_SAMPLER_PARAMS = {
    "steps": 12,
    "cfg_strength": 3.0,
}

# Add TRELLIS to path if not installed
sys.path.insert(0, './code')

def main():
    from trellis.pipelines import TrellisImageTo3DPipeline
    from trellis.utils import postprocessing_utils

    # Auto download and load pre-trained model
    print("Loading TRELLIS model...")
    pipeline = TrellisImageTo3DPipeline.from_pretrained("JeffreyXiang/TRELLIS-image-large")
    if torch.cuda.is_available():
        pipeline.cuda()
        print("Using GPU")
    else:
        print("Using CPU")

    # Load input image
    image_path = input("Enter image path: ")

    if not image_path or not os.path.exists(image_path):
        print("Invalid image path.")
        return

    image = Image.open(image_path)

    # Generate 3D model
    print("Generating 3D model...")
    outputs = pipeline.run(
        image,
        seed=SEED,
        sparse_structure_sampler_params=SPARSE_STRUCTURE_SAMPLER_PARAMS,
        slat_sampler_params=SLAT_SAMPLER_PARAMS
    )

    # Save outputs
    mesh = outputs['mesh'][0]
    glb = postprocessing_utils.to_glb(
        outputs['gaussian'][0],
        mesh,
        simplify=0.95,
        texture_size=1024
    )
    glb.export("output_3d_model.glb")
    print("3D model saved as output_3d_model.glb")

if __name__ == "__main__":
    main()