Spaces:
Paused
Paused
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333 unmodified lines3738394041424344454647484950515253548 unmodified lines636465666768697071729 unmodified lines8283848586878889902 unmodified lines939495969798991001011021031041051061071081091101111121136 unmodified lines1201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541 unmodified line1561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861878 unmodified lines19619719819920020120216 unmodified lines2192202212222232242252 unmodified lines22822923023123223314 unmodified lines2482492502512522535 unmodified lines2592602612622632648 unmodified lines2732742752762772782792802812822832848 unmodified lines293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351import osimport sysimport subprocessimport gcimport shutilfrom typing import *# [AUTO-INSTALL] accelerate 라이브러리try:import accelerateexcept ImportError:subprocess.check_call([sys.executable, "-m", "pip", "install", "accelerate"])# [중요] OOM 방지를 위한 메모리 파편화 설정os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"os.environ['SPCONV_ALGO'] = 'native'import torchimport torch.nn as nnimport numpy as npimport imageiofrom easydict import EasyDict as edictfrom PIL import Imageimport gradio as grfrom gradio_litmodel3d import LitModel3Dfrom trellis.pipelines import TrellisVGGTTo3DPipelinefrom trellis.representations import Gaussian, MeshExtractResultfrom trellis.utils import render_utils, postprocessing_utilsfrom accelerate import dispatch_model, infer_auto_device_mapMAX_SEED = np.iinfo(np.int32).maxTMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')3 unmodified linesuser_dir = os.path.join(TMP_DIR, str(req.session_hash))os.makedirs(user_dir, exist_ok=True)def end_session(req: gr.Request):user_dir = os.path.join(TMP_DIR, str(req.session_hash))if os.path.exists(user_dir):shutil.rmtree(user_dir)gc.collect()torch.cuda.empty_cache()def preprocess_image(image: Image.Image) -> Image.Image:processed_image = pipeline.preprocess_image(image)return processed_imagedef preprocess_videos(video: str) -> List[Tuple[Image.Image, str]]:vid = imageio.get_reader(video, 'ffmpeg')fps = vid.get_meta_data()['fps']images = []8 unmodified linesreturn processed_imagesdef preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:images = [image[0] for image in images]processed_images = [pipeline.preprocess_image(image) for image in images]return processed_imagesdef pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:return {'gaussian': {9 unmodified lines'faces': mesh.faces.cpu().numpy(),},}def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:device = 'cuda:0' if torch.cuda.is_available() else 'cpu'gs = Gaussian(aabb=state['gaussian']['aabb'],sh_degree=state['gaussian']['sh_degree'],2 unmodified linesopacity_bias=state['gaussian']['opacity_bias'],scaling_activation=state['gaussian']['scaling_activation'],)gs._xyz = torch.tensor(state['gaussian']['_xyz'], device=device)gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device=device)gs._scaling = torch.tensor(state['gaussian']['_scaling'], device=device)gs._rotation = torch.tensor(state['gaussian']['_rotation'], device=device)gs._opacity = torch.tensor(state['gaussian']['_opacity'], device=device)mesh = edict(vertices=torch.tensor(state['mesh']['vertices'], device=device),faces=torch.tensor(state['mesh']['faces'], device=device),)return gs, meshdef get_seed(randomize_seed: bool, seed: int) -> int:return np.random.randint(0, MAX_SEED) if randomize_seed else seeddef generate_and_extract_glb(multiimages: List[Tuple[Image.Image, str]],seed: int,6 unmodified linestexture_size: int,req: gr.Request,) -> Tuple[dict, str, str, str]:gc.collect()torch.cuda.empty_cache()user_dir = os.path.join(TMP_DIR, str(req.session_hash))image_files = [image[0] for image in multiimages]try:# [중요] 추론 시 그래디언트 계산 끔 (메모리 절약)with torch.no_grad():outputs, _, _ = pipeline.run(image=image_files,seed=seed,formats=["gaussian", "mesh"],preprocess_image=False,sparse_structure_sampler_params={"steps": ss_sampling_steps,"cfg_strength": ss_guidance_strength,},slat_sampler_params={"steps": slat_sampling_steps,"cfg_strength": slat_guidance_strength,},mode=multiimage_algo,)except Exception as e:torch.cuda.empty_cache()# 구체적인 에러 메시지 반환raise RuntimeError(f"Generation Failed: {str(e)}")video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']1 unmodified linevideo_path = os.path.join(user_dir, 'sample.mp4')imageio.mimsave(video_path, video, fps=15)gs = outputs['gaussian'][0]mesh = outputs['mesh'][0]glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)glb_path = os.path.join(user_dir, 'sample.glb')glb.export(glb_path)state = pack_state(gs, mesh)del outputs, gs, mesh, glbgc.collect()torch.cuda.empty_cache()return state, video_path, glb_path, glb_pathdef extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:user_dir = os.path.join(TMP_DIR, str(req.session_hash))gs, _ = unpack_state(state)gaussian_path = os.path.join(user_dir, 'sample.ply')gs.save_ply(gaussian_path)del gstorch.cuda.empty_cache()return gaussian_path, gaussian_pathdef prepare_multi_example() -> List[Image.Image]:if not os.path.exists("assets/example_multi_image"):return []multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))images = []for case in multi_case:8 unmodified linesimages.append(Image.fromarray(np.concatenate(_images, axis=1)))return imagesdef split_image(image: Image.Image) -> List[Image.Image]:image = np.array(image)alpha = image[..., 3]alpha = np.any(alpha>0, axis=0)16 unmodified lines""")with demo:gr.Markdown("# 💻 ReconViaGen (GPU 0 Freed)")with gr.Row():with gr.Column():2 unmodified linesinput_video = gr.Video(label="Upload Video", interactive=True, height=300)image_prompt = gr.Image(label="Image Prompt", format="png", visible=False, image_mode="RGBA", type="pil", height=300)multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)with gr.Accordion(label="Generation Settings", open=False):seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)14 unmodified linesgenerate_btn = gr.Button("Generate & Extract GLB", variant="primary")extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)with gr.Column():video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)5 unmodified linesoutput_buf = gr.State()with gr.Row() as multiimage_example:examples_multi = gr.Examples(examples=prepare_multi_example(),8 unmodified linesdemo.load(start_session)demo.unload(end_session)input_video.upload(preprocess_videos, inputs=[input_video], outputs=[multiimage_prompt])input_video.clear(lambda: tuple([None, None]), outputs=[input_video, multiimage_prompt])multiimage_prompt.upload(preprocess_images, inputs=[multiimage_prompt], outputs=[multiimage_prompt])generate_btn.click(get_seed, inputs=[randomize_seed, seed], outputs=[seed]).then(generate_and_extract_glb,inputs=[multiimage_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo, mesh_simplify, texture_size],8 unmodified linesoutputs=[extract_gs_btn, download_glb, download_gs],)extract_gs_btn.click(extract_gaussian, inputs=[output_buf], outputs=[model_output, download_gs]).then(lambda: gr.Button(interactive=True), outputs=[download_gs])model_output.clear(lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),outputs=[download_glb, download_gs],)# Launch Scriptif __name__ == "__main__":print("🚀 Initializing Pipeline...")# 1. Pipeline 로드pipeline = TrellisVGGTTo3DPipeline.from_pretrained("esther11/trellis-vggt-v0-2")# 2. 모든 모델을 일단 CUDA:0에 올려서 기본 설정(device mismatch 방지)을 완료함pipeline.cuda()pipeline._device = torch.device("cuda:0") # 내부 device 속성 고정gpu_count = torch.cuda.device_count()print(f"⚡ Detected {gpu_count} GPUs.")if gpu_count > 1:print("⚡ Multi-GPU Mode: Offloading VGGT from GPU 0.")# [핵심 로직] GPU 0을 비우기 위한 전략# VGGT 모델을 잠시 CPU로 내립니다.pipeline.VGGT_model.cpu()print(" - Calculating Device Map (Banning GPU 0 for VGGT)...")# max_memory 설정:# GPU 0: "10MiB" (사실상 VGGT 모델 적재 금지)# GPU 1~N: "20GiB" (여유롭게 할당)max_mem = {0: "10MiB"}for i in range(1, gpu_count):max_mem[i] = "20GiB"# 이 설정으로 맵을 짜면 accelerate는 GPU 0을 건너뛰고 GPU 1부터 모델을 채웁니다.device_map = infer_auto_device_map(pipeline.VGGT_model,max_memory=max_mem,no_split_module_classes=["Block", "ResnetBlock"])# 맵 적용하여 분산 로드pipeline.VGGT_model = dispatch_model(pipeline.VGGT_model, device_map=device_map)print("✅ VGGT Model successfully pushed to GPU 1+.")print(" - GPU 0: Birefnet (Preprocessing) + Controller")print(" - GPU 1+: VGGT (Inference)")else:print("⚠️ Warning: Only 1 GPU detected. Expect OOM if VRAM < 24GB.")demo.launch()12345678910111213141516171819203 unmodified lines2425262728293031323334353637383940414243444546474849505152535455565758596061626364658 unmodified lines74757677787980818283848586878889909192939495969 unmodified lines1061071081091101111121131142 unmodified lines1171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521536 unmodified lines1601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122131 unmodified line2152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572588 unmodified lines26726826927027127227327427527627727827928028128228328428528628716 unmodified lines3043053063073083093103113123133143153163173183193203213223233243252 unmodified lines32832933033133233333433533614 unmodified lines3513523533543553563573583595 unmodified lines3653663673683693703718 unmodified lines3803813823833843853863873883893903913923933943953963973983994004014024034048 unmodified lines413414415416417418419420421422423424425426427428429430431432433434435436437import gradio as grfrom gradio_litmodel3d import LitModel3Dimport osimport shutilos.environ['SPCONV_ALGO'] = 'native'from typing import *import torchimport numpy as npimport imageiofrom easydict import EasyDict as edictfrom PIL import Imagefrom trellis.pipelines import TrellisVGGTTo3DPipelinefrom trellis.representations import Gaussian, MeshExtractResultfrom trellis.utils import render_utils, postprocessing_utilsMAX_SEED = np.iinfo(np.int32).maxTMP_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'tmp')3 unmodified linesuser_dir = os.path.join(TMP_DIR, str(req.session_hash))os.makedirs(user_dir, exist_ok=True)def end_session(req: gr.Request):user_dir = os.path.join(TMP_DIR, str(req.session_hash))shutil.rmtree(user_dir)def preprocess_image(image: Image.Image) -> Image.Image:"""Preprocess the input image for 3D generation.This function is called when a user uploads an image or selects an example.It applies background removal and other preprocessing steps necessary foroptimal 3D model generation.Args:image (Image.Image): The input image from the userReturns:Image.Image: The preprocessed image ready for 3D generation"""processed_image = pipeline.preprocess_image(image)return processed_imagedef preprocess_videos(video: str) -> List[Tuple[Image.Image, str]]:"""Preprocess the input video for multi-image 3D generation.This function is called when a user uploads a video.It extracts frames from the video and processes each frame to prepare themfor the multi-image 3D generation pipeline.Args:video (str): The path to the input video fileReturns:List[Tuple[Image.Image, str]]: The list of preprocessed images ready for 3D generation"""vid = imageio.get_reader(video, 'ffmpeg')fps = vid.get_meta_data()['fps']images = []8 unmodified linesreturn processed_imagesdef preprocess_images(images: List[Tuple[Image.Image, str]]) -> List[Image.Image]:"""Preprocess a list of input images for multi-image 3D generation.This function is called when users upload multiple images in the gallery.It processes each image to prepare them for the multi-image 3D generation pipeline.Args:images (List[Tuple[Image.Image, str]]): The input images from the galleryReturns:List[Image.Image]: The preprocessed images ready for 3D generation"""images = [image[0] for image in images]processed_images = [pipeline.preprocess_image(image) for image in images]return processed_imagesdef pack_state(gs: Gaussian, mesh: MeshExtractResult) -> dict:return {'gaussian': {9 unmodified lines'faces': mesh.faces.cpu().numpy(),},}def unpack_state(state: dict) -> Tuple[Gaussian, edict, str]:gs = Gaussian(aabb=state['gaussian']['aabb'],sh_degree=state['gaussian']['sh_degree'],2 unmodified linesopacity_bias=state['gaussian']['opacity_bias'],scaling_activation=state['gaussian']['scaling_activation'],)gs._xyz = torch.tensor(state['gaussian']['_xyz'], device='cuda')gs._features_dc = torch.tensor(state['gaussian']['_features_dc'], device='cuda')gs._scaling = torch.tensor(state['gaussian']['_scaling'], device='cuda')gs._rotation = torch.tensor(state['gaussian']['_rotation'], device='cuda')gs._opacity = torch.tensor(state['gaussian']['_opacity'], device='cuda')mesh = edict(vertices=torch.tensor(state['mesh']['vertices'], device='cuda'),faces=torch.tensor(state['mesh']['faces'], device='cuda'),)return gs, meshdef get_seed(randomize_seed: bool, seed: int) -> int:"""Get the random seed for generation.This function is called by the generate button to determine whether to usea random seed or the user-specified seed value.Args:randomize_seed (bool): Whether to generate a random seedseed (int): The user-specified seed valueReturns:int: The seed to use for generation"""return np.random.randint(0, MAX_SEED) if randomize_seed else seeddef generate_and_extract_glb(multiimages: List[Tuple[Image.Image, str]],seed: int,6 unmodified linestexture_size: int,req: gr.Request,) -> Tuple[dict, str, str, str]:"""Convert an image to a 3D model and extract GLB file.Args:image (Image.Image): The input image.multiimages (List[Tuple[Image.Image, str]]): The input images in multi-image mode.is_multiimage (bool): Whether is in multi-image mode.seed (int): The random seed.ss_guidance_strength (float): The guidance strength for sparse structure generation.ss_sampling_steps (int): The number of sampling steps for sparse structure generation.slat_guidance_strength (float): The guidance strength for structured latent generation.slat_sampling_steps (int): The number of sampling steps for structured latent generation.multiimage_algo (Literal["multidiffusion", "stochastic"]): The algorithm for multi-image generation.mesh_simplify (float): The mesh simplification factor.texture_size (int): The texture resolution.Returns:dict: The information of the generated 3D model.str: The path to the video of the 3D model.str: The path to the extracted GLB file.str: The path to the extracted GLB file (for download)."""user_dir = os.path.join(TMP_DIR, str(req.session_hash))image_files = [image[0] for image in multiimages]# Generate 3D modeloutputs, _, _ = pipeline.run(image=image_files,seed=seed,formats=["gaussian", "mesh"],preprocess_image=False,sparse_structure_sampler_params={"steps": ss_sampling_steps,"cfg_strength": ss_guidance_strength,},slat_sampler_params={"steps": slat_sampling_steps,"cfg_strength": slat_guidance_strength,},mode=multiimage_algo,)# Render video# import uuid# output_id = str(uuid.uuid4())# os.makedirs(f"{TMP_DIR}/{output_id}", exist_ok=True)# video_path = f"{TMP_DIR}/{output_id}/preview.mp4"# glb_path = f"{TMP_DIR}/{output_id}/mesh.glb"video = render_utils.render_video(outputs['gaussian'][0], num_frames=120)['color']video_geo = render_utils.render_video(outputs['mesh'][0], num_frames=120)['normal']1 unmodified linevideo_path = os.path.join(user_dir, 'sample.mp4')imageio.mimsave(video_path, video, fps=15)# Extract GLBgs = outputs['gaussian'][0]mesh = outputs['mesh'][0]glb = postprocessing_utils.to_glb(gs, mesh, simplify=mesh_simplify, texture_size=texture_size, verbose=False)glb_path = os.path.join(user_dir, 'sample.glb')glb.export(glb_path)# Pack state for optional Gaussian extractionstate = pack_state(gs, mesh)torch.cuda.empty_cache()return state, video_path, glb_path, glb_pathdef extract_gaussian(state: dict, req: gr.Request) -> Tuple[str, str]:"""Extract a Gaussian splatting file from the generated 3D model.This function is called when the user clicks "Extract Gaussian" button.It converts the 3D model state into a .ply file format containingGaussian splatting data for advanced 3D applications.Args:state (dict): The state of the generated 3D model containing Gaussian datareq (gr.Request): Gradio request object for session managementReturns:Tuple[str, str]: Paths to the extracted Gaussian file (for display and download)"""user_dir = os.path.join(TMP_DIR, str(req.session_hash))gs, _ = unpack_state(state)gaussian_path = os.path.join(user_dir, 'sample.ply')gs.save_ply(gaussian_path)torch.cuda.empty_cache()return gaussian_path, gaussian_pathdef prepare_multi_example() -> List[Image.Image]:multi_case = list(set([i.split('_')[0] for i in os.listdir("assets/example_multi_image")]))images = []for case in multi_case:8 unmodified linesimages.append(Image.fromarray(np.concatenate(_images, axis=1)))return imagesdef split_image(image: Image.Image) -> List[Image.Image]:"""Split a multi-view image into separate view images.This function is called when users select multi-image examples that containmultiple views in a single concatenated image. It automatically splits thembased on alpha channel boundaries and preprocesses each view.Args:image (Image.Image): A concatenated image containing multiple viewsReturns:List[Image.Image]: List of individual preprocessed view images"""image = np.array(image)alpha = image[..., 3]alpha = np.any(alpha>0, axis=0)16 unmodified lines""")with demo:gr.Markdown("""# 💻 ReconViaGen<p align="center"><a title="Github" href="https://github.com/GAP-LAB-CUHK-SZ/ReconViaGen" target="_blank" rel="noopener noreferrer" style="display: inline-block;"><img src="https://img.shields.io/github/stars/GAP-LAB-CUHK-SZ/ReconViaGen?label=GitHub%20%E2%98%85&logo=github&color=C8C" alt="badge-github-stars"></a><a title="Website" href="https://jiahao620.github.io/reconviagen/" target="_blank" rel="noopener noreferrer" style="display: inline-block;"><img src="https://www.obukhov.ai/img/badges/badge-website.svg"></a><a title="arXiv" href="https://jiahao620.github.io/reconviagen/" target="_blank" rel="noopener noreferrer" style="display: inline-block;"><img src="https://www.obukhov.ai/img/badges/badge-pdf.svg"></a></p>✨This demo is partial. We will release the whole model later. Stay tuned!✨""")with gr.Row():with gr.Column():2 unmodified linesinput_video = gr.Video(label="Upload Video", interactive=True, height=300)image_prompt = gr.Image(label="Image Prompt", format="png", visible=False, image_mode="RGBA", type="pil", height=300)multiimage_prompt = gr.Gallery(label="Image Prompt", format="png", type="pil", height=300, columns=3)gr.Markdown("""Input different views of the object in separate images.""")with gr.Accordion(label="Generation Settings", open=False):seed = gr.Slider(0, MAX_SEED, label="Seed", value=0, step=1)14 unmodified linesgenerate_btn = gr.Button("Generate & Extract GLB", variant="primary")extract_gs_btn = gr.Button("Extract Gaussian", interactive=False)gr.Markdown("""*NOTE: Gaussian file can be very large (~50MB), it will take a while to display and download.*""")with gr.Column():video_output = gr.Video(label="Generated 3D Asset", autoplay=True, loop=True, height=300)5 unmodified linesoutput_buf = gr.State()# Example images at the bottom of the pagewith gr.Row() as multiimage_example:examples_multi = gr.Examples(examples=prepare_multi_example(),8 unmodified linesdemo.load(start_session)demo.unload(end_session)input_video.upload(preprocess_videos,inputs=[input_video],outputs=[multiimage_prompt],)input_video.clear(lambda: tuple([None, None]),outputs=[input_video, multiimage_prompt],)multiimage_prompt.upload(preprocess_images,inputs=[multiimage_prompt],outputs=[multiimage_prompt],)generate_btn.click(get_seed,inputs=[randomize_seed, seed],outputs=[seed],).then(generate_and_extract_glb,inputs=[multiimage_prompt, seed, ss_guidance_strength, ss_sampling_steps, slat_guidance_strength, slat_sampling_steps, multiimage_algo, mesh_simplify, texture_size],8 unmodified linesoutputs=[extract_gs_btn, download_glb, download_gs],)extract_gs_btn.click(extract_gaussian,inputs=[output_buf],outputs=[model_output, download_gs],).then(lambda: gr.Button(interactive=True),outputs=[download_gs],)model_output.clear(lambda: tuple([gr.Button(interactive=False), gr.Button(interactive=False)]),outputs=[download_glb, download_gs],)# Launch the Gradio appif __name__ == "__main__":pipeline = TrellisVGGTTo3DPipeline.from_pretrained("esther11/trellis-vggt-v0-2")pipeline.cuda()pipeline.VGGT_model.cuda()pipeline.birefnet_model.cuda()demo.launch()
app.py+200-114