Skip to content

Configuration Factories

Source

Convenience functions to create configurations for training and inference.

algorithm_factory(algorithm)

Create an algorithm model for training CAREamics.

Parameters:

Name Type Description Default
algorithm dict

Algorithm dictionary.

required

Returns:

Type Description
N2VAlgorithm or N2NAlgorithm or CAREAlgorithm or PN2VAlgorithm

Algorithm model for training CAREamics.

create_care_configuration(experiment_name, data_type, axes, patch_size, batch_size, num_epochs=100, num_steps=None, augmentations=None, independent_channels=True, loss='mae', n_channels_in=None, n_channels_out=None, logger='none', 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)

Create a configuration for training CARE.

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_in to the number of channels. Likewise, if you set the number of channels, then "C" must be present in axes.

To set the number of output channels, use the n_channels_out parameter. If it is not specified, it will be assumed to be equal to n_channels_in.

By default, all channels are trained together. To train all channels independently, set independent_channels to True.

By setting augmentations to None, the default augmentations (flip in X and Y, rotations by 90 degrees in the XY plane) are applied. Rather than the default augmentations, a list of augmentations can be passed to the augmentations parameter. To disable the augmentations, simply pass an empty list.

Parameters:

Name Type Description Default
experiment_name str

Name of the experiment.

required
data_type Literal['array', 'tiff', 'czi', 'custom']

Type of the data.

required
axes str

Axes of the data (e.g. SYX).

required
patch_size List[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.

100
num_steps int

Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to limit_train_batches in PyTorch Lightning Trainer. See relevant documentation for more details.

None
augmentations list of augmentations

List of augmentations 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
independent_channels bool

Whether to train all channels independently, by default False.

True
loss Literal['mae', 'mse']

Loss function to use.

"mae"
n_channels_in int or None

Number of channels in.

None
n_channels_out int or None

Number of channels out.

None
logger Literal['wandb', 'tensorboard', 'none']

Logger to use.

"none"
trainer_params dict

Parameters for the trainer class, see PyTorch Lightning documentation.

None
model_params dict

UNetModel parameters.

None
optimizer Literal['Adam', 'Adamax', 'SGD']

Optimizer to use.

"Adam"
optimizer_params dict

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

Parameters for the learning rate scheduler, see PyTorch documentation for more details.

None
train_dataloader_params dict

Parameters for the training dataloader, see the PyTorch docs for DataLoader. If left as None, the dict {"shuffle": True} will be used, this is set in the GeneralDataConfig.

None
val_dataloader_params dict

Parameters for the validation dataloader, see PyTorch the docs for DataLoader. If left as None, the empty dict {} will be used, this is set in the GeneralDataConfig.

None
checkpoint_params dict

Parameters for the checkpoint callback, see PyTorch Lightning documentation (ModelCheckpoint) for the list of available parameters.

None

Returns:

Type Description
Configuration

Configuration for training CARE.

Examples:

Minimum example:

>>> config = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100
... )

You can also limit the number of batches per epoch:

>>> config = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_steps=100  # limit to 100 batches per epoch
... )

To disable augmentations, simply set augmentations to an empty list:

>>> config = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     augmentations=[]
... )

A list of augmentations can be passed to the augmentations parameter: to replace the default augmentations:

>>> from careamics.config.augmentations import XYFlipConfig
>>> config = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     augmentations=[
...         # No rotation and only Y flipping
...         XYFlipConfig(flip_x = False, flip_y = True)
...     ]
... )

If you are training multiple channels they will be trained independently by default, you simply need to specify the number of channels input (and optionally, the number of channels output):

>>> config = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="array",
...     axes="YXC", # channels must be in the axes
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     n_channels_in=3, # number of input channels
...     n_channels_out=1 # if applicable
... )

If instead you want to train multiple channels together, you need to turn off the independent_channels parameter:

>>> config = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="array",
...     axes="YXC", # channels must be in the axes
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     independent_channels=False,
...     n_channels_in=3,
...     n_channels_out=1 # if applicable
... )

If you would like to train on CZI files, use "czi" as data_type and "SCYX" as axes for 2-D or "SCZYX" for 3-D denoising. Note that "SCYX" can also be used for 3-D data but spatial context along the Z dimension will then not be taken into account.

>>> config_2d = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="czi",
...     axes="SCYX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     n_channels_in=1,
... )
>>> config_3d = create_care_configuration(
...     experiment_name="care_experiment",
...     data_type="czi",
...     axes="SCZYX",
...     patch_size=[16, 64, 64],
...     batch_size=16,
...     num_epochs=100,
...     n_channels_in=1,
... )

create_hdn_configuration(experiment_name, data_type, axes, patch_size, batch_size, num_epochs=100, num_steps=None, encoder_conv_strides=(2, 2), decoder_conv_strides=(2, 2), multiscale_count=1, z_dims=(128, 128), output_channels=1, encoder_n_filters=32, decoder_n_filters=32, encoder_dropout=0.0, decoder_dropout=0.0, nonlinearity='ReLU', analytical_kl=False, predict_logvar=None, logvar_lowerbound=None, logger='none', trainer_params=None, augmentations=None, train_dataloader_params=None, val_dataloader_params=None)

Create a configuration for training HDN.

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_in to the number of channels. Likewise, if you set the number of channels, then "C" must be present in axes.

To set the number of output channels, use the n_channels_out parameter. If it is not specified, it will be assumed to be equal to n_channels_in.

By default, all channels are trained independently. To train all channels together, set independent_channels to False.

By setting augmentations to None, the default augmentations (flip in X and Y, rotations by 90 degrees in the XY plane) are applied. Rather than the default augmentations, a list of augmentations can be passed to the augmentations parameter. To disable the augmentations, simply pass an empty list.

TODO revisit the necessity of model_params

Parameters:

Name Type Description Default
experiment_name str

Name of the experiment.

required
data_type Literal['array', 'tiff', 'custom']

Type of the data.

required
axes str

Axes of the data (e.g. SYX).

required
patch_size List[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.

100
num_steps int

Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to limit_train_batches in PyTorch Lightning Trainer. See relevant documentation for more details.

None
encoder_conv_strides tuple[int, ...]

Strides for the encoder convolutional layers, by default (2, 2).

(2, 2)
decoder_conv_strides tuple[int, ...]

Strides for the decoder convolutional layers, by default (2, 2).

(2, 2)
multiscale_count int

Number of scales in the multiscale architecture, by default 1.

1
z_dims tuple[int, ...]

Dimensions of the latent space, by default (128, 128).

(128, 128)
output_channels int

Number of output channels, by default 1.

1
encoder_n_filters int

Number of filters in the encoder, by default 32.

32
decoder_n_filters int

Number of filters in the decoder, by default 32.

32
encoder_dropout float

Dropout rate for the encoder, by default 0.0.

0.0
decoder_dropout float

Dropout rate for the decoder, by default 0.0.

0.0
nonlinearity Literal

Nonlinearity function to use, by default "ReLU".

'ReLU'
analytical_kl bool

Whether to use analytical KL divergence, by default False.

False
predict_logvar Literal[None, 'pixelwise']

Type of log variance prediction, by default None.

None
logvar_lowerbound Union[float, None]

Lower bound for the log variance, by default None.

None
logger Literal['wandb', 'tensorboard', 'none']

Logger to use for training, by default "none".

'none'
trainer_params dict

Parameters for the trainer class, see PyTorch Lightning documentation.

None
augmentations list[XYFlipConfig | XYRandomRotate90Config] | None

List of augmentations to apply, by default None.

None
train_dataloader_params Optional[dict[str, Any]]

Parameters for the training dataloader, by default None.

None
val_dataloader_params Optional[dict[str, Any]]

Parameters for the validation dataloader, by default None.

None

Returns:

Type Description
Configuration

The configuration object for training HDN.

Examples:

Minimum example:

>>> config = create_hdn_configuration(
...     experiment_name="hdn_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100
... )

You can also limit the number of batches per epoch:

>>> config = create_hdn_configuration(
...     experiment_name="hdn_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_steps=100  # limit to 100 batches per epoch
... )

create_microsplit_configuration(experiment_name, data_type, axes, patch_size, batch_size, lr=0.001, num_epochs=100, num_steps=None, encoder_conv_strides=(2, 2), decoder_conv_strides=(2, 2), encoder_n_filters=32, decoder_n_filters=32, multiscale_count=3, grid_size=32, z_dims=(128, 128), output_channels=1, encoder_dropout=0.1, decoder_dropout=0.1, nonlinearity='ELU', analytical_kl=False, predict_logvar='pixelwise', logvar_lowerbound=-5.0, loss_type='denoisplit_musplit', kl_type='kl_restricted', reconstruction_weight=1.0, kl_weight=1.0, musplit_weight=0.1, denoisplit_weight=0.9, mmse_count=10, optimizer='Adamax', lr_scheduler_patience=30, logger='none', trainer_params=None, augmentations=None, nm_paths=None, data_stats=None, train_dataloader_params=None, val_dataloader_params=None)

Create a configuration for training MicroSplit.

Parameters:

Name Type Description Default
experiment_name str

Name of the experiment.

required
data_type Literal['array', 'tiff', '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
lr float

Learning rate, by default 1e-3.

0.001
num_epochs int

Number of epochs to train for. If provided, this will be added to trainer_params.

100
num_steps int

Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to limit_train_batches in PyTorch Lightning Trainer. See relevant documentation for more details.

None
encoder_conv_strides tuple[int, ...]

Strides for the encoder convolutional layers, by default (2, 2).

(2, 2)
decoder_conv_strides tuple[int, ...]

Strides for the decoder convolutional layers, by default (2, 2).

(2, 2)
encoder_n_filters int

Number of filters in the encoder, by default 32.

32
decoder_n_filters int

Number of filters in the decoder, by default 32.

32
multiscale_count int

Number of multiscale levels, by default 3.

3
grid_size int

Size of the grid for multiscale training, by default 32.

32
z_dims tuple[int, ...]

List of latent dims for each hierarchy level in the LVAE, default (128, 128).

(128, 128)
output_channels int

Number of output channels for the model, by default 1.

1
encoder_dropout float

Dropout rate for the encoder, by default 0.0.

0.1
decoder_dropout float

Dropout rate for the decoder, by default 0.0.

0.1
nonlinearity Literal

Nonlinearity to use in the model, by default "ELU".

'ELU'
analytical_kl bool

Whether to use analytical KL divergence, by default False.

False
predict_logvar Literal['pixelwise']

Type of log-variance prediction, by default "pixelwise".

'pixelwise'
logvar_lowerbound float | None

Lower bound for the log variance, by default -5.0.

-5.0
loss_type Literal['musplit', 'denoisplit', 'denoisplit_musplit']

Type of loss function, by default "denoisplit_musplit".

'denoisplit_musplit'
kl_type Literal['kl', 'kl_restricted']

Type of KL divergence, by default "kl_restricted".

'kl_restricted'
reconstruction_weight float

Weight for reconstruction loss, by default 1.0.

1.0
kl_weight float

Weight for KL loss, by default 1.0.

1.0
musplit_weight float

Weight for muSplit loss, by default 0.1.

0.1
denoisplit_weight float

Weight for denoiSplit loss, by default 0.9.

0.9
mmse_count int

Number of MMSE samples to use, by default 10.

10
optimizer Literal['Adam', 'SGD', 'Adamax']

Optimizer to use, by default "Adamax".

'Adamax'
lr_scheduler_patience int

Patience for learning rate scheduler, by default 30.

30
logger Literal['wandb', 'tensorboard', 'none']

Logger to use for training, by default "none".

'none'
trainer_params dict

Parameters for the trainer class, see PyTorch Lightning documentation.

None
augmentations list[Union[XYFlipConfig, XYRandomRotate90Config]] | None

List of augmentations to apply, by default None.

None
nm_paths list[str] | None

Paths to the noise model files, by default None.

None
data_stats tuple[float, float] | None

Data statistics (mean, std), by default None.

None
train_dataloader_params dict[str, Any] | None

Parameters for the training dataloader, by default None.

None
val_dataloader_params dict[str, Any] | None

Parameters for the validation dataloader, by default None.

None

Returns:

Type Description
Configuration

A configuration object for the microsplit algorithm.

Examples:

Minimum example:

>>> config = create_microsplit_configuration(

... experiment_name="microsplit_experiment",

... data_type="array",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... num_epochs=100

... )

You can also limit the number of batches per epoch:

>>> config = create_microsplit_configuration(

... experiment_name="microsplit_experiment",

... data_type="array",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... num_steps=100 # limit to 100 batches per epoch

... )

create_n2n_configuration(experiment_name, data_type, axes, patch_size, batch_size, num_epochs=100, num_steps=None, augmentations=None, independent_channels=True, loss='mae', n_channels_in=None, n_channels_out=None, logger='none', 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)

Create a configuration for training Noise2Noise.

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_in to the number of channels. Likewise, if you set the number of channels, then "C" must be present in axes.

To set the number of output channels, use the n_channels_out parameter. If it is not specified, it will be assumed to be equal to n_channels_in.

By default, all channels are trained together. To train all channels independently, set independent_channels to True.

By setting augmentations to None, the default augmentations (flip in X and Y, rotations by 90 degrees in the XY plane) are applied. Rather than the default augmentations, a list of augmentations can be passed to the augmentations parameter. To disable the augmentations, simply pass an empty list.

Parameters:

Name Type Description Default
experiment_name str

Name of the experiment.

required
data_type Literal['array', 'tiff', 'czi', 'custom']

Type of the data.

required
axes str

Axes of the data (e.g. SYX).

required
patch_size List[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.

100
num_steps int

Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to limit_train_batches in PyTorch Lightning Trainer. See relevant documentation for more details.

None
augmentations list of augmentations

List of augmentations 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
independent_channels bool

Whether to train all channels independently, by default False.

True
loss Literal['mae', 'mse']

Loss function to use, by default "mae".

'mae'
n_channels_in int or None

Number of channels in.

None
n_channels_out int or None

Number of channels out.

None
logger Literal['wandb', 'tensorboard', 'none']

Logger to use, by default "none".

'none'
trainer_params dict

Parameters for the trainer class, see PyTorch Lightning documentation.

None
model_params dict

UNetModel parameters.

None
optimizer Literal['Adam', 'Adamax', 'SGD']

Optimizer to use.

"Adam"
optimizer_params dict

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

Parameters for the learning rate scheduler, see PyTorch documentation for more details.

None
train_dataloader_params dict

Parameters for the training dataloader, see the PyTorch docs for DataLoader. If left as None, the dict {"shuffle": True} will be used, this is set in the GeneralDataConfig.

None
val_dataloader_params dict

Parameters for the validation dataloader, see PyTorch the docs for DataLoader. If left as None, the empty dict {} will be used, this is set in the GeneralDataConfig.

None
checkpoint_params dict

Parameters for the checkpoint callback, see PyTorch Lightning documentation (ModelCheckpoint) for the list of available parameters.

None

Returns:

Type Description
Configuration

Configuration for training Noise2Noise.

Examples:

Minimum example:

>>> config = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100
... )

You can also limit the number of batches per epoch:

>>> config = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_steps=100  # limit to 100 batches per epoch
... )

To disable augmentations, simply set augmentations to an empty list:

>>> config = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     augmentations=[]
... )

A list of augmentations can be passed to the augmentations parameter:

>>> from careamics.config.augmentations import XYFlipConfig
>>> config = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     augmentations=[
...         # No rotation and only Y flipping
...         XYFlipConfig(flip_x = False, flip_y = True)
...     ]
... )

If you are training multiple channels they will be trained independently by default, you simply need to specify the number of channels input (and optionally, the number of channels output):

>>> config = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="array",
...     axes="YXC", # channels must be in the axes
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     n_channels_in=3, # number of input channels
...     n_channels_out=1 # if applicable
... )

If instead you want to train multiple channels together, you need to turn off the independent_channels parameter:

>>> config = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="array",
...     axes="YXC", # channels must be in the axes
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     independent_channels=False,
...     n_channels_in=3,
...     n_channels_out=1 # if applicable
... )

If you would like to train on CZI files, use "czi" as data_type and "SCYX" as axes for 2-D or "SCZYX" for 3-D denoising. Note that "SCYX" can also be used for 3-D data but spatial context along the Z dimension will then not be taken into account.

>>> config_2d = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="czi",
...     axes="SCYX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     n_channels_in=1,
... )
>>> config_3d = create_n2n_configuration(
...     experiment_name="n2n_experiment",
...     data_type="czi",
...     axes="SCZYX",
...     patch_size=[16, 64, 64],
...     batch_size=16,
...     num_epochs=100,
...     n_channels_in=1,
... )

create_n2v_configuration(experiment_name, data_type, axes, patch_size, batch_size, num_epochs=100, num_steps=None, augmentations=None, independent_channels=True, use_n2v2=False, n_channels=None, roi_size=11, masked_pixel_percentage=0.2, struct_n2v_axis='none', struct_n2v_span=5, trainer_params=None, logger='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, 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 a random flip along X or Y, and a random 90 degrees rotation in the XY plane. Normalization is always applied, as well as the N2V manipulation.

By setting augmentations to None, the default augmentations (flip in X and Y, rotations by 90 degrees in the XY plane) are applied. Rather than the default augmentations, a list of augmentations can be passed to the augmentations parameter. 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.

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.

If you pass "horizontal" or "vertical" to struct_n2v_axis, then structN2V mask will be applied to each manipulated pixel.

Parameters:

Name Type Description Default
experiment_name str

Name of the experiment.

required
data_type Literal['array', 'tiff', 'czi', 'custom']

Type of the data.

required
axes str

Axes of the data (e.g. SYX).

required
patch_size List[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.

100
num_steps int

Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to limit_train_batches in PyTorch Lightning Trainer. See relevant documentation for more details.

None
augmentations list of augmentations

List of augmentations 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
independent_channels bool

Whether to train all channels together, by default True.

True
use_n2v2 bool

Whether to use N2V2, by default False.

False
n_channels int or None

Number of channels (in and out).

None
roi_size int

N2V pixel manipulation area, by default 11.

11
masked_pixel_percentage float

Percentage of pixels masked in each patch, by default 0.2.

0.2
struct_n2v_axis Literal['horizontal', 'vertical', 'none']

Axis along which to apply structN2V mask, by default "none".

'none'
struct_n2v_span int

Span of the structN2V mask, by default 5.

5
trainer_params dict

Parameters for the trainer, see the relevant documentation.

None
logger Literal['wandb', 'tensorboard', 'none']

Logger to use, by default "none".

'none'
model_params dict

UNetModel parameters.

None
optimizer Literal['Adam', 'Adamax', 'SGD']

Optimizer to use.

"Adam"
optimizer_params dict

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

Parameters for the learning rate scheduler, see PyTorch documentation for more details.

None
train_dataloader_params dict

Parameters for the training dataloader, see the PyTorch docs for DataLoader. If left as None, the dict {"shuffle": True} will be used, this is set in the GeneralDataConfig.

None
val_dataloader_params dict

Parameters for the validation dataloader, see PyTorch the docs for DataLoader. If left as None, the empty dict {} will be used, this is set in the GeneralDataConfig.

None
checkpoint_params dict

Parameters for the checkpoint callback, see PyTorch Lightning documentation (ModelCheckpoint) for the list of available parameters.

None
seed int or None

Random seed for reproducibility of N2V pixel manipulation, by default None.

None

Returns:

Type Description
Configuration

Configuration for training N2V.

Examples:

Minimum example:

>>> config = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100
... )

You can also limit the number of batches per epoch:

>>> config = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_steps=100  # limit to 100 batches per epoch
... )

To disable augmentations, simply set augmentations to an empty list:

>>> config = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     augmentations=[]
... )

A list of augmentations can be passed to the augmentations parameter:

>>> from careamics.config.augmentations import XYFlipConfig
>>> config = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="array",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     augmentations=[
...         # No rotation and only Y flipping
...         XYFlipConfig(flip_x = False, flip_y = True)
...     ]
... )

To use N2V2, simply pass the use_n2v2 parameter:

>>> config = create_n2v_configuration(
...     experiment_name="n2v2_experiment",
...     data_type="tiff",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     use_n2v2=True
... )

For structN2V, there are two parameters to set, struct_n2v_axis and struct_n2v_span:

>>> config = create_n2v_configuration(
...     experiment_name="structn2v_experiment",
...     data_type="tiff",
...     axes="YX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     struct_n2v_axis="horizontal",
...     struct_n2v_span=7
... )

If you are training multiple channels they will be trained independently by default, you simply need to specify the number of channels:

>>> config = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="array",
...     axes="YXC",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     n_channels=3
... )

If instead you want to train multiple channels together, you need to turn off the independent_channels parameter:

>>> config = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="array",
...     axes="YXC",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     independent_channels=False,
...     n_channels=3
... )

If you would like to train on CZI files, use "czi" as data_type and "SCYX" as axes for 2-D or "SCZYX" for 3-D denoising. Note that "SCYX" can also be used for 3-D data but spatial context along the Z dimension will then not be taken into account.

>>> config_2d = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="czi",
...     axes="SCYX",
...     patch_size=[64, 64],
...     batch_size=32,
...     num_epochs=100,
...     n_channels=1,
... )
>>> config_3d = create_n2v_configuration(
...     experiment_name="n2v_experiment",
...     data_type="czi",
...     axes="SCZYX",
...     patch_size=[16, 64, 64],
...     batch_size=16,
...     num_epochs=100,
...     n_channels=1,
... )

create_pn2v_configuration(experiment_name, data_type, axes, patch_size, batch_size, nm_path, num_epochs=100, num_steps=None, augmentations=None, independent_channels=True, use_n2v2=False, num_in_channels=1, num_out_channels=100, roi_size=11, masked_pixel_percentage=0.2, struct_n2v_axis='none', struct_n2v_span=5, trainer_params=None, logger='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, seed=None)

Create a configuration for training Probabilistic Noise2Void (PN2V).

PN2V extends N2V by incorporating a probabilistic noise model to estimate the posterior distibution of each pixel more precisely.

If "Z" is present in axes, then path_size must be a list of length 3, otherwise 2.

If "C" is present in axes, then you need to set num_in_channels to the number of channels.

By default, all channels are trained independently. To train all channels together, set independent_channels to False. When training independently, each input channel will have num_out_channels outputs (default 400). When training together, all input channels will share num_out_channels outputs.

By default, the augmentations applied are a random flip along X or Y, and a random 90 degrees rotation in the XY plane. Normalization is always applied, as well as the N2V manipulation.

By setting augmentations to None, the default augmentations (flip in X and Y, rotations by 90 degrees in the XY plane) are applied. Rather than the default augmentations, a list of augmentations can be passed to the augmentations parameter. 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.

The parameters of the UNet can be specified in the model_params (passed as a parameter-value dictionary). Note that use_n2v2, num_in_channels, and num_out_channels override the corresponding parameters passed in model_params.

If you pass "horizontal" or "vertical" to struct_n2v_axis, then structN2V mask will be applied to each manipulated pixel.

Parameters:

Name Type Description Default
experiment_name str

Name of the experiment.

required
data_type Literal['array', 'tiff', 'czi', 'custom']

Type of the data.

required
axes str

Axes of the data (e.g. SYX).

required
patch_size List[int]

Size of the patches along the spatial dimensions (e.g. [64, 64]).

required
batch_size int

Batch size.

required
nm_path str

Path to the noise model file.

required
num_epochs int

Number of epochs to train for. If provided, this will be added to trainer_params.

100
num_steps int

Number of batches in 1 epoch. If provided, this will be added to trainer_params. Translates to limit_train_batches in PyTorch Lightning Trainer. See relevant documentation for more details.

None
augmentations list of augmentations

List of augmentations to apply, either both or one of XYFlipModel and XYRandomRotate90Model. By default, it applies both XYFlip (on X and Y) and XYRandomRotate90 (in XY) to the images.

None
independent_channels bool

Whether to train all channels independently, by default True. If True, each input channel will correspond to num_out_channels output channels (e.g., 3 input channels with num_out_channels=400 results in 1200 total output channels).

True
use_n2v2 bool

Whether to use N2V2, by default False.

False
num_in_channels int

Number of input channels.

1
num_out_channels int

Number of output channels per input channel when independent_channels is True, or total number of output channels when independent_channels is False.

400
roi_size int

N2V pixel manipulation area, by default 11.

11
masked_pixel_percentage float

Percentage of pixels masked in each patch, by default 0.2.

0.2
struct_n2v_axis Literal['horizontal', 'vertical', 'none']

Axis along which to apply structN2V mask, by default "none".

'none'
struct_n2v_span int

Span of the structN2V mask, by default 5.

5
trainer_params dict

Parameters for the trainer, see the relevant documentation.

None
logger Literal['wandb', 'tensorboard', 'none']

Logger to use, by default "none".

'none'
model_params dict

UNetModel parameters.

None
optimizer Literal['Adam', 'Adamax', 'SGD']

Optimizer to use.

"Adam"
optimizer_params dict

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

Parameters for the learning rate scheduler, see PyTorch documentation for more details.

None
train_dataloader_params dict

Parameters for the training dataloader, see the PyTorch docs for DataLoader. If left as None, the dict {"shuffle": True} will be used, this is set in the GeneralDataConfig.

None
val_dataloader_params dict

Parameters for the validation dataloader, see PyTorch the docs for DataLoader. If left as None, the empty dict {} will be used, this is set in the GeneralDataConfig.

None
checkpoint_params dict

Parameters for the checkpoint callback, see PyTorch Lightning documentation (ModelCheckpoint) for the list of available parameters.

None
seed int or None

Random seed for reproducibility of N2V pixel manipulation, by default None.

None

Returns:

Type Description
Configuration

Configuration for training PN2V.

Examples:

Minimum example:

>>> config = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="array",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100

... )

You can also limit the number of batches per epoch:

>>> config = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="array",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_steps=100 # limit to 100 batches per epoch

... )

To disable augmentations, simply set augmentations to an empty list:

>>> config = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="array",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... augmentations=[]

... )

A list of augmentations can be passed to the augmentations parameter:

>>> from careamics.config.augmentations import XYFlipModel

>>> config = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="array",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... augmentations=[

... # No rotation and only Y flipping

... XYFlipModel(flip_x = False, flip_y = True)

... ]

... )

To use N2V2, simply pass the use_n2v2 parameter:

>>> config = create_pn2v_configuration(

... experiment_name="pn2v2_experiment",

... data_type="tiff",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... use_n2v2=True

... )

For structN2V, there are two parameters to set, struct_n2v_axis and

struct_n2v_span:

>>> config = create_pn2v_configuration(

... experiment_name="structpn2v_experiment",

... data_type="tiff",

... axes="YX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... struct_n2v_axis="horizontal",

... struct_n2v_span=7

... )

If you are training multiple channels they will be trained independently by

default, you simply need to specify the number of input channels. Each input

channel will correspond to num_out_channels outputs (1200 total for 3

channels with default num_out_channels=400):

>>> config = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="array",

... axes="YXC",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... num_in_channels=3

... )

If instead you want to train multiple channels together, you need to turn

off the independent_channels parameter (resulting in 400 total output

channels regardless of the number of input channels):

>>> config = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="array",

... axes="YXC",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... independent_channels=False,

... num_in_channels=3

... )

>>> config_2d = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="czi",

... axes="SCYX",

... patch_size=[64, 64],

... batch_size=32,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... num_in_channels=1,

... )

>>> config_3d = create_pn2v_configuration(

... experiment_name="pn2v_experiment",

... data_type="czi",

... axes="SCZYX",

... patch_size=[16, 64, 64],

... batch_size=16,

... nm_path="path/to/noise_model.npz",

... num_epochs=100,

... num_in_channels=1,

... )

get_likelihood_config(loss_type, predict_logvar=None, logvar_lowerbound=-5.0, nm_paths=None, data_stats=None)

Get the likelihood configuration for split models.

Returns a tuple containing the following optional entries: - GaussianLikelihoodConfig: Gaussian likelihood configuration for musplit losses - MultiChannelNMConfig: Multi-channel noise model configuration for denoisplit losses - NMLikelihoodConfig: Noise model likelihood configuration for denoisplit losses

Parameters:

Name Type Description Default
loss_type Literal['musplit', 'denoisplit', 'denoisplit_musplit']

The type of loss function to use.

required
predict_logvar Literal['pixelwise'] | None

Type of log variance prediction, by default None. Required when loss_type is "musplit" or "denoisplit_musplit".

None
logvar_lowerbound float | None

Lower bound for the log variance, by default -5.0. Used when loss_type is "musplit" or "denoisplit_musplit".

-5.0
nm_paths list[str] | None

Paths to the noise model files, by default None. Required when loss_type is "denoisplit" or "denoisplit_musplit".

None
data_stats tuple[float, float] | None

Data statistics (mean, std), by default None. Required when loss_type is "denoisplit" or "denoisplit_musplit".

None

Returns:

Name Type Description
gaussian_lik_config GaussianLikelihoodConfig | None

Gaussian likelihood configuration for musplit losses, or None.

nm_config MultiChannelNMConfig | None

Multi-channel noise model configuration for denoisplit losses, or None.

nm_lik_config NMLikelihoodConfig | None

Noise model likelihood configuration for denoisplit losses, or None.

Raises:

Type Description
ValueError

If required parameters are missing for the specified loss_type.

update_trainer_params(trainer_params=None, num_epochs=None, num_steps=None)

Update trainer parameters with num_epochs and num_steps.

Parameters:

Name Type Description Default
trainer_params dict

Parameters for Lightning Trainer class, by default None.

None
num_epochs int

Number of epochs to train for. If provided, this will be added as max_epochs to trainer_params, by default None.

None
num_steps int

Number of batches in 1 epoch. If provided, this will be added as limit_train_batches to trainer_params, by default None.

None

Returns:

Type Description
dict

Updated trainer parameters dictionary.