Sub topic 13
Where SAM 2 and Video Fit In
Assume you know basic Python, NumPy, pretrained checkpoints, and the difference between image segmentation and object detection. SAM 1 segments one image at a time. SAM 2 extends the promptable idea to video, where a prompt on one frame can guide masks on later frames.
From image embeddings to video memory
SAM 1 uses an image encoder, a prompt encoder, and a mask decoder. The image encoder runs once for an image, and the decoder can answer several prompts using the cached embedding.
SAM 2 keeps this design but adds memory for video. It processes frames over time and stores visual information about prompted objects. A memory mechanism helps the model locate the same object in later frames, even when the object moves or changes appearance.
A typical video workflow is:
- Load a SAM 2 video predictor.
- Initialize state from a directory of video frames.
- Add a point or box prompt on an initial frame.
- Propagate the object mask through later frames.
- Add correction prompts when the mask drifts.
The state is more than a cached image embedding. It includes information needed for object identities and temporal propagation. This makes SAM 2 useful for tracking-like segmentation without requiring a separate detector and tracker pipeline.
Prompting and propagating a mask
The official SAM 2 package provides a video predictor. The following example adds a foreground point to frame 0 and propagates object ID 1 through the frame directory.
import numpy as np
from sam2.build_sam import build_sam2_video_predictor
model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
checkpoint = "checkpoints/sam2.1_hiera_large.pt"
video_dir = "video_frames" # frames such as 00000.jpg, 00001.jpg, ...
predictor = build_sam2_video_predictor(model_cfg, checkpoint)
state = predictor.init_state(video_path=video_dir)
frame_index = 0
object_id = 1
points = np.array([[500, 375]], dtype=np.float32)
labels = np.array([1], dtype=np.int32) # 1 means foreground
_, object_ids, mask_logits = predictor.add_new_points_or_box(
inference_state=state,
frame_idx=frame_index,
obj_id=object_id,
points=points,
labels=labels,
)
for output_frame, output_ids, output_logits in predictor.propagate_in_video(state):
object_position = output_ids.index(object_id)
mask = (output_logits[object_position] > 0.0).cpu().numpy()
print(output_frame, mask.shape)A logit is an unnormalized score. Values above zero usually represent foreground after thresholding. The output mask is still a prediction, so inspect it visually before using it in measurement or automation.
To correct a failure, add a foreground point inside the object or a background point outside it on the frame where the error appears. SAM 2 can use that new information while continuing propagation. In practice, corrections are safer than blindly accepting every frame.
When to choose SAM 1 or SAM 2
| Situation | Better starting point | Reason |
|---|---|---|
| One image and interactive prompts | SAM 1 | Simple image-level workflow |
| Many independent images | SAM 1 | Each image can be encoded separately |
| One object across moving frames | SAM 2 | Temporal memory propagates the mask |
| Long video with scene cuts | SAM 2 with re-prompting | Memory may need to be reset or corrected |
SAM 2 can still segment individual images, but its main advantage is temporal context. It does not guarantee perfect tracking. Fast motion, severe occlusion, similar objects, lighting changes, camera cuts, and objects leaving the frame can cause identity swaps or mask drift.
Exercise
Exercise
A mask is correct for the first 30 frames but then includes a nearby object. What should you try first?
Show answer
Add a background point on the nearby object and a foreground point on the intended object at the frame where the error begins, then continue propagation and inspect the later masks.