facebookresearch / facebookresearch/segment-anything
Multiple Boxes as prompt
- Dominant language
- Jupyter Notebook
- Stars
- 54.9k
- Forks
- 6.4k
- PR merge metrics
- No merged PRs in 30d
Description
Hello, thanks for the nice work; well done.
Can the SAM take multiple Bounding boxes as prompts for segmentation? For example, if I draw boxes around two objects, say a building and a dog,

The SAM only segments the one as
masks, scores, logits = mask_predictor.predict(
box=box,
multimask_output=True
Here, it expects the size of the box as [1,4].
If it becomes [2,4], it raises this error.
157 if boxes is not None:
158 box_embeddings = self._embed_boxes(boxes)
--> 159 sparse_embeddings = torch.cat([sparse_embeddings, box_embeddings], dim=1)
160
161 if masks is not None:
RuntimeError: Sizes of tensors must match except in dimension 1. Expected size 1 but got size 2 for tensor number 1 in the list.
Not sure how to change the size of sparse_embeddings as well to [2,4]!!!
Thank you
Cheers
Abbas
Contributor guide
Research direction
The issue names mask_predictor.predict and the prompt-encoder line where sparse_embeddings is concatenated with box_embeddings. Start by reproducing the [2,4] box case and tracing tensor shapes through that call. Done means the prompt path accepts multiple boxes and returns masks for the requested objects without the reported size-mismatch error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, pytorch
- Domain
- computer-vision, machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100