Image Segmentation
Transformers
Safetensors
sam2
instance-segmentation
panoptic-segmentation
semantic-segmentation
zero-shot
open-vocabulary
beit3
fiftyone
Instructions to use Voxel51/openworld-sam with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Voxel51/openworld-sam with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-segmentation", model="Voxel51/openworld-sam")# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("Voxel51/openworld-sam", dtype="auto") - sam2
How to use Voxel51/openworld-sam with sam2:
# Use SAM2 with images import torch from sam2.sam2_image_predictor import SAM2ImagePredictor predictor = SAM2ImagePredictor.from_pretrained(Voxel51/openworld-sam) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): predictor.set_image(<your_image>) masks, _, _ = predictor.predict(<input_prompts>)# Use SAM2 with videos import torch from sam2.sam2_video_predictor import SAM2VideoPredictor predictor = SAM2VideoPredictor.from_pretrained(Voxel51/openworld-sam) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): state = predictor.init_state(<your_video>) # add new prompts and instantly get the output on the same frame frame_idx, object_ids, masks = predictor.add_new_points(state, <your_prompts>): # propagate the prompts to get masklets throughout the video for frame_idx, object_ids, masks in predictor.propagate_in_video(state): ... - Notebooks
- Google Colab
- Kaggle
feat: configurable mask_decoder_chunk_size
#2
by neerajaabhyankar - opened
- modeling_openworld_sam.py +28 -9
modeling_openworld_sam.py
CHANGED
|
@@ -319,16 +319,35 @@ class OpenWorldSAMModel(PreTrainedModel):
|
|
| 319 |
f[img_idx].unsqueeze(0) for f in _features["high_res_feats"]
|
| 320 |
]
|
| 321 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 322 |
with torch.no_grad():
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
|
| 333 |
pred_masks = low_res_masks.squeeze(1) # [total_tokens, H_low, W_low]
|
| 334 |
pred_logits = iou_pred.squeeze(1) # [total_tokens]
|
|
|
|
| 319 |
f[img_idx].unsqueeze(0) for f in _features["high_res_feats"]
|
| 320 |
]
|
| 321 |
|
| 322 |
+
# The decoder's repeat_image path duplicates image_embeddings and
|
| 323 |
+
# image_pe once per candidate query, so scoring all num_classes *
|
| 324 |
+
# num_tokens candidates in one call can reach double-digit GiB
|
| 325 |
+
# for large vocabularies. `chunk_size` bounds peak memory
|
| 326 |
+
# rather than total candidate count.
|
| 327 |
+
image_embed_img = _features["image_embed"][img_idx].unsqueeze(0)
|
| 328 |
+
image_pe = self.evf_sam2.visual_model.sam_prompt_encoder.get_dense_pe()
|
| 329 |
+
chunk_size = self.config.mask_decoder_chunk_size
|
| 330 |
+
num_total_tokens = sparse_embeddings.shape[0]
|
| 331 |
+
|
| 332 |
+
low_res_masks_chunks = []
|
| 333 |
+
iou_pred_chunks = []
|
| 334 |
+
|
| 335 |
with torch.no_grad():
|
| 336 |
+
for start in range(0, num_total_tokens, chunk_size):
|
| 337 |
+
end = min(start + chunk_size, num_total_tokens)
|
| 338 |
+
chunk_low_res_masks, chunk_iou_pred, _, _ = self.evf_sam2.visual_model.sam_mask_decoder(
|
| 339 |
+
image_embeddings=image_embed_img,
|
| 340 |
+
image_pe=image_pe,
|
| 341 |
+
sparse_prompt_embeddings=sparse_embeddings[start:end],
|
| 342 |
+
dense_prompt_embeddings=dense_embeddings[start:end],
|
| 343 |
+
multimask_output=False,
|
| 344 |
+
repeat_image=True,
|
| 345 |
+
high_res_features=high_res_features,
|
| 346 |
+
)
|
| 347 |
+
low_res_masks_chunks.append(chunk_low_res_masks)
|
| 348 |
+
iou_pred_chunks.append(chunk_iou_pred)
|
| 349 |
+
low_res_masks = torch.cat(low_res_masks_chunks, dim=0)
|
| 350 |
+
iou_pred = torch.cat(iou_pred_chunks, dim=0)
|
| 351 |
|
| 352 |
pred_masks = low_res_masks.squeeze(1) # [total_tokens, H_low, W_low]
|
| 353 |
pred_logits = iou_pred.squeeze(1) # [total_tokens]
|