Sub topic 6
Ambiguous Prompts and Multiple Masks
This chapter assumes that you know basic Python and NumPy, understand image segmentation, and have loaded a SAM checkpoint. It also assumes that you know the SamPredictor workflow: call set_image once, then call predict with prompts.
Why one prompt can have several valid masks
A prompt does not always identify one exact object. A point inside a bicycle could refer to the wheel, the whole bicycle, or a larger connected region. A point near a boundary could belong to either of two touching objects. A loose box can contain several objects.
SAM handles this uncertainty by returning multiple candidate masks when you set multimask_output=True. In the official predictor, this normally produces three masks and three predicted quality scores.
The candidates represent different interpretations of the same prompt. They are not three independent predictions from three different images.
What the quality scores mean
The returned scores array contains SAM's predicted intersection-over-union, or predicted IoU, for each mask. IoU measures overlap between a predicted mask and an expected target mask:
IoU = \frac{\text{intersection area}}{\text{union area}}A higher predicted IoU usually indicates a better candidate. However, it is a model estimate, not a proof that the mask matches your intended object. If the prompt itself is ambiguous, SAM can confidently choose the wrong interpretation.
SAM also returns low-resolution logits. A logit is an unthresholded value used to form the mask. These values are useful when refining a mask with additional prompts.
| Prompt situation | Typical ambiguity | Useful response |
|---|---|---|
| Point inside a large object | Whole object versus a part | Inspect all candidates |
| Point between touching objects | Either neighboring object | Add positive and negative points |
| Loose bounding box | Several objects inside the box | Add points or use a tighter box |
Selecting a candidate
The simplest strategy is to choose the candidate with the largest score:
import numpy as np
masks, scores, logits = predictor.predict(
point_coords=np.array([[500, 375]]),
point_labels=np.array([1]),
multimask_output=True,
)
best_index = int(np.argmax(scores))
best_mask = masks[best_index]
print("candidate scores:", scores)
print("selected candidate:", best_index)This works well when the prompt clearly identifies one object. For an ambiguous prompt, inspect the masks instead of trusting argmax blindly. Useful checks include the mask area, its bounding box, and whether it follows the intended boundary.
A candidate with a very large area may cover the whole scene. A small candidate may cover only a visible part. Neither size is automatically correct. The intended label and the application determine the right choice.
When to use one mask instead
Set multimask_output=False when you have a precise prompt, such as a tight box or several corrective points. SAM then returns one mask and one score. This reduces selection work, but it does not remove errors caused by poor prompts.
A practical workflow is:
- Start with a point and request multiple masks.
- Inspect the candidates.
- Select the intended interpretation.
- Add foreground or background points if needed.
- Request one refined mask using the previous logits.
> callout note > Predicted IoU ranks SAM's candidate interpretations. It does not know your task label, business rule, or which object you intended.
Common mistakes
- Treating all three masks as three objects. They are alternative interpretations of one prompt.
- Selecting the largest mask automatically. The largest region may include connected background or neighboring objects.
- Assuming a high score guarantees correctness. Scores are predictions and can be wrong at boundaries, under occlusion, or with unusual images.
- Using a single point for a crowded scene. Add negative points to exclude nearby objects.
Exercise
A point lies on the handle of a mug, and SAM returns masks for the handle, the whole mug, and the mug plus its shadow. Which mask should you choose, and what can you do if the score ranking is unclear?
Show answer
Choose the mask that matches the task label, not automatically the highest-scoring mask. Inspect the candidates and add a foreground point on the mug or a background point on the shadow to refine the result.