Shortcuts

models package

Submodules

models.loss_weight_scheduler module

Scheduler to reduce pseudo-label loss weights as training progresses.

class models.loss_weight_scheduler.LossWeightScheduler(algorithm, start_weight, end_weight=None, start_step=0, end_step=None)[source]

Bases: object

Scheduler to reduce pseudo-label loss weights as training progresses.

Parameters
  • algorithm (string) – Scheduling algorithm to be used: “fixed” | “linear” | “cosine”: - “fixed”: the pseudo-label loss weight is not changed. - “linear”: the pseudo-label loss weight is reduced by the same value in each step. - “cosine”: the decay of the pseudo-label loss weight follows a cosine curve.

  • start_weight (float) – Initial pseudo-label loss weight.

  • end_weight (float, optional) – Final pseudo-label loss weight. Must be specified when algorithm is set to “linear” or “cosine”. Defaults to None.

  • start_step (int, optional) – Initial step index. Defaults to 0.

  • end_step (int, optional) – Final step index. Must be specified when algorithm is set to “linear” or “cosine”. Defaults to None.

current_weight()[source]
Returns

Current pseudo-label loss weight.

Return type

float

step()[source]

Increases scheduler step.

models.pytorch_model module

Base classes to implement models with pytorch

class models.pytorch_model.PytorchModel(learning_rate=0.0001, lr_scheduler=None, loss_config=None, optimizer='adam', train_metrics=None, train_metric_confidence_levels=None, test_metrics=None, test_metric_confidence_levels=None, **kwargs)[source]

Bases: pytorch_lightning.core.lightning.LightningModule, abc.ABC

Base class to implement Pytorch models.

Parameters
  • learning_rate (float, optional) – The step size at each iteration while moving towards a minimum of the loss. Defaults to 0.0001.

  • lr_scheduler (string, optional) – Algorithm used for dynamically updating the learning rate during training: "reduceLROnPlateau" | "cosineAnnealingLR". Defaults to using no scheduler.

  • loss_config (Dict[str, Any], optional) – Dictionary with loss parameters.

  • optimizer (string, optional) – Algorithm used to calculate the loss and update the weights: "adam" | "sgd". Defaults to "adam".

  • train_metrics (Iterable[str], optional) – A list with the names of the metrics that should be computed and logged in each training and validation epoch of the training loop. Available options: "dice_score" | "sensitivity" | "specificity" | "hausdorff95". Defaults to [“dice_score”].

  • train_metric_confidence_levels (Iterable[float], optional) – A list of confidence levels for which the metrics specified in the train_metrics parameter should be computed in the training loop (trainer.fit()). This parameter is used only for multi-label classification tasks. Defaults to [0.5].

  • test_metrics (Iterable[str], optional) – A list with the names of the metrics that should be computed and logged in the model validation or testing loop (trainer.validate(), trainer.test()). Available options: "dice_score" | "sensitivity" | "specificity" | "hausdorff95" Defaults to [“dice_score”, “sensitivity”, “specificity”, “hausdorff95”].

  • test_metric_confidence_levels (Iterable[float], optional) – A list of confidence levels for which the metrics specified in the test_metrics parameter should be computed in the validation or testing loop. This parameter is used only for multi-label classification tasks. Defaults to [0.5].

  • **kwargs – Further, dataset specific parameters.

static configure_loss(loss, multi_label=False, **kwargs)[source]

Configures the loss.

Parameters
  • loss (string) – The optimization criterion: "cross_entropy" | "dice" | "cross_entropy_dice" | "general_dice" | "fp" | "fp_dice" | "focal".

  • multi_label (bool, optional) – Determines if data is multilabel or not (default = False).

  • kwargs – Additional parameters for the loss.

Returns

The loss object.

configure_metrics()[source]

Configures metrics.

configure_optimizers()[source]

This method is called by the PyTorch lightning framework before starting model training.

Returns

The optimizer object as a list and optionally a learning rate scheduler object as a list.

property current_epoch
get_test_metrics()[source]
Returns

A list of metrics to be updated in each testing step.

get_train_metrics()[source]
Returns

A list of metrics to be updated in each training step.

get_val_metrics()[source]
Returns

A list of metrics to be updated in each validation step.

abstract input_dimensionality()[source]

The dimensionality of the input. Usually 2 or 3.

property loss_weight_pseudo_labels

Returns: Union[float, None]: Pseudo-label loss weight if loss_weight_pseudo_labels_scheduler is not None,

else None.

predict(batch)[source]

Computes predictions for a given batch of model inputs.

Parameters

batch – A batch of model inputs.

Returns

Predictions for the given inputs.

predict_step(batch, batch_idx, dataloader_idx=0)[source]

Compute the model’s predictions on a given batch of model inputs. This method should match the requirements of the pytorch lightning framework. See the pytorch lightning documentation for more details.

Parameters
  • batch – A batch of model inputs.

  • batch_idx – Index of the current batch.

  • dataloader_idx – The index of the dataloader that produced this batch. (only if multiple val dataloaders used)

abstract reset_parameters()[source]

This method is called when resetting the weights is activated for the active learing loop

setup(stage=None)[source]

Setup hook as defined by PyTorch Lightning. Called at the beginning of fit (train + validate), validate, test, or predict.

Parameters

stage (string, optional) – Either ‘fit’, ‘validate’, ‘test’, or ‘predict’.

step_loss_weight_pseudo_labels_scheduler()[source]

Increases step of pseudo-label loss weight scheduler if loss_weight_pseudo_labels_scheduler is not None.

test_epoch_end(outputs)[source]

This method is called by the Pytorch Lightning framework at the end of each testing epoch.

Parameters

outputs – List of return values of all validation steps of the current testing epoch.

abstract test_step(batch, batch_idx, dataloader_idx=None)[source]

Compute the model’s predictions on a given batch of model inputs from the test set.

Parameters
  • batch – The output of your DataLoader.

  • batch_idx – The index of this batch.

  • dataloader_id – The index of the dataloader that produced this batch. (only if multiple test dataloaders used).

training
training_epoch_end(outputs)[source]

This method is called by the Pytorch Lightning framework at the end of each training epoch.

Parameters

outputs – List of return values of all training steps of the current training epoch.

abstract training_step(batch, batch_idx)[source]

Trains the model on a given batch of model inputs. This method should match the requirements of the pytorch lightning framework. See the pytorch lighting documentation for more details.

Parameters
  • batch – A batch of model inputs.

  • batch_idx – Index of the current batch.

Returns

Training loss.

validation_epoch_end(outputs)[source]

This method is called by the Pytorch Lightning framework at the end of each validation epoch.

Parameters

outputs – List of return values of all validation steps of the current validation epoch.

abstract validation_step(batch, batch_idx)[source]

Validates the model on a given batch of model inputs. This method should match the requirements of the pytorch lightning framework. See the pytorch lightning documentation for more details.

Parameters
  • batch – A batch of model inputs.

  • batch_idx – Index of the current batch.

Returns

Validation loss.

models.pytorch_u_net module

U-Net architecture wrapped as PytorchModel

class models.pytorch_u_net.PytorchUNet(num_levels=4, dim=2, in_channels=1, out_channels=2, multi_label=False, p_dropout=0, **kwargs)[source]

Bases: models.pytorch_model.PytorchModel

U-Net architecture wrapped as PytorchModel. Details about the architecture can be found in this paper.

Parameters
  • num_levels (int, optional) – Number levels (encoder and decoder blocks) in the U-Net. Defaults to 4.

  • dim (int, optional) – The dimensionality of the U-Net. Defaults to 2.

  • in_channels (int, optional) – Number of input channels. Defaults to 1

  • out_channels (int) – Number of output channels. Should be equal to the number of classes (for multi-label segmentation tasks excluding the background class). Defaults to 2.

  • multi_label (bool, optional) – Whether the model should produce single-label or multi-label outputs. If set to False, the model’s predictions are computed using a Softmax activation layer. to If set to True, sigmoid activation layers are used to compute the model’s predicitions. Defaults to False.

  • p_dropout (float, optional) – Probability of applying dropout to the outputs of the encoder layers. Defaults to 0.

  • **kwargs – Further, dataset specific parameters.

eval()[source]

Sets model to evaluation mode.

forward(x)[source]
Parameters

x (Tensor) – Batch of input images.

Returns

Segmentation masks.

Return type

Tensor

input_dimensionality()[source]
parameters(recurse=True)[source]
Returns

Model parameters.

Return type

Iterable

precision
predict_step(batch, batch_idx, dataloader_idx=0)[source]

Uses the model to predict a given batch of input images.

Parameters
  • batch (Tensor) – Batch of prediction images.

  • batch_idx – Index of the prediction batch.

  • dataloader_idx – Index of the dataloader.

reset_parameters()[source]

This method is called when resetting the weights is activated for the active learing loop

test_step(batch, batch_idx, dataloader_idx=0)[source]

Tests the model on a given batch of input images.

Parameters
  • batch (Tensor) – Batch of prediction images.

  • batch_idx – Index of the prediction batch.

  • dataloader_idx – Index of the dataloader.

train(mode=True)[source]

Sets model to training mode.

training
training_step(batch, batch_idx)[source]

Trains the model on a given batch of input images.

Parameters
  • batch (Tensor) – Batch of training images.

  • batch_idx – Index of the training batch.

Returns

Loss on the training batch.

use_amp
validation_step(batch, batch_idx)[source]

Validates the model on a given batch of input images.

Parameters
  • batch (Tensor) – Batch of validation images.

  • batch_idx – Index of the validation batch.

Returns

Loss on the validation batch.

models.u_net module

U-Net architecture.

class models.u_net.UNet(in_channels, out_channels, multi_label=False, init_features=32, num_levels=4, dim=2, p_dropout=0)[source]

Bases: torch.nn.modules.module.Module

This U-Net implementation was originally taken from this implementation and adapted to a flexible number of levels and for optional 3d mode.

Parameters
  • in_channels (int) – Number of input channels.

  • out_channels (int) – Number of output channels. Should be equal to the number of classes (for multi-label segmentation tasks excluding the background class).

  • multi_label (bool, optional) – Whether the model should produce single-label or multi-label outputs. If set to False, the model’s predictions are computed using a Softmax activation layer. to If set to True, sigmoid activation layers are used to compute the model’s predicitions. Defaults to False.

  • init_features (int, optional) – Number of feature channels of the first U-Net block, in each down-sampling block, the number of feature channels is doubled. Defaults to 32.

  • num_levels (int, optional) – Number levels (encoder and decoder blocks) in the U-Net. Defaults to 4.

  • dim (int, optional) – The dimensionality of the U-Net. Defaults to 2.

  • p_dropout (float, optional) – Probability of applying dropout to the outputs of the encoder layers. Defaults to 0.

forward(x)[source]
Parameters

x (Tensor) – Batch of input images.

Returns

Segmentation masks.

Return type

Tensor

training

Module contents

Docs

Access comprehensive developer documentation for Active Segmentation

View Docs