Skip to main content

sam3-mlx

Segment objects in images from text, box or point prompts with Meta's SAM 3. Native MLX port of SAM 3 for Apple Silicon.

PyPI Python License

Weights · Upstream · MedMLX

A photograph of an astronaut, then the same photograph with SAM 3 masks for the text prompts person and helmet

Photograph of astronaut Eileen Collins: NASA, public domain, via the scikit-image sample data.

Input One 2D image (PNG, JPEG or any Pillow-readable format), or one chosen frame of an image folder or video; a text prompt, one box and/or one point
Output out/masks.npy, boolean (N, height, width) at the input resolution, and out/prediction.json with pixel boxes and scores
Weights mlx-community/sam3-image, 3.4 GB, SAM License; downloads on first use
Hardware macOS 14 or later on Apple Silicon; about 12 GB peak memory at the default 1008-pixel resolution
Intended use Research and education. Not a medical device; outputs require human review.

Use with MedMLX

MedMLX installs this runtime with others behind one medmlx command and an MCP server. The segment extra also installs 11 other segmentation runtimes, including MedSAM2, nnU-Net and TotalSegmentator.

uv tool install --python 3.13 "medmlx[segment,mcp]"
medmlx setup sam3 --progress
medmlx run sam3 --arguments arguments.json --root ~/medmlx-work

arguments.json, with paths relative to --root:

{
  "image_path": "inputs/astronaut.png",
  "text_prompt": "helmet",
  "output_dir": "results/sam3"
}

To let an AI agent run it, register the server, then restart the client:

mkdir -p ~/medmlx-work
claude mcp add --scope user medmlx -- "$(uv tool dir --bin)/medmlx" serve --root "$HOME/medmlx-work"
codex mcp add medmlx -- "$(uv tool dir --bin)/medmlx" serve --root "$HOME/medmlx-work"

Put images in ~/medmlx-work; outputs are written there too. Then ask, for example: "Use SAM 3 to segment the helmets in astronaut.png."

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

Install

Requires uv. If uv --version fails, run curl -LsSf https://astral.sh/uv/install.sh | sh first.

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

Run

Segment a sample photograph:

curl -LO https://raw.githubusercontent.com/scikit-image/scikit-image/v0.25.2/skimage/data/astronaut.png
python - <<'PY'
from sam3_mlx.medmlx import run

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

This writes out/masks.npy and out/prediction.json. The first run downloads the 3.4 GB weights.

Python API

Runner Use Key arguments
run Segment one image, or one frame of an image folder or video image_path or video_path, output_dir, text_prompt, box_prompt, point_prompt, confidence_threshold, resolution, frame_index, weights_path (optional)

box_prompt is [center_x, center_y, width, height] and point_prompt is [x, y], both as fractions of the image size (between 0 and 1; box width and height above 0). box_label and point_label mark a prompt as positive (True) or negative. Prompts given together are combined in one request. The result has outputs["masks"], outputs["prediction"] and details, which repeats the prediction metadata.

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(precision="bf16") runs the backbone and detector projections in BF16 from the FP32 weights; compile=True compiles the image graph with MLX. Both can change outputs slightly.

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)

Weights

Weights download on first use to ~/.cache/medmlx/sam3 (set MEDMLX_ASSET_CACHE to move it). Later runs work offline. To download ahead of time or pin a revision:

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

To use your own copy, pass the folder containing model.safetensors as weights_path. Only these converted MLX weights load; Meta's PyTorch .pt checkpoints do not. Weight terms: the SAM License: royalty-free, including commercial use, but not for military, weapons or other uses barred by trade controls, and publications must acknowledge SAM. Meta's original checkpoints on Hugging Face require an access request; this converted copy does not.

Inputs and limitations

  • Images are 2D and decoded with Pillow; DICOM, NIfTI and 3D volumes are not read. Export a slice to PNG first.
  • run segments one video frame_index per call, exactly like an image, with frame-local object IDs. Video files need uv pip install "sam3-mlx[video]>=0.2.3"; image folders do not.
  • Tracked sessions (track=True) 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.
  • Image files and frames must be 8-bit. 16- and 32-bit images are rejected rather than clipped; convert them to 8-bit first.
  • resolution must be a positive multiple of 14. The default, 1008, is upstream's input size.
  • SAM 3 is a general-purpose model; its accuracy on medical images is not established. Tests compare preprocessing and individual layers with PyTorch and TorchVision.
  • The model runs in full FP32: the code sets an unset MLX_ENABLE_TF32 to 0. If another library has already started MLX with reduced precision, the builder stops with an error; start a new Python process.

Outputs

File Contents
masks.npy Boolean NumPy array (N, height, width), one mask per detection, at the input image or frame resolution
prediction.json Pixel boxes [x0, y0, x1, y1], scores, frame-local object_ids (video only, otherwise null) and the request settings

Only detections scoring above confidence_threshold (default 0.5) are kept.

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.

For research and education. Not a medical device; outputs require human review.

Metadata

Release files for sam3-mlx 0.2.4

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.2.4
File Size Uploaded Provenance Actions
sam3_mlx-0.2.4.tar.gz 4.0 MB Without provenance Details for sam3_mlx-0.2.4.tar.gz

Built distribution (wheel)

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

Total release size: 5.4 MB

Release files / sam3_mlx-0.2.4.tar.gz

Download URL sam3_mlx-0.2.4.tar.gz
Size 4.0 MB
Tags Source
SHA-256 checksum
How to use checksums
9b949a41b82e5e9c2a1b1a23c9ca005f2362da5df6e0ca9c6db3ce1115b15d7f
BLAKE2b-256 checksum
How to use checksums
c4732726aba4d351d722503adf1388d99f01e9cbf6ffa453bf606aa67aa6a5e1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.2.4-py3-none-any.whl

Download URL sam3_mlx-0.2.4-py3-none-any.whl
Size 1.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
f09d1f3670abaf10fc6f8b9ad73e8e70c2984a3a0547e92794a135f118f2dfdd
BLAKE2b-256 checksum
How to use checksums
e43f6798c6d607f0a6e154b7db7ce77646303d1d44a76747aaf9070e77b783d3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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.2.4

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