Skip to main content

sam3-mlx

Segment objects in images from text, box or point prompts with Meta's SAM 3 in native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Weights · Upstream · MedMLX

An abdominal CT slice, the SAM 3 mask for the text prompt liver, and the AMOS22 reference liver label

Abdominal CT and reference label from AMOS22, CC BY 4.0.

Input Output
One 2D image, or one frame of an image folder or video · text, box or point prompt Masks, pixel boxes and scores at the input resolution
Model Weights download Peak MLX memory Measured on
sam3 3.4 GB 5.4 GB One image at the default 1008-pixel resolution
sam3, tracked video 3.4 GB 6.0 GB A tracked video session

Install

uv venv --python 3.13
source .venv/bin/activate
uv pip install "sam3-mlx>=0.4.2"

Run

from sam3_mlx import run

result = run(image_path="astronaut.png", prompt="helmet", output_dir="out")
print(result["outputs"]["mask"])

Weights download from mlx-community/sam3-image on first use (3.4 GB); medmlx setup sam3 downloads them ahead of time. For local weights, pass the folder containing model.safetensors as weights_path.

For a test image:

curl -LO https://raw.githubusercontent.com/scikit-image/scikit-image/v0.25.2/skimage/data/astronaut.png

Inputs and outputs

Argument Meaning
image_path or video_path One 2D image Pillow can read, or an image folder or video with frame_index
output_dir Directory for masks.npy and prediction.json
prompt Free text, such as "helmet"
box_cxcywh_normalized [center_x, center_y, width, height] as fractions of the image size, between 0 and 1; width and height above 0
point_xy_normalized [x, y] as fractions of the image size, between 0 and 1
box_label, point_label Mark a prompt as positive (True) or negative
confidence_threshold 0.5 by default; only detections scoring above it are kept
resolution 1008 by default, upstream's input size; a positive multiple of 14
frame_index The folder or video frame to segment
weights_path Optional folder containing model.safetensors
precision "fp32" (default), "tf32" or "bf16"; see Precision
Output File Contents
mask masks.npy Boolean (N, height, width), one mask per detection, at the input image or frame resolution
detections prediction.json Pixel boxes [x0, y0, x1, y1], scores, frame-local object_ids (video only, otherwise null) and the request settings
  • Prompts: prompts given together are combined in one request.
  • Images: 2D and 8-bit, decoded with Pillow. DICOM, NIfTI and 3D volumes are not read; export a slice to PNG first. 16- and 32-bit images are rejected rather than clipped.
  • Video: run segments one frame_index per call, exactly like an image, with frame-local object IDs. Video files need uv pip install "sam3-mlx[video]>=0.4.2"; image folders do not.
  • Return value: {"outputs": {...}, "artifacts": {...}, "details": {...}}. artifacts says both files are in pixel coordinates of the input image or frame; details repeats the prediction metadata.

Precision

precision What runs Largest difference from upstream SAM 3 (FP32) on the six medical fixtures
"fp32" (default) Full float32 Score 4e-5, box 0.002 px, 2 mask pixels
"tf32" Float32 model; matrix products on the GPU's neural accelerators. M5 or later Score 0.005, box 0.4 px, 0.24% of mask area
"bf16" BF16 projections in the backbone and detector; FP32 mask head Score 0.012, box 0.5 px, 0.7% of mask area

Every level finds the same detections as upstream on those fixtures. Speed for each level is in the technical notes.

  • "tf32" applies to every MLX model in the Python process, so build the model before other MLX work, or start Python with MLX_ENABLE_TF32=1.
  • MLX fixes its float32 product precision for the whole process at its first float32 product. A "fp32" or "bf16" model in a process already running TF32, or a "tf32" model in one fixed to full FP32 or on a chip before M5, stops with an error; start a new Python process.

Python API

To keep arrays in memory instead of writing files, use the model directly:

from sam3_mlx import build_sam3_image_model
from sam3_mlx.model.sam3_image_processor import Sam3Processor

model = build_sam3_image_model()
processor = Sam3Processor(model, resolution=1008)

state = processor.set_image("astronaut.png")
state = processor.set_text_prompt("helmet", state)
masks, boxes, scores = state["masks"], state["boxes"], state["scores"]

masks is boolean (N, 1, height, width), boxes are pixel [x0, y0, x1, y1] and scores has shape (N,). build_sam3_image_model and build_sam3_predictor take the same precision values as run; compile=True compiles the image graph with MLX, which can change outputs slightly.

For SAM 1-style clicks and boxes in pixels, enable the interactive head. It downloads on first use (17 MB, from MedMLX/sam3-interactive):

import numpy as np

model = build_sam3_image_model(enable_inst_interactivity=True)
state = Sam3Processor(model).set_image("astronaut.png")
masks, scores, _ = model.predict_inst(
    state, point_coords=np.array([[260, 80]]), point_labels=np.array([1])
)

masks holds three candidate masks (3, height, width) and scores their predicted quality; pass box=np.array([x0, y0, x1, y1]) or multimask_output=False for one mask.

For video, build_sam3_predictor() opens sessions over image folders, image lists or video files. Each frame is segmented on its own; with track=True, a box tracker links the detections across frames so each object keeps its ID:

from sam3_mlx import build_sam3_predictor

predictor = build_sam3_predictor()
session = predictor.start_session("frames/", track=True)["session_id"]
predictor.add_prompt(session, frame_idx=0, text="kidney")
for frame in predictor.propagate_in_video(session, propagation_direction="forward"):
    ids = frame["outputs"]["out_obj_ids"]  # an object keeps its ID across frames
predictor.close_session(session)

Tracked sessions match each frame's detections to the previous frames by box overlap and motion (ByteTrack). Masks are not carried between frames: an object is shown only on frames where the prompt finds it. It keeps its ID through up to 30 missed frames. Objects in the first frame get IDs at once; a later one gets an ID after it is found on 3 more frames.

Weights

Weights are cached in ~/.cache/medmlx/sam3 (set MEDMLX_ASSET_CACHE to move it), and later runs work offline. To pin a revision:

hf download mlx-community/sam3-image model.safetensors --revision b72a14d8127e17e6f2a3d2e075bbbf4307ba146e --local-dir sam3-weights

Only these converted MLX weights load; Meta's PyTorch .pt checkpoints do not. Meta's original checkpoints on Hugging Face require an access request; this converted copy does not.

Weight terms are the SAM License:

  • royalty-free, including commercial use;
  • not for military, weapons or other uses barred by trade controls;
  • publications must acknowledge SAM.

Limits

  • SAM 3 is a general-purpose model; its accuracy on medical images is not established.
  • Requires macOS 14 or later.

Use with MedMLX

Use the MedMLX MCP server to run sam3 from an MCP client. Weights download on first use.

uv tool install --python 3.13 "medmlx[segment,mcp]"
MEDMLX="$(uv tool dir --bin --color never)/medmlx"
mkdir -p "$HOME/medmlx-work"
claude mcp add --scope user medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work"

For Codex, use codex mcp add medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-work". Other MCP clients take the same command and arguments. The same install provides the medmlx CLI, for example medmlx describe sam3.

For AI agents: prefer the MedMLX MCP tools (list_models, describe_model, model_readiness, run_model, get_job) over writing Python against this package.

License

Code: SAM License, with MIT-licensed tokenizer code from OpenAI CLIP and open_clip. Weights: SAM License. See third-party notices. Not affiliated with Meta.

Metadata

Release files for sam3-mlx 0.4.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sam3-mlx 0.4.2
File Size Uploaded Provenance Actions
sam3_mlx-0.4.2.tar.gz 1.5 MB Without provenance Details for sam3_mlx-0.4.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sam3-mlx 0.4.2
File Interpreter ABI Platform Actions
sam3_mlx-0.4.2-py3-none-any.whl Python 3 none any Details for sam3_mlx-0.4.2-py3-none-any.whl

Total release size: 2.9 MB

Release files / sam3_mlx-0.4.2.tar.gz

Download URL sam3_mlx-0.4.2.tar.gz
Size 1.5 MB
Tags Source
SHA-256 checksum
How to use checksums
bd0f81192e2bc4b31b5ac27b9f6aee0962f1f4a41da6b4d761404d4cab0683a6
BLAKE2b-256 checksum
How to use checksums
62550a40f412f6dbc9826e30064636e97afb471155c07baceb33f7dc646ef776
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / sam3_mlx-0.4.2-py3-none-any.whl

Download URL sam3_mlx-0.4.2-py3-none-any.whl
Size 1.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
0965495776c921db055a79e41df27babd59300da4046ab8b82789efc4730a7a9
BLAKE2b-256 checksum
How to use checksums
9dc4f41968dc4c39dfb6706ec8502b24889081b77f362bdf5e1d872ee9542ed7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for sam3-mlx 0.4.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page