segresnet-mlx 0.2.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.
Potentially problematic release.
This version of segresnet-mlx might be problematic. Click here for more details.
- segresnet_mlx-0.2.1/.gitignore +26 -0
- segresnet_mlx-0.2.1/LICENSE +202 -0
- segresnet_mlx-0.2.1/PKG-INFO +94 -0
- segresnet_mlx-0.2.1/README.md +65 -0
- segresnet_mlx-0.2.1/THIRD_PARTY_NOTICES.md +21 -0
- segresnet_mlx-0.2.1/pyproject.toml +90 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/__init__.py +67 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/_arrays.py +36 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/_nifti.py +63 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/_records.py +78 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/_runtime.py +22 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/_types.py +58 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/inference.py +37 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/medmlx.py +669 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/network.py +195 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/predictor.py +303 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/py.typed +0 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/telemetry.py +166 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/topology.py +49 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/transforms.py +669 -0
- segresnet_mlx-0.2.1/src/segresnet_mlx/weights.py +183 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
__pycache__/
|
|
2
|
+
*.py[cod]
|
|
3
|
+
.venv/
|
|
4
|
+
*.egg-info/
|
|
5
|
+
dist/
|
|
6
|
+
build/
|
|
7
|
+
.pytest_cache/
|
|
8
|
+
.ruff_cache/
|
|
9
|
+
|
|
10
|
+
# Checkpoints and converted weights are never committed.
|
|
11
|
+
checkpoints/
|
|
12
|
+
weights/
|
|
13
|
+
*.safetensors
|
|
14
|
+
*.npz
|
|
15
|
+
!tests/fixtures/*.npz
|
|
16
|
+
!tests/fixtures/*/*.npz
|
|
17
|
+
*.pt
|
|
18
|
+
*.pth
|
|
19
|
+
*.ckpt
|
|
20
|
+
|
|
21
|
+
# Imaging data is never committed.
|
|
22
|
+
data/
|
|
23
|
+
*.nii
|
|
24
|
+
*.nii.gz
|
|
25
|
+
*.dcm
|
|
26
|
+
.DS_Store
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: segresnet-mlx
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: Native MLX SegResNet with NumPy sliding-window logit inference
|
|
5
|
+
Project-URL: Homepage, https://github.com/MedMLX/segresnet-mlx
|
|
6
|
+
Project-URL: Documentation, https://github.com/MedMLX/segresnet-mlx/blob/main/docs/technical.md
|
|
7
|
+
Project-URL: Repository, https://github.com/MedMLX/segresnet-mlx
|
|
8
|
+
Project-URL: Issues, https://github.com/MedMLX/segresnet-mlx/issues
|
|
9
|
+
Author: Joseph Sandoval, Alfredo Sandoval Jr.
|
|
10
|
+
License-Expression: Apache-2.0
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
License-File: THIRD_PARTY_NOTICES.md
|
|
13
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
14
|
+
Requires-Python: <3.15,>=3.13
|
|
15
|
+
Requires-Dist: medmlx-core==0.2.3
|
|
16
|
+
Requires-Dist: nibabel<6,>=5.3
|
|
17
|
+
Requires-Dist: numpy<3,>=2.0
|
|
18
|
+
Provides-Extra: conversion
|
|
19
|
+
Requires-Dist: torch<3,>=2.6; extra == 'conversion'
|
|
20
|
+
Provides-Extra: dev
|
|
21
|
+
Requires-Dist: hypothesis<7,>=6; extra == 'dev'
|
|
22
|
+
Requires-Dist: monai==1.6.1; extra == 'dev'
|
|
23
|
+
Requires-Dist: pyright==1.1.414; extra == 'dev'
|
|
24
|
+
Requires-Dist: pytest<10,>=8.4; extra == 'dev'
|
|
25
|
+
Requires-Dist: pytorch-ignite<0.6,>=0.4.11; extra == 'dev'
|
|
26
|
+
Requires-Dist: ruff<1,>=0.13; extra == 'dev'
|
|
27
|
+
Requires-Dist: torch<3,>=2.6; extra == 'dev'
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# segresnet-mlx
|
|
31
|
+
|
|
32
|
+
Segment brain tumors, renal structures and whole-body CT anatomy with MONAI SegResNet models in native MLX.
|
|
33
|
+
|
|
34
|
+
[Docs](https://github.com/MedMLX/segresnet-mlx/blob/main/docs/technical.md) · [MedMLX](https://github.com/MedMLX)
|
|
35
|
+
|
|
36
|
+

|
|
37
|
+
|
|
38
|
+
*BraTS bundle output on a Medical Segmentation Decathlon brain MRI: tumor core
|
|
39
|
+
(red), edema (green) and enhancing tumor (yellow).*
|
|
40
|
+
|
|
41
|
+
| Input | Output |
|
|
42
|
+
| :--- | :--- |
|
|
43
|
+
| MRI sequences or CT volumes | Segmentation on the input grid |
|
|
44
|
+
|
|
45
|
+
**Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)
|
|
46
|
+
|
|
47
|
+
## Quick start
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
uv venv --python 3.13 --seed
|
|
51
|
+
source .venv/bin/activate
|
|
52
|
+
python -m pip install "segresnet-mlx==0.2.1" huggingface-hub
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The tagged GitHub release is an alternative installation route:
|
|
56
|
+
|
|
57
|
+
```bash
|
|
58
|
+
repo="https://github.com/MedMLX/segresnet-mlx.git@v0.2.1"
|
|
59
|
+
python -m pip install "segresnet-mlx @ git+$repo" huggingface-hub
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Download the public converted BraTS bundle separately, then run:
|
|
63
|
+
|
|
64
|
+
```python
|
|
65
|
+
from huggingface_hub import hf_hub_download
|
|
66
|
+
from segresnet_mlx.medmlx import run
|
|
67
|
+
|
|
68
|
+
bundle = "brats_mri_segmentation"
|
|
69
|
+
weights = hf_hub_download("MedMLX/segresnet-brats-mlx", "segresnet-brats.safetensors")
|
|
70
|
+
|
|
71
|
+
result = run(
|
|
72
|
+
bundle=bundle,
|
|
73
|
+
t1c_path="t1c.nii.gz",
|
|
74
|
+
t1_path="t1.nii.gz",
|
|
75
|
+
t2_path="t2.nii.gz",
|
|
76
|
+
flair_path="flair.nii.gz",
|
|
77
|
+
weights_path=weights,
|
|
78
|
+
output_dir="out",
|
|
79
|
+
)
|
|
80
|
+
print(result["outputs"]["mask"])
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Keep input volumes on the same voxel grid. The package contains code only.
|
|
84
|
+
Public converted bundles are available separately for
|
|
85
|
+
[BraTS](https://huggingface.co/MedMLX/segresnet-brats-mlx) and
|
|
86
|
+
[renal CECT](https://huggingface.co/MedMLX/segresnet-renal-cect-mlx); follow each
|
|
87
|
+
weight card and its terms. Docs cover local upstream checkpoint conversion and
|
|
88
|
+
whole-body CT bundles. Qualification differs by bundle; see the technical notes.
|
|
89
|
+
|
|
90
|
+
## License
|
|
91
|
+
|
|
92
|
+
[Apache-2.0](https://github.com/MedMLX/segresnet-mlx/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/segresnet-mlx/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
|
|
93
|
+
|
|
94
|
+
For research and education. Outputs require human review.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
# segresnet-mlx
|
|
2
|
+
|
|
3
|
+
Segment brain tumors, renal structures and whole-body CT anatomy with MONAI SegResNet models in native MLX.
|
|
4
|
+
|
|
5
|
+
[Docs](https://github.com/MedMLX/segresnet-mlx/blob/main/docs/technical.md) · [MedMLX](https://github.com/MedMLX)
|
|
6
|
+
|
|
7
|
+

|
|
8
|
+
|
|
9
|
+
*BraTS bundle output on a Medical Segmentation Decathlon brain MRI: tumor core
|
|
10
|
+
(red), edema (green) and enhancing tumor (yellow).*
|
|
11
|
+
|
|
12
|
+
| Input | Output |
|
|
13
|
+
| :--- | :--- |
|
|
14
|
+
| MRI sequences or CT volumes | Segmentation on the input grid |
|
|
15
|
+
|
|
16
|
+
**Apple Silicon · Metal · Python 3.13 / 3.14** · [uv](https://docs.astral.sh/uv/)
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
uv venv --python 3.13 --seed
|
|
22
|
+
source .venv/bin/activate
|
|
23
|
+
python -m pip install "segresnet-mlx==0.2.1" huggingface-hub
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The tagged GitHub release is an alternative installation route:
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
repo="https://github.com/MedMLX/segresnet-mlx.git@v0.2.1"
|
|
30
|
+
python -m pip install "segresnet-mlx @ git+$repo" huggingface-hub
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Download the public converted BraTS bundle separately, then run:
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from huggingface_hub import hf_hub_download
|
|
37
|
+
from segresnet_mlx.medmlx import run
|
|
38
|
+
|
|
39
|
+
bundle = "brats_mri_segmentation"
|
|
40
|
+
weights = hf_hub_download("MedMLX/segresnet-brats-mlx", "segresnet-brats.safetensors")
|
|
41
|
+
|
|
42
|
+
result = run(
|
|
43
|
+
bundle=bundle,
|
|
44
|
+
t1c_path="t1c.nii.gz",
|
|
45
|
+
t1_path="t1.nii.gz",
|
|
46
|
+
t2_path="t2.nii.gz",
|
|
47
|
+
flair_path="flair.nii.gz",
|
|
48
|
+
weights_path=weights,
|
|
49
|
+
output_dir="out",
|
|
50
|
+
)
|
|
51
|
+
print(result["outputs"]["mask"])
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Keep input volumes on the same voxel grid. The package contains code only.
|
|
55
|
+
Public converted bundles are available separately for
|
|
56
|
+
[BraTS](https://huggingface.co/MedMLX/segresnet-brats-mlx) and
|
|
57
|
+
[renal CECT](https://huggingface.co/MedMLX/segresnet-renal-cect-mlx); follow each
|
|
58
|
+
weight card and its terms. Docs cover local upstream checkpoint conversion and
|
|
59
|
+
whole-body CT bundles. Qualification differs by bundle; see the technical notes.
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
[Apache-2.0](https://github.com/MedMLX/segresnet-mlx/blob/main/LICENSE). See [third-party notices](https://github.com/MedMLX/segresnet-mlx/blob/main/THIRD_PARTY_NOTICES.md). Model weights retain their publisher terms.
|
|
64
|
+
|
|
65
|
+
For research and education. Outputs require human review.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
MedMLX-authored additions are licensed under [Apache-2.0](LICENSE).
|
|
4
|
+
|
|
5
|
+
## MONAI SegResNet and model bundles
|
|
6
|
+
|
|
7
|
+
The native MLX network, inferer and transforms are adapted from
|
|
8
|
+
[MONAI](https://github.com/Project-MONAI/MONAI), with the numerical reference
|
|
9
|
+
pinned to revision `eccefc57550b111ed781d82249dfe77872a0e918` (MONAI 1.6.0).
|
|
10
|
+
Copyright (c) MONAI Consortium. These source portions retain Apache-2.0 terms;
|
|
11
|
+
the full license is included in [LICENSE](LICENSE).
|
|
12
|
+
|
|
13
|
+
Bundle definitions originate in the
|
|
14
|
+
[MONAI model zoo](https://github.com/Project-MONAI/model-zoo), revision
|
|
15
|
+
`db9eeeef7ba64ce2ced0230d9bd4c524f69b7f0c`. The source files and bundle versions
|
|
16
|
+
are identified in [scripts/upstream_reference.json](scripts/upstream_reference.json).
|
|
17
|
+
The port changes execution to native MLX and host NumPy image processing.
|
|
18
|
+
|
|
19
|
+
Pretrained checkpoints and input datasets retain their publisher terms.
|
|
20
|
+
Dependencies declared in [pyproject.toml](pyproject.toml) retain their respective
|
|
21
|
+
licenses independently of the package source license.
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling>=1.27"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "segresnet-mlx"
|
|
7
|
+
version = "0.2.1"
|
|
8
|
+
description = "Native MLX SegResNet with NumPy sliding-window logit inference"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.13,<3.15"
|
|
11
|
+
classifiers = ["Operating System :: MacOS :: MacOS X"]
|
|
12
|
+
license = "Apache-2.0"
|
|
13
|
+
license-files = ["LICENSE", "THIRD_PARTY_NOTICES.md"]
|
|
14
|
+
authors = [{ name = "Joseph Sandoval" }, { name = "Alfredo Sandoval Jr." }]
|
|
15
|
+
dependencies = [
|
|
16
|
+
"medmlx-core==0.2.3",
|
|
17
|
+
"nibabel>=5.3,<6",
|
|
18
|
+
"numpy>=2.0,<3",
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
[project.entry-points."medmlx.models"]
|
|
22
|
+
segresnet = "segresnet_mlx.medmlx:MODEL"
|
|
23
|
+
|
|
24
|
+
[project.urls]
|
|
25
|
+
Homepage = "https://github.com/MedMLX/segresnet-mlx"
|
|
26
|
+
Documentation = "https://github.com/MedMLX/segresnet-mlx/blob/main/docs/technical.md"
|
|
27
|
+
Repository = "https://github.com/MedMLX/segresnet-mlx"
|
|
28
|
+
Issues = "https://github.com/MedMLX/segresnet-mlx/issues"
|
|
29
|
+
|
|
30
|
+
[project.optional-dependencies]
|
|
31
|
+
conversion = ["torch>=2.6,<3"]
|
|
32
|
+
dev = [
|
|
33
|
+
"pyright==1.1.414",
|
|
34
|
+
"pytorch-ignite>=0.4.11,<0.6",
|
|
35
|
+
"hypothesis>=6,<7",
|
|
36
|
+
"monai==1.6.1", # Pinned upstream reference tests and generation only.
|
|
37
|
+
"pytest>=8.4,<10",
|
|
38
|
+
"ruff>=0.13,<1",
|
|
39
|
+
"torch>=2.6,<3",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[tool.hatch.metadata]
|
|
43
|
+
allow-direct-references = true
|
|
44
|
+
|
|
45
|
+
[tool.hatch.build.targets.wheel]
|
|
46
|
+
packages = ["src/segresnet_mlx"]
|
|
47
|
+
|
|
48
|
+
[tool.pytest.ini_options]
|
|
49
|
+
testpaths = ["tests"]
|
|
50
|
+
addopts = "--strict-config --strict-markers"
|
|
51
|
+
markers = ["mlx: native MLX execution requiring Apple Silicon Metal"]
|
|
52
|
+
|
|
53
|
+
[tool.uv]
|
|
54
|
+
constraint-dependencies = ["setuptools>=84.1.0", "filelock>=4.2.1", ]
|
|
55
|
+
environments = ["sys_platform == 'darwin' and platform_machine == 'arm64'"]
|
|
56
|
+
|
|
57
|
+
[tool.ruff]
|
|
58
|
+
line-length = 100
|
|
59
|
+
target-version = "py313"
|
|
60
|
+
|
|
61
|
+
[tool.ruff.lint]
|
|
62
|
+
extend-select = ["TID251", "TID252", "ANN401"]
|
|
63
|
+
select = ["E", "F", "I", "UP", "B", "SIM", "RUF"]
|
|
64
|
+
|
|
65
|
+
[tool.pyright]
|
|
66
|
+
include = ["scripts","src","tests"]
|
|
67
|
+
extraPaths = ["src"]
|
|
68
|
+
pythonVersion = "3.13"
|
|
69
|
+
typeCheckingMode = "strict"
|
|
70
|
+
enableTypeIgnoreComments = false
|
|
71
|
+
reportUnnecessaryTypeIgnoreComment = "error"
|
|
72
|
+
|
|
73
|
+
[tool.ruff.lint.flake8-tidy-imports]
|
|
74
|
+
ban-relative-imports = "all"
|
|
75
|
+
|
|
76
|
+
[tool.ruff.lint.flake8-tidy-imports.banned-api]
|
|
77
|
+
"typing.Any" = {msg = "Use a precise type and parse untyped values at the boundary."}
|
|
78
|
+
"typing_extensions.Any" = {msg = "Use a precise type and parse untyped values at the boundary."}
|
|
79
|
+
|
|
80
|
+
[tool.uv.sources]
|
|
81
|
+
medmlx-core = { git = "https://github.com/MedMLX/medmlx.git", rev = "e765f0a94aa31e8dfcdc599e4a4206eaca84f8b0", subdirectory = "packages/core" }
|
|
82
|
+
|
|
83
|
+
[tool.hatch.build.targets.sdist]
|
|
84
|
+
only-include = [
|
|
85
|
+
"src/segresnet_mlx",
|
|
86
|
+
"pyproject.toml",
|
|
87
|
+
"README.md",
|
|
88
|
+
"LICENSE",
|
|
89
|
+
"THIRD_PARTY_NOTICES.md",
|
|
90
|
+
]
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Explicit MLX port of MONAI SegResNet inference.
|
|
2
|
+
|
|
3
|
+
Public names load on first access, so importing the package (or the medmlx
|
|
4
|
+
declaration in ``segresnet_mlx.medmlx``) does not import MLX.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from importlib import import_module
|
|
8
|
+
from typing import TYPE_CHECKING
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from segresnet_mlx.inference import forward_patch as forward_patch
|
|
12
|
+
from segresnet_mlx.network import SegResNet as SegResNet
|
|
13
|
+
from segresnet_mlx.predictor import BUNDLE_CONFIGS as BUNDLE_CONFIGS
|
|
14
|
+
from segresnet_mlx.predictor import BundlePredictor as BundlePredictor
|
|
15
|
+
from segresnet_mlx.predictor import SlidingWindowConfig as SlidingWindowConfig
|
|
16
|
+
from segresnet_mlx.predictor import predict_logits as predict_logits
|
|
17
|
+
from segresnet_mlx.telemetry import WindowTelemetry as WindowTelemetry
|
|
18
|
+
from segresnet_mlx.topology import SegResNetSpec as SegResNetSpec
|
|
19
|
+
from segresnet_mlx.weights import convert_checkpoint as convert_checkpoint
|
|
20
|
+
from segresnet_mlx.weights import convert_state_dict as convert_state_dict
|
|
21
|
+
from segresnet_mlx.weights import load_model as load_model
|
|
22
|
+
from segresnet_mlx.weights import required_shapes as required_shapes
|
|
23
|
+
|
|
24
|
+
__version__ = "0.2.1"
|
|
25
|
+
|
|
26
|
+
_EXPORTS = {
|
|
27
|
+
"BUNDLE_CONFIGS": "segresnet_mlx.predictor",
|
|
28
|
+
"BundlePredictor": "segresnet_mlx.predictor",
|
|
29
|
+
"SegResNet": "segresnet_mlx.network",
|
|
30
|
+
"SegResNetSpec": "segresnet_mlx.topology",
|
|
31
|
+
"SlidingWindowConfig": "segresnet_mlx.predictor",
|
|
32
|
+
"WindowTelemetry": "segresnet_mlx.telemetry",
|
|
33
|
+
"convert_checkpoint": "segresnet_mlx.weights",
|
|
34
|
+
"convert_state_dict": "segresnet_mlx.weights",
|
|
35
|
+
"forward_patch": "segresnet_mlx.inference",
|
|
36
|
+
"load_model": "segresnet_mlx.weights",
|
|
37
|
+
"predict_logits": "segresnet_mlx.predictor",
|
|
38
|
+
"required_shapes": "segresnet_mlx.weights",
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
__all__ = [
|
|
42
|
+
"BUNDLE_CONFIGS",
|
|
43
|
+
"BundlePredictor",
|
|
44
|
+
"SegResNet",
|
|
45
|
+
"SegResNetSpec",
|
|
46
|
+
"SlidingWindowConfig",
|
|
47
|
+
"WindowTelemetry",
|
|
48
|
+
"convert_checkpoint",
|
|
49
|
+
"convert_state_dict",
|
|
50
|
+
"forward_patch",
|
|
51
|
+
"load_model",
|
|
52
|
+
"predict_logits",
|
|
53
|
+
"required_shapes",
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def __getattr__(name: str) -> object:
|
|
58
|
+
module = _EXPORTS.get(name)
|
|
59
|
+
if module is None:
|
|
60
|
+
raise AttributeError(f"module 'segresnet_mlx' has no attribute {name!r}")
|
|
61
|
+
value = getattr(import_module(module), name)
|
|
62
|
+
globals()[name] = value
|
|
63
|
+
return value
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def __dir__() -> list[str]:
|
|
67
|
+
return sorted({*globals(), *_EXPORTS})
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"""Host array dtypes at model, checkpoint and image boundaries."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from typing import TYPE_CHECKING, cast
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
if TYPE_CHECKING:
|
|
11
|
+
from segresnet_mlx._types import Array
|
|
12
|
+
|
|
13
|
+
type HostArray = np.ndarray[tuple[int, ...], np.dtype[np.generic]]
|
|
14
|
+
type Float32Array = np.ndarray[tuple[int, ...], np.dtype[np.float32]]
|
|
15
|
+
type Float64Array = np.ndarray[tuple[int, ...], np.dtype[np.float64]]
|
|
16
|
+
type Int64Array = np.ndarray[tuple[int, ...], np.dtype[np.int64]]
|
|
17
|
+
type MaskArray = Float32Array | Int64Array
|
|
18
|
+
|
|
19
|
+
type NumericScalar = (
|
|
20
|
+
np.bool_
|
|
21
|
+
| np.int8
|
|
22
|
+
| np.int16
|
|
23
|
+
| np.int32
|
|
24
|
+
| np.int64
|
|
25
|
+
| np.uint8
|
|
26
|
+
| np.uint16
|
|
27
|
+
| np.uint32
|
|
28
|
+
| np.uint64
|
|
29
|
+
| np.float16
|
|
30
|
+
| np.float32
|
|
31
|
+
| np.float64
|
|
32
|
+
| np.complex64
|
|
33
|
+
| np.complex128
|
|
34
|
+
)
|
|
35
|
+
type NumericArray = np.ndarray[tuple[int, ...], np.dtype[NumericScalar]]
|
|
36
|
+
as_numpy = cast("Callable[[Array], NumericArray]", np.array)
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""Typed protocols for the NiBabel NIfTI, header and orientation calls used here."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
from os import PathLike
|
|
7
|
+
from typing import Protocol, cast
|
|
8
|
+
|
|
9
|
+
import nibabel
|
|
10
|
+
import numpy as np
|
|
11
|
+
from nibabel.filebasedimages import ImageFileError as ImageFileError
|
|
12
|
+
|
|
13
|
+
from segresnet_mlx._arrays import Float64Array, HostArray
|
|
14
|
+
|
|
15
|
+
type PathArg = str | PathLike[str]
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ArrayBuffer(Protocol):
|
|
19
|
+
def __array__(
|
|
20
|
+
self, dtype: np.dtype[np.generic] | None = None, copy: bool | None = None
|
|
21
|
+
) -> HostArray: ...
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class NiftiHeader(Protocol):
|
|
25
|
+
def get_zooms(self) -> tuple[float, ...]: ...
|
|
26
|
+
def set_zooms(self, zooms: Sequence[float] | Float64Array) -> None: ...
|
|
27
|
+
def __getitem__(self, key: str) -> HostArray: ...
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class NiftiImage(Protocol):
|
|
31
|
+
shape: tuple[int, ...]
|
|
32
|
+
affine: Float64Array
|
|
33
|
+
dataobj: ArrayBuffer | HostArray
|
|
34
|
+
header: NiftiHeader
|
|
35
|
+
|
|
36
|
+
def __init__(self, dataobj: HostArray, affine: HostArray) -> None: ...
|
|
37
|
+
def get_data_dtype(self) -> np.dtype[np.generic]: ...
|
|
38
|
+
def get_sform(self) -> Float64Array: ...
|
|
39
|
+
def get_qform(self) -> Float64Array: ...
|
|
40
|
+
def set_sform(self, affine: Float64Array, code: int | None = None) -> None: ...
|
|
41
|
+
def set_qform(self, affine: Float64Array, code: int | None = None) -> None: ...
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
class Orientations(Protocol):
|
|
45
|
+
def axcodes2ornt(self, axcodes: str, labels: tuple[tuple[str, str], ...]) -> Float64Array: ...
|
|
46
|
+
def ornt_transform(self, start_ornt: Float64Array, end_ornt: Float64Array) -> Float64Array: ...
|
|
47
|
+
def inv_ornt_aff(self, ornt: Float64Array, shape: Sequence[int]) -> Float64Array: ...
|
|
48
|
+
def aff2axcodes(
|
|
49
|
+
self, aff: Float64Array, labels: tuple[tuple[str, str], ...]
|
|
50
|
+
) -> tuple[str | None, ...]: ...
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
class Nibabel(Protocol):
|
|
54
|
+
Nifti1Image: type[NiftiImage]
|
|
55
|
+
orientations: Orientations
|
|
56
|
+
|
|
57
|
+
def load(self, filename: PathArg) -> NiftiImage: ...
|
|
58
|
+
def save(self, img: NiftiImage, filename: PathArg) -> None: ...
|
|
59
|
+
def io_orientation(self, affine: Float64Array) -> Float64Array: ...
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
# Bound to the real module; this runner admits only NIfTI images.
|
|
63
|
+
nib = cast(Nibabel, nibabel)
|