Chao Xu commited on
Commit
5101aba
โ€ข
1 Parent(s): 2c4ee95

change elev and turn on autocast

Browse files
Files changed (3) hide show
  1. .gitignore +1 -0
  2. app.py +24 -33
  3. env.sh +0 -4
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ weights/
app.py CHANGED
@@ -5,23 +5,24 @@ python gradio_new.py 0
5
  '''
6
  import os, sys
7
  from huggingface_hub import snapshot_download
8
- code_dir = snapshot_download("One-2-3-45/code", token=os.environ['TOKEN'])
9
- # code_dir = "../code"
 
 
 
10
  sys.path.append(code_dir)
11
 
12
  elev_est_dir = os.path.join(code_dir, "one2345_elev_est/")
13
  sys.path.append(elev_est_dir)
14
 
15
- # sparseneus_dir = os.path.join(code_dir, "SparseNeuS_demo_v1/")
16
- # sys.path.append(sparseneus_dir)
17
-
18
- import subprocess
19
- subprocess.run(["sh", os.path.join(elev_est_dir, "install.sh")], cwd=elev_est_dir)
20
- # export TORCH_CUDA_ARCH_LIST="7.0;7.2;8.0;8.6"
21
- # export IABN_FORCE_CUDA=1
22
- os.environ["TORCH_CUDA_ARCH_LIST"] = "7.0;7.2;8.0;8.6"
23
- os.environ["IABN_FORCE_CUDA"] = "1"
24
- subprocess.run(["pip", "install", "inplace_abn"])
25
 
26
  import inspect
27
  import shutil
@@ -36,13 +37,12 @@ import sys
36
  from functools import partial
37
 
38
  from lovely_numpy import lo
39
- # from omegaconf import OmegaConf
40
  import cv2
41
  from PIL import Image
42
  import trimesh
43
  import tempfile
44
  from zero123_utils import init_model, predict_stage1_gradio, zero123_infer
45
- from sam_utils import sam_init, sam_out, sam_out_nosave
46
  from utils import image_preprocess_nosave, gen_poses
47
  from one2345_elev_est.tools.estimate_wild_imgs import estimate_elev
48
  from rembg import remove
@@ -51,7 +51,6 @@ _GPU_INDEX = 0
51
 
52
  _TITLE = 'One-2-3-45: Any Single Image to 3D Mesh in 45 Seconds without Per-Shape Optimization'
53
 
54
- # This demo allows you to generate novel viewpoints of an object depicted in an input image using a fine-tuned version of Stable Diffusion.
55
  _DESCRIPTION = '''
56
  We reconstruct a 3D textured mesh from a single image by initially predicting multi-view images and then lifting them to 3D.
57
  '''
@@ -252,7 +251,7 @@ class CameraVisualizer:
252
 
253
 
254
  def stage1_run(models, device, cam_vis, tmp_dir,
255
- input_im, scale, ddim_steps, rerun_all=[],
256
  *btn_retrys):
257
  is_rerun = True if cam_vis is None else False
258
 
@@ -273,11 +272,12 @@ def stage1_run(models, device, cam_vis, tmp_dir,
273
  output_ims_2 = predict_stage1_gradio(models['turncam'], input_im, save_path=stage1_dir, adjust_set=list(range(4,8)), device=device, ddim_steps=ddim_steps, scale=scale)
274
  else:
275
  output_ims_2 = predict_stage1_gradio(models['turncam'], input_im, save_path=stage1_dir, adjust_set=list(range(8,12)), device=device, ddim_steps=ddim_steps, scale=scale)
276
- return (elev_output, new_fig, *output_ims, *output_ims_2)
277
  else:
278
  rerun_idx = [i for i in range(len(btn_retrys)) if btn_retrys[i]]
279
- elev_output = estimate_elev(tmp_dir)
280
- if elev_output > 75:
 
281
  rerun_idx_in = [i if i < 4 else i+4 for i in rerun_idx]
282
  else:
283
  rerun_idx_in = rerun_idx
@@ -295,7 +295,7 @@ def stage1_run(models, device, cam_vis, tmp_dir,
295
  def stage2_run(models, device, tmp_dir,
296
  elev, scale, rerun_all=[], stage2_steps=50):
297
  # print("elev", elev)
298
- flag_lower_cam = int(elev["label"]) <= 75
299
  is_rerun = True if rerun_all else False
300
  if not is_rerun:
301
  if flag_lower_cam:
@@ -529,8 +529,6 @@ def run_demo(
529
  bbox_block = gr.Image(type='pil', label="Bounding box", interactive=False).style(height=300)
530
  sam_block = gr.Image(type='pil', label="SAM output", interactive=False)
531
  max_width = max_height = 256
532
- # with gr.Row():
533
- # gr.Markdown('After uploading the image, a bounding box will be generated automatically. If the result is not satisfactory, you can also use the slider below to manually select the object.')
534
  with gr.Row():
535
  x_min_slider = gr.Slider(
536
  label="X min",
@@ -590,21 +588,16 @@ def run_demo(
590
  label='Number of diffusion inference steps')
591
 
592
  # with gr.Row():
593
- run_btn = gr.Button('Run Generation', variant='primary')
594
  # guide_title = gr.Markdown(_GUIDE_TITLE, visible=True)
595
  guide_text = gr.Markdown(_USER_GUIDE, visible=True)
596
 
597
- # with gr.Row():
598
- # height does not work [a bug]
599
- mesh_output = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="One-2-3-45's Textured Mesh", elem_id="model-3d-out") #.style(height=800)
600
 
601
  with gr.Row(variant='panel'):
602
  with gr.Column(scale=0.85):
603
  with gr.Row():
604
- # with gr.Column(scale=8):
605
- elev_output = gr.Label(label='Estimated elevation / polar angle of the input image (degree, w.r.t. the Z axis)')
606
- # with gr.Column(scale=1):
607
- # theta_output = gr.Image(value="./theta_mini.png", interactive=False, show_label=False).style(width=100)
608
  vis_output = gr.Plot(
609
  label='Camera poses of the input view (red) and predicted views (blue)')
610
 
@@ -691,10 +684,8 @@ def run_demo(
691
  # make regen_btn visible when any of the btn_retry is checked
692
  for btn_retry in btn_retrys:
693
  # Add the event handlers to the btn_retry buttons
694
- # btn_retry.change(fn=on_retry_button_click, inputs=[*btn_retrys], outputs=regen_btn)
695
  btn_retry.change(fn=on_retry_button_click, inputs=[*btn_retrys], outputs=[regen_view_btn, regen_mesh_btn])
696
 
697
-
698
 
699
  run_btn.click(fn=partial(update_guide, _SAM), outputs=[guide_text]
700
  ).success(fn=partial(preprocess_run, predictor, models),
@@ -712,7 +703,7 @@ def run_demo(
712
 
713
 
714
  regen_view_btn.click(fn=partial(stage1_run, models, device, None),
715
- inputs=[tmp_dir, sam_block, scale_slider, steps_slider, rerun_idx, *btn_retrys],
716
  outputs=[rerun_idx, *btn_retrys, *views]
717
  ).success(fn=partial(update_guide, _REGEN_1), outputs=[guide_text])
718
  regen_mesh_btn.click(fn=partial(stage2_run, models, device),
 
5
  '''
6
  import os, sys
7
  from huggingface_hub import snapshot_download
8
+
9
+ is_local_run = True
10
+
11
+ code_dir = snapshot_download("One-2-3-45/code", token=os.environ['TOKEN']) if not is_local_run else "../code"
12
+
13
  sys.path.append(code_dir)
14
 
15
  elev_est_dir = os.path.join(code_dir, "one2345_elev_est/")
16
  sys.path.append(elev_est_dir)
17
 
18
+ if not is_local_run:
19
+ import subprocess
20
+ subprocess.run(["sh", os.path.join(elev_est_dir, "install.sh")], cwd=elev_est_dir)
21
+ # export TORCH_CUDA_ARCH_LIST="7.0;7.2;8.0;8.6"
22
+ # export IABN_FORCE_CUDA=1
23
+ os.environ["TORCH_CUDA_ARCH_LIST"] = "8.0;8.6"
24
+ os.environ["IABN_FORCE_CUDA"] = "1"
25
+ subprocess.run(["pip", "install", "inplace_abn"])
 
 
26
 
27
  import inspect
28
  import shutil
 
37
  from functools import partial
38
 
39
  from lovely_numpy import lo
 
40
  import cv2
41
  from PIL import Image
42
  import trimesh
43
  import tempfile
44
  from zero123_utils import init_model, predict_stage1_gradio, zero123_infer
45
+ from sam_utils import sam_init, sam_out_nosave
46
  from utils import image_preprocess_nosave, gen_poses
47
  from one2345_elev_est.tools.estimate_wild_imgs import estimate_elev
48
  from rembg import remove
 
51
 
52
  _TITLE = 'One-2-3-45: Any Single Image to 3D Mesh in 45 Seconds without Per-Shape Optimization'
53
 
 
54
  _DESCRIPTION = '''
55
  We reconstruct a 3D textured mesh from a single image by initially predicting multi-view images and then lifting them to 3D.
56
  '''
 
251
 
252
 
253
  def stage1_run(models, device, cam_vis, tmp_dir,
254
+ input_im, scale, ddim_steps, elev=None, rerun_all=[],
255
  *btn_retrys):
256
  is_rerun = True if cam_vis is None else False
257
 
 
272
  output_ims_2 = predict_stage1_gradio(models['turncam'], input_im, save_path=stage1_dir, adjust_set=list(range(4,8)), device=device, ddim_steps=ddim_steps, scale=scale)
273
  else:
274
  output_ims_2 = predict_stage1_gradio(models['turncam'], input_im, save_path=stage1_dir, adjust_set=list(range(8,12)), device=device, ddim_steps=ddim_steps, scale=scale)
275
+ return (90-elev_output, new_fig, *output_ims, *output_ims_2)
276
  else:
277
  rerun_idx = [i for i in range(len(btn_retrys)) if btn_retrys[i]]
278
+ # elev_output = estimate_elev(tmp_dir)
279
+ # if elev_output > 75:
280
+ if 90-elev >75:
281
  rerun_idx_in = [i if i < 4 else i+4 for i in rerun_idx]
282
  else:
283
  rerun_idx_in = rerun_idx
 
295
  def stage2_run(models, device, tmp_dir,
296
  elev, scale, rerun_all=[], stage2_steps=50):
297
  # print("elev", elev)
298
+ flag_lower_cam = 90-int(elev["label"]) <= 75
299
  is_rerun = True if rerun_all else False
300
  if not is_rerun:
301
  if flag_lower_cam:
 
529
  bbox_block = gr.Image(type='pil', label="Bounding box", interactive=False).style(height=300)
530
  sam_block = gr.Image(type='pil', label="SAM output", interactive=False)
531
  max_width = max_height = 256
 
 
532
  with gr.Row():
533
  x_min_slider = gr.Slider(
534
  label="X min",
 
588
  label='Number of diffusion inference steps')
589
 
590
  # with gr.Row():
591
+ run_btn = gr.Button('Run Generation', variant='primary', interactive=False)
592
  # guide_title = gr.Markdown(_GUIDE_TITLE, visible=True)
593
  guide_text = gr.Markdown(_USER_GUIDE, visible=True)
594
 
595
+ mesh_output = gr.Model3D(clear_color=[0.0, 0.0, 0.0, 0.0], label="One-2-3-45's Textured Mesh", elem_id="model-3d-out")
 
 
596
 
597
  with gr.Row(variant='panel'):
598
  with gr.Column(scale=0.85):
599
  with gr.Row():
600
+ elev_output = gr.Label(label='Estimated elevation (degree, w.r.t. the horizontal plane)')
 
 
 
601
  vis_output = gr.Plot(
602
  label='Camera poses of the input view (red) and predicted views (blue)')
603
 
 
684
  # make regen_btn visible when any of the btn_retry is checked
685
  for btn_retry in btn_retrys:
686
  # Add the event handlers to the btn_retry buttons
 
687
  btn_retry.change(fn=on_retry_button_click, inputs=[*btn_retrys], outputs=[regen_view_btn, regen_mesh_btn])
688
 
 
689
 
690
  run_btn.click(fn=partial(update_guide, _SAM), outputs=[guide_text]
691
  ).success(fn=partial(preprocess_run, predictor, models),
 
703
 
704
 
705
  regen_view_btn.click(fn=partial(stage1_run, models, device, None),
706
+ inputs=[tmp_dir, sam_block, scale_slider, steps_slider, elev_output, rerun_idx, *btn_retrys],
707
  outputs=[rerun_idx, *btn_retrys, *views]
708
  ).success(fn=partial(update_guide, _REGEN_1), outputs=[guide_text])
709
  regen_mesh_btn.click(fn=partial(stage2_run, models, device),
env.sh DELETED
@@ -1,4 +0,0 @@
1
- #!/bin/bash
2
-
3
- export TORCH_CUDA_ARCH_LIST="{archs}"
4
- export IABN_FORCE_CUDA=1