konfai 1.0.0__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 konfai might be problematic. Click here for more details.

Files changed (44) hide show
  1. konfai-1.0.0/LICENSE +201 -0
  2. konfai-1.0.0/PKG-INFO +68 -0
  3. konfai-1.0.0/README.md +37 -0
  4. konfai-1.0.0/konfai/__init__.py +16 -0
  5. konfai-1.0.0/konfai/data/HDF5.py +326 -0
  6. konfai-1.0.0/konfai/data/__init__.py +0 -0
  7. konfai-1.0.0/konfai/data/augmentation.py +597 -0
  8. konfai-1.0.0/konfai/data/dataset.py +470 -0
  9. konfai-1.0.0/konfai/data/transform.py +536 -0
  10. konfai-1.0.0/konfai/evaluator.py +146 -0
  11. konfai-1.0.0/konfai/main.py +43 -0
  12. konfai-1.0.0/konfai/metric/__init__.py +0 -0
  13. konfai-1.0.0/konfai/metric/measure.py +488 -0
  14. konfai-1.0.0/konfai/metric/schedulers.py +49 -0
  15. konfai-1.0.0/konfai/models/classification/convNeXt.py +175 -0
  16. konfai-1.0.0/konfai/models/classification/resnet.py +116 -0
  17. konfai-1.0.0/konfai/models/generation/cStyleGan.py +137 -0
  18. konfai-1.0.0/konfai/models/generation/ddpm.py +218 -0
  19. konfai-1.0.0/konfai/models/generation/diffusionGan.py +557 -0
  20. konfai-1.0.0/konfai/models/generation/gan.py +134 -0
  21. konfai-1.0.0/konfai/models/generation/vae.py +72 -0
  22. konfai-1.0.0/konfai/models/registration/registration.py +136 -0
  23. konfai-1.0.0/konfai/models/representation/representation.py +57 -0
  24. konfai-1.0.0/konfai/models/segmentation/NestedUNet.py +53 -0
  25. konfai-1.0.0/konfai/models/segmentation/UNet.py +58 -0
  26. konfai-1.0.0/konfai/network/__init__.py +0 -0
  27. konfai-1.0.0/konfai/network/blocks.py +348 -0
  28. konfai-1.0.0/konfai/network/network.py +950 -0
  29. konfai-1.0.0/konfai/predictor.py +366 -0
  30. konfai-1.0.0/konfai/trainer.py +330 -0
  31. konfai-1.0.0/konfai/utils/ITK.py +269 -0
  32. konfai-1.0.0/konfai/utils/Registration.py +199 -0
  33. konfai-1.0.0/konfai/utils/__init__.py +0 -0
  34. konfai-1.0.0/konfai/utils/config.py +218 -0
  35. konfai-1.0.0/konfai/utils/dataset.py +764 -0
  36. konfai-1.0.0/konfai/utils/utils.py +493 -0
  37. konfai-1.0.0/konfai.egg-info/PKG-INFO +68 -0
  38. konfai-1.0.0/konfai.egg-info/SOURCES.txt +42 -0
  39. konfai-1.0.0/konfai.egg-info/dependency_links.txt +1 -0
  40. konfai-1.0.0/konfai.egg-info/entry_points.txt +3 -0
  41. konfai-1.0.0/konfai.egg-info/requires.txt +19 -0
  42. konfai-1.0.0/konfai.egg-info/top_level.txt +1 -0
  43. konfai-1.0.0/pyproject.toml +43 -0
  44. konfai-1.0.0/setup.cfg +4 -0
konfai-1.0.0/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
konfai-1.0.0/PKG-INFO ADDED
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.4
2
+ Name: konfai
3
+ Version: 1.0.0
4
+ Summary: Modular and configurable Deep Learning framework with YAML and PyTorch
5
+ Author-email: Valentin Boussot <boussot.v@gmail.com>
6
+ License-Expression: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/vboussot/KonfAI
8
+ Project-URL: Repository, https://github.com/vboussot/KonfAI
9
+ Project-URL: Issues, https://github.com/vboussot/KonfAI/issues
10
+ Project-URL: License, https://www.apache.org/licenses/LICENSE-2.0
11
+ Requires-Python: >=3.8
12
+ Description-Content-Type: text/markdown
13
+ License-File: LICENSE
14
+ Requires-Dist: torch
15
+ Requires-Dist: tqdm
16
+ Requires-Dist: numpy
17
+ Requires-Dist: ruamel.yaml
18
+ Requires-Dist: psutil
19
+ Requires-Dist: tensorboard
20
+ Requires-Dist: SimpleITK
21
+ Requires-Dist: lxml
22
+ Requires-Dist: h5py
23
+ Requires-Dist: pynvml
24
+ Provides-Extra: vtk
25
+ Requires-Dist: vtk; extra == "vtk"
26
+ Provides-Extra: lpips
27
+ Requires-Dist: lpips; extra == "lpips"
28
+ Provides-Extra: cluster
29
+ Requires-Dist: submitit; extra == "cluster"
30
+ Dynamic: license-file
31
+
32
+
33
+ # 🧠 KonfAI
34
+ <img src="logo.png" alt="KonfAI Logo" width="200" align="right"/>
35
+
36
+ **KonfAI** is a modular and highly configurable deep learning framework built on PyTorch, driven entirely by YAML configuration files.
37
+
38
+ It is designed to support complex medical imaging workflows, flexible model architectures, customizable training loops, and advanced loss scheduling, without hardcoding anything.
39
+
40
+ ---
41
+
42
+ ## 🔧 Key Features
43
+
44
+ - 🔀 Full training/prediction/evaluation orchestration via YAML configuration file
45
+ - 🧩 Modular plugin-like structure (transforms, augmentations, models, losses, schedulers)
46
+ - 🔄 Dynamic criterion scheduling per head / target
47
+ - 🧠 Multi-branch / multi-output model support
48
+ - 🖥️ Cluster-ready
49
+ - 📈 TensorBoard and custom logging support
50
+
51
+ ---
52
+
53
+ ## 🚀 Installation
54
+
55
+ ```bash
56
+ git clone https://github.com/vboussot/KonfAI.git && cd KonfAI
57
+ pip install -e .
58
+ ```
59
+
60
+ ---
61
+
62
+ ## 🧪 Usage
63
+
64
+ ```bash
65
+ konfai TRAIN --gpu 0
66
+ konfai PREDICTION --gpu 0
67
+ konfai EVALUATION
68
+ ```
konfai-1.0.0/README.md ADDED
@@ -0,0 +1,37 @@
1
+
2
+ # 🧠 KonfAI
3
+ <img src="logo.png" alt="KonfAI Logo" width="200" align="right"/>
4
+
5
+ **KonfAI** is a modular and highly configurable deep learning framework built on PyTorch, driven entirely by YAML configuration files.
6
+
7
+ It is designed to support complex medical imaging workflows, flexible model architectures, customizable training loops, and advanced loss scheduling, without hardcoding anything.
8
+
9
+ ---
10
+
11
+ ## 🔧 Key Features
12
+
13
+ - 🔀 Full training/prediction/evaluation orchestration via YAML configuration file
14
+ - 🧩 Modular plugin-like structure (transforms, augmentations, models, losses, schedulers)
15
+ - 🔄 Dynamic criterion scheduling per head / target
16
+ - 🧠 Multi-branch / multi-output model support
17
+ - 🖥️ Cluster-ready
18
+ - 📈 TensorBoard and custom logging support
19
+
20
+ ---
21
+
22
+ ## 🚀 Installation
23
+
24
+ ```bash
25
+ git clone https://github.com/vboussot/KonfAI.git && cd KonfAI
26
+ pip install -e .
27
+ ```
28
+
29
+ ---
30
+
31
+ ## 🧪 Usage
32
+
33
+ ```bash
34
+ konfai TRAIN --gpu 0
35
+ konfai PREDICTION --gpu 0
36
+ konfai EVALUATION
37
+ ```
@@ -0,0 +1,16 @@
1
+ import os
2
+ import datetime
3
+
4
+ MODELS_DIRECTORY = lambda : os.environ["DL_API_MODELS_DIRECTORY"]
5
+ CHECKPOINTS_DIRECTORY =lambda : os.environ["DL_API_CHECKPOINTS_DIRECTORY"]
6
+ MODEL = lambda : os.environ["DL_API_MODEL"]
7
+ PREDICTIONS_DIRECTORY =lambda : os.environ["DL_API_PREDICTIONS_DIRECTORY"]
8
+ EVALUATIONS_DIRECTORY =lambda : os.environ["DL_API_EVALUATIONS_DIRECTORY"]
9
+ STATISTICS_DIRECTORY = lambda : os.environ["DL_API_STATISTICS_DIRECTORY"]
10
+ SETUPS_DIRECTORY = lambda : os.environ["DL_API_SETUPS_DIRECTORY"]
11
+ CONFIG_FILE = lambda : os.environ["DEEP_LEARNING_API_CONFIG_FILE"]
12
+ DL_API_STATE = lambda : os.environ["DL_API_STATE"]
13
+ DEEP_LEARNING_API_ROOT = lambda : os.environ["DEEP_LEARNING_API_ROOT"]
14
+ CUDA_VISIBLE_DEVICES = lambda : os.environ["CUDA_VISIBLE_DEVICES"]
15
+
16
+ DATE = lambda : datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
@@ -0,0 +1,326 @@
1
+ from abc import ABC, abstractmethod
2
+ import SimpleITK as sitk
3
+ import numpy as np
4
+ import torch
5
+ import os
6
+ import torch.nn.functional as F
7
+ from typing import Any, Iterator
8
+ from typing import Union
9
+ import itertools
10
+ import copy
11
+ from functools import partial
12
+ from KonfAI.konfai.utils.config import config
13
+ from KonfAI.konfai.utils.utils import get_patch_slices_from_shape
14
+ from KonfAI.konfai.utils.dataset import Dataset, Attribute
15
+ from KonfAI.konfai.data.transform import Transform, Save
16
+ from KonfAI.konfai.data.augmentation import DataAugmentationsList
17
+
18
+
19
+ class PathCombine(ABC):
20
+
21
+ def __init__(self) -> None:
22
+ self.data: torch.Tensor = None
23
+ self.overlap: int = None
24
+
25
+ """
26
+ A = slice(0, overlap)
27
+ B = slice(-overlap, None)
28
+ C = slice(overlap, -overlap)
29
+
30
+ 1D
31
+ A+B
32
+ 2D :
33
+ AA+AB+BA+BB
34
+
35
+ AC+BC
36
+ CA+CB
37
+ 3D :
38
+ AAA+AAB+ABA+ABB+BAA+BAB+BBA+BBB
39
+
40
+ AAC+ABC+BAC+BBC
41
+ ACA+ACB+BCA+BCB
42
+ CAA+CAB+CBA+CBB
43
+
44
+ ACC+BCC
45
+ CAC+CBC
46
+ CCA+CCB
47
+
48
+ """
49
+ def setPatchConfig(self, patch_size: list[int], overlap: int):
50
+ self.data = F.pad(torch.ones([size-overlap*2 for size in patch_size]), [overlap]*2*len(patch_size), mode="constant", value=0)
51
+ self.data = self._setFunction(self.data, overlap)
52
+ dim = len(patch_size)
53
+
54
+ A = slice(0, overlap)
55
+ B = slice(-overlap, None)
56
+ C = slice(overlap, -overlap)
57
+
58
+ for i in range(dim):
59
+ slices_badge = list(itertools.product(*[[A, B] for _ in range(dim-i)]))
60
+ for indexs in itertools.combinations([0,1,2], i):
61
+ result = []
62
+ for slices in slices_badge:
63
+ slices = list(slices)
64
+ for index in indexs:
65
+ slices.insert(index, C)
66
+ result.append(tuple(slices))
67
+ for patch, s in zip(PathCombine._normalise([self.data[s] for s in result]), result):
68
+ self.data[s] = patch
69
+
70
+
71
+ @staticmethod
72
+ def _normalise(patchs: list[torch.Tensor]) -> list[torch.Tensor]:
73
+ data_sum = torch.sum(torch.concat([patch.unsqueeze(0) for patch in patchs], dim=0), dim=0)
74
+ return [d/data_sum for d in patchs]
75
+
76
+ def __call__(self, input: torch.Tensor) -> torch.Tensor:
77
+ return self.data.repeat([input.shape[0]]+[1]*(len(input.shape)-1)).to(input.device)*input
78
+
79
+ @abstractmethod
80
+ def _setFunction(self, data: torch.Tensor, overlap: int) -> torch.Tensor:
81
+ pass
82
+
83
+ class Mean(PathCombine):
84
+
85
+ @config("Mean")
86
+ def __init__(self) -> None:
87
+ super().__init__()
88
+
89
+ def _setFunction(self, data: torch.Tensor, overlap: int) -> torch.Tensor:
90
+ return torch.ones_like(self.data)
91
+
92
+ class Cosinus(PathCombine):
93
+
94
+ @config("Cosinus")
95
+ def __init__(self) -> None:
96
+ super().__init__()
97
+
98
+ def _function_sides(self, overlap: int, x: float):
99
+ return np.clip(np.cos(np.pi/(2*(overlap+1))*x), 0, 1)
100
+
101
+ def _setFunction(self, data: torch.Tensor, overlap: int) -> torch.Tensor:
102
+ image = sitk.GetImageFromArray(np.asarray(data, dtype=np.uint8))
103
+ danielssonDistanceMapImageFilter = sitk.DanielssonDistanceMapImageFilter()
104
+ distance = torch.tensor(sitk.GetArrayFromImage(danielssonDistanceMapImageFilter.Execute(image)))
105
+ return distance.apply_(partial(self._function_sides, overlap))
106
+
107
+ class Accumulator():
108
+
109
+ def __init__(self, patch_slices: list[tuple[slice]], patch_size: list[int], patchCombine: Union[PathCombine, None] = None, batch: bool = True) -> None:
110
+ self._layer_accumulator: list[Union[torch.Tensor, None]] = [None for i in range(len(patch_slices))]
111
+ self.patch_slices = []
112
+ for patch in patch_slices:
113
+ slices = []
114
+ for s, shape in zip(patch, patch_size):
115
+ slices.append(slice(s.start, s.start+shape))
116
+ self.patch_slices.append(tuple(slices))
117
+ self.shape = max([[v.stop for v in patch] for patch in patch_slices])
118
+ self.patch_size = patch_size
119
+ self.patchCombine = patchCombine
120
+ self.batch = batch
121
+
122
+ def addLayer(self, index: int, layer: torch.Tensor) -> None:
123
+ self._layer_accumulator[index] = layer
124
+
125
+ def isFull(self) -> bool:
126
+ return len(self.patch_slices) == len([v for v in self._layer_accumulator if v is not None])
127
+
128
+ def assemble(self) -> torch.Tensor:
129
+ if all([self._layer_accumulator[0].shape[i-len(self.patch_size)] == size for i, size in enumerate(self.patch_size)]):
130
+ N = 2 if self.batch else 1
131
+
132
+ result = torch.zeros((list(self._layer_accumulator[0].shape[:N])+list(max([[v.stop for v in patch] for patch in self.patch_slices]))), dtype=self._layer_accumulator[0].dtype).to(self._layer_accumulator[0].device)
133
+ for patch_slice, data in zip(self.patch_slices, self._layer_accumulator):
134
+ slices_dest = tuple([slice(result.shape[i]) for i in range(N)] + list(patch_slice))
135
+
136
+ for dim, s in enumerate(patch_slice):
137
+ if s.stop-s.start == 1:
138
+ data = data.unsqueeze(dim=dim+N)
139
+ if self.patchCombine is not None:
140
+ result[slices_dest] += self.patchCombine(data)
141
+ else:
142
+ result[slices_dest] = data
143
+ result = result[tuple([slice(None, None)]+[slice(0, s) for s in self.shape])]
144
+ else:
145
+ result = torch.cat(tuple(self._layer_accumulator), dim=0)
146
+ self._layer_accumulator.clear()
147
+ return result
148
+
149
+ class Patch(ABC):
150
+
151
+ def __init__(self, patch_size: list[int], overlap: Union[int, None], path_mask: Union[str, None] = None, padValue: float = 0, extend_slice: int = 0) -> None:
152
+ self.patch_size = patch_size
153
+ self.overlap = overlap
154
+ self._patch_slices : dict[int, list[tuple[slice]]] = {}
155
+ self._nb_patch_per_dim: dict[int, list[tuple[int, bool]]] = {}
156
+ self.path_mask = path_mask
157
+ self.mask = None
158
+ self.padValue = padValue
159
+ self.extend_slice = extend_slice
160
+ if self.path_mask is not None:
161
+ if os.path.exists(self.path_mask):
162
+ self.mask = torch.tensor(sitk.GetArrayFromImage(sitk.ReadImage(self.path_mask)))
163
+ else:
164
+ raise NameError('Mask file not found')
165
+
166
+ def load(self, shape : dict[int, list[int]], a: int = 0) -> None:
167
+ self._patch_slices[a], self._nb_patch_per_dim[a] = get_patch_slices_from_shape(self.patch_size, shape, self.overlap)
168
+
169
+ def getPatch_slices(self, a: int = 0):
170
+ return self._patch_slices[a]
171
+
172
+ @abstractmethod
173
+ def getData(self, data : torch.Tensor, index : int, a: int, isInput: bool) -> torch.Tensor:
174
+ pass
175
+
176
+ def getData(self, data : torch.Tensor, index : int, a: int, isInput: bool) -> list[torch.Tensor]:
177
+ slices_pre = []
178
+ for max in data.shape[:-len(self.patch_size)]:
179
+ slices_pre.append(slice(max))
180
+ extend_slice = self.extend_slice if isInput else 0
181
+
182
+ bottom = extend_slice//2
183
+ top = int(np.ceil(extend_slice/2))
184
+ s = slice(self._patch_slices[a][index][0].start-bottom if self._patch_slices[a][index][0].start-bottom >= 0 else 0, self._patch_slices[a][index][0].stop+top if self._patch_slices[a][index][0].stop+top <= data.shape[len(slices_pre)] else data.shape[len(slices_pre)])
185
+ slices = [s] + list(self._patch_slices[a][index][1:])
186
+ data_sliced = data[slices_pre+slices]
187
+ if data_sliced.shape[len(slices_pre)] < bottom+top+1:
188
+ pad_bottom = 0
189
+ pad_top = 0
190
+ if self._patch_slices[a][index][0].start-bottom < 0:
191
+ pad_bottom = bottom-s.start
192
+ if self._patch_slices[a][index][0].stop+top > data.shape[len(slices_pre)]:
193
+ pad_top = self._patch_slices[a][index][0].stop+top-data.shape[len(slices_pre)]
194
+ data_sliced = F.pad(data_sliced, [0 for _ in range((len(slices)-1)*2)]+[pad_bottom, pad_top], 'reflect')
195
+
196
+ padding = []
197
+ for dim_it, _slice in enumerate(reversed(slices)):
198
+ p = 0 if _slice.start+self.patch_size[-dim_it-1] <= data.shape[-dim_it-1] else self.patch_size[-dim_it-1]-(data.shape[-dim_it-1]-_slice.start)
199
+ padding.append(0)
200
+ padding.append(p)
201
+
202
+ data_sliced = F.pad(data_sliced, tuple(padding), "constant", 0 if data_sliced.dtype == torch.uint8 and self.padValue < 0 else self.padValue)
203
+ if self.mask is not None:
204
+ outside = torch.ones_like(data_sliced)*(0 if data_sliced.dtype == torch.uint8 and self.padValue < 0 else self.padValue)
205
+ data_sliced = torch.where(self.mask == 0, outside, data_sliced)
206
+
207
+ for d in [i for i, v in enumerate(reversed(self.patch_size)) if v == 1]:
208
+ data_sliced = torch.squeeze(data_sliced, dim = len(data_sliced.shape)-d-1)
209
+ return torch.cat([data_sliced[:, i, ...] for i in range(data_sliced.shape[1])], dim=0) if extend_slice > 0 else data_sliced
210
+
211
+ def getSize(self, a: int = 0) -> int:
212
+ return len(self._patch_slices[a])
213
+
214
+ class DatasetPatch(Patch):
215
+
216
+ @config("Patch")
217
+ def __init__(self, patch_size : list[int] = [128, 256, 256], overlap : Union[int, None] = None, mask: Union[str, None] = None, padValue: float = 0, extend_slice: int = 0) -> None:
218
+ super().__init__(patch_size, overlap, mask, padValue, extend_slice)
219
+
220
+ class ModelPatch(Patch):
221
+
222
+ @config("Patch")
223
+ def __init__(self, patch_size : list[int] = [128, 256, 256], overlap : Union[int, None] = None, patchCombine: Union[str, None] = None, mask: Union[str, None] = None, padValue: float = 0, extend_slice: int = 0) -> None:
224
+ super().__init__(patch_size, overlap, mask, padValue, extend_slice)
225
+ self.patchCombine = patchCombine
226
+
227
+ def disassemble(self, *dataList: torch.Tensor) -> Iterator[list[torch.Tensor]]:
228
+ for i in range(self.getSize()):
229
+ yield [self.getData(data, i, 0, True) for data in dataList]
230
+
231
+ class DatasetManager():
232
+
233
+ def __init__(self, index: int, group_src: str, group_dest : str, name: str, dataset : Dataset, patch : Union[DatasetPatch, None], pre_transforms : list[Transform], dataAugmentationsList : list[DataAugmentationsList]) -> None:
234
+ self.group_src = group_src
235
+ self.group_dest = group_dest
236
+ self.name = name
237
+ self.index = index
238
+ self.dataset = dataset
239
+ self.loaded = False
240
+ self.cache_attributes: list[Attribute] = []
241
+ _shape, cache_attribute = self.dataset.getInfos(self.group_src, name)
242
+ self.cache_attributes.append(cache_attribute)
243
+ _shape = list(_shape[1:])
244
+
245
+ self.data : list[torch.Tensor] = list()
246
+
247
+ for transformFunction in pre_transforms:
248
+ _shape = transformFunction.transformShape(_shape, cache_attribute)
249
+
250
+ self.patch = DatasetPatch(patch_size=patch.patch_size, overlap=patch.overlap, mask=patch.path_mask, padValue=patch.padValue, extend_slice=patch.extend_slice) if patch else DatasetPatch(_shape)
251
+ self.patch.load(_shape, 0)
252
+ self.shape = _shape
253
+ self.dataAugmentationsList = dataAugmentationsList
254
+ self.resetAugmentation()
255
+ self.cache_attributes_bak = copy.deepcopy(self.cache_attributes)
256
+
257
+ def resetAugmentation(self):
258
+ i = 1
259
+ for dataAugmentations in self.dataAugmentationsList:
260
+ shape = []
261
+ caches_attribute = []
262
+ for _ in range(dataAugmentations.nb):
263
+ shape.append(self.shape)
264
+ caches_attribute.append(copy.deepcopy(self.cache_attributes[0]))
265
+
266
+ for dataAugmentation in dataAugmentations.dataAugmentations:
267
+ shape = dataAugmentation.state_init(self.index, shape, caches_attribute)
268
+ for it, s in enumerate(shape):
269
+ self.cache_attributes.append(caches_attribute[it])
270
+ self.patch.load(s, i)
271
+ i+=1
272
+
273
+ def load(self, pre_transform : list[Transform], dataAugmentationsList : list[DataAugmentationsList], device: torch.device) -> None:
274
+ if self.loaded:
275
+ return
276
+ i = len(pre_transform)
277
+ data = None
278
+ for transformFunction in reversed(pre_transform):
279
+ if isinstance(transformFunction, Save):
280
+ filename, format = transformFunction.save.split(":")
281
+ dataset = Dataset(filename, format)
282
+ if dataset.isDatasetExist(self.group_dest, self.name):
283
+ data, attrib = dataset.readData(self.group_dest, self.name)
284
+ self.cache_attributes[0].update(attrib)
285
+ break
286
+ i-=1
287
+
288
+ if i==0:
289
+ data, _ = self.dataset.readData(self.group_src, self.name)
290
+
291
+
292
+ data = torch.from_numpy(data)
293
+
294
+ if len(pre_transform):
295
+ for transformFunction in pre_transform[i:]:
296
+ data = transformFunction(self.name, data, self.cache_attributes[0])
297
+ if isinstance(transformFunction, Save):
298
+ filename, format = transformFunction.save.split(":")
299
+ dataset = Dataset(filename, format)
300
+ dataset.write(self.group_dest, self.name, data.numpy(), self.cache_attributes[0])
301
+ self.data : list[torch.Tensor] = list()
302
+ self.data.append(data)
303
+
304
+ for dataAugmentations in dataAugmentationsList:
305
+ a_data = [data.clone() for _ in range(dataAugmentations.nb)]
306
+ for dataAugmentation in dataAugmentations.dataAugmentations:
307
+ a_data = dataAugmentation(self.index, a_data, device)
308
+
309
+ for d in a_data:
310
+ self.data.append(d)
311
+ self.loaded = True
312
+
313
+ def unload(self) -> None:
314
+ if hasattr(self, "data"):
315
+ del self.data
316
+ self.cache_attributes = copy.deepcopy(self.cache_attributes_bak)
317
+ self.loaded = False
318
+
319
+ def getData(self, index : int, a : int, post_transforms : list[Transform], isInput: bool) -> torch.Tensor:
320
+ data = self.patch.getData(self.data[a], index, a, isInput)
321
+ for transformFunction in post_transforms:
322
+ data = transformFunction(self.name, data, self.cache_attributes[a])
323
+ return data
324
+
325
+ def getSize(self, a: int) -> int:
326
+ return self.patch.getSize(a)
File without changes