bartduis commited on
Commit
560e9b5
·
1 Parent(s): dc82be6
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -12,6 +12,7 @@ from PIL import Image
12
  import matplotlib.pyplot as plt
13
  from eval_wrapper.eval import EvalWrapper, eval_scene
14
  from torchvision import transforms
 
15
 
16
  outdir = "/tmp/rayst3r"
17
 
@@ -19,16 +20,16 @@ outdir = "/tmp/rayst3r"
19
  print("Loading DINOv2 model")
20
  dino_model = torch.hub.load('facebookresearch/dinov2', "dinov2_vitl14_reg")
21
  dino_model.eval()
22
- dino_model.to("cuda")
23
 
24
  print("Loading MoGe model")
25
- device = torch.device("cuda")
26
  # Load the model from huggingface hub (or load from local).
27
- moge_model = MoGeModel.from_pretrained("Ruicheng/moge-vitl").to(device)
28
 
29
  print("Loading RaySt3R model")
30
  rayst3r_checkpoint = hf_hub_download("bartduis/rayst3r", "rayst3r.pth")
31
- rayst3r_model = EvalWrapper(rayst3r_checkpoint,device=device)
32
 
33
  def depth2uint16(depth):
34
  return depth * torch.iinfo(torch.uint16).max / 10.0 # threshold is in m, convert to uint16 value
@@ -89,14 +90,19 @@ def prep_for_rayst3r(img,depth_dict,mask):
89
  depth = depth_dict["depth"].cpu()
90
  depth = depth2uint16(depth)
91
  save_tensor_as_png(depth, os.path.join(input_dir, "depth.png"),dtype=torch.uint16)
92
-
93
  # save mask as bool
94
  save_tensor_as_png(torch.from_numpy(mask).bool(), os.path.join(input_dir, "mask.png"),dtype=torch.bool)
95
  # save image
96
  save_tensor_as_png(torch.from_numpy(img), os.path.join(input_dir, "rgb.png"))
97
 
 
98
  def rayst3r_to_glb(img,depth_dict,mask,max_total_points=10e6,rotated=False):
99
  prep_for_rayst3r(img,depth_dict,mask)
 
 
 
 
100
  rayst3r_points = eval_scene(rayst3r_model,os.path.join(outdir, "input"),do_filter_all_masks=True,dino_model=dino_model).cpu()
101
 
102
  # subsample points
@@ -145,9 +151,11 @@ def input_to_glb(outdir,img,depth_dict,mask,rotated=False):
145
  scene.export(outfile)
146
  return outfile
147
 
 
148
  def depth_moge(input_img):
149
- input_img_torch = torch.tensor(input_img / 255, dtype=torch.float32, device=device).permute(2, 0, 1)
150
- output = moge_model.infer(input_img_torch)
 
151
  return output
152
 
153
  def mask_rembg(input_img):
@@ -168,7 +176,10 @@ def mask_rembg(input_img):
168
  rgb = output_np[:,:,:3]
169
  return mask, rgb
170
 
 
171
  def process_image(input_img):
 
 
172
  # resize the input image
173
  rotated = False
174
  #if input_img.shape[0] > input_img.shape[1]:
 
12
  import matplotlib.pyplot as plt
13
  from eval_wrapper.eval import EvalWrapper, eval_scene
14
  from torchvision import transforms
15
+ from spaces import GPU
16
 
17
  outdir = "/tmp/rayst3r"
18
 
 
20
  print("Loading DINOv2 model")
21
  dino_model = torch.hub.load('facebookresearch/dinov2', "dinov2_vitl14_reg")
22
  dino_model.eval()
23
+ #dino_model.to("cuda")
24
 
25
  print("Loading MoGe model")
26
+ #device = torch.device("cuda")
27
  # Load the model from huggingface hub (or load from local).
28
+ moge_model = MoGeModel.from_pretrained("Ruicheng/moge-vitl")
29
 
30
  print("Loading RaySt3R model")
31
  rayst3r_checkpoint = hf_hub_download("bartduis/rayst3r", "rayst3r.pth")
32
+ rayst3r_model = EvalWrapper(rayst3r_checkpoint,device='cpu')
33
 
34
  def depth2uint16(depth):
35
  return depth * torch.iinfo(torch.uint16).max / 10.0 # threshold is in m, convert to uint16 value
 
90
  depth = depth_dict["depth"].cpu()
91
  depth = depth2uint16(depth)
92
  save_tensor_as_png(depth, os.path.join(input_dir, "depth.png"),dtype=torch.uint16)
93
+
94
  # save mask as bool
95
  save_tensor_as_png(torch.from_numpy(mask).bool(), os.path.join(input_dir, "mask.png"),dtype=torch.bool)
96
  # save image
97
  save_tensor_as_png(torch.from_numpy(img), os.path.join(input_dir, "rgb.png"))
98
 
99
+ @GPU
100
  def rayst3r_to_glb(img,depth_dict,mask,max_total_points=10e6,rotated=False):
101
  prep_for_rayst3r(img,depth_dict,mask)
102
+
103
+ dino_model.to("cuda")
104
+ rayst3r_model.to("cuda")
105
+
106
  rayst3r_points = eval_scene(rayst3r_model,os.path.join(outdir, "input"),do_filter_all_masks=True,dino_model=dino_model).cpu()
107
 
108
  # subsample points
 
151
  scene.export(outfile)
152
  return outfile
153
 
154
+ @GPU
155
  def depth_moge(input_img):
156
+ moge_model.to("cuda")
157
+ input_img_torch = torch.tensor(input_img / 255, dtype=torch.float32, device='cuda').permute(2, 0, 1)
158
+ output = moge_model.infer(input_img_torch).cpu()
159
  return output
160
 
161
  def mask_rembg(input_img):
 
176
  rgb = output_np[:,:,:3]
177
  return mask, rgb
178
 
179
+ @GPU
180
  def process_image(input_img):
181
+
182
+
183
  # resize the input image
184
  rotated = False
185
  #if input_img.shape[0] > input_img.shape[1]: