SAM3 image segmentation and video tracking¶
Native float32 MLX inference for the original
SAM3 checkpoint. Registered types:
sam3 (concept detector), sam3_tracker (interactive image segmentation),
sam3_tracker_video (temporal tracker architecture), and sam3_video
(composite detector/tracker). The detector includes the ViT feature pyramid,
CLIP text encoder, geometry encoder, DETR decoder, presence scoring, and masks.
The interactive path uses point/box/mask prompts and a two-way decoder. The
composite video path shares the image backbone and adds temporal memory,
association, reconditioning, and object lifecycle state.
All four registered SAM3 types are supported for their recorded primary workflows. The current campaign includes image/point prompts, native composite and standalone offline/streaming video sessions, actual CLI output and a 32-frame natural-video standalone tracker/reference run. See the current inventory and repairs and diagnostic limits. An optional MPS reference streaming discrepancy remains recorded. Earlier controlled fixture results retain their original scope. Training, text generation, HTTP/UI integration, quantization, reduced-precision defaults, and SAM3.1 are unsupported.
Model Architecture¶
The diagram follows the native implementation. Layer counts, widths, head counts, and input sizes come from the checkpoint configuration; this is an architecture map, not an additional support or qualification claim.
RGB image / video frame → preprocessing → shared ViT + feature pyramid
↓
Features reused by detector and tracker paths
│
┌──────────────────── Concept detector (sam3) ────────────────────┐
│ Features + CLIP text encoding + box geometry encoding │
│ ↓ │
│ DETR encoder/decoder → presence / instance scores, boxes, masks │
└─────────────────────────────────────────────────────────────────┘
┌───────────────── Interactive model (sam3_tracker) ─────────────┐
│ Features + encoded points / box / refinement mask │
│ ↓ │
│ Two-way mask decoder → candidate masks + predicted-IoU scores │
└────────────────────────────────────────────────────────────────┘
┌──────────────────── Composite video (sam3_video) ──────────────┐
│ Shared features + prior mask memory + object pointers │
│ ↓ │
│ Memory attention / tracker ←→ detector association │
│ ↓ │
│ Object lifecycle → frame masks + stable object/prompt IDs │
└────────────────────────────────────────────────────────────────┘
Papers and upstream references¶
Paper: SAM 3: Segment Anything with Concepts.
The image detector and temporal tracker share the backbone in the composite video model. The boxes show separate task paths, not a detector-to-interactive cascade; detector outputs feed the composite video's association logic. Concept confidence and interactive predicted-IoU are distinct outputs. The diagram covers the original SAM3 architecture, not SAM3.1, and does not broaden the fixture qualification recorded above.
Installation and assets¶
On Apple Silicon, install the existing Pillow vision extra:
Request and accept authorized access at the publisher's checkpoint page first.
Do not put access tokens in scripts or prompt JSON. The checkpoint license is
separate from mlx-one's Apache-2.0 license; see notices.
Loading uses safe .safetensors, config/processor JSON, and tokenizer assets;
pickle .pt weights and remote code are not loaded. The default revision for
facebook/sam3 is 3c879f39826c281e95690f02c7821c4de09afae7.
An offline directory must contain a complete compatible snapshot, not only weights.
FFmpeg and ffprobe on PATH are needed only for encoded local video files.
Frame directories and Python RGB frame iterators need no video decoder. NumPy
inputs must be uint8 RGB arrays of shape (height, width, 3); paths and PIL
images are also accepted. Frame dimensions must stay fixed within a session.
Image API¶
Examples below show available APIs, not qualification of every prompt.
Coordinates are original-image pixels; boxes are XYXY and point labels are
1 (positive) or 0 (negative). Use concept box labels separately from point labels.
from mlx_one.segmentation import load_segmentation_model, segment_image
model = load_segmentation_model("facebook/sam3", task="concept")
result = segment_image(model, "image.jpg", text="person")
print(result.scores, result.boxes, result.masks.shape)
Concept segmentation also accepts boxes=[[x0, y0, x1, y1], ...] and
box_labels=[1, 0, ...] for positive/negative exemplars. These prompt combinations
still need real-checkpoint parity qualification.
from mlx_one.segmentation import load_segmentation_model, predict_masks, generate_masks
model = load_segmentation_model("facebook/sam3", task="interactive")
result = predict_masks(model, "image.jpg", points=[[100, 120]], point_labels=[1])
best = int(result.predicted_iou.argmax())
refined = predict_masks(
model, "image.jpg", points=[[100, 120]], point_labels=[1],
mask=result.low_res_logits[best], multimask=False,
)
automatic = generate_masks(model, "image.jpg", points_per_side=32)
Interactive calls accept one box per prompt group, and refinement accepts finite 2-D mask logits, not concept confidence. Automatic generation currently uses a single-image point grid, predicted-IoU/stability filtering, and mask-IoU NMS. It does not implement crop-layer/box-NMS automatic generation parity; it remains unqualified. Large grids retain CPU candidate masks and can be expensive.
All results contain original-resolution boolean masks, pixel XYXY boxes, original
dimensions, and checkpoint provenance. Concept confidence (scores) is distinct
from interactive predicted_iou; low-resolution logits support refinement.
Video API¶
from mlx_one.segmentation import load_segmentation_model
model = load_segmentation_model("facebook/sam3", task="video", offline=True)
with model.video_session("frames/") as session:
prompt_id = session.add_prompt("person")
for result in session.propagate():
print(result.frame_index, result.object_ids, result.prompt_ids)
frames/ contains lexically ordered images; use zero-padded filenames. A local
video path or an iterator of RGB arrays can replace it. Offline sessions retain
original frames on temporary disk for corrections and repropagation.
correct_object(object_id, frame_index, points=..., point_labels=..., box=...,
mask=..., prompt_id=...) targets an object; new objects need an existing concept
prompt_id. A video correction mask is an original-resolution boolean array.
Object removal, prompt removal, reset, close, and cancel are session-owned operations.
For chronological Python streams, use model.video_session(streaming=True) and
session.process_frame(frame) per frame. Streaming rejects past-frame corrections
and omits future-dependent hotstart removal; qualify it separately from offline
propagation. CPU histories and bounded GPU feature retention are separate from
text dense/APC/block caches. Exceeding the process budget raises a resource-limit
error rather than changing tracking settings or silently dropping objects.
Context exit releases session state without unloading another session's shared model.
CLI and exports¶
mlx-one segment facebook/sam3 image.jpg --mode concept --text person --output results-concept
mlx-one segment facebook/sam3 image.jpg --mode interactive --prompts-json prompts.json --output results-interactive
mlx-one segment facebook/sam3 image.jpg --mode automatic --output results-automatic
mlx-one track facebook/sam3 frames/ --text person --output results-offline
mlx-one track facebook/sam3 video.mp4 --streaming --prompts-json video-prompts.json --output results-stream
Both commands accept --revision, --offline, and --cache-dir. Image prompt JSON
is an object containing text, boxes, box_labels, points, point_labels, and/or
mask_path, as applicable to the mode. Video JSON is an array of records with
frame_index, action (add, update, remove), text/prompt IDs or object-specific
coordinates/labels/mask_path. Unknown fields are rejected. PNG masks and a JSON
manifest record scores, identifiers, boxes, dimensions, and provenance. Existing
output directories are never overwritten; interrupted exports stay marked partial.
If loading fails, verify gated access, the pinned revision, all processor/tokenizer assets, and float32-compatible tensor names/shapes. If Metal fails in a sandbox, run natively on the inspected host; a backend-free config test is not GPU evidence. Variable frame sizes and out-of-bounds coordinates are errors, not implicit rescaling.
Tests and references¶
- Configuration, registry, and strict loading tests.
- Inputs, exports, session isolation, correction, cancellation, and cleanup tests.
- Capture runner and fixed-ID video comparison.
- Publisher source, Transformers image reference, video reference, and pinned source provenance.