Skip to main content

flexible-unet-mlx

Segment surgical tools in RGB endoscopy frames with FlexibleUNet and native MLX.

PyPI Python License Apple Silicon Hugging Face MedMLX

Endoscopy frame and the predicted surgical instrument mask with the reference outline

Frame and reference outline from Kvasir-Instrument, for research and education.

Input Output
RGB image Tool mask at the original size
Model Weights download Peak MLX memory Measured on
flexible-unet 46 MB 0.27 GB 768×576 endoscopy frame

Install

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

Run

from flexible_unet_mlx import run

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

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

To segment a sequence of frames with one loaded network, use load_model. Each model.run call takes the same image, output and overwrite arguments as run:

from flexible_unet_mlx import load_model

with load_model() as model:
    for frame in ["frame1", "frame2"]:
        model.run(image_path=f"{frame}.jpg", output_dir=f"out/{frame}")

Inputs and outputs

Argument Meaning
image_path One RGB image Pillow can read; the bundle used JPEG
output_dir Directory for mask.png, created if missing
weights_path Optional folder containing flexibleunet.safetensors
overwrite False by default; an existing mask is replaced only when True

This 2D model reads images, not NIfTI volumes.

  • Mask: out/mask.png, an 8-bit grayscale mask at the original frame size: 255 for tools and 0 for background.
  • Return value: {"outputs": {"mask": path}, "artifacts": {"mask": ...}, "details": {...}}: the mask artifact names the labels and the frame grid it is on, and details holds run metadata.
Error Raised when
InvalidInputError A frame is invalid
AssetNotReadyError Weights are missing or mismatched
ModelExecutionError Inference fails
MissingDependencyError The native runtime is missing

Python API

from pathlib import Path

import numpy as np
from flexible_unet_mlx import forward_patch, load_network

network = load_network(Path("flexibleunet.safetensors"))
logits = forward_patch(network, np.zeros((1, 3, 736, 480), dtype=np.float32))
  • forward_patch takes finite float32 (B, 3, 736, 480) frames, whose spatial axes are width and height as the bundle's reader returns them, and returns raw (B, 2, 736, 480) logits; channel 1 is the tool class.
  • Preprocessing and postprocessing are up to you. FlexibleUNet(weights, mx=mx) runs on MLX arrays directly.
  • load_network checks names, shapes, dtypes and finite values.

Use with MedMLX

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

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 flexible-unet-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 flexible-unet-mlx 0.4.1
File Size Uploaded Provenance Actions
flexible_unet_mlx-0.4.1.tar.gz 26.9 kB Without provenance Details for flexible_unet_mlx-0.4.1.tar.gz

Built distribution (wheel)

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

Total release size: 62.2 kB

Release files / flexible_unet_mlx-0.4.1.tar.gz

Download URL flexible_unet_mlx-0.4.1.tar.gz
Size 26.9 kB
Tags Source
SHA-256 checksum
How to use checksums
cffa3f30dcc90edbf862f204acd0583d43ed0b590ac7d6e2ca77dc838dcc0cdd
BLAKE2b-256 checksum
How to use checksums
65e87c13120d7630af8f5a9e713f07016be08955b3a24f35db0546c0e346f48c
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 / flexible_unet_mlx-0.4.1-py3-none-any.whl

Download URL flexible_unet_mlx-0.4.1-py3-none-any.whl
Size 35.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7a9c11074f4e332eb401d3d7387ab49865ecc02836d02e48c7059d2b36b6ea8b
BLAKE2b-256 checksum
How to use checksums
94a1fdd9eb6be22c810ba11f47fc2163a00364b4ed419d51a25b327aac305ac8
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 flexible-unet-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