Skip to content

Video understanding, tracking, generation, and world models · Video segmentation · VID039 · Model

Segment Anything Model 2 (SAM 2)

Performs promptable segmentation in images and videos, using streaming memory to maintain an object across frames and incorporate corrections. Prompts may identify an object through points, boxes or masks;

Explained here Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Performs promptable segmentation in images and videos, using streaming memory to maintain an object across frames and incorporate corrections. Prompts may identify an object through points, boxes or masks; the task differs from autonomous semantic class recognition.

Question / objective

Maintain an explicitly identified object mask across frames under a defined annotation or prompting protocol. This is a learning or inference objective, not a test of a statistical null hypothesis.

Method type and supervision

model · Video segmentation. Annotated training plus an object initialization/prompt protocol at inference; not purely unsupervised.

Architecture / computational structure

Image encoder, prompt encoder, memory attention and streaming mask decoder.

Assumptions

Inputs must satisfy the temporal, modality and annotation protocol described below. Prompt ambiguity, occlusion, similar objects and scene cuts can cause drift. Future-frame correction must not be counted as purely causal performance, and prompt count belongs in the evaluation budget.

Formula / computational definition

m^t=D(E(xt),pt,Read⁡(M<t))\hat m_t=D(E(x_t),p_t,\operatorname{Read}(M_{<t})) schematically combines frame features, prompt ptp_t and memory. Training uses segmentation and related quality/objectness objectives; the public implementation defines their exact composition.

Parameters and notation

Learned/estimated quantities: Visual/key/value/prompt/decoder weights are learned as applicable; stored per-video memories and masks are runtime state. Symbols specific to the objective are defined with the equation. Hyperparameters and design choices: Prompt placement, correction policy, memory settings, frame resolution and propagation direction determine the interaction protocol.

Fitting, fine-tuning, and adaptation

Tune prompt/mask interfaces first when labels are limited; selectively unfreeze the visual encoder for large domain changes. Train with realistic prompt corrections and monitor interactive performance. Use a separate adaptation validation split, monitor the target metric and retention of previously useful behavior, and save the exact preprocessing and training configuration.

How to use and calibrate

Fix the input protocol and target before fitting; choose design settings on held-out source videos, then freeze the evaluation procedure. Report mask overlap/contours, clicks or corrections required, latency and temporal consistency under a specified prompting procedure.

Example application

Interactively isolate one object across a video with occasional corrections.

Interpretation / pitfalls

Watch out

An optimized training objective or a favorable example does not establish generalization. Report mask overlap/contours, clicks or corrections required, latency and temporal consistency under a specified prompting procedure. The example below isolates one mechanism and cannot substantiate a full-model benchmark claim.

Python and R code examples

Component: Score a prompted mask and explicitly account for a correction. Dependencies: NumPy in Python; base R in R; run the shared setup above. Python component: executed successfully. R component: unexecuted. No complete model training or pretrained inference was executed.

Full implementation route — Python: Use the official predictor and record every prompt and reset boundary. R: Bridge the Python predictor; compute mask IoU, object continuity and interaction-cost summaries in R.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — Score a prompted mask and explicitly account for a correction.

Shared setup for this chapter
import numpy as np

def softmax(x):
    z = np.asarray(x, dtype=float)
    e = np.exp(z - z.max())
    return e / e.sum()
truth = np.array([[0,1,1],[0,1,0]],dtype=bool)
pred = np.array([[0,1,0],[0,1,1]],dtype=bool)
iou = lambda a,b: np.logical_and(a,b).sum()/np.logical_or(a,b).sum()
print("before",iou(pred,truth))
pred[0,2]=True; pred[1,2]=False
print("after corrective mask",iou(pred,truth),"corrections",1)
# Correction is supplied; this does not simulate SAM 2's learned response.
R

Component demo, not a trained model — Score a prompted mask and explicitly account for a correction.

Shared setup for this chapter
softmax <- function(x) {
  e <- exp(x - max(x))
  e / sum(e)
}
truth <- matrix(c(0,1,1,0,1,0),2,byrow=TRUE)>0
pred <- matrix(c(0,1,0,0,1,1),2,byrow=TRUE)>0
iou <- function(a,b)sum(a&b)/sum(a|b)
print(iou(pred,truth))
pred[1,3] <- TRUE; pred[2,3] <- FALSE
print(c(after=iou(pred,truth),corrections=1)) # supplied correction

Sources

From the CodeSplash reference, entry VID039.