LutaoJiang commited on
Commit
e27ec68
·
1 Parent(s): bce015a
app.py CHANGED
@@ -5,40 +5,31 @@ import spaces
5
  import ctypes
6
  import shlex
7
  import torch
8
- import argparse
9
  print(f'gradio version: {gr.__version__}')
10
 
11
- # Add command line argument parsing
12
- parser = argparse.ArgumentParser(description='DiMeR Demo')
13
- parser.add_argument('--ui_only', action='store_true', help='Only load the UI interface, do not initialize models (for UI debugging)')
14
- args = parser.parse_args()
15
 
16
- UI_ONLY_MODE = args.ui_only
17
- print(f"UI_ONLY_MODE: {UI_ONLY_MODE}")
18
-
19
- if not UI_ONLY_MODE:
20
- subprocess.run(
21
- shlex.split(
22
- "pip install ./custom_diffusers --force-reinstall --no-deps"
23
- )
24
  )
25
- subprocess.run(
26
- shlex.split(
27
- "pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu121_pyt240/download.html"
28
- )
29
  )
 
30
 
31
- subprocess.run(
32
- shlex.split(
33
- "pip install ./extension/nvdiffrast-0.3.1+torch-py3-none-any.whl --force-reinstall --no-deps"
34
- )
35
  )
 
36
 
37
- subprocess.run(
38
- shlex.split(
39
- "pip install ./extension/renderutils_plugin-0.1.0-cp310-cp310-linux_x86_64.whl --force-reinstall --no-deps"
40
- )
41
  )
 
42
 
43
  # Status variables for tracking if detailed prompt and image have been generated
44
  generated_detailed_prompt = False
@@ -61,9 +52,7 @@ def install_cuda_toolkit():
61
  os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
62
  print("==> finished installation")
63
 
64
- # Only execute CUDA installation in non-UI debug mode
65
- if not UI_ONLY_MODE:
66
- install_cuda_toolkit()
67
 
68
  @spaces.GPU
69
  def check_gpu():
@@ -74,17 +63,9 @@ def check_gpu():
74
  os.environ['LD_LIBRARY_PATH'] = "/usr/local/cuda-12.1/lib64:" + os.environ.get('LD_LIBRARY_PATH', '')
75
  subprocess.run(['nvidia-smi']) # Test if CUDA is available
76
  print(f"torch.cuda.is_available:{torch.cuda.is_available()}")
77
- print("Device count:", torch.cuda.device_count())
78
-
79
- # test nvdiffrast
80
- import nvdiffrast.torch as dr
81
- dr.RasterizeCudaContext(device="cuda:0")
82
- print("nvdiffrast initialized successfully")
83
 
84
-
85
- # Only check GPU in non-UI debug mode
86
- if not UI_ONLY_MODE:
87
- check_gpu()
88
 
89
 
90
  import base64
@@ -108,23 +89,19 @@ import random
108
  import time
109
  import numpy as np
110
 
111
- # Only import video rendering module and initialize models in non-UI debug mode
112
- if not UI_ONLY_MODE:
113
- from video_render import render_video_from_obj
114
 
115
- access_token = os.getenv("HUGGINGFACE_TOKEN")
116
- from pipeline.kiss3d_wrapper import init_wrapper_from_config, run_text_to_3d, run_image_to_3d, image2mesh_preprocess, image2mesh_main
117
 
118
  # Add logo file path and hyperlinks
119
  LOGO_PATH = "app_assets/logo_temp_.png" # Update this to the actual path of your logo
120
  ARXIV_LINK = "https://arxiv.org/pdf/2504.17670"
121
  GITHUB_LINK = "https://github.com/lutao2021/DiMeR"
122
 
123
- # Only initialize models in non-UI debug mode
124
- if not UI_ONLY_MODE:
125
- k3d_wrapper = init_wrapper_from_config('./pipeline/pipeline_config/default.yaml')
126
- from models.ISOMER.scripts.utils import fix_vert_color_glb
127
- torch.backends.cuda.matmul.allow_tf32 = True
128
 
129
  TEMP_MESH_ADDRESS=''
130
 
@@ -166,11 +143,6 @@ def save_py3dmesh_with_trimesh_fast(meshes, save_glb_path=TEMP_MESH_ADDRESS, app
166
 
167
  @spaces.GPU
168
  def text_to_detailed(prompt, seed=None):
169
- # test nvdiffrast
170
- import nvdiffrast.torch as dr
171
- dr.RasterizeCudaContext(device="cuda:0")
172
- print("nvdiffrast initialized successfully")
173
-
174
  print(f"torch.cuda.is_available():{torch.cuda.is_available()}")
175
  # print(f"Before text_to_detailed: {torch.cuda.memory_allocated() / 1024**3} GB")
176
  return k3d_wrapper.get_detailed_prompt(prompt, seed)
@@ -240,7 +212,7 @@ def image2mesh_main_(reference_3d_bundle_image, caption, seed, strength1=0.5, st
240
  return gen_save_path, recon_mesh_path, mesh_cache
241
  # return gen_save_path, recon_mesh_path
242
 
243
- @spaces.GPU(duration=120)
244
  def bundle_image_to_mesh(
245
  gen_3d_bundle_image,
246
  camera_radius=3.5,
@@ -328,8 +300,7 @@ def image_to_base64(image_path):
328
 
329
  # def main():
330
 
331
- if not UI_ONLY_MODE:
332
- torch.set_grad_enabled(False)
333
 
334
  # Convert the logo image to base64
335
  logo_base64 = image_to_base64(LOGO_PATH)
 
5
  import ctypes
6
  import shlex
7
  import torch
 
8
  print(f'gradio version: {gr.__version__}')
9
 
 
 
 
 
10
 
11
+ subprocess.run(
12
+ shlex.split(
13
+ "pip install ./custom_diffusers --force-reinstall --no-deps"
 
 
 
 
 
14
  )
15
+ )
16
+ subprocess.run(
17
+ shlex.split(
18
+ "pip install --no-index --no-cache-dir pytorch3d -f https://dl.fbaipublicfiles.com/pytorch3d/packaging/wheels/py310_cu121_pyt240/download.html"
19
  )
20
+ )
21
 
22
+ subprocess.run(
23
+ shlex.split(
24
+ "pip install ./extension/nvdiffrast-0.3.1+torch-py3-none-any.whl --force-reinstall --no-deps"
 
25
  )
26
+ )
27
 
28
+ subprocess.run(
29
+ shlex.split(
30
+ "pip install ./extension/renderutils_plugin-0.1.0-cp310-cp310-linux_x86_64.whl --force-reinstall --no-deps"
 
31
  )
32
+ )
33
 
34
  # Status variables for tracking if detailed prompt and image have been generated
35
  generated_detailed_prompt = False
 
52
  os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
53
  print("==> finished installation")
54
 
55
+ install_cuda_toolkit()
 
 
56
 
57
  @spaces.GPU
58
  def check_gpu():
 
63
  os.environ['LD_LIBRARY_PATH'] = "/usr/local/cuda-12.1/lib64:" + os.environ.get('LD_LIBRARY_PATH', '')
64
  subprocess.run(['nvidia-smi']) # Test if CUDA is available
65
  print(f"torch.cuda.is_available:{torch.cuda.is_available()}")
66
+ print("Device count:", torch.cuda.device_count())
 
 
 
 
 
67
 
68
+ check_gpu()
 
 
 
69
 
70
 
71
  import base64
 
89
  import time
90
  import numpy as np
91
 
92
+ from video_render import render_video_from_obj
 
 
93
 
94
+ access_token = os.getenv("HUGGINGFACE_TOKEN")
95
+ from pipeline.kiss3d_wrapper import init_wrapper_from_config, run_text_to_3d, run_image_to_3d, image2mesh_preprocess, image2mesh_main
96
 
97
  # Add logo file path and hyperlinks
98
  LOGO_PATH = "app_assets/logo_temp_.png" # Update this to the actual path of your logo
99
  ARXIV_LINK = "https://arxiv.org/pdf/2504.17670"
100
  GITHUB_LINK = "https://github.com/lutao2021/DiMeR"
101
 
102
+ k3d_wrapper = init_wrapper_from_config('./pipeline/pipeline_config/default.yaml')
103
+ from models.ISOMER.scripts.utils import fix_vert_color_glb
104
+ torch.backends.cuda.matmul.allow_tf32 = True
 
 
105
 
106
  TEMP_MESH_ADDRESS=''
107
 
 
143
 
144
  @spaces.GPU
145
  def text_to_detailed(prompt, seed=None):
 
 
 
 
 
146
  print(f"torch.cuda.is_available():{torch.cuda.is_available()}")
147
  # print(f"Before text_to_detailed: {torch.cuda.memory_allocated() / 1024**3} GB")
148
  return k3d_wrapper.get_detailed_prompt(prompt, seed)
 
212
  return gen_save_path, recon_mesh_path, mesh_cache
213
  # return gen_save_path, recon_mesh_path
214
 
215
+ @spaces.GPU(duration=30)
216
  def bundle_image_to_mesh(
217
  gen_3d_bundle_image,
218
  camera_radius=3.5,
 
300
 
301
  # def main():
302
 
303
+ torch.set_grad_enabled(False)
 
304
 
305
  # Convert the logo image to base64
306
  logo_base64 = image_to_base64(LOGO_PATH)
models/DiMeR/models/DiMeR.py CHANGED
@@ -86,11 +86,8 @@ class DiMeR(nn.Module):
86
 
87
  @spaces.GPU
88
  def init_flexicubes_geometry(self, device, fovy=50.0):
89
- print(1)
90
  camera = PerspectiveCamera(fovy=fovy, device=device)
91
- print(2)
92
  renderer = NeuralRender(device, camera_model=camera)
93
- print(3)
94
  self.geometry = FlexiCubesGeometry(
95
  grid_res=self.grid_res,
96
  scale=self.grid_scale,
 
86
 
87
  @spaces.GPU
88
  def init_flexicubes_geometry(self, device, fovy=50.0):
 
89
  camera = PerspectiveCamera(fovy=fovy, device=device)
 
90
  renderer = NeuralRender(device, camera_model=camera)
 
91
  self.geometry = FlexiCubesGeometry(
92
  grid_res=self.grid_res,
93
  scale=self.grid_scale,
models/DiMeR/models/geometry/camera/perspective_camera.py CHANGED
@@ -24,13 +24,9 @@ def projection(x=0.1, n=1.0, f=50.0, near_plane=None):
24
  class PerspectiveCamera(Camera):
25
  def __init__(self, fovy=49.0, device='cuda'):
26
  super(PerspectiveCamera, self).__init__()
27
- print(1.1)
28
  self.device = device
29
- print(1.2)
30
  focal = np.tan(fovy / 180.0 * np.pi * 0.5)
31
- print(1.3)
32
  self.proj_mtx = torch.from_numpy(projection(x=focal, f=1000.0, n=1.0, near_plane=0.1)).to(self.device).unsqueeze(dim=0)
33
- print(1.4)
34
 
35
  def project(self, points_bxnx4):
36
  out = torch.matmul(
 
24
  class PerspectiveCamera(Camera):
25
  def __init__(self, fovy=49.0, device='cuda'):
26
  super(PerspectiveCamera, self).__init__()
 
27
  self.device = device
 
28
  focal = np.tan(fovy / 180.0 * np.pi * 0.5)
 
29
  self.proj_mtx = torch.from_numpy(projection(x=focal, f=1000.0, n=1.0, near_plane=0.1)).to(self.device).unsqueeze(dim=0)
 
30
 
31
  def project(self, points_bxnx4):
32
  out = torch.matmul(
models/DiMeR/models/geometry/render/neural_render.py CHANGED
@@ -72,15 +72,10 @@ class NeuralRender(Renderer):
72
  @spaces.GPU
73
  def __init__(self, device='cuda', camera_model=None):
74
  super(NeuralRender, self).__init__()
75
- print(2.1)
76
  self.device = device
77
- print(2.2)
78
  self.ctx = dr.RasterizeCudaContext(device=device)
79
- print(2.3)
80
  self.projection_mtx = None
81
- print(2.4)
82
  self.camera = camera_model
83
- print(2.5)
84
 
85
  # ==============================================================================================
86
  # pixel shader
 
72
  @spaces.GPU
73
  def __init__(self, device='cuda', camera_model=None):
74
  super(NeuralRender, self).__init__()
 
75
  self.device = device
 
76
  self.ctx = dr.RasterizeCudaContext(device=device)
 
77
  self.projection_mtx = None
 
78
  self.camera = camera_model
 
79
 
80
  # ==============================================================================================
81
  # pixel shader
models/DiMeR/models/geometry/rep_3d/flexicubes_geometry.py CHANGED
@@ -40,35 +40,26 @@ class FlexiCubesGeometry(Geometry):
40
  self, grid_res=64, scale=2.0, device='cuda', renderer=None,
41
  render_type='neural_render', args=None):
42
  super(FlexiCubesGeometry, self).__init__()
43
- print(3.1)
44
  self.grid_res = grid_res
45
  self.device = device
46
  self.args = args
47
- print(3.2)
48
  self.fc = FlexiCubes(device, weight_scale=0.5)
49
- print(3.3)
50
  self.verts, self.indices = self.fc.construct_voxel_grid(grid_res)
51
- print(3.4)
52
  if isinstance(scale, list):
53
  self.verts[:, 0] = self.verts[:, 0] * scale[0]
54
  self.verts[:, 1] = self.verts[:, 1] * scale[1]
55
  self.verts[:, 2] = self.verts[:, 2] * scale[1]
56
  else:
57
  self.verts = self.verts * scale
58
- print(3.5)
59
 
60
  all_edges = self.indices[:, self.fc.cube_edges].reshape(-1, 2)
61
  self.all_edges = torch.unique(all_edges, dim=0)
62
 
63
  # Parameters used for fix boundary sdf
64
- print(3.6)
65
  self.center_indices, self.boundary_indices = get_center_boundary_index(self.grid_res, device)
66
- print(3.7)
67
  self.renderer = renderer
68
  self.render_type = render_type
69
- print(3.8)
70
  self.ctx = dr.RasterizeCudaContext(device=device)
71
- print(3.9)
72
 
73
  # self.verts.requires_grad_(True)
74
 
 
40
  self, grid_res=64, scale=2.0, device='cuda', renderer=None,
41
  render_type='neural_render', args=None):
42
  super(FlexiCubesGeometry, self).__init__()
 
43
  self.grid_res = grid_res
44
  self.device = device
45
  self.args = args
 
46
  self.fc = FlexiCubes(device, weight_scale=0.5)
 
47
  self.verts, self.indices = self.fc.construct_voxel_grid(grid_res)
 
48
  if isinstance(scale, list):
49
  self.verts[:, 0] = self.verts[:, 0] * scale[0]
50
  self.verts[:, 1] = self.verts[:, 1] * scale[1]
51
  self.verts[:, 2] = self.verts[:, 2] * scale[1]
52
  else:
53
  self.verts = self.verts * scale
 
54
 
55
  all_edges = self.indices[:, self.fc.cube_edges].reshape(-1, 2)
56
  self.all_edges = torch.unique(all_edges, dim=0)
57
 
58
  # Parameters used for fix boundary sdf
 
59
  self.center_indices, self.boundary_indices = get_center_boundary_index(self.grid_res, device)
 
60
  self.renderer = renderer
61
  self.render_type = render_type
 
62
  self.ctx = dr.RasterizeCudaContext(device=device)
 
63
 
64
  # self.verts.requires_grad_(True)
65