Skip to content
MedMLXPublic

About

Renal CT segmentation with MONAI UNesT in native MLX on Apple Silicon

Topics

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

33 Commits

Folders and files

Repository files navigation

unest-mlx

Segment kidney cortex, medulla and collecting system in contrast-enhanced CT with UNesT and native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Contrast CT → kidney cortex, medulla and collecting system, with a detailed renal view.
Input Output
Contrast-enhanced CT · NIfTI Renal labels on the input grid
Model Weights download Peak MLX memory Measured on
unest 349 MB 7.2 GB 478×277×161 contrast CT

Install

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

Run

from unest_mlx import run

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

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

Inputs and outputs

Argument Meaning
image_path One 3D CT in NIfTI (.nii or .nii.gz), in Hounsfield units
output_dir Directory for mask.nii.gz
weights_path Optional folder containing unest.safetensors
overwrite False by default, which refuses to replace an existing mask
sw_batch_size Windows per network call, 4 by default; fewer lower peak memory
  • Input: arterial or portal-venous contrast-enhanced CT. On the non-contrast scans we tried, the kidneys were missed. Spatial units must be millimeters or unspecified; meters and microns are not converted.

  • Mask: float32 labels on the input grid as the reader interprets its header:

    Label Structure
    0 Background
    1 Kidney cortex
    2 Medulla
    3 Pelvicalyceal system
  • Return value: {"outputs": {"mask": path}, "artifacts": {"mask": ...}, "details": {...}}. The mask artifact is a label map on the input grid with the four labels above; details holds label counts, shapes, orientation, sliding-window settings, MLX device and elapsed time.

  • Errors: invalid inputs raise InvalidInputError; missing or mismatched weights raise AssetNotReadyError with the download location.

Python API

import mlx.core as mx
import numpy as np
from unest_mlx import UNesT, UNesTPredictor, validate_weights

loaded = mx.load("unest.safetensors")
weights = validate_weights({key: np.array(value) for key, value in loaded.items()})
network = UNesT({key: mx.array(value) for key, value in weights.items()}, mx=mx)
predictor = UNesTPredictor(network, mx=mx)

volume = np.zeros((1, 1, 96, 96, 96), dtype=np.float32)  # preprocessed CT, Bx1xDxHxW
logits = predictor(volume)  # NumPy float32, Bx4xDxHxW

UNesTPredictor uses the upstream 96³ windows with overlap 0.5 and constant blending; sw_batch_size defaults to four.

Use with MedMLX

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

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.

About

Renal CT segmentation with MONAI UNesT in native MLX on Apple Silicon

Topics

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages