Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,35 +1,32 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import subprocess
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
# [AUTO-INSTALL] accelerate
|
| 6 |
try:
|
| 7 |
import accelerate
|
| 8 |
except ImportError:
|
| 9 |
-
print("โ ๏ธ accelerate not found. Installing now...")
|
| 10 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "accelerate"])
|
| 11 |
-
print("โ
accelerate installed.")
|
| 12 |
|
| 13 |
-
# [์ค์] OOM ๋ฐฉ์ง ์ค์
|
| 14 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 15 |
os.environ['SPCONV_ALGO'] = 'native'
|
| 16 |
|
| 17 |
-
import gradio as gr
|
| 18 |
-
from gradio_litmodel3d import LitModel3D
|
| 19 |
-
import shutil
|
| 20 |
-
from typing import *
|
| 21 |
import torch
|
| 22 |
import torch.nn as nn
|
| 23 |
import numpy as np
|
| 24 |
import imageio
|
| 25 |
-
import gc
|
| 26 |
from easydict import EasyDict as edict
|
| 27 |
from PIL import Image
|
|
|
|
|
|
|
|
|
|
| 28 |
from trellis.pipelines import TrellisVGGTTo3DPipeline
|
| 29 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 30 |
from trellis.utils import render_utils, postprocessing_utils
|
| 31 |
-
|
| 32 |
-
# [์์ ] infer_auto_device_map ์ถ๊ฐ ์ํฌํธ
|
| 33 |
from accelerate import dispatch_model, infer_auto_device_map
|
| 34 |
|
| 35 |
MAX_SEED = np.iinfo(np.int32).max
|
|
@@ -88,7 +85,6 @@ def pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:
|
|
| 88 |
|
| 89 |
def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
| 90 |
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
| 91 |
-
|
| 92 |
gs = Gaussian(
|
| 93 |
aabb=state['gaussian']['aabb'],
|
| 94 |
sh_degree=state['gaussian']['sh_degree'],
|
|
@@ -150,7 +146,7 @@ def generate_and_extract_glb(
|
|
| 150 |
)
|
| 151 |
except Exception as e:
|
| 152 |
torch.cuda.empty_cache()
|
| 153 |
-
raise RuntimeError(f"Generation Failed: {str(e)}
|
| 154 |
|
| 155 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 156 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
|
@@ -177,7 +173,6 @@ def extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:
|
|
| 177 |
gs, _ = unpack_state(state)
|
| 178 |
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
| 179 |
gs.save_ply(gaussian_path)
|
| 180 |
-
|
| 181 |
del gs
|
| 182 |
torch.cuda.empty_cache()
|
| 183 |
return gaussian_path, gaussian_path
|
|
@@ -222,9 +217,7 @@ demo = gr.Blocks(
|
|
| 222 |
"""
|
| 223 |
)
|
| 224 |
with demo:
|
| 225 |
-
gr.Markdown(""
|
| 226 |
-
# ๐ป ReconViaGen (Fixed Multi-GPU)
|
| 227 |
-
""")
|
| 228 |
|
| 229 |
with gr.Row():
|
| 230 |
with gr.Column():
|
|
@@ -311,34 +304,41 @@ with demo:
|
|
| 311 |
if __name__ == "__main__":
|
| 312 |
print("๐ Initializing Pipeline...")
|
| 313 |
pipeline = TrellisVGGTTo3DPipeline.from_pretrained("esther11/trellis-vggt-v0-2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 314 |
|
| 315 |
gpu_count = torch.cuda.device_count()
|
| 316 |
print(f"โก Detected {gpu_count} GPUs.")
|
| 317 |
|
| 318 |
if gpu_count > 1:
|
| 319 |
-
print("โก Multi-GPU Mode
|
| 320 |
|
| 321 |
-
# [์์ ]
|
| 322 |
-
#
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
|
|
|
|
|
|
| 339 |
|
| 340 |
else:
|
| 341 |
print("โ ๏ธ Warning: Only 1 GPU detected.")
|
| 342 |
-
pipeline.cuda()
|
| 343 |
|
| 344 |
demo.launch()
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
import subprocess
|
| 4 |
+
import gc
|
| 5 |
+
import shutil
|
| 6 |
+
from typing import *
|
| 7 |
|
| 8 |
+
# [AUTO-INSTALL] accelerate
|
| 9 |
try:
|
| 10 |
import accelerate
|
| 11 |
except ImportError:
|
|
|
|
| 12 |
subprocess.check_call([sys.executable, "-m", "pip", "install", "accelerate"])
|
|
|
|
| 13 |
|
| 14 |
+
# [์ค์] OOM ๋ฐฉ์ง ๋ฐ ์๊ณ ๋ฆฌ์ฆ ์ค์
|
| 15 |
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 16 |
os.environ['SPCONV_ALGO'] = 'native'
|
| 17 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
import torch
|
| 19 |
import torch.nn as nn
|
| 20 |
import numpy as np
|
| 21 |
import imageio
|
|
|
|
| 22 |
from easydict import EasyDict as edict
|
| 23 |
from PIL import Image
|
| 24 |
+
import gradio as gr
|
| 25 |
+
from gradio_litmodel3d import LitModel3D
|
| 26 |
+
|
| 27 |
from trellis.pipelines import TrellisVGGTTo3DPipeline
|
| 28 |
from trellis.representations import Gaussian, MeshExtractResult
|
| 29 |
from trellis.utils import render_utils, postprocessing_utils
|
|
|
|
|
|
|
| 30 |
from accelerate import dispatch_model, infer_auto_device_map
|
| 31 |
|
| 32 |
MAX_SEED = np.iinfo(np.int32).max
|
|
|
|
| 85 |
|
| 86 |
def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:
|
| 87 |
device = 'cuda:0' if torch.cuda.is_available() else 'cpu'
|
|
|
|
| 88 |
gs = Gaussian(
|
| 89 |
aabb=state['gaussian']['aabb'],
|
| 90 |
sh_degree=state['gaussian']['sh_degree'],
|
|
|
|
| 146 |
)
|
| 147 |
except Exception as e:
|
| 148 |
torch.cuda.empty_cache()
|
| 149 |
+
raise RuntimeError(f"Generation Failed: {str(e)}")
|
| 150 |
|
| 151 |
video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']
|
| 152 |
video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']
|
|
|
|
| 173 |
gs, _ = unpack_state(state)
|
| 174 |
gaussian_path = os.path.join(user_dir, 'sample.ply')
|
| 175 |
gs.save_ply(gaussian_path)
|
|
|
|
| 176 |
del gs
|
| 177 |
torch.cuda.empty_cache()
|
| 178 |
return gaussian_path, gaussian_path
|
|
|
|
| 217 |
"""
|
| 218 |
)
|
| 219 |
with demo:
|
| 220 |
+
gr.Markdown("# ๐ป ReconViaGen (Corrected Multi-GPU)")
|
|
|
|
|
|
|
| 221 |
|
| 222 |
with gr.Row():
|
| 223 |
with gr.Column():
|
|
|
|
| 304 |
if __name__ == "__main__":
|
| 305 |
print("๐ Initializing Pipeline...")
|
| 306 |
pipeline = TrellisVGGTTo3DPipeline.from_pretrained("esther11/trellis-vggt-v0-2")
|
| 307 |
+
|
| 308 |
+
# [ํต์ฌ ์์ ] ํ์ดํ๋ผ์ธ ์ ์ฒด๋ฅผ ๋จผ์ CUDA:0์ผ๋ก ์ด๋
|
| 309 |
+
# ์ด๋ ๊ฒ ํด์ผ pipeline.device๊ฐ "cuda:0"์ผ๋ก ์ค์ ๋๊ณ ,
|
| 310 |
+
# preprocess_image ํธ์ถ ์ ์
๋ ฅ ํ
์๊ฐ ์๋์ผ๋ก GPU๋ก ์ด๋ํ์ฌ birefnet(GPU0)๊ณผ ๋ง๋ฉ๋๋ค.
|
| 311 |
+
pipeline.cuda()
|
| 312 |
|
| 313 |
gpu_count = torch.cuda.device_count()
|
| 314 |
print(f"โก Detected {gpu_count} GPUs.")
|
| 315 |
|
| 316 |
if gpu_count > 1:
|
| 317 |
+
print("โก Multi-GPU Mode: Distributing VGGT model.")
|
| 318 |
|
| 319 |
+
# [ํต์ฌ ์์ ] VGGT ๋ชจ๋ธ๋ง ๋ค์ CPU๋ก ๋ด๋ ค์ ๋งต์ ๊ณ์ฐํ๊ฑฐ๋,
|
| 320 |
+
# ๊ทธ๋ฅ infer_auto_device_map์ ๋งก๊น๋๋ค.
|
| 321 |
+
# ์ด๋ฏธ GPU0์ ๋ค ์ฌ๋ผ๊ฐ ์์ผ๋ฏ๋ก, 'max_memory' ์ ํ์ ์ฃผ์ง ์์ผ๋ฉด "GPU0์ ๋ค ๋ค์ด๊ฐ๋ค?" ํ๊ณ ๋ถ์ฐ์ ์ ํ ์ ์์ต๋๋ค.
|
| 322 |
+
# ๋ฐ๋ผ์ VGGT๋ฅผ ๊ฐ์ ๋ก ์ฐข๊ธฐ ์ํด max_memory๋ฅผ ์ค์ ํฉ๋๋ค.
|
| 323 |
+
|
| 324 |
+
# ์: ๊ฐ GPU๋น 12GB ์ ๋๋ง ์ฐ๋๋ก ์ ๋ํ์ฌ ๋ถ์ฐ์ ๊ฐ์ ํจ (VGGT 5GB + ํ์ฑ ๋ฉ๋ชจ๋ฆฌ ๊ณ ๋ ค)
|
| 325 |
+
# ํน์ infer_auto_device_map์ด ํ์ฌ ํ ๋น๋ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ๊ณ ๋ คํ๋๋ก ํฉ๋๋ค.
|
| 326 |
+
|
| 327 |
+
pipeline.VGGT_model.cpu() # ๋งต ๊ณ์ฐ์ ์ํด ์ ์ CPU๋ก ์ด๋ (ํ์คํ ๋ถ์ฐ์ ์ํจ)
|
| 328 |
+
|
| 329 |
+
print(" - Calculating Device Map for VGGT...")
|
| 330 |
+
# ๋ชจ๋ GPU๋ฅผ ๊ณจ๊ณ ๋ฃจ ์ฐ๋๋ก ์ ๋
|
| 331 |
+
device_map = infer_auto_device_map(
|
| 332 |
+
pipeline.VGGT_model,
|
| 333 |
+
max_memory={i: "10GiB" for i in range(gpu_count)}, # ๊ฐ์ ๋ถํ ์ ์ํ ๋ฉ๋ชจ๋ฆฌ ์ ํ
|
| 334 |
+
no_split_module_classes=["Block", "ResnetBlock"] # ๋ ์ด์ด ์ค๊ฐ์ด ์งค๋ฆฌ์ง ์๋๋ก ๋ณดํธ
|
| 335 |
+
)
|
| 336 |
+
|
| 337 |
+
pipeline.VGGT_model = dispatch_model(pipeline.VGGT_model, device_map=device_map)
|
| 338 |
+
print("โ
VGGT Model dispatched.")
|
| 339 |
|
| 340 |
else:
|
| 341 |
print("โ ๏ธ Warning: Only 1 GPU detected.")
|
| 342 |
+
# ์ด๋ฏธ ์์์ pipeline.cuda() ํ์ผ๋ฏ๋ก ์ถ๊ฐ ์์
๋ถํ์
|
| 343 |
|
| 344 |
demo.launch()
|