cavargas10 commited on
Commit
3b7142f
1 Parent(s): 90df5e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -36
app.py CHANGED
@@ -1,6 +1,9 @@
1
  import spaces
2
  from spaces.zero.decorator import GPU
3
 
 
 
 
4
  import numpy as np
5
  import tqdm
6
  import torch
@@ -10,6 +13,7 @@ import torchvision.transforms.functional as TF
10
  from safetensors.torch import load_file
11
  import rembg
12
  import gradio as gr
 
13
  import kiui
14
  from kiui.op import recenter
15
  from kiui.cam import orbit_camera
@@ -25,6 +29,9 @@ from huggingface_hub import hf_hub_download
25
  import spaces
26
 
27
  IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
 
 
 
28
  GRADIO_OBJ_ALBEDO_PATH = 'gradio_output_albedo.obj'
29
  GRADIO_OBJ_SHADING_PATH = 'gradio_output_shading.obj'
30
 
@@ -79,6 +86,13 @@ if opt.resume is not None:
79
 
80
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
81
  model = model.half().to(device)
 
 
 
 
 
 
 
82
  proj_matrix[3, 2] = - (opt.zfar * opt.znear) / (opt.zfar - opt.znear)
83
  proj_matrix[2, 3] = 1
84
 
@@ -120,7 +134,6 @@ if os.path.exists(unet_path):
120
  unet_ckpt_path = unet_path
121
  else:
122
  unet_ckpt_path = hf_hub_download(repo_id="TencentARC/InstantMesh", filename="diffusion_pytorch_model.bin", repo_type="model")
123
-
124
  state_dict = torch.load(unet_ckpt_path, map_location='cpu')
125
  pipe_image_plus.unet.load_state_dict(state_dict, strict=True)
126
  pipe_image_plus = pipe_image_plus.to(device)
@@ -169,7 +182,6 @@ def generate_mv(condition_input_image, prompt, prompt_neg='', input_elevation=0,
169
 
170
  mv_image_grid = np.concatenate([mv_image[1], mv_image[2],mv_image[3], mv_image[0]],axis=1)
171
  input_image = np.stack([mv_image[1], mv_image[2], mv_image[3], mv_image[0]], axis=0)
172
-
173
  else:
174
  from PIL import Image
175
  from einops import rearrange, repeat
@@ -184,7 +196,6 @@ def generate_mv(condition_input_image, prompt, prompt_neg='', input_elevation=0,
184
  input_image = mv_image
185
  return mv_image_grid, processed_image, input_image
186
 
187
-
188
  @spaces.GPU
189
  def generate_3d(input_image, condition_input_image, mv_moedl_option=None, input_seed=42):
190
  kiui.seed_everything(input_seed)
@@ -303,46 +314,14 @@ def generate_3d(input_image, condition_input_image, mv_moedl_option=None, input_
303
  _TITLE = '''LDM: Large Tensorial SDF Model for Textured Mesh Generation'''
304
 
305
  _DESCRIPTION = '''
306
-
307
-
308
-
309
  * Input can be text prompt, image.
310
  * The currently supported multi-view diffusion models include the image-conditioned MVdream and Zero123plus, as well as the text-conditioned Imagedream.
311
  * If you find the output unsatisfying, try using different multi-view diffusion models or seeds!
 
312
  '''
313
 
314
-
315
-
316
-
317
-
318
-
319
-
320
-
321
-
322
-
323
-
324
-
325
-
326
-
327
-
328
-
329
-
330
-
331
-
332
-
333
-
334
-
335
-
336
-
337
-
338
-
339
-
340
-
341
  block = gr.Blocks(title=_TITLE).queue()
342
  with block:
343
-
344
-
345
-
346
  with gr.Row():
347
  with gr.Column(scale=1):
348
  gr.Markdown('# ' + _TITLE)
 
1
  import spaces
2
  from spaces.zero.decorator import GPU
3
 
4
+ import os
5
+ import tyro
6
+ import imageio
7
  import numpy as np
8
  import tqdm
9
  import torch
 
13
  from safetensors.torch import load_file
14
  import rembg
15
  import gradio as gr
16
+
17
  import kiui
18
  from kiui.op import recenter
19
  from kiui.cam import orbit_camera
 
29
  import spaces
30
 
31
  IMAGENET_DEFAULT_MEAN = (0.485, 0.456, 0.406)
32
+ IMAGENET_DEFAULT_STD = (0.229, 0.224, 0.225)
33
+ GRADIO_VIDEO_PATH = 'gradio_output.mp4'
34
+ GRADIO_OBJ_PATH = 'gradio_output_rgb.obj'
35
  GRADIO_OBJ_ALBEDO_PATH = 'gradio_output_albedo.obj'
36
  GRADIO_OBJ_SHADING_PATH = 'gradio_output_shading.obj'
37
 
 
86
 
87
  device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
88
  model = model.half().to(device)
89
+ model.eval()
90
+
91
+ tan_half_fov = np.tan(0.5 * np.deg2rad(opt.fovy))
92
+ proj_matrix = torch.zeros(4, 4, dtype=torch.float32).to(device)
93
+ proj_matrix[0, 0] = 1 / tan_half_fov
94
+ proj_matrix[1, 1] = 1 / tan_half_fov
95
+ proj_matrix[2, 2] = (opt.zfar + opt.znear) / (opt.zfar - opt.znear)
96
  proj_matrix[3, 2] = - (opt.zfar * opt.znear) / (opt.zfar - opt.znear)
97
  proj_matrix[2, 3] = 1
98
 
 
134
  unet_ckpt_path = unet_path
135
  else:
136
  unet_ckpt_path = hf_hub_download(repo_id="TencentARC/InstantMesh", filename="diffusion_pytorch_model.bin", repo_type="model")
 
137
  state_dict = torch.load(unet_ckpt_path, map_location='cpu')
138
  pipe_image_plus.unet.load_state_dict(state_dict, strict=True)
139
  pipe_image_plus = pipe_image_plus.to(device)
 
182
 
183
  mv_image_grid = np.concatenate([mv_image[1], mv_image[2],mv_image[3], mv_image[0]],axis=1)
184
  input_image = np.stack([mv_image[1], mv_image[2], mv_image[3], mv_image[0]], axis=0)
 
185
  else:
186
  from PIL import Image
187
  from einops import rearrange, repeat
 
196
  input_image = mv_image
197
  return mv_image_grid, processed_image, input_image
198
 
 
199
  @spaces.GPU
200
  def generate_3d(input_image, condition_input_image, mv_moedl_option=None, input_seed=42):
201
  kiui.seed_everything(input_seed)
 
314
  _TITLE = '''LDM: Large Tensorial SDF Model for Textured Mesh Generation'''
315
 
316
  _DESCRIPTION = '''
 
 
 
317
  * Input can be text prompt, image.
318
  * The currently supported multi-view diffusion models include the image-conditioned MVdream and Zero123plus, as well as the text-conditioned Imagedream.
319
  * If you find the output unsatisfying, try using different multi-view diffusion models or seeds!
320
+ * The project code is available at [https://github.com/rgxie/LDM](https://github.com/rgxie/LDM).
321
  '''
322
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
  block = gr.Blocks(title=_TITLE).queue()
324
  with block:
 
 
 
325
  with gr.Row():
326
  with gr.Column(scale=1):
327
  gr.Markdown('# ' + _TITLE)