JasonYinnnn commited on
Commit
454fd88
·
1 Parent(s): 5add261

run dpt on GPU

Browse files
Files changed (1) hide show
  1. app.py +10 -6
app.py CHANGED
@@ -7,6 +7,7 @@ import gradio as gr
7
  import spaces
8
 
9
  import os
 
10
  import uuid
11
  from typing import Any, List, Optional, Union
12
  import cv2
@@ -184,6 +185,7 @@ def run_segmentation(
184
 
185
  return seg_map_pil
186
 
 
187
  def run_depth_estimation(
188
  image_prompts: Any,
189
  seg_image: Union[str, Image.Image],
@@ -192,6 +194,10 @@ def run_depth_estimation(
192
 
193
  rgb_image = rgb_image.resize((1024, 1024), Image.Resampling.LANCZOS)
194
 
 
 
 
 
195
  global dpt_pack
196
  global work_space
197
  if work_space is None:
@@ -210,7 +216,7 @@ def run_depth_estimation(
210
  W, H = rgb_image.size
211
 
212
  input_image = np.array(rgb_image).astype(np.float32)
213
- input_image = torch.tensor(input_image / 255, dtype=torch.float32, device='cpu').permute(2, 0, 1)
214
 
215
  with torch.no_grad():
216
  output = moge_v2_dpt_model.infer(input_image)
@@ -227,7 +233,7 @@ def run_depth_estimation(
227
  [0, intrinsics[1, 1].item() * H, 0.5*H],
228
  [0, 0, 1]
229
  ])
230
- ).to(dtype=torch.float32, device='cpu')
231
 
232
  dpt_pack = {
233
  'c2w': c2w.to('cpu'),
@@ -356,8 +362,6 @@ def run_generation(
356
  generated_object_map = {}
357
  run_id = str(uuid.uuid4())
358
 
359
- # pipeline.cuda()
360
-
361
  if not isinstance(rgb_image, Image.Image) and "image" in rgb_image:
362
  rgb_image = rgb_image["image"]
363
 
@@ -844,12 +848,12 @@ if __name__ == '__main__':
844
  segmenter_id = "facebook/sam-vit-base"
845
  sam_processor = AutoProcessor.from_pretrained(segmenter_id)
846
  sam_segmentator = AutoModelForMaskGeneration.from_pretrained(segmenter_id).to(
847
- "cpu", torch.float32
848
  )
849
 
850
  mogev2_id = 'Ruicheng/moge-2-vitl'
851
  moge_v2_dpt_model = MoGeModel.from_pretrained(mogev2_id).to(
852
- "cpu", torch.float32
853
  )
854
 
855
  ############## 3D-Fixer model
 
7
  import spaces
8
 
9
  import os
10
+ os.environ['SPCONV_ALGO'] = 'native'
11
  import uuid
12
  from typing import Any, List, Optional, Union
13
  import cv2
 
185
 
186
  return seg_map_pil
187
 
188
+ @spaces.GPU
189
  def run_depth_estimation(
190
  image_prompts: Any,
191
  seg_image: Union[str, Image.Image],
 
194
 
195
  rgb_image = rgb_image.resize((1024, 1024), Image.Resampling.LANCZOS)
196
 
197
+ device = 'cuda' if torch.cuda.is_available() else 'cpu'
198
+ dtype = torch.float16 if device == 'cuda' else torch.float32
199
+ moge_v2_dpt_model = moge_v2_dpt_model.to(device=device, dtype=dtype)
200
+
201
  global dpt_pack
202
  global work_space
203
  if work_space is None:
 
216
  W, H = rgb_image.size
217
 
218
  input_image = np.array(rgb_image).astype(np.float32)
219
+ input_image = torch.tensor(input_image / 255, dtype=torch.float32, device=device).permute(2, 0, 1)
220
 
221
  with torch.no_grad():
222
  output = moge_v2_dpt_model.infer(input_image)
 
233
  [0, intrinsics[1, 1].item() * H, 0.5*H],
234
  [0, 0, 1]
235
  ])
236
+ ).to(dtype=torch.float32, device=device)
237
 
238
  dpt_pack = {
239
  'c2w': c2w.to('cpu'),
 
362
  generated_object_map = {}
363
  run_id = str(uuid.uuid4())
364
 
 
 
365
  if not isinstance(rgb_image, Image.Image) and "image" in rgb_image:
366
  rgb_image = rgb_image["image"]
367
 
 
848
  segmenter_id = "facebook/sam-vit-base"
849
  sam_processor = AutoProcessor.from_pretrained(segmenter_id)
850
  sam_segmentator = AutoModelForMaskGeneration.from_pretrained(segmenter_id).to(
851
+ "cpu", dtype=torch.float32
852
  )
853
 
854
  mogev2_id = 'Ruicheng/moge-2-vitl'
855
  moge_v2_dpt_model = MoGeModel.from_pretrained(mogev2_id).to(
856
+ "cpu", dtype=torch.float32
857
  )
858
 
859
  ############## 3D-Fixer model