careamics 0.0.9__py3-none-any.whl → 0.0.11__py3-none-any.whl
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 careamics might be problematic. Click here for more details.
- careamics/__init__.py +0 -4
- careamics/careamist.py +0 -1
- careamics/config/__init__.py +1 -13
- careamics/config/algorithms/care_algorithm_model.py +84 -0
- careamics/config/algorithms/n2n_algorithm_model.py +85 -0
- careamics/config/algorithms/n2v_algorithm_model.py +269 -1
- careamics/config/configuration.py +21 -13
- careamics/config/configuration_factories.py +179 -187
- careamics/config/configuration_io.py +2 -2
- careamics/config/data/__init__.py +1 -4
- careamics/config/data/data_model.py +46 -62
- careamics/config/support/supported_transforms.py +1 -1
- careamics/config/transformations/__init__.py +0 -2
- careamics/config/transformations/n2v_manipulate_model.py +15 -0
- careamics/config/transformations/transform_unions.py +0 -13
- careamics/dataset/dataset_utils/iterate_over_files.py +2 -2
- careamics/dataset/dataset_utils/running_stats.py +7 -3
- careamics/dataset/in_memory_dataset.py +3 -10
- careamics/dataset/in_memory_pred_dataset.py +3 -5
- careamics/dataset/in_memory_tiled_pred_dataset.py +2 -2
- careamics/dataset/iterable_dataset.py +2 -2
- careamics/dataset/iterable_pred_dataset.py +3 -5
- careamics/dataset/iterable_tiled_pred_dataset.py +3 -3
- careamics/dataset_ng/dataset/__init__.py +3 -0
- careamics/dataset_ng/dataset/dataset.py +184 -0
- careamics/dataset_ng/demo_dataset.ipynb +271 -0
- careamics/dataset_ng/demo_patch_extractor.py +53 -0
- careamics/dataset_ng/demo_patch_extractor_factory.py +37 -0
- careamics/dataset_ng/patch_extractor/__init__.py +10 -0
- careamics/dataset_ng/patch_extractor/demo_custom_image_stack_loader.py +111 -0
- careamics/dataset_ng/patch_extractor/image_stack/__init__.py +9 -0
- careamics/dataset_ng/patch_extractor/image_stack/image_stack_protocol.py +53 -0
- careamics/dataset_ng/patch_extractor/image_stack/in_memory_image_stack.py +55 -0
- careamics/dataset_ng/patch_extractor/image_stack/zarr_image_stack.py +163 -0
- careamics/dataset_ng/patch_extractor/image_stack_loader.py +140 -0
- careamics/dataset_ng/patch_extractor/patch_extractor.py +29 -0
- careamics/dataset_ng/patch_extractor/patch_extractor_factory.py +208 -0
- careamics/dataset_ng/patching_strategies/__init__.py +11 -0
- careamics/dataset_ng/patching_strategies/patching_strategy_protocol.py +82 -0
- careamics/dataset_ng/patching_strategies/random_patching.py +338 -0
- careamics/dataset_ng/patching_strategies/sequential_patching.py +75 -0
- careamics/lightning/lightning_module.py +78 -27
- careamics/lightning/train_data_module.py +8 -39
- careamics/losses/fcn/losses.py +17 -10
- careamics/model_io/bioimage/bioimage_utils.py +5 -3
- careamics/model_io/bioimage/model_description.py +3 -3
- careamics/model_io/bmz_io.py +2 -2
- careamics/model_io/model_io_utils.py +2 -2
- careamics/transforms/__init__.py +2 -1
- careamics/transforms/compose.py +5 -15
- careamics/transforms/n2v_manipulate_torch.py +143 -0
- careamics/transforms/pixel_manipulation.py +1 -0
- careamics/transforms/pixel_manipulation_torch.py +418 -0
- careamics/utils/version.py +38 -0
- {careamics-0.0.9.dist-info → careamics-0.0.11.dist-info}/METADATA +7 -8
- {careamics-0.0.9.dist-info → careamics-0.0.11.dist-info}/RECORD +59 -42
- careamics/config/care_configuration.py +0 -100
- careamics/config/data/n2v_data_model.py +0 -193
- careamics/config/n2n_configuration.py +0 -101
- careamics/config/n2v_configuration.py +0 -266
- {careamics-0.0.9.dist-info → careamics-0.0.11.dist-info}/WHEEL +0 -0
- {careamics-0.0.9.dist-info → careamics-0.0.11.dist-info}/entry_points.txt +0 -0
- {careamics-0.0.9.dist-info → careamics-0.0.11.dist-info}/licenses/LICENSE +0 -0
careamics/__init__.py
CHANGED
|
@@ -11,8 +11,6 @@ __all__ = [
|
|
|
11
11
|
"CAREamist",
|
|
12
12
|
"Configuration",
|
|
13
13
|
"algorithm_factory",
|
|
14
|
-
"configuration_factory",
|
|
15
|
-
"data_factory",
|
|
16
14
|
"load_configuration",
|
|
17
15
|
"save_configuration",
|
|
18
16
|
]
|
|
@@ -21,8 +19,6 @@ from .careamist import CAREamist
|
|
|
21
19
|
from .config import (
|
|
22
20
|
Configuration,
|
|
23
21
|
algorithm_factory,
|
|
24
|
-
configuration_factory,
|
|
25
|
-
data_factory,
|
|
26
22
|
load_configuration,
|
|
27
23
|
save_configuration,
|
|
28
24
|
)
|
careamics/careamist.py
CHANGED
|
@@ -157,7 +157,6 @@ class CAREamist:
|
|
|
157
157
|
self.cfg = load_configuration(source)
|
|
158
158
|
|
|
159
159
|
# instantiate model
|
|
160
|
-
# TODO call model factory here
|
|
161
160
|
if isinstance(self.cfg.algorithm_config, UNetBasedAlgorithm):
|
|
162
161
|
self.model = FCNModule(
|
|
163
162
|
algorithm_config=self.cfg.algorithm_config,
|
careamics/config/__init__.py
CHANGED
|
@@ -8,29 +8,22 @@ while `*_configuration` is reserved for the main configuration models, including
|
|
|
8
8
|
|
|
9
9
|
__all__ = [
|
|
10
10
|
"CAREAlgorithm",
|
|
11
|
-
"CAREConfiguration",
|
|
12
11
|
"CheckpointModel",
|
|
13
12
|
"Configuration",
|
|
14
13
|
"DataConfig",
|
|
15
14
|
"GaussianMixtureNMConfig",
|
|
16
|
-
"GeneralDataConfig",
|
|
17
15
|
"InferenceConfig",
|
|
18
16
|
"LVAELossConfig",
|
|
19
17
|
"MultiChannelNMConfig",
|
|
20
18
|
"N2NAlgorithm",
|
|
21
|
-
"N2NConfiguration",
|
|
22
19
|
"N2VAlgorithm",
|
|
23
|
-
"N2VConfiguration",
|
|
24
|
-
"N2VDataConfig",
|
|
25
20
|
"TrainingConfig",
|
|
26
21
|
"UNetBasedAlgorithm",
|
|
27
22
|
"VAEBasedAlgorithm",
|
|
28
23
|
"algorithm_factory",
|
|
29
|
-
"configuration_factory",
|
|
30
24
|
"create_care_configuration",
|
|
31
25
|
"create_n2n_configuration",
|
|
32
26
|
"create_n2v_configuration",
|
|
33
|
-
"data_factory",
|
|
34
27
|
"load_configuration",
|
|
35
28
|
"save_configuration",
|
|
36
29
|
]
|
|
@@ -43,21 +36,16 @@ from .algorithms import (
|
|
|
43
36
|
VAEBasedAlgorithm,
|
|
44
37
|
)
|
|
45
38
|
from .callback_model import CheckpointModel
|
|
46
|
-
from .care_configuration import CAREConfiguration
|
|
47
39
|
from .configuration import Configuration
|
|
48
40
|
from .configuration_factories import (
|
|
49
41
|
algorithm_factory,
|
|
50
|
-
configuration_factory,
|
|
51
42
|
create_care_configuration,
|
|
52
43
|
create_n2n_configuration,
|
|
53
44
|
create_n2v_configuration,
|
|
54
|
-
data_factory,
|
|
55
45
|
)
|
|
56
46
|
from .configuration_io import load_configuration, save_configuration
|
|
57
|
-
from .data import DataConfig
|
|
47
|
+
from .data import DataConfig
|
|
58
48
|
from .inference_model import InferenceConfig
|
|
59
49
|
from .loss_model import LVAELossConfig
|
|
60
|
-
from .n2n_configuration import N2NConfiguration
|
|
61
|
-
from .n2v_configuration import N2VConfiguration
|
|
62
50
|
from .nm_model import GaussianMixtureNMConfig, MultiChannelNMConfig
|
|
63
51
|
from .training_model import TrainingConfig
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from typing import Annotated, Literal
|
|
4
4
|
|
|
5
|
+
from bioimageio.spec.generic.v0_3 import CiteEntry
|
|
5
6
|
from pydantic import AfterValidator
|
|
6
7
|
|
|
7
8
|
from careamics.config.architectures import UNetModel
|
|
@@ -12,6 +13,23 @@ from careamics.config.validators import (
|
|
|
12
13
|
|
|
13
14
|
from .unet_algorithm_model import UNetBasedAlgorithm
|
|
14
15
|
|
|
16
|
+
CARE = "CARE"
|
|
17
|
+
|
|
18
|
+
CARE_DESCRIPTION = (
|
|
19
|
+
"Content-aware image restoration (CARE) is a deep-learning-based "
|
|
20
|
+
"algorithm that uses a U-Net architecture to restore images. CARE "
|
|
21
|
+
"is a supervised algorithm that requires pairs of noisy and "
|
|
22
|
+
"clean images to train the network. The algorithm learns to "
|
|
23
|
+
"predict the clean image from the noisy image. CARE is "
|
|
24
|
+
"particularly useful for denoising images acquired in low-light "
|
|
25
|
+
"conditions, such as fluorescence microscopy images."
|
|
26
|
+
)
|
|
27
|
+
CARE_REF = CiteEntry(
|
|
28
|
+
text='Weigert, Martin, et al. "Content-aware image restoration: pushing the '
|
|
29
|
+
'limits of fluorescence microscopy." Nature methods 15.12 (2018): 1090-1097.',
|
|
30
|
+
doi="10.1038/s41592-018-0216-7",
|
|
31
|
+
)
|
|
32
|
+
|
|
15
33
|
|
|
16
34
|
class CAREAlgorithm(UNetBasedAlgorithm):
|
|
17
35
|
"""CARE algorithm configuration.
|
|
@@ -36,3 +54,69 @@ class CAREAlgorithm(UNetBasedAlgorithm):
|
|
|
36
54
|
AfterValidator(model_without_final_activation),
|
|
37
55
|
]
|
|
38
56
|
"""UNet without a final activation function and without the `n2v2` modifications."""
|
|
57
|
+
|
|
58
|
+
def get_algorithm_friendly_name(self) -> str:
|
|
59
|
+
"""
|
|
60
|
+
Get the algorithm friendly name.
|
|
61
|
+
|
|
62
|
+
Returns
|
|
63
|
+
-------
|
|
64
|
+
str
|
|
65
|
+
Friendly name of the algorithm.
|
|
66
|
+
"""
|
|
67
|
+
return CARE
|
|
68
|
+
|
|
69
|
+
def get_algorithm_keywords(self) -> list[str]:
|
|
70
|
+
"""
|
|
71
|
+
Get algorithm keywords.
|
|
72
|
+
|
|
73
|
+
Returns
|
|
74
|
+
-------
|
|
75
|
+
list[str]
|
|
76
|
+
List of keywords.
|
|
77
|
+
"""
|
|
78
|
+
return [
|
|
79
|
+
"restoration",
|
|
80
|
+
"UNet",
|
|
81
|
+
"3D" if self.model.is_3D() else "2D",
|
|
82
|
+
"CAREamics",
|
|
83
|
+
"pytorch",
|
|
84
|
+
CARE,
|
|
85
|
+
]
|
|
86
|
+
|
|
87
|
+
def get_algorithm_references(self) -> str:
|
|
88
|
+
"""
|
|
89
|
+
Get the algorithm references.
|
|
90
|
+
|
|
91
|
+
This is used to generate the README of the BioImage Model Zoo export.
|
|
92
|
+
|
|
93
|
+
Returns
|
|
94
|
+
-------
|
|
95
|
+
str
|
|
96
|
+
Algorithm references.
|
|
97
|
+
"""
|
|
98
|
+
return CARE_REF.text + " doi: " + CARE_REF.doi
|
|
99
|
+
|
|
100
|
+
def get_algorithm_citations(self) -> list[CiteEntry]:
|
|
101
|
+
"""
|
|
102
|
+
Return a list of citation entries of the current algorithm.
|
|
103
|
+
|
|
104
|
+
This is used to generate the model description for the BioImage Model Zoo.
|
|
105
|
+
|
|
106
|
+
Returns
|
|
107
|
+
-------
|
|
108
|
+
List[CiteEntry]
|
|
109
|
+
List of citation entries.
|
|
110
|
+
"""
|
|
111
|
+
return [CARE_REF]
|
|
112
|
+
|
|
113
|
+
def get_algorithm_description(self) -> str:
|
|
114
|
+
"""
|
|
115
|
+
Get the algorithm description.
|
|
116
|
+
|
|
117
|
+
Returns
|
|
118
|
+
-------
|
|
119
|
+
str
|
|
120
|
+
Algorithm description.
|
|
121
|
+
"""
|
|
122
|
+
return CARE_DESCRIPTION
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
from typing import Annotated, Literal
|
|
4
4
|
|
|
5
|
+
from bioimageio.spec.generic.v0_3 import CiteEntry
|
|
5
6
|
from pydantic import AfterValidator
|
|
6
7
|
|
|
7
8
|
from careamics.config.architectures import UNetModel
|
|
@@ -12,6 +13,24 @@ from careamics.config.validators import (
|
|
|
12
13
|
|
|
13
14
|
from .unet_algorithm_model import UNetBasedAlgorithm
|
|
14
15
|
|
|
16
|
+
N2N = "Noise2Noise"
|
|
17
|
+
|
|
18
|
+
N2N_DESCRIPTION = (
|
|
19
|
+
"Noise2Noise is a deep-learning-based algorithm that uses a U-Net "
|
|
20
|
+
"architecture to restore images. Noise2Noise is a self-supervised "
|
|
21
|
+
"algorithm that requires only noisy images to train the network. "
|
|
22
|
+
"The algorithm learns to predict the clean image from the noisy "
|
|
23
|
+
"image. Noise2Noise is particularly useful when clean images are "
|
|
24
|
+
"not available for training."
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
N2N_REF = CiteEntry(
|
|
28
|
+
text="Lehtinen, J., Munkberg, J., Hasselgren, J., Laine, S., Karras, T., "
|
|
29
|
+
'Aittala, M. and Aila, T., 2018. "Noise2Noise: Learning image restoration '
|
|
30
|
+
'without clean data". arXiv preprint arXiv:1803.04189.',
|
|
31
|
+
doi="10.48550/arXiv.1803.04189",
|
|
32
|
+
)
|
|
33
|
+
|
|
15
34
|
|
|
16
35
|
class N2NAlgorithm(UNetBasedAlgorithm):
|
|
17
36
|
"""Noise2Noise Algorithm configuration."""
|
|
@@ -28,3 +47,69 @@ class N2NAlgorithm(UNetBasedAlgorithm):
|
|
|
28
47
|
AfterValidator(model_without_final_activation),
|
|
29
48
|
]
|
|
30
49
|
"""UNet without a final activation function and without the `n2v2` modifications."""
|
|
50
|
+
|
|
51
|
+
def get_algorithm_friendly_name(self) -> str:
|
|
52
|
+
"""
|
|
53
|
+
Get the algorithm friendly name.
|
|
54
|
+
|
|
55
|
+
Returns
|
|
56
|
+
-------
|
|
57
|
+
str
|
|
58
|
+
Friendly name of the algorithm.
|
|
59
|
+
"""
|
|
60
|
+
return N2N
|
|
61
|
+
|
|
62
|
+
def get_algorithm_keywords(self) -> list[str]:
|
|
63
|
+
"""
|
|
64
|
+
Get algorithm keywords.
|
|
65
|
+
|
|
66
|
+
Returns
|
|
67
|
+
-------
|
|
68
|
+
list[str]
|
|
69
|
+
List of keywords.
|
|
70
|
+
"""
|
|
71
|
+
return [
|
|
72
|
+
"restoration",
|
|
73
|
+
"UNet",
|
|
74
|
+
"3D" if self.model.is_3D() else "2D",
|
|
75
|
+
"CAREamics",
|
|
76
|
+
"pytorch",
|
|
77
|
+
N2N,
|
|
78
|
+
]
|
|
79
|
+
|
|
80
|
+
def get_algorithm_references(self) -> str:
|
|
81
|
+
"""
|
|
82
|
+
Get the algorithm references.
|
|
83
|
+
|
|
84
|
+
This is used to generate the README of the BioImage Model Zoo export.
|
|
85
|
+
|
|
86
|
+
Returns
|
|
87
|
+
-------
|
|
88
|
+
str
|
|
89
|
+
Algorithm references.
|
|
90
|
+
"""
|
|
91
|
+
return N2N_REF.text + " doi: " + N2N_REF.doi
|
|
92
|
+
|
|
93
|
+
def get_algorithm_citations(self) -> list[CiteEntry]:
|
|
94
|
+
"""
|
|
95
|
+
Return a list of citation entries of the current algorithm.
|
|
96
|
+
|
|
97
|
+
This is used to generate the model description for the BioImage Model Zoo.
|
|
98
|
+
|
|
99
|
+
Returns
|
|
100
|
+
-------
|
|
101
|
+
List[CiteEntry]
|
|
102
|
+
List of citation entries.
|
|
103
|
+
"""
|
|
104
|
+
return [N2N_REF]
|
|
105
|
+
|
|
106
|
+
def get_algorithm_description(self) -> str:
|
|
107
|
+
"""
|
|
108
|
+
Get the algorithm description.
|
|
109
|
+
|
|
110
|
+
Returns
|
|
111
|
+
-------
|
|
112
|
+
str
|
|
113
|
+
Algorithm description.
|
|
114
|
+
"""
|
|
115
|
+
return N2N_DESCRIPTION
|
|
@@ -2,9 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
from typing import Annotated, Literal
|
|
4
4
|
|
|
5
|
-
from
|
|
5
|
+
from bioimageio.spec.generic.v0_3 import CiteEntry
|
|
6
|
+
from pydantic import AfterValidator, ConfigDict, model_validator
|
|
7
|
+
from typing_extensions import Self
|
|
6
8
|
|
|
7
9
|
from careamics.config.architectures import UNetModel
|
|
10
|
+
from careamics.config.support import SupportedPixelManipulation, SupportedStructAxis
|
|
11
|
+
from careamics.config.transformations import N2VManipulateModel
|
|
8
12
|
from careamics.config.validators import (
|
|
9
13
|
model_matching_in_out_channels,
|
|
10
14
|
model_without_final_activation,
|
|
@@ -12,18 +16,282 @@ from careamics.config.validators import (
|
|
|
12
16
|
|
|
13
17
|
from .unet_algorithm_model import UNetBasedAlgorithm
|
|
14
18
|
|
|
19
|
+
N2V = "Noise2Void"
|
|
20
|
+
N2V2 = "N2V2"
|
|
21
|
+
STRUCT_N2V = "StructN2V"
|
|
22
|
+
STRUCT_N2V2 = "StructN2V2"
|
|
23
|
+
|
|
24
|
+
N2V_REF = CiteEntry(
|
|
25
|
+
text='Krull, A., Buchholz, T.O. and Jug, F., 2019. "Noise2Void - Learning '
|
|
26
|
+
'denoising from single noisy images". In Proceedings of the IEEE/CVF '
|
|
27
|
+
"conference on computer vision and pattern recognition (pp. 2129-2137).",
|
|
28
|
+
doi="10.1109/cvpr.2019.00223",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
N2V2_REF = CiteEntry(
|
|
32
|
+
text="Höck, E., Buchholz, T.O., Brachmann, A., Jug, F. and Freytag, A., "
|
|
33
|
+
'2022. "N2V2 - Fixing Noise2Void checkerboard artifacts with modified '
|
|
34
|
+
'sampling strategies and a tweaked network architecture". In European '
|
|
35
|
+
"Conference on Computer Vision (pp. 503-518).",
|
|
36
|
+
doi="10.1007/978-3-031-25069-9_33",
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
STRUCTN2V_REF = CiteEntry(
|
|
40
|
+
text="Broaddus, C., Krull, A., Weigert, M., Schmidt, U. and Myers, G., 2020."
|
|
41
|
+
'"Removing structured noise with self-supervised blind-spot '
|
|
42
|
+
'networks". In 2020 IEEE 17th International Symposium on Biomedical '
|
|
43
|
+
"Imaging (ISBI) (pp. 159-163).",
|
|
44
|
+
doi="10.1109/isbi45749.2020.9098336",
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
N2V_DESCRIPTION = (
|
|
48
|
+
"Noise2Void is a UNet-based self-supervised algorithm that "
|
|
49
|
+
"uses blind-spot training to denoise images. In short, in every "
|
|
50
|
+
"patches during training, random pixels are selected and their "
|
|
51
|
+
"value replaced by a neighboring pixel value. The network is then "
|
|
52
|
+
"trained to predict the original pixel value. The algorithm "
|
|
53
|
+
"relies on the continuity of the signal (neighboring pixels have "
|
|
54
|
+
"similar values) and the pixel-wise independence of the noise "
|
|
55
|
+
"(the noise in a pixel is not correlated with the noise in "
|
|
56
|
+
"neighboring pixels)."
|
|
57
|
+
)
|
|
58
|
+
|
|
59
|
+
N2V2_DESCRIPTION = (
|
|
60
|
+
"N2V2 is a variant of Noise2Void. "
|
|
61
|
+
+ N2V_DESCRIPTION
|
|
62
|
+
+ "\nN2V2 introduces blur-pool layers and removed skip "
|
|
63
|
+
"connections in the UNet architecture to remove checkboard "
|
|
64
|
+
"artefacts, a common artefacts ocurring in Noise2Void."
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
STR_N2V_DESCRIPTION = (
|
|
68
|
+
"StructN2V is a variant of Noise2Void. "
|
|
69
|
+
+ N2V_DESCRIPTION
|
|
70
|
+
+ "\nStructN2V uses a linear mask (horizontal or vertical) to replace "
|
|
71
|
+
"the pixel values of neighbors of the masked pixels by a random "
|
|
72
|
+
"value. Such masking allows removing 1D structured noise from the "
|
|
73
|
+
"the images, the main failure case of the original N2V."
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
STR_N2V2_DESCRIPTION = (
|
|
77
|
+
"StructN2V2 is a a variant of Noise2Void that uses both "
|
|
78
|
+
"structN2V and N2V2. "
|
|
79
|
+
+ N2V_DESCRIPTION
|
|
80
|
+
+ "\nStructN2V2 uses a linear mask (horizontal or vertical) to replace "
|
|
81
|
+
"the pixel values of neighbors of the masked pixels by a random "
|
|
82
|
+
"value. Such masking allows removing 1D structured noise from the "
|
|
83
|
+
"the images, the main failure case of the original N2V."
|
|
84
|
+
"\nN2V2 introduces blur-pool layers and removed skip connections in "
|
|
85
|
+
"the UNet architecture to remove checkboard artefacts, a common "
|
|
86
|
+
"artefacts ocurring in Noise2Void."
|
|
87
|
+
)
|
|
88
|
+
|
|
15
89
|
|
|
16
90
|
class N2VAlgorithm(UNetBasedAlgorithm):
|
|
17
91
|
"""N2V Algorithm configuration."""
|
|
18
92
|
|
|
93
|
+
model_config = ConfigDict(validate_assignment=True)
|
|
94
|
+
|
|
19
95
|
algorithm: Literal["n2v"] = "n2v"
|
|
20
96
|
"""N2V Algorithm name."""
|
|
21
97
|
|
|
22
98
|
loss: Literal["n2v"] = "n2v"
|
|
23
99
|
"""N2V loss function."""
|
|
24
100
|
|
|
101
|
+
n2v_config: N2VManipulateModel = N2VManipulateModel()
|
|
102
|
+
|
|
25
103
|
model: Annotated[
|
|
26
104
|
UNetModel,
|
|
27
105
|
AfterValidator(model_matching_in_out_channels),
|
|
28
106
|
AfterValidator(model_without_final_activation),
|
|
29
107
|
]
|
|
108
|
+
|
|
109
|
+
@model_validator(mode="after")
|
|
110
|
+
def validate_n2v2(self) -> Self:
|
|
111
|
+
"""Validate that the N2V2 strategy and models are set correctly.
|
|
112
|
+
|
|
113
|
+
Returns
|
|
114
|
+
-------
|
|
115
|
+
Self
|
|
116
|
+
The validateed configuration.
|
|
117
|
+
|
|
118
|
+
Raises
|
|
119
|
+
------
|
|
120
|
+
ValueError
|
|
121
|
+
If N2V2 is used with the wrong pixel manipulation strategy.
|
|
122
|
+
"""
|
|
123
|
+
if self.model.n2v2:
|
|
124
|
+
if self.n2v_config.strategy != SupportedPixelManipulation.MEDIAN.value:
|
|
125
|
+
raise ValueError(
|
|
126
|
+
f"N2V2 can only be used with the "
|
|
127
|
+
f"{SupportedPixelManipulation.MEDIAN} pixel manipulation strategy. "
|
|
128
|
+
f"Change the `strategy` parameters in `n2v_config` to "
|
|
129
|
+
f"{SupportedPixelManipulation.MEDIAN}."
|
|
130
|
+
)
|
|
131
|
+
else:
|
|
132
|
+
if self.n2v_config.strategy != SupportedPixelManipulation.UNIFORM.value:
|
|
133
|
+
raise ValueError(
|
|
134
|
+
f"N2V can only be used with the "
|
|
135
|
+
f"{SupportedPixelManipulation.UNIFORM} pixel manipulation strategy."
|
|
136
|
+
f" Change the `strategy` parameters in `n2v_config` to "
|
|
137
|
+
f"{SupportedPixelManipulation.UNIFORM}."
|
|
138
|
+
)
|
|
139
|
+
return self
|
|
140
|
+
|
|
141
|
+
def set_n2v2(self, use_n2v2: bool) -> None:
|
|
142
|
+
"""
|
|
143
|
+
Set the configuration to use N2V2 or the vanilla Noise2Void.
|
|
144
|
+
|
|
145
|
+
This method ensures that N2V2 is set correctly and remain coherent, as opposed
|
|
146
|
+
to setting the different parameters individually.
|
|
147
|
+
|
|
148
|
+
Parameters
|
|
149
|
+
----------
|
|
150
|
+
use_n2v2 : bool
|
|
151
|
+
Whether to use N2V2.
|
|
152
|
+
"""
|
|
153
|
+
if use_n2v2:
|
|
154
|
+
self.n2v_config.strategy = SupportedPixelManipulation.MEDIAN.value
|
|
155
|
+
self.model.n2v2 = True
|
|
156
|
+
else:
|
|
157
|
+
self.n2v_config.strategy = SupportedPixelManipulation.UNIFORM.value
|
|
158
|
+
self.model.n2v2 = False
|
|
159
|
+
|
|
160
|
+
def is_struct_n2v(self) -> bool:
|
|
161
|
+
"""Check if the configuration is using structN2V.
|
|
162
|
+
|
|
163
|
+
Returns
|
|
164
|
+
-------
|
|
165
|
+
bool
|
|
166
|
+
Whether the configuration is using structN2V.
|
|
167
|
+
"""
|
|
168
|
+
return self.n2v_config.struct_mask_axis != SupportedStructAxis.NONE.value
|
|
169
|
+
|
|
170
|
+
def get_algorithm_friendly_name(self) -> str:
|
|
171
|
+
"""
|
|
172
|
+
Get the friendly name of the algorithm.
|
|
173
|
+
|
|
174
|
+
Returns
|
|
175
|
+
-------
|
|
176
|
+
str
|
|
177
|
+
Friendly name.
|
|
178
|
+
"""
|
|
179
|
+
use_n2v2 = self.model.n2v2
|
|
180
|
+
use_structN2V = self.is_struct_n2v()
|
|
181
|
+
|
|
182
|
+
if use_n2v2 and use_structN2V:
|
|
183
|
+
return STRUCT_N2V2
|
|
184
|
+
elif use_n2v2:
|
|
185
|
+
return N2V2
|
|
186
|
+
elif use_structN2V:
|
|
187
|
+
return STRUCT_N2V
|
|
188
|
+
else:
|
|
189
|
+
return N2V
|
|
190
|
+
|
|
191
|
+
def get_algorithm_keywords(self) -> list[str]:
|
|
192
|
+
"""
|
|
193
|
+
Get algorithm keywords.
|
|
194
|
+
|
|
195
|
+
Returns
|
|
196
|
+
-------
|
|
197
|
+
list[str]
|
|
198
|
+
List of keywords.
|
|
199
|
+
"""
|
|
200
|
+
use_n2v2 = self.model.n2v2
|
|
201
|
+
use_structN2V = self.is_struct_n2v()
|
|
202
|
+
|
|
203
|
+
keywords = [
|
|
204
|
+
"denoising",
|
|
205
|
+
"restoration",
|
|
206
|
+
"UNet",
|
|
207
|
+
"3D" if self.model.is_3D() else "2D",
|
|
208
|
+
"CAREamics",
|
|
209
|
+
"pytorch",
|
|
210
|
+
N2V,
|
|
211
|
+
]
|
|
212
|
+
|
|
213
|
+
if use_n2v2:
|
|
214
|
+
keywords.append(N2V2)
|
|
215
|
+
if use_structN2V:
|
|
216
|
+
keywords.append(STRUCT_N2V)
|
|
217
|
+
|
|
218
|
+
return keywords
|
|
219
|
+
|
|
220
|
+
def get_algorithm_references(self) -> str:
|
|
221
|
+
"""
|
|
222
|
+
Get the algorithm references.
|
|
223
|
+
|
|
224
|
+
This is used to generate the README of the BioImage Model Zoo export.
|
|
225
|
+
|
|
226
|
+
Returns
|
|
227
|
+
-------
|
|
228
|
+
str
|
|
229
|
+
Algorithm references.
|
|
230
|
+
"""
|
|
231
|
+
use_n2v2 = self.model.n2v2
|
|
232
|
+
use_structN2V = self.is_struct_n2v()
|
|
233
|
+
|
|
234
|
+
references = [
|
|
235
|
+
N2V_REF.text + " doi: " + N2V_REF.doi,
|
|
236
|
+
N2V2_REF.text + " doi: " + N2V2_REF.doi,
|
|
237
|
+
STRUCTN2V_REF.text + " doi: " + STRUCTN2V_REF.doi,
|
|
238
|
+
]
|
|
239
|
+
|
|
240
|
+
# return the (struct)N2V(2) references
|
|
241
|
+
if use_n2v2 and use_structN2V:
|
|
242
|
+
return "\n".join(references)
|
|
243
|
+
elif use_n2v2:
|
|
244
|
+
references.pop(-1)
|
|
245
|
+
return "\n".join(references)
|
|
246
|
+
elif use_structN2V:
|
|
247
|
+
references.pop(-2)
|
|
248
|
+
return "\n".join(references)
|
|
249
|
+
else:
|
|
250
|
+
return references[0]
|
|
251
|
+
|
|
252
|
+
def get_algorithm_citations(self) -> list[CiteEntry]:
|
|
253
|
+
"""
|
|
254
|
+
Return a list of citation entries of the current algorithm.
|
|
255
|
+
|
|
256
|
+
This is used to generate the model description for the BioImage Model Zoo.
|
|
257
|
+
|
|
258
|
+
Returns
|
|
259
|
+
-------
|
|
260
|
+
List[CiteEntry]
|
|
261
|
+
List of citation entries.
|
|
262
|
+
"""
|
|
263
|
+
use_n2v2 = self.model.n2v2
|
|
264
|
+
use_structN2V = self.is_struct_n2v()
|
|
265
|
+
|
|
266
|
+
references = [N2V_REF]
|
|
267
|
+
|
|
268
|
+
if use_n2v2:
|
|
269
|
+
references.append(N2V2_REF)
|
|
270
|
+
|
|
271
|
+
if use_structN2V:
|
|
272
|
+
references.append(STRUCTN2V_REF)
|
|
273
|
+
|
|
274
|
+
return references
|
|
275
|
+
|
|
276
|
+
def get_algorithm_description(self) -> str:
|
|
277
|
+
"""
|
|
278
|
+
Return a description of the algorithm.
|
|
279
|
+
|
|
280
|
+
This method is used to generate the README of the BioImage Model Zoo export.
|
|
281
|
+
|
|
282
|
+
Returns
|
|
283
|
+
-------
|
|
284
|
+
str
|
|
285
|
+
Description of the algorithm.
|
|
286
|
+
"""
|
|
287
|
+
use_n2v2 = self.model.n2v2
|
|
288
|
+
use_structN2V = self.is_struct_n2v()
|
|
289
|
+
|
|
290
|
+
if use_n2v2 and use_structN2V:
|
|
291
|
+
return STR_N2V2_DESCRIPTION
|
|
292
|
+
elif use_n2v2:
|
|
293
|
+
return N2V2_DESCRIPTION
|
|
294
|
+
elif use_structN2V:
|
|
295
|
+
return STR_N2V_DESCRIPTION
|
|
296
|
+
else:
|
|
297
|
+
return N2V_DESCRIPTION
|
|
@@ -10,10 +10,20 @@ from bioimageio.spec.generic.v0_3 import CiteEntry
|
|
|
10
10
|
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
|
11
11
|
from typing_extensions import Self
|
|
12
12
|
|
|
13
|
-
from careamics.config.algorithms import
|
|
14
|
-
|
|
13
|
+
from careamics.config.algorithms import (
|
|
14
|
+
CAREAlgorithm,
|
|
15
|
+
N2NAlgorithm,
|
|
16
|
+
N2VAlgorithm,
|
|
17
|
+
)
|
|
18
|
+
from careamics.config.data import DataConfig
|
|
15
19
|
from careamics.config.training_model import TrainingConfig
|
|
16
20
|
|
|
21
|
+
ALGORITHMS = Union[
|
|
22
|
+
CAREAlgorithm,
|
|
23
|
+
N2NAlgorithm,
|
|
24
|
+
N2VAlgorithm,
|
|
25
|
+
]
|
|
26
|
+
|
|
17
27
|
|
|
18
28
|
class Configuration(BaseModel):
|
|
19
29
|
"""
|
|
@@ -87,7 +97,7 @@ class Configuration(BaseModel):
|
|
|
87
97
|
Examples
|
|
88
98
|
--------
|
|
89
99
|
Minimum example:
|
|
90
|
-
>>> from careamics import
|
|
100
|
+
>>> from careamics import Configuration
|
|
91
101
|
>>> config_dict = {
|
|
92
102
|
... "experiment_name": "N2V_experiment",
|
|
93
103
|
... "algorithm_config": {
|
|
@@ -106,7 +116,7 @@ class Configuration(BaseModel):
|
|
|
106
116
|
... "axes": "SYX",
|
|
107
117
|
... },
|
|
108
118
|
... }
|
|
109
|
-
>>> config =
|
|
119
|
+
>>> config = Configuration(**config_dict)
|
|
110
120
|
"""
|
|
111
121
|
|
|
112
122
|
model_config = ConfigDict(
|
|
@@ -123,13 +133,11 @@ class Configuration(BaseModel):
|
|
|
123
133
|
"""Name of the experiment, used to name logs and checkpoints."""
|
|
124
134
|
|
|
125
135
|
# Sub-configurations
|
|
126
|
-
algorithm_config:
|
|
127
|
-
discriminator="algorithm"
|
|
128
|
-
)
|
|
136
|
+
algorithm_config: ALGORITHMS = Field(discriminator="algorithm")
|
|
129
137
|
"""Algorithm configuration, holding all parameters required to configure the
|
|
130
138
|
model."""
|
|
131
139
|
|
|
132
|
-
data_config:
|
|
140
|
+
data_config: DataConfig
|
|
133
141
|
"""Data configuration, holding all parameters required to configure the training
|
|
134
142
|
data loader."""
|
|
135
143
|
|
|
@@ -233,7 +241,7 @@ class Configuration(BaseModel):
|
|
|
233
241
|
str
|
|
234
242
|
Algorithm name.
|
|
235
243
|
"""
|
|
236
|
-
|
|
244
|
+
return self.algorithm_config.get_algorithm_friendly_name()
|
|
237
245
|
|
|
238
246
|
def get_algorithm_description(self) -> str:
|
|
239
247
|
"""
|
|
@@ -246,7 +254,7 @@ class Configuration(BaseModel):
|
|
|
246
254
|
str
|
|
247
255
|
Description of the algorithm.
|
|
248
256
|
"""
|
|
249
|
-
|
|
257
|
+
return self.algorithm_config.get_algorithm_description()
|
|
250
258
|
|
|
251
259
|
def get_algorithm_citations(self) -> list[CiteEntry]:
|
|
252
260
|
"""
|
|
@@ -259,7 +267,7 @@ class Configuration(BaseModel):
|
|
|
259
267
|
List[CiteEntry]
|
|
260
268
|
List of citation entries.
|
|
261
269
|
"""
|
|
262
|
-
|
|
270
|
+
return self.algorithm_config.get_algorithm_citations()
|
|
263
271
|
|
|
264
272
|
def get_algorithm_references(self) -> str:
|
|
265
273
|
"""
|
|
@@ -272,7 +280,7 @@ class Configuration(BaseModel):
|
|
|
272
280
|
str
|
|
273
281
|
Algorithm references.
|
|
274
282
|
"""
|
|
275
|
-
|
|
283
|
+
return self.algorithm_config.get_algorithm_references()
|
|
276
284
|
|
|
277
285
|
def get_algorithm_keywords(self) -> list[str]:
|
|
278
286
|
"""
|
|
@@ -283,7 +291,7 @@ class Configuration(BaseModel):
|
|
|
283
291
|
list[str]
|
|
284
292
|
List of keywords.
|
|
285
293
|
"""
|
|
286
|
-
return
|
|
294
|
+
return self.algorithm_config.get_algorithm_keywords()
|
|
287
295
|
|
|
288
296
|
def model_dump(
|
|
289
297
|
self,
|