Tobias Cornille commited on
Commit
9d95507
1 Parent(s): d738b86

Fix device

Browse files
Files changed (2) hide show
  1. app.py +11 -31
  2. requirements.txt +3 -2
app.py CHANGED
@@ -1,7 +1,5 @@
1
  import subprocess, os, sys
2
 
3
- os.environ["CUDA_VISIBLE_DEVICES"] = "0"
4
-
5
  result = subprocess.run(["pip", "install", "-e", "GroundingDINO"], check=True)
6
  print(f"pip install GroundingDINO = {result}")
7
 
@@ -55,20 +53,8 @@ from segment_anything import build_sam, SamPredictor
55
  from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
56
 
57
 
58
- def get_device():
59
- from numba import cuda
60
-
61
- if cuda.is_available():
62
- device = "cuda:0" # cuda.get_current_device()
63
- else:
64
- device = "cpu"
65
- return device
66
-
67
-
68
- def load_model_hf(repo_id, filename, ckpt_config_filename, device="cpu"):
69
- cache_config_file = hf_hub_download(repo_id=repo_id, filename=ckpt_config_filename)
70
-
71
- args = SLConfig.fromfile(cache_config_file)
72
  model = build_model(args)
73
  args.device = device
74
 
@@ -298,17 +284,13 @@ def generate_panoptic_mask(
298
  image = image.convert("RGB")
299
  image_array = np.asarray(image)
300
 
301
- groundingdino_device = "cpu"
302
  if device != "cpu":
303
  try:
304
  from GroundingDINO.groundingdino import _C
305
-
306
- groundingdino_device = "cuda:0"
307
  except:
308
  warnings.warn(
309
  "Failed to load custom C++ ops. Running on CPU mode Only in groundingdino!"
310
  )
311
- groundingdino_device = "cpu"
312
 
313
  # detect boxes for "thing" categories using Grounding DINO
314
  thing_boxes, _ = dino_detection(
@@ -319,7 +301,7 @@ def generate_panoptic_mask(
319
  category_name_to_id,
320
  dino_box_threshold,
321
  dino_text_threshold,
322
- groundingdino_device,
323
  )
324
  # compute SAM image embedding
325
  sam_predictor.set_image(image_array)
@@ -376,29 +358,27 @@ def generate_panoptic_mask(
376
  return fig
377
 
378
 
 
379
  ckpt_repo_id = "ShilongLiu/GroundingDINO"
380
- ckpt_filename = "groundingdino_swinb_cogcoor.pth"
381
- ckpt_config_filename = "GroundingDINO_SwinB.cfg.py"
382
-
383
  sam_checkpoint = "./sam_vit_h_4b8939.pth"
384
- output_dir = "outputs"
385
- device = "cuda"
386
-
387
- device = get_device()
388
 
389
- print(f"device={device}")
390
 
391
  # initialize groundingdino model
392
- dino_model = load_model_hf(ckpt_repo_id, ckpt_filename, ckpt_config_filename, device)
393
  dino_model = dino_model.to(device)
394
 
395
  # initialize SAM
396
- sam_predictor = SamPredictor(build_sam(checkpoint=sam_checkpoint))
 
 
397
 
398
  clipseg_processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
399
  clipseg_model = CLIPSegForImageSegmentation.from_pretrained(
400
  "CIDAS/clipseg-rd64-refined"
401
  )
 
402
 
403
 
404
  if __name__ == "__main__":
 
1
  import subprocess, os, sys
2
 
 
 
3
  result = subprocess.run(["pip", "install", "-e", "GroundingDINO"], check=True)
4
  print(f"pip install GroundingDINO = {result}")
5
 
 
53
  from transformers import CLIPSegProcessor, CLIPSegForImageSegmentation
54
 
55
 
56
+ def load_model_hf(model_config_path, repo_id, filename, device):
57
+ args = SLConfig.fromfile(model_config_path)
 
 
 
 
 
 
 
 
 
 
 
 
58
  model = build_model(args)
59
  args.device = device
60
 
 
284
  image = image.convert("RGB")
285
  image_array = np.asarray(image)
286
 
 
287
  if device != "cpu":
288
  try:
289
  from GroundingDINO.groundingdino import _C
 
 
290
  except:
291
  warnings.warn(
292
  "Failed to load custom C++ ops. Running on CPU mode Only in groundingdino!"
293
  )
 
294
 
295
  # detect boxes for "thing" categories using Grounding DINO
296
  thing_boxes, _ = dino_detection(
 
301
  category_name_to_id,
302
  dino_box_threshold,
303
  dino_text_threshold,
304
+ device,
305
  )
306
  # compute SAM image embedding
307
  sam_predictor.set_image(image_array)
 
358
  return fig
359
 
360
 
361
+ config_file = "GroundingDINO/groundingdino/config/GroundingDINO_SwinT_OGC.py"
362
  ckpt_repo_id = "ShilongLiu/GroundingDINO"
363
+ ckpt_filename = "groundingdino_swint_ogc.pth"
 
 
364
  sam_checkpoint = "./sam_vit_h_4b8939.pth"
 
 
 
 
365
 
366
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
367
 
368
  # initialize groundingdino model
369
+ dino_model = load_model_hf(config_file, ckpt_repo_id, ckpt_filename, device)
370
  dino_model = dino_model.to(device)
371
 
372
  # initialize SAM
373
+ sam = build_sam(checkpoint=sam_checkpoint)
374
+ sam.to(device=device)
375
+ sam_predictor = SamPredictor(sam)
376
 
377
  clipseg_processor = CLIPSegProcessor.from_pretrained("CIDAS/clipseg-rd64-refined")
378
  clipseg_model = CLIPSegForImageSegmentation.from_pretrained(
379
  "CIDAS/clipseg-rd64-refined"
380
  )
381
+ clipseg_model.to(device)
382
 
383
 
384
  if __name__ == "__main__":
requirements.txt CHANGED
@@ -19,7 +19,8 @@ torch
19
  torchvision
20
  transformers
21
  yapf
22
- numba
23
  segment_anything
24
  scikit-image
25
- segments-ai
 
 
 
19
  torchvision
20
  transformers
21
  yapf
 
22
  segment_anything
23
  scikit-image
24
+ segments-ai
25
+ --extra-index-url https://download.pytorch.org/whl/cu113
26
+ torch