Skip to content

About

Radiograph anatomy segmentation with FleXray in native MLX on Apple Silicon

Topics

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

30 Commits

Folders and files

Repository files navigation

flexray-mlx

Segment anatomical structures in radiographs with FleXray and native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Chest radiograph with lung, heart and individual rib segmentations
Input Output
Raster image or single-frame DICOM Structure masks and probabilities
Model Weights download Peak MLX memory Measured on
flexray 2.0 GB for all five ensemble members 1.2 GB 1929×2207 chest radiograph, default single model

Install

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

Run

from flexray_mlx import run

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

Use label_name to select a structure.

ensemble Weights
False (default) The flagship flux0375. It downloads from MedMLX/flexray-mlx on first use; medmlx setup flexray downloads it ahead of time
True All five members, in an explicit weights_path folder

A local folder holds ensemble.json and members/flux0375/{config.yml,label_schema.json,model.safetensors,preprocessing.json}.

Inputs and outputs

Argument Meaning
image_path One radiograph: an image Pillow reads (PNG, TIFF, ...) or a single-frame 2D DICOM
output_dir Output directory, created if missing
weights_path Optional bundle folder, as above
label_name Structure to threshold into the mask, lungs by default (any label, such as heart, rib_5 or vertebra_l1)
ensemble False by default; True averages all five released members
threshold Probability cutoff in [0, 1], 0.5 by default
tta_samples Passes per member, 1 by default: one plain view plus augmented views
tta_seed None by default, unseeded as in FleXray; an integer reproduces Torch CPU augmentation draws
normalization_scheme None keeps the bundle's; or none, minmax, percentile_minmax or standardize
overwrite False by default; existing outputs are replaced only when True
member_residency all by default; one loads ensemble members one at a time, spooling per-view probabilities to a temporary folder, with identical results
  • DICOM: files end in .dcm or .dicom (any case), or have no suffix and DICM at byte 128; other suffixes, such as .ima, are read as raster images. Accepting a file does not mean the model suits its modality.
  • Output paths must differ from each other, the input and the bundle files, including links, even with overwrite.
Output File Content
mask source_image_label.png uint8 0/1 mask of the selected structure on the source pixel grid
preview source_image.png 8-bit grayscale source the mask overlays
probabilities probabilities.npy float32 61×256×256 sigmoid probabilities on the padded model grid
metadata prediction.json Labels, selected channel, threshold, members and content box
  • Source-grid PNGs: FleXray thresholds on the model grid and exports no source-grid images; the PNGs are a MedMLX addition. The mask is thresholded first, then resized with nearest neighbor to the padded canvas and unpadded, which inverts the preprocessing geometry, so no new decisions come from interpolation.
  • Return value: {"outputs": {...}, "artifacts": {...}, "details": {...}}. artifacts marks the mask as a binary mask named for label_name on the input's pixel grid, and the probabilities as a field on the model grid; run metadata is in details.
Error Raised when
AssetNotReadyError Assets are missing or incompatible
InvalidInputError Inputs are invalid or outputs already exist
ModelExecutionError Probabilities are non-finite; raised before anything is written

Ensemble members. The published bundle holds only flux0375; its ensemble.json still lists all five members in order (flux0375, flux000, flux025, flux050, flux075). For ensemble=True, use the upstream snapshot with all five member folders.

Augmentation and normalization. Augmented views follow FleXray's tta_v3 chain: flips, inversion, CLAHE or gamma, contrast, sharpness and Gaussian noise. Gamma on negative normalized pixels can produce NaN, which the runner rejects. The released weights were trained with percentile normalization; other schemes are not validated.

Python API

from pathlib import Path

import numpy as np
from flexray_mlx import FleXrayMlxModel

model = FleXrayMlxModel(Path("flexray/members/flux0375"))
logits = model(np.zeros((1, 1, 256, 256), dtype=np.float32))  # Bx1x256x256 -> Bx61x256x256
  • The member folder needs the released config.yml and model.safetensors. Loading converts convolution weights to MLX layout and every tensor to float32, keeping the names, and rejects tensor names or shapes that differ from the released network.
  • Inputs are prepared images; preparation, sigmoid, ensembling, TTA and source-grid restoration are up to you.
  • flexray_mlx.tta.predict_with_tta(forward_fn, images, tta_samples=..., seed=..., normalizer=...) takes one or more forward callables and already normalized float32 BxCxHxW images, and returns (logits, probabilities). Pass the normalize method of flexray_mlx.medmlx.ImagePreprocessing as normalizer to follow the bundle. flexray_mlx.tta.tta_probabilities takes the same arguments and returns only the probabilities.

Use with MedMLX

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

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. FleXray weights are noncommercial (CC-BY-NC-4.0).

About

Radiograph anatomy segmentation with FleXray in native MLX on Apple Silicon

Topics

Resources

Security policy

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages