Sub topic 17
Promptable segmentation models
Assume you know binary masks, IoU, convolutional neural networks, and basic object detection. Promptable segmentation models produce a mask from a user-provided hint instead of requiring a fixed object class list.
The prompt-to-mask workflow
A prompt identifies the region or object of interest. Common prompts include:
- A point inside the object.
- A box around the object.
- Several positive and negative points.
- A rough mask.
- In video, a mask from an earlier frame.
A vision encoder converts the image into features. A prompt encoder converts the hint into another feature representation. A mask decoder combines both representations and predicts one or more candidate masks. A confidence score helps choose between candidates.
Models such as the Segment Anything Model, or SAM, are trained on very large collections of images and masks. Their broad training allows them to segment many visual objects without retraining a separate classifier for every category. However, “segmentable” does not always mean “correctly identified.” The model may follow texture, lighting, or an object part when the prompt is unclear.
A box-prompt example
The original SAM Python package accepts an image and a bounding-box prompt. The box coordinates use pixel order [x_min, y_min, x_max, y_max].
import cv2
import numpy as np
from segment_anything import sam_model_registry, SamPredictor
image = cv2.cvtColor(cv2.imread("image.jpg"), cv2.COLOR_BGR2RGB)
sam = sam_model_registry["vit_b"](
checkpoint="sam_vit_b_01ec64.pth"
)
predictor = SamPredictor(sam)
predictor.set_image(image)
box = np.array([100, 80, 520, 430])
masks, scores, _ = predictor.predict(
box=box,
multimask_output=True
)
best = masks[np.argmax(scores)]
print("mask shape:", best.shape)
print("foreground pixels:", int(best.sum()))With multimask_output=True, the decoder returns alternatives because one prompt can be ambiguous. Inspect the candidates rather than blindly accepting the first mask. A box that covers several touching objects can produce a combined region or an unexpected single object.
Choosing promptable models
Promptable models are useful for interactive annotation, rapid prototyping, and bootstrapping a training dataset. They can reduce manual tracing, but their masks still need review. For a production system that must classify every object automatically, a class-aware semantic or instance model is often more predictable.
Common failure modes include small objects disappearing after image resizing, thin structures being broken, overlapping objects being merged, and domain-specific imagery differing from the training data. Measure quality on your own images with IoU or mask AP. If prompts come from a detector, detector errors also limit the final masks.
Exercise
A box contains two touching cars, but the model returns one combined mask. What should you try first?
Show answer
Use multimask output and add positive points on the desired car plus negative points on the neighboring car.