flexray-mlx 0.3.0__tar.gz → 0.3.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/PKG-INFO +10 -10
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/README.md +7 -7
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/pyproject.toml +3 -3
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/__init__.py +1 -1
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/_kernels.py +18 -26
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/_types.py +3 -23
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/mlx_model.py +3 -2
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/tta.py +2 -3
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/.gitignore +0 -0
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/LICENSE +0 -0
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/THIRD_PARTY_NOTICES.md +0 -0
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/_random.py +0 -0
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/medmlx.py +0 -0
- {flexray_mlx-0.3.0 → flexray_mlx-0.3.1}/src/flexray_mlx/py.typed +0 -0
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: flexray-mlx
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.1
|
|
4
4
|
Summary: Segment anatomical structures in radiographs with FleXray and native MLX
|
|
5
5
|
Project-URL: Homepage, https://medmlx.org
|
|
6
|
-
Project-URL: Documentation, https://github.com/MedMLX/flexray-mlx/blob/v0.3.
|
|
6
|
+
Project-URL: Documentation, https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/README.md
|
|
7
7
|
Project-URL: Repository, https://github.com/MedMLX/flexray-mlx
|
|
8
8
|
Project-URL: Issues, https://github.com/MedMLX/flexray-mlx/issues
|
|
9
9
|
Project-URL: Weights, https://huggingface.co/MedMLX/flexray-mlx
|
|
@@ -24,7 +24,7 @@ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
|
24
24
|
Classifier: Topic :: Scientific/Engineering :: Image Processing
|
|
25
25
|
Classifier: Topic :: Scientific/Engineering :: Medical Science Apps.
|
|
26
26
|
Requires-Python: <3.14,>=3.13
|
|
27
|
-
Requires-Dist: medmlx-core<0.4,>=0.3
|
|
27
|
+
Requires-Dist: medmlx-core<0.4,>=0.3.2
|
|
28
28
|
Requires-Dist: mlx==0.32.4; platform_system == 'Darwin' and platform_machine == 'arm64'
|
|
29
29
|
Requires-Dist: numpy<3,>=2.0
|
|
30
30
|
Requires-Dist: pillow<14,>=13
|
|
@@ -45,7 +45,7 @@ Segment anatomical structures in radiographs with FleXray and native MLX.
|
|
|
45
45
|
|
|
46
46
|
[](https://pypi.org/project/flexray-mlx/)
|
|
47
47
|
[](https://pypi.org/project/flexray-mlx/)
|
|
48
|
-
[](https://github.com/MedMLX/flexray-mlx/blob/v0.3.
|
|
48
|
+
[](https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/LICENSE)
|
|
49
49
|
[](https://github.com/ml-explore/mlx)
|
|
50
50
|
[](https://huggingface.co/MedMLX/flexray-mlx)
|
|
51
51
|
[](https://github.com/MedMLX/medmlx)
|
|
@@ -67,7 +67,7 @@ Segment anatomical structures in radiographs with FleXray and native MLX.
|
|
|
67
67
|
```bash
|
|
68
68
|
uv venv --python 3.13
|
|
69
69
|
source .venv/bin/activate
|
|
70
|
-
uv pip install "flexray-mlx>=0.3.
|
|
70
|
+
uv pip install "flexray-mlx>=0.3.1"
|
|
71
71
|
```
|
|
72
72
|
|
|
73
73
|
## Run
|
|
@@ -83,7 +83,7 @@ Use `label_name` to select a structure.
|
|
|
83
83
|
|
|
84
84
|
| `ensemble` | Weights |
|
|
85
85
|
| :--- | :--- |
|
|
86
|
-
| `False` (default) | The flagship `flux0375`.
|
|
86
|
+
| `False` (default) | The flagship `flux0375`. It downloads from `MedMLX/flexray-mlx` on first use; `medmlx setup flexray` downloads it ahead of time |
|
|
87
87
|
| `True` | All five members, in an explicit `weights_path` folder |
|
|
88
88
|
|
|
89
89
|
A local folder holds `ensemble.json` and
|
|
@@ -105,7 +105,8 @@ A local folder holds `ensemble.json` and
|
|
|
105
105
|
| `overwrite` | `False` by default; existing outputs are replaced only when `True` |
|
|
106
106
|
| `member_residency` | `all` by default; `one` loads ensemble members one at a time, spooling per-view probabilities to a temporary folder, with identical results |
|
|
107
107
|
|
|
108
|
-
- **DICOM:** files end in `.dcm` or `.dicom` (any case) or have `DICM`
|
|
108
|
+
- **DICOM:** files end in `.dcm` or `.dicom` (any case), or have no suffix and `DICM`
|
|
109
|
+
at byte 128; other suffixes, such as `.ima`, are read as raster images.
|
|
109
110
|
Accepting a file does not mean the model suits its modality.
|
|
110
111
|
- **Output paths** must differ from each other, the input and the bundle files,
|
|
111
112
|
including links, even with `overwrite`.
|
|
@@ -158,14 +159,13 @@ logits = model(np.zeros((1, 1, 256, 256), dtype=np.float32)) # Bx1x256x256 -> B
|
|
|
158
159
|
converts convolution weights to MLX layout and every tensor to float32, keeping the
|
|
159
160
|
names, and rejects tensor names or shapes that differ from the released network.
|
|
160
161
|
- Inputs are prepared images; preparation, sigmoid, ensembling, TTA and source-grid
|
|
161
|
-
restoration are
|
|
162
|
+
restoration are up to you.
|
|
162
163
|
- `flexray_mlx.tta.predict_with_tta(forward_fn, images, tta_samples=..., seed=..., normalizer=...)`
|
|
163
164
|
takes one or more forward callables and already normalized float32 BxCxHxW images, and
|
|
164
165
|
returns `(logits, probabilities)`. Pass the `normalize` method of
|
|
165
166
|
`flexray_mlx.medmlx.ImagePreprocessing` as `normalizer` to follow the bundle.
|
|
166
167
|
`flexray_mlx.tta.tta_probabilities` takes the same arguments and returns only the
|
|
167
168
|
probabilities.
|
|
168
|
-
- Inference requires Apple Silicon with Metal.
|
|
169
169
|
|
|
170
170
|
## Use with MedMLX
|
|
171
171
|
|
|
@@ -184,4 +184,4 @@ For Codex, use `codex mcp add medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-wor
|
|
|
184
184
|
|
|
185
185
|
## License
|
|
186
186
|
|
|
187
|
-
[Apache-2.0](https://github.com/MedMLX/flexray-mlx/blob/v0.3.
|
|
187
|
+
[Apache-2.0](https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/LICENSE). See [third-party notices](https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms. FleXray weights are noncommercial (CC-BY-NC-4.0).
|
|
@@ -6,7 +6,7 @@ Segment anatomical structures in radiographs with FleXray and native MLX.
|
|
|
6
6
|
|
|
7
7
|
[](https://pypi.org/project/flexray-mlx/)
|
|
8
8
|
[](https://pypi.org/project/flexray-mlx/)
|
|
9
|
-
[](https://github.com/MedMLX/flexray-mlx/blob/v0.3.
|
|
9
|
+
[](https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/LICENSE)
|
|
10
10
|
[](https://github.com/ml-explore/mlx)
|
|
11
11
|
[](https://huggingface.co/MedMLX/flexray-mlx)
|
|
12
12
|
[](https://github.com/MedMLX/medmlx)
|
|
@@ -28,7 +28,7 @@ Segment anatomical structures in radiographs with FleXray and native MLX.
|
|
|
28
28
|
```bash
|
|
29
29
|
uv venv --python 3.13
|
|
30
30
|
source .venv/bin/activate
|
|
31
|
-
uv pip install "flexray-mlx>=0.3.
|
|
31
|
+
uv pip install "flexray-mlx>=0.3.1"
|
|
32
32
|
```
|
|
33
33
|
|
|
34
34
|
## Run
|
|
@@ -44,7 +44,7 @@ Use `label_name` to select a structure.
|
|
|
44
44
|
|
|
45
45
|
| `ensemble` | Weights |
|
|
46
46
|
| :--- | :--- |
|
|
47
|
-
| `False` (default) | The flagship `flux0375`.
|
|
47
|
+
| `False` (default) | The flagship `flux0375`. It downloads from `MedMLX/flexray-mlx` on first use; `medmlx setup flexray` downloads it ahead of time |
|
|
48
48
|
| `True` | All five members, in an explicit `weights_path` folder |
|
|
49
49
|
|
|
50
50
|
A local folder holds `ensemble.json` and
|
|
@@ -66,7 +66,8 @@ A local folder holds `ensemble.json` and
|
|
|
66
66
|
| `overwrite` | `False` by default; existing outputs are replaced only when `True` |
|
|
67
67
|
| `member_residency` | `all` by default; `one` loads ensemble members one at a time, spooling per-view probabilities to a temporary folder, with identical results |
|
|
68
68
|
|
|
69
|
-
- **DICOM:** files end in `.dcm` or `.dicom` (any case) or have `DICM`
|
|
69
|
+
- **DICOM:** files end in `.dcm` or `.dicom` (any case), or have no suffix and `DICM`
|
|
70
|
+
at byte 128; other suffixes, such as `.ima`, are read as raster images.
|
|
70
71
|
Accepting a file does not mean the model suits its modality.
|
|
71
72
|
- **Output paths** must differ from each other, the input and the bundle files,
|
|
72
73
|
including links, even with `overwrite`.
|
|
@@ -119,14 +120,13 @@ logits = model(np.zeros((1, 1, 256, 256), dtype=np.float32)) # Bx1x256x256 -> B
|
|
|
119
120
|
converts convolution weights to MLX layout and every tensor to float32, keeping the
|
|
120
121
|
names, and rejects tensor names or shapes that differ from the released network.
|
|
121
122
|
- Inputs are prepared images; preparation, sigmoid, ensembling, TTA and source-grid
|
|
122
|
-
restoration are
|
|
123
|
+
restoration are up to you.
|
|
123
124
|
- `flexray_mlx.tta.predict_with_tta(forward_fn, images, tta_samples=..., seed=..., normalizer=...)`
|
|
124
125
|
takes one or more forward callables and already normalized float32 BxCxHxW images, and
|
|
125
126
|
returns `(logits, probabilities)`. Pass the `normalize` method of
|
|
126
127
|
`flexray_mlx.medmlx.ImagePreprocessing` as `normalizer` to follow the bundle.
|
|
127
128
|
`flexray_mlx.tta.tta_probabilities` takes the same arguments and returns only the
|
|
128
129
|
probabilities.
|
|
129
|
-
- Inference requires Apple Silicon with Metal.
|
|
130
130
|
|
|
131
131
|
## Use with MedMLX
|
|
132
132
|
|
|
@@ -145,4 +145,4 @@ For Codex, use `codex mcp add medmlx -- "$MEDMLX" serve --root "$HOME/medmlx-wor
|
|
|
145
145
|
|
|
146
146
|
## License
|
|
147
147
|
|
|
148
|
-
[Apache-2.0](https://github.com/MedMLX/flexray-mlx/blob/v0.3.
|
|
148
|
+
[Apache-2.0](https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/LICENSE). See [third-party notices](https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms. FleXray weights are noncommercial (CC-BY-NC-4.0).
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "flexray-mlx"
|
|
7
|
-
version = "0.3.
|
|
7
|
+
version = "0.3.1"
|
|
8
8
|
description = "Segment anatomical structures in radiographs with FleXray and native MLX"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.13,<3.14"
|
|
@@ -37,7 +37,7 @@ authors = [
|
|
|
37
37
|
{ name = "Alfredo Sandoval Jr." },
|
|
38
38
|
]
|
|
39
39
|
dependencies = [
|
|
40
|
-
"medmlx-core>=0.3,<0.4",
|
|
40
|
+
"medmlx-core>=0.3.2,<0.4",
|
|
41
41
|
"mlx==0.32.4; platform_system == 'Darwin' and platform_machine == 'arm64'",
|
|
42
42
|
"numpy>=2.0,<3",
|
|
43
43
|
"pillow>=13,<14",
|
|
@@ -48,7 +48,7 @@ dependencies = [
|
|
|
48
48
|
|
|
49
49
|
[project.urls]
|
|
50
50
|
Homepage = "https://medmlx.org"
|
|
51
|
-
Documentation = "https://github.com/MedMLX/flexray-mlx/blob/v0.3.
|
|
51
|
+
Documentation = "https://github.com/MedMLX/flexray-mlx/blob/v0.3.1/README.md"
|
|
52
52
|
Repository = "https://github.com/MedMLX/flexray-mlx"
|
|
53
53
|
Issues = "https://github.com/MedMLX/flexray-mlx/issues"
|
|
54
54
|
Weights = "https://huggingface.co/MedMLX/flexray-mlx"
|
|
@@ -3,11 +3,9 @@
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
5
|
from functools import lru_cache
|
|
6
|
-
from typing import TYPE_CHECKING
|
|
6
|
+
from typing import TYPE_CHECKING
|
|
7
7
|
|
|
8
|
-
from medmlx_core.typing import MetalKernel
|
|
9
|
-
|
|
10
|
-
from flexray_mlx._types import NativeRuntime
|
|
8
|
+
from medmlx_core.typing import MetalKernel, MlxRuntime
|
|
11
9
|
|
|
12
10
|
if TYPE_CHECKING:
|
|
13
11
|
from mlx.core import array
|
|
@@ -47,21 +45,18 @@ out[i] = ((1.0f - fy) * (1.0f - fx)) * a
|
|
|
47
45
|
|
|
48
46
|
|
|
49
47
|
@lru_cache(maxsize=1)
|
|
50
|
-
def _resize_concat_kernel(mx:
|
|
48
|
+
def _resize_concat_kernel(mx: MlxRuntime) -> MetalKernel:
|
|
51
49
|
"""Compile and cache the Metal kernel for corner-aligned resize and concatenation."""
|
|
52
|
-
return
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
source=_RESIZE_CONCAT,
|
|
59
|
-
compile_options={"math_mode": "safe"},
|
|
60
|
-
),
|
|
50
|
+
return mx.fast.metal_kernel(
|
|
51
|
+
name="flexray_bilinear_concat",
|
|
52
|
+
input_names=["inp", "skip"],
|
|
53
|
+
output_names=["out"],
|
|
54
|
+
source=_RESIZE_CONCAT,
|
|
55
|
+
compile_options={"math_mode": "safe"},
|
|
61
56
|
)
|
|
62
57
|
|
|
63
58
|
|
|
64
|
-
def upsample_concat(x: array, skip: array, *, mx:
|
|
59
|
+
def upsample_concat(x: array, skip: array, *, mx: MlxRuntime) -> array:
|
|
65
60
|
"""Join a 2x bilinear NHWC resize with ``skip``, keeping resized channels first.
|
|
66
61
|
|
|
67
62
|
Inputs must be FP32, with the same positive batch size and a skip grid
|
|
@@ -152,21 +147,18 @@ if (bits >= 0x7E800000u && bits < 0x7F800000u) {
|
|
|
152
147
|
|
|
153
148
|
|
|
154
149
|
@lru_cache(maxsize=1)
|
|
155
|
-
def _sigmoid_kernel(mx:
|
|
150
|
+
def _sigmoid_kernel(mx: MlxRuntime) -> MetalKernel:
|
|
156
151
|
"""Compile and cache the Torch-CPU-exact sigmoid kernel."""
|
|
157
|
-
return
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
source=_SLEEF_SIGMOID,
|
|
164
|
-
compile_options={"math_mode": "safe"},
|
|
165
|
-
),
|
|
152
|
+
return mx.fast.metal_kernel(
|
|
153
|
+
name="flexray_torch_cpu_sigmoid",
|
|
154
|
+
input_names=["logits", "flipped"],
|
|
155
|
+
output_names=["out"],
|
|
156
|
+
source=_SLEEF_SIGMOID,
|
|
157
|
+
compile_options={"math_mode": "safe"},
|
|
166
158
|
)
|
|
167
159
|
|
|
168
160
|
|
|
169
|
-
def torch_cpu_sigmoid(logits: array, flipped: array, *, mx:
|
|
161
|
+
def torch_cpu_sigmoid(logits: array, flipped: array, *, mx: MlxRuntime) -> array:
|
|
170
162
|
"""Unflip NHWC logits and return Torch-CPU-exact sigmoid probabilities in NCHW.
|
|
171
163
|
|
|
172
164
|
Args:
|
|
@@ -2,18 +2,14 @@
|
|
|
2
2
|
|
|
3
3
|
from __future__ import annotations
|
|
4
4
|
|
|
5
|
-
from collections.abc import Callable
|
|
6
5
|
from pathlib import Path
|
|
7
|
-
from typing import
|
|
6
|
+
from typing import Literal, NotRequired, Protocol, TypedDict
|
|
8
7
|
|
|
9
8
|
import numpy as np
|
|
10
9
|
from medmlx_core.outputs import RunResult as _RunResult
|
|
11
|
-
from medmlx_core.typing import
|
|
10
|
+
from medmlx_core.typing import ArrayLayer
|
|
12
11
|
from numpy.typing import NDArray
|
|
13
12
|
|
|
14
|
-
if TYPE_CHECKING:
|
|
15
|
-
from mlx.core import StreamOrDevice, array
|
|
16
|
-
|
|
17
13
|
type HostArray = NDArray[np.generic]
|
|
18
14
|
type RealArray = NDArray[np.bool_ | np.integer | np.floating]
|
|
19
15
|
type FloatArray = NDArray[np.float32]
|
|
@@ -24,27 +20,11 @@ type UIntArray = NDArray[np.uint32]
|
|
|
24
20
|
type JsonValue = bool | int | float | str | list[JsonValue] | dict[str, JsonValue] | None
|
|
25
21
|
|
|
26
22
|
|
|
27
|
-
class NativeRuntime(MlxRuntime, Protocol):
|
|
28
|
-
def square(self, x: array, /, *, stream: StreamOrDevice = None) -> array:
|
|
29
|
-
"""Square an MLX array elementwise."""
|
|
30
|
-
...
|
|
31
|
-
|
|
32
|
-
def compile(self, fun: Callable[[array], array]) -> Callable[[array], array]:
|
|
33
|
-
"""Compile a pure one-array MLX function."""
|
|
34
|
-
...
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
class NativeOperation(Protocol):
|
|
38
|
-
def __call__(self, x: array) -> array:
|
|
39
|
-
"""Apply a native operation to an MLX array."""
|
|
40
|
-
...
|
|
41
|
-
|
|
42
|
-
|
|
43
23
|
class NeuralRuntime(Protocol):
|
|
44
24
|
def MaxPool2d(
|
|
45
25
|
self, kernel_size: int | tuple[int, int], stride: int | tuple[int, int] | None = None,
|
|
46
26
|
padding: int | tuple[int, int] | None = 0,
|
|
47
|
-
) ->
|
|
27
|
+
) -> ArrayLayer:
|
|
48
28
|
"""Construct a two-dimensional MLX max-pooling layer."""
|
|
49
29
|
...
|
|
50
30
|
|
|
@@ -20,12 +20,13 @@ from flexray_mlx._kernels import upsample_concat
|
|
|
20
20
|
from flexray_mlx._types import FloatArray
|
|
21
21
|
|
|
22
22
|
if TYPE_CHECKING:
|
|
23
|
+
from medmlx_core.typing import MlxRuntime
|
|
23
24
|
from mlx.core import array
|
|
24
25
|
|
|
25
|
-
from flexray_mlx._types import
|
|
26
|
+
from flexray_mlx._types import NeuralRuntime, SafetensorsRuntime
|
|
26
27
|
|
|
27
28
|
# Namespaces stay lazy until this native implementation is admitted.
|
|
28
|
-
mx = cast("
|
|
29
|
+
mx = cast("MlxRuntime", import_mlx())
|
|
29
30
|
nn = cast("NeuralRuntime", import_module("mlx.nn"))
|
|
30
31
|
safetensors = cast("SafetensorsRuntime", import_module("safetensors"))
|
|
31
32
|
|
|
@@ -21,10 +21,9 @@ from flexray_mlx._random import TorchRandom
|
|
|
21
21
|
from flexray_mlx._types import BoolArray, FloatArray, IntArray
|
|
22
22
|
|
|
23
23
|
if TYPE_CHECKING:
|
|
24
|
+
from medmlx_core.typing import MlxRuntime
|
|
24
25
|
from mlx.core import array
|
|
25
26
|
|
|
26
|
-
from flexray_mlx._types import NativeRuntime
|
|
27
|
-
|
|
28
27
|
Forward = Callable[[FloatArray], FloatArray]
|
|
29
28
|
|
|
30
29
|
|
|
@@ -188,7 +187,7 @@ def view_probabilities(forward: Forward, view: FloatArray, flipped: BoolArray |
|
|
|
188
187
|
|
|
189
188
|
from flexray_mlx._kernels import torch_cpu_sigmoid
|
|
190
189
|
|
|
191
|
-
mx = cast("
|
|
190
|
+
mx = cast("MlxRuntime", import_mlx())
|
|
192
191
|
native = cast(NativeForward, forward)
|
|
193
192
|
flags = np.zeros(len(view), np.int32) if flipped is None else flipped.astype(np.int32)
|
|
194
193
|
logits = native.forward_native(mx.array(np.asarray(view, dtype=np.float32)))
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|