-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cbf971f
commit 3957a1b
Showing
7 changed files
with
273 additions
and
194 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 103 additions & 58 deletions
161
guides/careamist_api/configuration/build_configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,93 +1,138 @@ | ||
#!/usr/bin/env python | ||
# %% | ||
# --8<-- [start:as_dict] | ||
from careamics import Configuration | ||
|
||
config_as_dict = { | ||
"experiment_name": "my_experiment", # (1)! | ||
"algorithm_config": { # (2)! | ||
"algorithm_type": "fcn", | ||
"algorithm": "n2v", | ||
"loss": "n2v", | ||
"model": { # (3)! | ||
"architecture": "UNet", | ||
}, | ||
}, | ||
"data_config": { # (4)! | ||
"data_type": "array", | ||
"patch_size": [128, 128], | ||
"axes": "YX", | ||
}, | ||
"training_config": { | ||
"num_epochs": 1, | ||
}, | ||
} | ||
config = Configuration(**config_as_dict) # (5)! | ||
# --8<-- [end:as_dict] | ||
|
||
# %% | ||
# --8<-- [start:pydantic] | ||
from careamics import Configuration | ||
from careamics.config import ( # (1)! | ||
DataConfig, | ||
FCNAlgorithmConfig, | ||
N2VAlgorithm, | ||
N2VConfiguration, | ||
N2VDataConfig, | ||
TrainingConfig, | ||
configuration_factory, | ||
) | ||
from careamics.config.architectures import UNetModel | ||
from careamics.config.callback_model import CheckpointModel, EarlyStoppingModel | ||
from careamics.config.support import ( | ||
SupportedAlgorithm, | ||
SupportedArchitecture, | ||
SupportedData, | ||
SupportedLogger, | ||
SupportedLoss, | ||
SupportedTransform, | ||
) | ||
from careamics.config.transformations import N2VManipulateModel | ||
|
||
experiment_name = "Pydantic N2V2 example" | ||
|
||
# build AlgorithmConfig for the fully convolutional network | ||
algorithm_model = FCNAlgorithmConfig( # (2)! | ||
algorithm_type="fcn", | ||
algorithm=SupportedAlgorithm.N2V.value, # (3)! | ||
loss=SupportedLoss.N2V.value, | ||
model=UNetModel( # (4)! | ||
architecture=SupportedArchitecture.UNET.value, | ||
in_channels=1, | ||
num_classes=1, | ||
), | ||
from careamics.config.transformations import N2VManipulateModel, XYFlipModel | ||
|
||
experiment_name = "N2V_example" | ||
|
||
# build the model and algorithm configurations | ||
model = UNetModel( | ||
architecture="UNet", # (2)! | ||
num_channels_init=64, # (3)! | ||
depth=3, | ||
# (4)! | ||
) | ||
|
||
algorithm_model = N2VAlgorithm( # (5)! | ||
model=model, | ||
# (6)! | ||
) | ||
|
||
# then the DataConfig | ||
data_model = DataConfig( | ||
# then the N2VDataConfig | ||
data_model = N2VDataConfig( # (7)! | ||
data_type=SupportedData.ARRAY.value, | ||
patch_size=(256, 256), | ||
batch_size=8, | ||
axes="YX", | ||
transforms=[ | ||
{ # (5)! | ||
"name": SupportedTransform.XY_FLIP.value, | ||
}, | ||
N2VManipulateModel( # (6)! | ||
masked_pixel_percentage=0.15, | ||
), | ||
], | ||
dataloader_params={ # (7)! | ||
transforms=[XYFlipModel(flip_y=False), N2VManipulateModel()], # (8)! # (9)! | ||
dataloader_params={ # (10)! | ||
"num_workers": 4, | ||
}, | ||
) | ||
|
||
# then the TrainingConfig | ||
earlystopping = EarlyStoppingModel( | ||
# (11)! | ||
) | ||
|
||
checkpoints = CheckpointModel(every_n_epochs=10) # (12)! | ||
|
||
training_model = TrainingConfig( | ||
num_epochs=30, | ||
logger=SupportedLogger.WANDB.value, | ||
early_stopping_callback=earlystopping, | ||
checkpoint_callback=checkpoints, | ||
# (13)! | ||
) | ||
|
||
# finally, build the Configuration | ||
config = Configuration( # (8)! | ||
config = N2VConfiguration( # (14)! | ||
experiment_name=experiment_name, | ||
algorithm_config=algorithm_model, | ||
data_config=data_model, | ||
training_config=training_model, | ||
) | ||
|
||
# alternatively, use the factory method | ||
config2 = configuration_factory( # (15)! | ||
{ | ||
"experiment_name": experiment_name, | ||
"algorithm_config": algorithm_model, | ||
"data_config": data_model, | ||
"training_config": training_model, | ||
} | ||
) | ||
# --8<-- [end:pydantic] | ||
|
||
if config != config2: | ||
raise ValueError("Configurations are not equal (Pydantic).") | ||
|
||
# %% | ||
# --8<-- [start:as_dict] | ||
from careamics.config import N2VConfiguration, configuration_factory | ||
|
||
config_dict = { | ||
"experiment_name": "N2V_example", | ||
"algorithm_config": { | ||
"algorithm": "n2v", # (1)! | ||
"loss": "n2v", | ||
"model": { | ||
"architecture": "UNet", # (2)! | ||
"num_channels_init": 64, | ||
"depth": 3, | ||
}, | ||
}, | ||
"data_config": { | ||
"data_type": "array", | ||
"patch_size": [256, 256], | ||
"batch_size": 8, | ||
"axes": "YX", | ||
"transforms": [ | ||
{ | ||
"name": "XYFlip", | ||
"flip_y": False, | ||
}, | ||
{ | ||
"name": "N2VManipulate", | ||
}, | ||
], | ||
"dataloader_params": { | ||
"num_workers": 4, | ||
}, | ||
}, | ||
"training_config": { | ||
"num_epochs": 30, | ||
"logger": "wandb", | ||
"early_stopping_callback": {}, # (3)! | ||
"checkpoint_callback": { | ||
"every_n_epochs": 10, | ||
}, | ||
}, | ||
} | ||
|
||
# instantiate specific configuration | ||
config_as_dict = N2VConfiguration(**config_dict) # (4)! | ||
|
||
# alternatively, use the factory method | ||
config_as_dict2 = configuration_factory(config_dict) # (5)! | ||
# --8<-- [end:as_dict] | ||
|
||
if config_as_dict != config_as_dict2: | ||
raise ValueError("Configurations are not equal (Dict).") | ||
|
||
if config != config_as_dict: | ||
raise ValueError("Configurations are not equal (Pydantic vs Dict).") |
33 changes: 33 additions & 0 deletions
33
guides/careamist_api/configuration/configuration_errors.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python | ||
# %% | ||
from careamics.config import create_n2v_configuration | ||
|
||
# %% | ||
# create_n2v_configuration( | ||
# experiment_name="N2V_example}", | ||
# data_type="array", | ||
# axes="YX", | ||
# patch_size=[256, 256, 512], | ||
# batch_size=8, | ||
# num_epochs=30, | ||
# ) | ||
|
||
# %% | ||
create_n2v_configuration( | ||
experiment_name="N2V_example", | ||
data_type="arrray", | ||
axes="YX", | ||
patch_size=[256, 256], | ||
batch_size=8, | ||
num_epochs=30, | ||
) | ||
|
||
# %% | ||
create_n2v_configuration( | ||
experiment_name="N2V_example", | ||
data_type="array", | ||
axes="YX", | ||
patch_size=[256, 256, 512], | ||
batch_size=8, | ||
num_epochs=30, | ||
) |
Oops, something went wrong.