Metadata-Version: 2.5
Name: medsam2-mlx
Version: 0.2.2
Summary: Native MLX implementation of MedSAM2
Project-URL: Homepage, https://github.com/MedMLX/medsam2-mlx
Project-URL: Documentation, https://github.com/MedMLX/medsam2-mlx/blob/main/docs/technical.md
Project-URL: Repository, https://github.com/MedMLX/medsam2-mlx
Project-URL: Issues, https://github.com/MedMLX/medsam2-mlx/issues
Author: Joseph Sandoval, Alfredo Sandoval Jr.
License-Expression: Apache-2.0
License-File: LICENSE
License-File: THIRD_PARTY_NOTICES.md
Classifier: Operating System :: MacOS :: MacOS X
Requires-Python: <3.15,>=3.13
Requires-Dist: medmlx-core==0.2.4
Requires-Dist: mlx<0.33,>=0.32.4
Requires-Dist: nibabel<6,>=5.3
Requires-Dist: numpy<3,>=2.0
Requires-Dist: pillow<14,>=13
Requires-Dist: scipy<2,>=1.13
Provides-Extra: conversion
Requires-Dist: torch<3,>=2.6; extra == 'conversion'
Provides-Extra: dev
Requires-Dist: hypothesis<7,>=6.156; extra == 'dev'
Requires-Dist: pyright==1.1.414; extra == 'dev'
Requires-Dist: pytest<10,>=8.4; extra == 'dev'
Requires-Dist: ruff<1,>=0.13; extra == 'dev'
Requires-Dist: simpleitk<3,>=2.5.6; extra == 'dev'
Requires-Dist: torch<3,>=2.6; extra == 'dev'
Provides-Extra: reference
Requires-Dist: hydra-core<2,>=1.3.2; extra == 'reference'
Requires-Dist: iopath<0.2,>=0.1.10; extra == 'reference'
Requires-Dist: opencv-python-headless==5.0.0.93; extra == 'reference'
Requires-Dist: pillow<14,>=13; extra == 'reference'
Requires-Dist: simpleitk<3,>=2.5; extra == 'reference'
Requires-Dist: torch<3,>=2.6; extra == 'reference'
Requires-Dist: tqdm<5,>=4.66; extra == 'reference'
Description-Content-Type: text/markdown

# medsam2-mlx

Segment lesions or organs from a box, point or mask prompt, then track them through a volume with MedSAM2.

[Docs](https://github.com/MedMLX/medsam2-mlx/blob/main/docs/technical.md) · [MedMLX](https://github.com/MedMLX)

![One box prompt → nodule masks propagated across neighboring CT slices.](https://raw.githubusercontent.com/MedMLX/medsam2-mlx/main/docs/showcase.png)

*One box prompt → nodule masks propagated across neighboring CT slices.*

| Input | Output |
| :--- | :--- |
| Scan + slice-level prompt | Tracked masks on the input grid |

**Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)

## Quick start

```bash
uv venv --python 3.13
source .venv/bin/activate
uv pip install "medsam2-mlx[conversion]" huggingface-hub
```

Download the [MedSAM2](https://huggingface.co/wanglab/MedSAM2) checkpoint and convert it once, then run:

```python
from pathlib import Path

from huggingface_hub import hf_hub_download
from medmlx_core import import_mlx
from medsam2_mlx.conversion import convert_weights

mx = import_mlx()
weights = convert_weights(Path(hf_hub_download("wanglab/MedSAM2", "MedSAM2_CTLesion.pt")))
mx.save_safetensors(
    "MedSAM2_CTLesion.safetensors", {name: mx.array(value) for name, value in weights.items()}
)
```

```python
from medsam2_mlx.medmlx import run

result = run(
    image_path="ct.nii.gz",
    weights_path="MedSAM2_CTLesion.safetensors",
    output_dir="out",
    box_xyxy=[120, 140, 180, 200],
    frame_idx=40,
    propagation="both",
)
print(result["outputs"])
```

For NIfTI, `frame_idx` uses the third voxel axis. Box and point coordinates use the first two stored voxel axes.

## License

[Apache-2.0](https://github.com/MedMLX/medsam2-mlx/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/medsam2-mlx/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.

For research and education. Outputs require human review.
