John Ho commited on
Commit
b2e3d42
·
1 Parent(s): 7afaf9e

added input validation

Browse files
Files changed (2) hide show
  1. README.md +15 -0
  2. app.py +11 -1
README.md CHANGED
@@ -11,3 +11,18 @@ short_description: sam2 images and video inference on ZeroGPU
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
14
+
15
+ # Changelog
16
+
17
+ this huggingspace demo borrows extensively from [SkalskiP/florence-sam](https://huggingface.co/spaces/SkalskiP/florence-sam/tree/main).
18
+ Here are some changes that's noteworthy:
19
+
20
+ * the `RuntimeError: No available kernel. Aborting execution.` error documented [here](https://github.com/facebookresearch/sam2/issues/48), [here](https://huggingface.co/spaces/SkalskiP/florence-sam/discussions/5), and [here](https://github.com/facebookresearch/sam2/issues/100) is fixed with the following lines in `app.py` (see [this commit](https://huggingface.co/spaces/SkalskiP/florence-sam/commit/488d99ebc8b60d6a87b84a545dc0561cfadefc65)) with the `@torch.inference_mode()` and
21
+ `@torch.autocast(device_type="cuda", dtype=torch.bfloat16)` before
22
+ the inference function:
23
+ ```python
24
+ torch.autocast(device_type="cuda", dtype=torch.bfloat16).__enter__()
25
+ if torch.cuda.get_device_properties(0).major >= 8:
26
+ torch.backends.cuda.matmul.allow_tf32 = True
27
+ torch.backends.cudnn.allow_tf32 = True
28
+ ```
app.py CHANGED
@@ -69,7 +69,17 @@ def detect_image(
69
  Returns:
70
  list: a list of masks
71
  """
72
- bboxes = json.loads(bboxes) if isinstance(bboxes, str) else bboxes
 
 
 
 
 
 
 
 
 
 
73
  model = load_im_model(variant=variant)
74
  return run_sam_im_inference(
75
  model, image=im, bboxes=bboxes, get_pil_mask=False, b64_encode_mask=True
 
69
  Returns:
70
  list: a list of masks
71
  """
72
+ bboxes = (
73
+ json.loads(bboxes)
74
+ if isinstance(bboxes, str) and type(bboxes) != type(None)
75
+ else bboxes
76
+ )
77
+ assert bboxes or points, f"either bboxes or points must be provided."
78
+ if points:
79
+ assert len(points) == len(
80
+ point_labels
81
+ ), f"{len(points)} points provided but there are {len(point_labels)} labels."
82
+
83
  model = load_im_model(variant=variant)
84
  return run_sam_im_inference(
85
  model, image=im, bboxes=bboxes, get_pil_mask=False, b64_encode_mask=True