Skip to main content

totalsegmentator-mlx

Segment 117 CT structures with TotalSegmentator v2 in native MLX. Includes standard, fast and coarse modes.

PyPI Python License Apple Silicon Hugging Face MedMLX

TotalSegmentator standard-mode (1.5 mm) 3D surfaces and axial CT segmentation

Standard mode (1.5 mm): 3D surfaces from autoPET-III and axial CT from AMOS22. Surfaces and contours are smoothed for display.

Input Output
3D CT · NIfTI Multilabel mask on the input grid
Model Weights download Peak MLX memory Measured on
totalsegmentator 824 MB 5.0 GB Standard mode, 768×768×101 abdominal CT

Install

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

Run

from totalsegmentator_mlx.medmlx import run

result = run(image_path="ct.nii.gz", output_dir="out")
print(result["outputs"]["mask"])

Weights download from MedMLX/totalsegmentator-mlx on first use. Use medmlx setup totalsegmentator to download ahead of time. For local weights, pass the downloaded bundle folder as weights_path.

Inputs and outputs

Argument Meaning
image_path One finite 3D CT in NIfTI-1 or NIfTI-2, in Hounsfield units
output_dir Directory for mask.nii.gz
weights_path Optional root of the model folders below
mode standard (default, five 1.5 mm parts), fast (3 mm model, upstream --fast) or coarse (6 mm model, upstream --fastest)
prompt Structure names to keep after inference; label IDs stay global
roi_subset Locate these structures, crop to them plus 20 mm and drop other labels
robust_crop Locate roi_subset with the 3 mm model instead of the 6 mm one
body_seg Crop to the Dataset300 body model plus 3 mm
overwrite False by default; an existing mask is never replaced
  • Cropping: with roi_subset, standard mode runs only the parts that contain those structures. roi_subset cannot be combined with prompt or body_seg, and robust_crop requires roi_subset. Cropped regions are restored by zero filling; an empty localization returns an empty mask.
  • Units: spatial units must be millimetres or unspecified.
  • Mask: a uint8 NIfTI-1 with global labels 1–117 (0 background) on the input grid, with the upstream label table as a header extension.
  • Return value: {"outputs": {"mask": path}, "details": {...}}, with run metadata in details.
  • Errors: invalid inputs raise InvalidInputError; missing or mismatched weights raise AssetNotReadyError.

Model folders

A local weights_path holds one folder per dataset, each with plans.json, dataset.json and fold_0/checkpoint_final/ containing state.json and weights.safetensors. Only the folders the chosen options use are required, and a single model folder can be passed directly when only that dataset is needed.

Folder Model Used by
291/–295/ five 1.5 mm parts standard
297/ 3 mm total model fast; roi_subset with robust_crop
298/ 6 mm total model coarse; roi_subset
300/ 6 mm body model body_seg

Command line

Download the safetensors model bundle, then segment:

python - <<'PY'
from huggingface_hub import snapshot_download

snapshot_download(
    "MedMLX/totalsegmentator-mlx",
    revision="6c03d6f90ccf53f224c3985cbdecf1eaa96555af",
    local_dir="models",
)
PY
totalsegmentator-mlx predict -i ct.nii.gz -o mask.nii.gz --model-dir models

predict takes the runner options as flags (--mode, --organ for prompt, --roi-subset, --robust-crop, --body-seg, --overwrite) and adds three more:

Flag Effect
--model-part Runs one standard part: organs, vertebrae, cardiac, muscles or ribs
--force-split Uses upstream's three overlapping slabs
--probabilities-path probabilities.npz Also exports local-label probabilities for one unsplit, uncropped model

totalsegmentator-mlx labels prints the label IDs.

Python API

from totalsegmentator_mlx import segment

labels = segment("ct.nii.gz", model_dir="models", mode="fast")  # nibabel image, uint8

segment takes the command-line options as keyword arguments, plus an optional output_path. It never downloads weights.

For a long-lived Python process, the default cache retains up to eight model predictors. Set an explicit policy before a job when memory takes priority:

from totalsegmentator_mlx import configure_predictor_cache, clear_predictor_cache

configure_predictor_cache(max_entries=1, max_bytes=2 * 1024**3)
# Run segment(...) calls with one retained network at a time.
clear_predictor_cache()

The byte budget covers retained model state; inference scratch and MLX's free allocator cache are separate. A model exceeding the budget runs uncached. predictor_cache_info() reports these counters separately. Passing no limits to configure_predictor_cache() restores the eight-model policy.

Limits

  • Supports the total CT task: Dataset291–295, 297 and 298 (117 labels), plus Dataset300 for body cropping.
  • Models use 3d_fullres, fold 0, without mirroring.
  • Input and output are NIfTI. Other TotalSegmentator tasks and its extra export, statistics and training options are unsupported.
  • Inference requires Apple Silicon with Metal.

Use with MedMLX

Use the MedMLX MCP server to run totalsegmentator 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 totalsegmentator.

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

Apache-2.0. See third-party notices. Model weights retain their publisher terms.

Metadata

Release files for totalsegmentator-mlx 0.2.0

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

Source distribution (sdist)

Source distribution for totalsegmentator-mlx 0.2.0
File Size Uploaded Provenance Actions
totalsegmentator_mlx-0.2.0.tar.gz 32.7 kB Without provenance Details for totalsegmentator_mlx-0.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for totalsegmentator-mlx 0.2.0
File Interpreter ABI Platform Actions
totalsegmentator_mlx-0.2.0-py3-none-any.whl Python 3 none any Details for totalsegmentator_mlx-0.2.0-py3-none-any.whl

Total release size: 73.2 kB

Release files / totalsegmentator_mlx-0.2.0.tar.gz

Download URL totalsegmentator_mlx-0.2.0.tar.gz
Size 32.7 kB
Tags Source
SHA-256 checksum
How to use checksums
62d20f721c8587bd41b75a67c891cd9ece5316e4085fdafb65a97c2606bfa028
BLAKE2b-256 checksum
How to use checksums
1dee5daad3391bee3ccd515b0da6acb9832edfc4f6dc8ff4381a6c9fe8ef77a3
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 / totalsegmentator_mlx-0.2.0-py3-none-any.whl

Download URL totalsegmentator_mlx-0.2.0-py3-none-any.whl
Size 40.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e72bbda9a101905a74dabbdf27ba3ad3d0df2fbe4c48f286467a3c393673c44
BLAKE2b-256 checksum
How to use checksums
e35df570b5d4533de8d3da518491bc4ae05785f815b7e3ad963c32cdd706c114
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 totalsegmentator-mlx 0.2.0

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