configuration
Pydantic CAREamics configuration.
Configuration
#
Bases: BaseModel
CAREamics configuration.
The configuration defines all parameters used to build and train a CAREamics model. These parameters are validated to ensure that they are compatible with each other.
It contains three sub-configurations:
- AlgorithmModel: configuration for the algorithm training, which includes the architecture, loss function, optimizer, and other hyperparameters.
- DataModel: configuration for the dataloader, which includes the type of data, transformations, mean/std and other parameters.
- TrainingModel: configuration for the training, which includes the number of epochs or the callbacks.
Attributes:
Name | Type | Description |
---|---|---|
experiment_name | str | Name of the experiment, used when saving logs and checkpoints. |
algorithm | AlgorithmModel | Algorithm configuration. |
data | DataModel | Data configuration. |
training | TrainingModel | Training configuration. |
Methods:
Name | Description |
---|---|
set_3D | Switch configuration between 2D and 3D. |
model_dump | exclude_defaults: bool = False, exclude_none: bool = True, **kwargs: Dict ) -> Dict Export configuration to a dictionary. |
Raises:
Type | Description |
---|---|
ValueError | Configuration parameter type validation errors. |
ValueError | If the experiment name contains invalid characters or is empty. |
ValueError | If the algorithm is 3D but there is not "Z" in the data axes, or 2D algorithm with "Z" in data axes. |
ValueError | Algorithm, data or training validation errors. |
Notes
We provide convenience methods to create standards configurations, for instance:
from careamics.config import create_n2v_configuration config = create_n2v_configuration( ... experiment_name="n2v_experiment", ... data_type="array", ... axes="YX", ... patch_size=[64, 64], ... batch_size=32, ... num_epochs=100 ... )
The configuration can be exported to a dictionary using the model_dump method:
config_dict = config.model_dump()
Configurations can also be exported or imported from yaml files:
from careamics.config import save_configuration, load_configuration path_to_config = save_configuration(config, my_path / "config.yml") other_config = load_configuration(path_to_config)
Examples:
Minimum example:
>>> from careamics import Configuration
>>> config_dict = {
... "experiment_name": "N2V_experiment",
... "algorithm_config": {
... "algorithm": "n2v",
... "loss": "n2v",
... "model": {
... "architecture": "UNet",
... },
... },
... "training_config": {
... "num_epochs": 200,
... },
... "data_config": {
... "data_type": "tiff",
... "patch_size": [64, 64],
... "axes": "SYX",
... },
... }
>>> config = Configuration(**config_dict)
Source code in src/careamics/config/configuration.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
algorithm_config = Field(discriminator='algorithm')
class-attribute
instance-attribute
#
Algorithm configuration, holding all parameters required to configure the model.
data_config
instance-attribute
#
Data configuration, holding all parameters required to configure the training data loader.
experiment_name
instance-attribute
#
Name of the experiment, used to name logs and checkpoints.
training_config
instance-attribute
#
Training configuration, holding all parameters required to configure the training process.
version = '0.1.0'
class-attribute
instance-attribute
#
CAREamics configuration version.
__str__()
#
Pretty string reprensenting the configuration.
Returns:
Type | Description |
---|---|
str | Pretty string. |
get_algorithm_citations()
#
Return a list of citation entries of the current algorithm.
This is used to generate the model description for the BioImage Model Zoo.
Returns:
Type | Description |
---|---|
List[CiteEntry] | List of citation entries. |
Source code in src/careamics/config/configuration.py
get_algorithm_description()
#
Return a description of the algorithm.
This method is used to generate the README of the BioImage Model Zoo export.
Returns:
Type | Description |
---|---|
str | Description of the algorithm. |
Source code in src/careamics/config/configuration.py
get_algorithm_friendly_name()
#
Get the algorithm name.
Returns:
Type | Description |
---|---|
str | Algorithm name. |
get_algorithm_keywords()
#
Get algorithm keywords.
Returns:
Type | Description |
---|---|
list[str] | List of keywords. |
get_algorithm_references()
#
Get the algorithm references.
This is used to generate the README of the BioImage Model Zoo export.
Returns:
Type | Description |
---|---|
str | Algorithm references. |
Source code in src/careamics/config/configuration.py
model_dump(*, mode='python', include=None, exclude=None, context=None, by_alias=False, exclude_unset=False, exclude_defaults=False, exclude_none=True, round_trip=False, warnings=True, serialize_as_any=False)
#
Override model_dump method in order to set default values.
As opposed to the parent model_dump method, this method sets exclude none by default.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mode | Literal['json', 'python'] | str | The serialization format. | 'python' |
include | Any | None | Attributes to include. | None |
exclude | Any | None | Attributes to exclude. | None |
context | Any | None | Additional context to pass to the serialization functions. | None |
by_alias | bool | Whether to use attribute aliases. | False |
exclude_unset | bool | Whether to exclude fields that are not set. | False |
exclude_defaults | bool | Whether to exclude fields that have default values. | False |
exclude_none | bool | Whether to exclude fields that have None values. | true |
round_trip | bool | Whether to dump and load the data to ensure that the output is a valid representation. | False |
warnings | bool | Literal['none', 'warn', 'error'] | Whether to emit warnings. | True |
serialize_as_any | bool | Whether to serialize all types as Any. | False |
Returns:
Type | Description |
---|---|
dict | Dictionary containing the model parameters. |
Source code in src/careamics/config/configuration.py
no_symbol(name)
classmethod
#
Validate experiment name.
A valid experiment name is a non-empty string with only contains letters, numbers, underscores, dashes and spaces.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name | str | Name to validate. | required |
Returns:
Type | Description |
---|---|
str | Validated name. |
Raises:
Type | Description |
---|---|
ValueError | If the name is empty or contains invalid characters. |
Source code in src/careamics/config/configuration.py
set_3D(is_3D, axes, patch_size)
#
Set 3D flag and axes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
is_3D | bool | Whether the algorithm is 3D or not. | required |
axes | str | Axes of the data. | required |
patch_size | list[int] | Patch size. | required |
Source code in src/careamics/config/configuration.py
validate_3D()
#
Change algorithm dimensions to match data.axes.
Returns:
Type | Description |
---|---|
Self | Validated configuration. |