n2v_factory
Convenience function to create N2V configurations.
create_advanced_n2v_config(experiment_name, data_type, axes, patch_size, batch_size, num_epochs=30, num_steps=None, n_channels=None, augmentations=None, in_memory=None, channels=None, independent_channels=True, normalization='mean_std', normalization_params=None, use_n2v2=False, roi_size=11, masked_pixel_percentage=0.2, struct_n2v_axis='none', struct_n2v_span=5, num_workers=0, trainer_params=None, model_params=None, optimizer='Adam', optimizer_params=None, lr_scheduler='ReduceLROnPlateau', lr_scheduler_params=None, train_dataloader_params=None, val_dataloader_params=None, checkpoint_params=None, logger='none', seed=None) #
Create a configuration for training Noise2Void.
N2V uses a UNet model to denoise images in a self-supervised manner. To use its variants structN2V and N2V2, set the struct_n2v_axis and struct_n2v_span (structN2V) parameters, or set use_n2v2 to True (N2V2).
N2V2 modifies the UNet architecture by adding blur pool layers and removes the skip connections, thus removing checkboard artefacts. StructN2V is used when vertical or horizontal correlations are present in the noise; it applies an additional mask to the manipulated pixel neighbors.
If "Z" is present in axes, then patch_size must be a list of length 3, otherwise 2.
If "C" is present in axes, then you need to set n_channels to the number of channels.
By default, all channels are trained independently. To train all channels together, set independent_channels to False.
By default, the augmentations applied are random flips along X or Y, and random 90 degrees rotations in the XY plane. To disable the augmentations, simply pass an empty list.
The roi_size parameter specifies the size of the area around each pixel that will be manipulated by N2V. The masked_pixel_percentage parameter specifies how many pixels per patch will be manipulated.
If you pass "horizontal" or "vertical" to struct_n2v_axis, then structN2V mask will be applied to each manipulated pixel.
The parameters of the UNet can be specified in the model_params (passed as a parameter-value dictionary). Note that use_n2v2 and 'n_channels' override the corresponding parameters passed in model_params.
Note that num_workers is applied to all dataloaders unless explicitly overridden in the respective dataloader parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_name | str | Name of the experiment. | required |
data_type | Literal['array', 'tiff', 'zarr', 'czi', 'custom'] | Type of the data. | required |
axes | str | Axes of the data (e.g. SYX). | required |
patch_size | Sequence[int] | Size of the patches along the spatial dimensions (e.g. [64, 64]). | required |
batch_size | int | Batch size. | required |
num_epochs | int | Number of epochs to train for. If provided, this will be added to trainer_params. | 30 |
num_steps | int | None | Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to | None |
n_channels | int | None | Number of channels (in and out). If | None |
augmentations | Sequence[{x_flip, y_flip, rotate_90}] | None | List of transforms to apply, either both or one of XYFlipConfig and XYRandomRotate90Config. By default, it applies both XYFlip (on X and Y) and XYRandomRotate90 (in XY) to the images. | None |
in_memory | bool | None | Whether to load all data into memory. This is only supported for 'array', 'tiff' and 'custom' data types. If | None |
channels | Sequence[int] | None | List of channels to use. If | None |
independent_channels | bool | Whether to train all channels independently. | True |
normalization | (mean_std, minmax, quantile, none) | Normalization strategy to use. | "mean_std" |
normalization_params | dict[str, Any] | None | Strategy-specific normalization parameters. If None, default values are used. For "mean_std": {"input_means": [...], "input_stds": [...]} (optional) For "minmax": {"input_mins": [...], "input_maxes": [...]} (optional) For "quantile": {"lower_quantile": 0.01, "upper_quantile": 0.99} (optional) For "none": No parameters needed. | None |
use_n2v2 | bool | Whether to use N2V2. | False |
roi_size | int | N2V pixel manipulation area. | 11 |
masked_pixel_percentage | float | Percentage of pixels masked in each patch. | 0.2 |
struct_n2v_axis | Literal['horizontal', 'vertical', 'none'] | Axis along which to apply structN2V mask. | "none" |
struct_n2v_span | int | Span of the structN2V mask. | 5 |
num_workers | int | Number of workers for data loading. Unless explicitly overridden in | 0 |
trainer_params | dict | None | Parameters for the trainer, see the relevant documentation. | None |
model_params | dict | None | UNetModel parameters. | None |
optimizer | Literal['Adam', 'Adamax', 'SGD'] | Optimizer to use. | "Adam" |
optimizer_params | dict[str, Any] | None | Parameters for the optimizer, see PyTorch documentation for more details. | None |
lr_scheduler | Literal['ReduceLROnPlateau', 'StepLR'] | Learning rate scheduler to use. | "ReduceLROnPlateau" |
lr_scheduler_params | dict[str, Any] | None | Parameters for the learning rate scheduler, see PyTorch documentation for more details. | None |
train_dataloader_params | dict[str, Any] | None | Parameters for the training dataloader, see the PyTorch docs for | None |
val_dataloader_params | dict[str, Any] | None | Parameters for the validation dataloader, see PyTorch the docs for | None |
checkpoint_params | dict[str, Any] | None | Parameters for the checkpoint callback, see PyTorch Lightning documentation ( | None |
logger | Literal['wandb', 'tensorboard', 'none'] | Logger to use. | "none" |
seed | int | None | Random seed for reproducibility. | None |
Returns:
| Type | Description |
|---|---|
N2VConfiguration | Configuration for training N2V. |
Source code in src/careamics/config/ng_factories/n2v_factory.py
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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 | |
create_n2v_config(experiment_name, data_type, axes, patch_size, batch_size, num_epochs=30, num_steps=None, augmentations=None, use_n2v2=False, n_channels=None) #
Create a configuration for training N2V.
To activate N2V2, set use_n2v2 to True.
The axes parameters must reflect the actual axes and axis order from the data, and should be the same throughout all images. The accepted axes are STCZYX. If "C" is in axes, then you need to set n_channels to the number of channels.
By default, CAREamics will go through the entire training data once per epoch. For large datasets, this can lead to very long epochs. To limit the number of batches per epoch, set the num_steps parameter to the desired number of batches.
If the content of your data is expected to always have the same orientation, consider disabling certain augmentations. By default augmentations=None will apply random flips along X and Y, and random 90 degrees rotations in the XY plane. To disable augmentations, set augmentations=[].
See create_advanced_n2v_config for more parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_name | str | Name of the experiment. | required |
data_type | Literal['array', 'tiff', 'zarr', 'czi', 'custom'] | Type of the data. | required |
axes | str | Axes of the data (e.g. SYX). | required |
patch_size | Sequence[int] | Size of the patches along the spatial dimensions (e.g. [64, 64]). | required |
batch_size | int | Batch size. | required |
num_epochs | int | Number of epochs to train for. | 30 |
num_steps | int | Number of batches in 1 epoch. | None |
augmentations | Sequence of {"x_flip", "y_flip", "rotate_90"} | List of augmentations to apply. If | None |
use_n2v2 | bool | Whether to use N2V2. | False |
n_channels | int or None | Number of channels (in and out). | None |
Returns:
| Type | Description |
|---|---|
N2VConfiguration | Configuration for training N2V. |
Source code in src/careamics/config/ng_factories/n2v_factory.py
create_structn2v_config(experiment_name, data_type, axes, patch_size, batch_size, struct_n2v_axis, struct_n2v_span=5, num_epochs=30, num_steps=None, use_n2v2=False, n_channels=None) #
Create a configuration for training structN2V.
The structN2V mask is applied a horizontal or vertical axis, with extent defined by struct_n2v_span (default=5, leading to a mask of size 11). For structN2V, augmentations are disabled.
To activate N2V2, set use_n2v2 to True.
The axes parameters must reflect the actual axes and axis order from the data, and should be the same throughout all images. The accepted axes are STCZYX. If "C" is in axes, then you need to set n_channels to the number of channels.
patch_size is only along the spatial dimensions and should be of length 3 if "Z" is present in axes, otherwise of length 2.
By default, CAREamics will go through the entire training data once per epoch. For large datasets, this can lead to very long epochs. To limit the number of batches per epoch, set the num_steps parameter to the desired number of batches.
See create_advanced_n2v_config for more parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
experiment_name | str | Name of the experiment. | required |
data_type | Literal['array', 'tiff', 'zarr', 'czi', 'custom'] | Type of the data. | required |
axes | str | Axes of the data (e.g. SYX). | required |
patch_size | Sequence[int] | Size of the patches along the spatial dimensions (e.g. [64, 64]). | required |
batch_size | int | Batch size. | required |
struct_n2v_axis | Literal['horizontal', 'vertical'] | Axis along which to apply structN2V mask. | required |
struct_n2v_span | int | Span of the structN2V mask. | 5 |
num_epochs | int | Number of epochs to train for. | 30 |
num_steps | int | Number of batches in 1 epoch. | None |
use_n2v2 | bool | Whether to use N2V2. | False |
n_channels | int or None | Number of channels (in and out). | None |
Returns:
| Type | Description |
|---|---|
N2VConfiguration | Configuration for training structN2V. |
Source code in src/careamics/config/ng_factories/n2v_factory.py
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 | |