mlx-segment-ct 0.2.1__tar.gz → 0.2.2__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.

Potentially problematic release.


This version of mlx-segment-ct might be problematic. Click here for more details.

Files changed (28) hide show
  1. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/PKG-INFO +5 -8
  2. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/README.md +3 -6
  3. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/pyproject.toml +3 -3
  4. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/__init__.py +1 -1
  5. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/instance_norm.py +39 -7
  6. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_network.py +17 -9
  7. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/.gitignore +0 -0
  8. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/LICENSE +0 -0
  9. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/THIRD_PARTY_NOTICES.md +0 -0
  10. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/_types.py +0 -0
  11. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/activations.py +0 -0
  12. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/bundle_transforms.py +0 -0
  13. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/checkpoint_sources.py +0 -0
  14. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/correction.py +0 -0
  15. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/correction_inferer.py +0 -0
  16. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/interactive.py +0 -0
  17. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/labels.py +0 -0
  18. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/medmlx.py +0 -0
  19. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_correction_weights.py +0 -0
  20. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_point.py +0 -0
  21. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_point_window.py +0 -0
  22. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_upsample.py +0 -0
  23. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_weights.py +0 -0
  24. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/mlx_worker.py +0 -0
  25. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/projections.py +0 -0
  26. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/py.typed +0 -0
  27. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/restoration.py +0 -0
  28. {mlx_segment_ct-0.2.1 → mlx_segment_ct-0.2.2}/src/mlx_segment_ct/streaming.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: mlx-segment-ct
3
- Version: 0.2.1
3
+ Version: 0.2.2
4
4
  Summary: Native MLX implementation of NV-Segment-CT (VISTA-3D)
5
5
  Author: Joseph Sandoval, Alfredo Sandoval Jr.
6
6
  License-Expression: Apache-2.0 AND BSD-3-Clause
@@ -8,7 +8,7 @@ License-File: LICENSE
8
8
  License-File: THIRD_PARTY_NOTICES.md
9
9
  Classifier: Operating System :: MacOS :: MacOS X
10
10
  Requires-Python: <3.15,>=3.13
11
- Requires-Dist: medmlx-core==0.2.3
11
+ Requires-Dist: medmlx-core==0.2.4
12
12
  Requires-Dist: nibabel<6,>=5.3
13
13
  Requires-Dist: numpy<3,>=2.0
14
14
  Requires-Dist: scipy<2,>=1.14
@@ -30,7 +30,7 @@ Description-Content-Type: text/markdown
30
30
 
31
31
  Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA VISTA-3D in native MLX.
32
32
 
33
- [Docs](docs/technical.md) · [MedMLX](https://github.com/MedMLX)
33
+ [Docs](https://github.com/MedMLX/mlx-segment-ct/blob/main/docs/technical.md) · [MedMLX](https://github.com/MedMLX)
34
34
 
35
35
  ![Automatic CT segmentation with selected organs and pelvic bones in 3D.](https://raw.githubusercontent.com/MedMLX/mlx-segment-ct/main/docs/showcase.png)
36
36
 
@@ -47,10 +47,7 @@ Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA
47
47
  ```bash
48
48
  uv venv --python 3.13
49
49
  source .venv/bin/activate
50
- repo="https://github.com/MedMLX/mlx-segment-ct"
51
- uv pip install \
52
- "mlx-segment-ct[conversion] @ git+$repo" \
53
- huggingface-hub
50
+ uv pip install "mlx-segment-ct[conversion]" huggingface-hub
54
51
  ```
55
52
 
56
53
  Download and convert the checkpoint once, then run:
@@ -94,6 +91,6 @@ Docs cover click prompts, interactive correction and the NV-Segment-CTMR checkpo
94
91
 
95
92
  ## License
96
93
 
97
- [Apache-2.0](LICENSE). See [third-party notices](THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
94
+ [Apache-2.0](https://github.com/MedMLX/mlx-segment-ct/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/mlx-segment-ct/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
98
95
 
99
96
  For research and education. Outputs require human review.
@@ -2,7 +2,7 @@
2
2
 
3
3
  Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA VISTA-3D in native MLX.
4
4
 
5
- [Docs](docs/technical.md) · [MedMLX](https://github.com/MedMLX)
5
+ [Docs](https://github.com/MedMLX/mlx-segment-ct/blob/main/docs/technical.md) · [MedMLX](https://github.com/MedMLX)
6
6
 
7
7
  ![Automatic CT segmentation with selected organs and pelvic bones in 3D.](https://raw.githubusercontent.com/MedMLX/mlx-segment-ct/main/docs/showcase.png)
8
8
 
@@ -19,10 +19,7 @@ Segment CT anatomy automatically or with anatomy names and clicks, using NVIDIA
19
19
  ```bash
20
20
  uv venv --python 3.13
21
21
  source .venv/bin/activate
22
- repo="https://github.com/MedMLX/mlx-segment-ct"
23
- uv pip install \
24
- "mlx-segment-ct[conversion] @ git+$repo" \
25
- huggingface-hub
22
+ uv pip install "mlx-segment-ct[conversion]" huggingface-hub
26
23
  ```
27
24
 
28
25
  Download and convert the checkpoint once, then run:
@@ -66,6 +63,6 @@ Docs cover click prompts, interactive correction and the NV-Segment-CTMR checkpo
66
63
 
67
64
  ## License
68
65
 
69
- [Apache-2.0](LICENSE). See [third-party notices](THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
66
+ [Apache-2.0](https://github.com/MedMLX/mlx-segment-ct/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/mlx-segment-ct/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
70
67
 
71
68
  For research and education. Outputs require human review.
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "mlx-segment-ct"
7
- version = "0.2.1"
7
+ version = "0.2.2"
8
8
  description = "Native MLX implementation of NV-Segment-CT (VISTA-3D)"
9
9
  readme = "README.md"
10
10
  classifiers = ["Operating System :: MacOS :: MacOS X"]
@@ -13,7 +13,7 @@ license = "Apache-2.0 AND BSD-3-Clause"
13
13
  license-files = ["LICENSE", "THIRD_PARTY_NOTICES.md"]
14
14
  authors = [{ name = "Joseph Sandoval" }, { name = "Alfredo Sandoval Jr." }]
15
15
  dependencies = [
16
- "medmlx-core==0.2.3",
16
+ "medmlx-core==0.2.4",
17
17
  "nibabel>=5.3,<6",
18
18
  "numpy>=2.0,<3",
19
19
  "scipy>=1.14,<2",
@@ -73,7 +73,7 @@ ban-relative-imports = "all"
73
73
  constraint-dependencies = ["setuptools>=84.1.0", "filelock>=4.2.1", "Pillow>=13,<14"]
74
74
 
75
75
  [tool.uv.sources]
76
- medmlx-core = { git = "https://github.com/MedMLX/medmlx.git", rev = "e765f0a94aa31e8dfcdc599e4a4206eaca84f8b0", subdirectory = "packages/core" }
76
+ medmlx-core = { git = "https://github.com/MedMLX/medmlx.git", rev = "f98e0924f50228e2b9509474947b9672054b3217", subdirectory = "packages/core" }
77
77
 
78
78
  [tool.hatch.build.targets.sdist]
79
79
  only-include = [
@@ -25,7 +25,7 @@ if TYPE_CHECKING:
25
25
  from mlx_segment_ct.mlx_worker import automatic_logits
26
26
  from mlx_segment_ct.streaming import LabelReducer, bounded_labels, rolling_logits
27
27
 
28
- __version__ = "0.2.1"
28
+ __version__ = "0.2.2"
29
29
 
30
30
  _EXPORTS = {
31
31
  "AutomaticModel": "mlx_segment_ct.mlx_network",
@@ -5,7 +5,7 @@ from __future__ import annotations
5
5
  import struct
6
6
  from itertools import count
7
7
  from math import prod
8
- from typing import TYPE_CHECKING, cast
8
+ from typing import TYPE_CHECKING, Literal, cast
9
9
 
10
10
  from medmlx_core.typing import MetalKernel
11
11
 
@@ -80,14 +80,19 @@ _SOURCES = {
80
80
  """,
81
81
  ),
82
82
  "affine": (
83
- ["X", "SCALE", "BIAS"],
83
+ ["X", "SCALE", "BIAS", "RESIDUAL"],
84
84
  ["Y"],
85
85
  """
86
86
  uint i = thread_position_in_grid.x;
87
87
  if (i >= GROUPS * N) return;
88
88
  uint g = i / N;
89
89
  // Torch's vectorized affine rounds the multiplication before addition.
90
- Y[i] = X[i] * SCALE[g] + BIAS[g];
90
+ float value = X[i] * SCALE[g] + BIAS[g];
91
+ // Keep residual addition after the rounded affine, before activation.
92
+ if (ADD_RESIDUAL) value = value + RESIDUAL[i];
93
+ if (ACTIVATION == 1) value = isnan(value) ? value : max(value, 0.0f);
94
+ if (ACTIVATION == 2) value = value >= 0.0f ? value : 0.01f * value;
95
+ Y[i] = value;
91
96
  """,
92
97
  ),
93
98
  }
@@ -116,11 +121,21 @@ class InstanceNorm:
116
121
  for name, (inputs, outputs, source) in _SOURCES.items()
117
122
  }
118
123
 
119
- def __call__(self, values: array, weight: array, bias: array) -> array:
124
+ def __call__(
125
+ self,
126
+ values: array,
127
+ weight: array,
128
+ bias: array,
129
+ *,
130
+ activation: Literal["none", "relu", "leaky_relu"] = "none",
131
+ residual: array | None = None,
132
+ ) -> array:
120
133
  """Normalize each (sample, channel) over its spatial voxels, then scale and shift.
121
134
 
122
135
  ``values`` is float32 ``(B, C, D, H, W)``; ``weight`` and ``bias`` are ``(C,)``.
123
- Returns float32 with the shape of ``values``.
136
+ An optional float32 residual of the same shape is added after the affine,
137
+ before the activation (leaky ReLU uses slope 0.01). The two-pass moments
138
+ and each float32 rounding step are unchanged. Returns the input shape.
124
139
  """
125
140
  batch, channels = values.shape[:2]
126
141
  size = prod(values.shape[2:])
@@ -131,9 +146,20 @@ class InstanceNorm:
131
146
  def run(
132
147
  name: str, inputs: list[array], shape: tuple[int, ...], *, variance: bool = False
133
148
  ) -> list[array]:
149
+ template: list[tuple[str, int | bool]] = [
150
+ *constants.items(),
151
+ ("VARIANCE", variance),
152
+ ]
153
+ if name == "affine":
154
+ template.extend(
155
+ [
156
+ ("ACTIVATION", ("none", "relu", "leaky_relu").index(activation)),
157
+ ("ADD_RESIDUAL", residual is not None),
158
+ ]
159
+ )
134
160
  return self.kernels[name](
135
161
  inputs=inputs,
136
- template=[*constants.items(), ("VARIANCE", variance)],
162
+ template=template,
137
163
  grid=(groups * tiles if name == "sum" else prod(shape), 1, 1),
138
164
  threadgroup=(256, 1, 1),
139
165
  output_shapes=[shape] * len(_SOURCES[name][1]),
@@ -146,4 +172,10 @@ class InstanceNorm:
146
172
  mean = run("mean", [totals], (groups,))[0]
147
173
  totals = run("sum", [values, mean], (groups, tiles, 2), variance=True)[0]
148
174
  scale, offset = run("coeff", [totals, mean, weight, bias, self.eps], (groups,))
149
- return run("affine", [values, scale, offset], tuple(values.shape))[0]
175
+ # The disabled residual slot uses an already contiguous small coefficient
176
+ # buffer, avoiding a second contiguous copy when values is a strided view.
177
+ return run(
178
+ "affine",
179
+ [values, scale, offset, scale if residual is None else residual],
180
+ tuple(values.shape),
181
+ )[0]
@@ -4,7 +4,7 @@ from __future__ import annotations
4
4
 
5
5
  from collections.abc import Mapping, Sequence
6
6
  from math import prod
7
- from typing import TYPE_CHECKING, cast
7
+ from typing import TYPE_CHECKING, Literal, cast
8
8
 
9
9
  from mlx_segment_ct._types import ArrayValue, CoreRuntime
10
10
 
@@ -88,21 +88,29 @@ class FeatureModel:
88
88
  )
89
89
 
90
90
  def residual(self, x: array, prefix: str) -> array:
91
- y = self.conv(self.mx.maximum(self.norm(x, f"{prefix}.norm1"), 0), f"{prefix}.conv1")
92
- y = self.conv(self.mx.maximum(self.norm(y, f"{prefix}.norm2"), 0), f"{prefix}.conv2")
91
+ y = self.conv(self.norm(x, f"{prefix}.norm1", activation="relu"), f"{prefix}.conv1")
92
+ y = self.conv(self.norm(y, f"{prefix}.norm2", activation="relu"), f"{prefix}.conv2")
93
93
  return x + y
94
94
 
95
95
  def image_mapping(self, x: array, prefix: str) -> array:
96
- y = self.norm(self.conv(x, f"{prefix}.conv1.conv"), None)
97
- y = self.mx.where(y >= 0, y, 0.01 * y)
98
- y = self.norm(self.conv(y, f"{prefix}.conv2.conv"), None) + x
99
- return self.mx.where(y >= 0, y, 0.01 * y)
96
+ y = self.norm(self.conv(x, f"{prefix}.conv1.conv"), None, activation="leaky_relu")
97
+ return self.norm(
98
+ self.conv(y, f"{prefix}.conv2.conv"), None, activation="leaky_relu", residual=x
99
+ )
100
100
 
101
- def norm(self, x: array, prefix: str | None) -> array:
101
+ def norm(
102
+ self,
103
+ x: array,
104
+ prefix: str | None,
105
+ *,
106
+ activation: Literal["none", "relu", "leaky_relu"] = "none",
107
+ residual: array | None = None,
108
+ ) -> array:
109
+ """Normalize NCDHW values and fuse the caller's ordered affine epilogue."""
102
110
  channels = x.shape[1]
103
111
  weight = self.weights[f"{prefix}.weight"] if prefix else self.mx.ones((channels,))
104
112
  bias = self.weights[f"{prefix}.bias"] if prefix else self.mx.zeros((channels,))
105
- return self.instance_norm(x, weight, bias)
113
+ return self.instance_norm(x, weight, bias, activation=activation, residual=residual)
106
114
 
107
115
  def conv(self, x: array, prefix: str, *, padding: int = 1, stride: int = 1) -> array:
108
116
  return self.operators.conv(
File without changes