flexible-unet-mlx
Segment surgical tools in RGB endoscopy frames with FlexibleUNet and native MLX.
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.3.0"
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": {...}}: themaskartifact names the labels and the frame grid it is on, anddetailsholds 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_patchtakes 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 the caller's.
FlexibleUNet(weights, mx=mx)runs on MLX arrays directly. load_networkchecks names, shapes, dtypes and finite values.- Inference requires Apple Silicon with Metal.
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.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| flexible_unet_mlx-0.3.0.tar.gz | 28.2 kB | Without provenance | Details for flexible_unet_mlx-0.3.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| flexible_unet_mlx-0.3.0-py3-none-any.whl | Python 3 | none | any | Details for flexible_unet_mlx-0.3.0-py3-none-any.whl |
Total release size: 65.0 kB
Release files / flexible_unet_mlx-0.3.0.tar.gz
| Download URL | flexible_unet_mlx-0.3.0.tar.gz |
|---|---|
| Size | 28.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ffd01deafbe52ff71cff34a434abb7754d484e7cecb969e8d079f716c234ad0b
|
|
BLAKE2b-256 checksum How to use checksums |
42641146eb725d0ca78c85cf973b57c04b19f226cedef7fbcf3d493dcb468132
|
| 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.3.0-py3-none-any.whl
| Download URL | flexible_unet_mlx-0.3.0-py3-none-any.whl |
|---|---|
| Size | 36.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
136f8e76c5c808ae554bfb257208a7a667f134e1331ac6c2635236138328a5e9
|
|
BLAKE2b-256 checksum How to use checksums |
28f935961f53bd1431de3771cc452a4699e5f3ae6059b31292796550aa6cb622
|
| 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.3.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.