Skip to main content

swin-unetr-mlx

Segment 13 abdominal organs in CT with MONAI SwinUNETR and native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Axial CT slices scrolling with their organ segmentation beside a rotating 3D rendering of the segmented anatomy
Input Output
3D abdominal CT · NIfTI Organ mask on the input grid
Model Weights download Peak MLX memory Measured on
swin-unetr 253 MB 7.4 GB 768×768×101 abdominal CT

Install

uv venv --python 3.13
source .venv/bin/activate
uv pip install "swin-unetr-mlx>=0.4.1"

Run

from swin_unetr_mlx import run

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

Weights download from MedMLX/swin-unetr-mlx on first use; medmlx setup swin-unetr downloads them ahead of time. For local weights, pass the folder containing swinunetr.safetensors as weights_path.

Inputs

Argument Meaning
image_path One 3D abdominal CT in NIfTI-1 or NIfTI-2, in Hounsfield units, with an invertible affine
output_dir Directory for mask.nii.gz, created if needed
weights_path Optional folder containing swinunetr.safetensors
overwrite False by default; an existing mask is replaced only when True
sw_batch_size Windows per network call, 4 by default
streaming False by default; True blends into temporary memory-mapped files instead of holding the logits in memory
scratch_dir Parent folder for the streaming maps; the system temporary folder by default

Voxels must be finite and representable in float32. Meter and micron headers are converted to millimeters, and unspecified units are read as millimeters. The image is rejected when its qform and sform headers make ITK and NiBabel disagree on the affine.

Outputs

The mask is out/mask.nii.gz, with the input shape and the ITK reader's affine. It holds float32 label indices from the bundled BTCV label map:

Index Name Index Name
0 background 7 stomach
1 spleen 8 aorta
2 right kidney 9 inferior vena cava
3 left kidney 10 portal and splenic veins
4 gallbladder 11 pancreas
5 esophagus 12 right adrenal gland
6 liver 13 left adrenal gland

run returns {"outputs": {"mask": path}, "artifacts": {"mask": ...}, "details": {...}}. The mask artifact is a label map on the input grid with the 14 classes above; details holds the labels present, shapes, sliding-window settings, MLX device, peak memory and timings.

Error Raised when
InvalidInputError The input is invalid
AssetNotReadyError Weights are missing or mismatched
ModelExecutionError Inference fails
MissingDependencyError The host is unsupported or Metal is unavailable

Python API

from pathlib import Path

import numpy as np
from swin_unetr_mlx import forward_patch, load_network

network = load_network(Path("swinunetr.safetensors"))
logits = forward_patch(network, np.zeros((1, 1, 96, 96, 96), dtype=np.float32))
  • forward_patch takes finite float32 Bx1x96x96x96 windows and returns raw Bx14x96x96x96 logits, without file IO, intensity scaling, resampling, softmax or stitching. SwinUNETR(weights, mx=mx) runs on MLX arrays directly.
  • Both default to merging_profile="monai-1.4", the released bundle's repeated patch-merging phases; pass merging_profile="monai-1.6" to compare with MONAI 1.6.
  • load_network checks names, shapes, dtypes, finite values and position-index ranges, and raises AssetNotReadyError when the weights do not match.

Use with MedMLX

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

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 swin-unetr-mlx 0.4.1

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

Source distribution (sdist)

Source distribution for swin-unetr-mlx 0.4.1
File Size Uploaded Provenance Actions
swin_unetr_mlx-0.4.1.tar.gz 28.7 kB Without provenance Details for swin_unetr_mlx-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for swin-unetr-mlx 0.4.1
File Interpreter ABI Platform Actions
swin_unetr_mlx-0.4.1-py3-none-any.whl Python 3 none any Details for swin_unetr_mlx-0.4.1-py3-none-any.whl

Total release size: 66.5 kB

Release files / swin_unetr_mlx-0.4.1.tar.gz

Download URL swin_unetr_mlx-0.4.1.tar.gz
Size 28.7 kB
Tags Source
SHA-256 checksum
How to use checksums
a5774684191af836679a4ee5bcdb973d7c8e602692c2b0b30fc783693ae2cbeb
BLAKE2b-256 checksum
How to use checksums
a06ee5a6f105ce739d422ef8ad1bc2d6265a8e5d0739c25be6fd03fead14d905
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 / swin_unetr_mlx-0.4.1-py3-none-any.whl

Download URL swin_unetr_mlx-0.4.1-py3-none-any.whl
Size 37.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
502c0bcecd4ee03e2285d06726ceb191b79b94cfe9f6650dc8194049c14c2cb6
BLAKE2b-256 checksum
How to use checksums
1586eaaf9b3a04ca1e836628f17a4d3830b52a48ba39b8e9420198b485304e12
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 swin-unetr-mlx 0.4.1

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