ChangeDetectionTask#

class torchgeo.trainers.ChangeDetectionTask(model='unet', backbone='resnet50', weights=None, in_channels=3, task='binary', num_classes=None, num_labels=None, labels=None, num_filters=3, pos_weight=None, loss='bce', class_weights=None, ignore_index=None, lr=0.001, patience=10, freeze_backbone=False, freeze_decoder=False)[source]#

Bases: ClassificationMixin, BaseTask

Change Detection. Supports binary, multiclass, and multilabel change detection.

Added in version 0.8.

__init__(model='unet', backbone='resnet50', weights=None, in_channels=3, task='binary', num_classes=None, num_labels=None, labels=None, num_filters=3, pos_weight=None, loss='bce', class_weights=None, ignore_index=None, lr=0.001, patience=10, freeze_backbone=False, freeze_decoder=False)[source]#

Initialize a new ChangeDetectionTask instance.

Parameters:
  • model (Literal['unet', 'deeplabv3+', 'fcn', 'upernet', 'segformer', 'dpt', 'fcsiamdiff', 'fcsiamconc', 'changevit', 'btc']) – Name of the model to use.

  • backbone (str) – Name of the timm or smp backbone to use.

  • weights (WeightsEnum | str | bool | None) – Initial model weights. Either a weight enum, the string representation of a weight enum, True for ImageNet weights, False or None for random weights, or the path to a saved model state dict. FCN model does not support pretrained weights.

  • in_channels (int) – Number of channels per image.

  • task (Literal['binary', 'multiclass', 'multilabel']) – One of ‘binary’, ‘multiclass’, or ‘multilabel’.

  • num_classes (int | None) – Number of prediction classes (only for task='multiclass').

  • num_labels (int | None) – Number of prediction labels (only for task='multilabel').

  • labels (list[str] | None) – List of class names.

  • num_filters (int) – Number of filters. Only applicable when model=’fcn’.

  • pos_weight (Tensor | None) – A weight of positive examples and used with ‘bce’ loss.

  • loss (Literal['ce', 'bce', 'jaccard', 'focal', 'dice']) – Name of the loss function, currently supports ‘ce’, ‘bce’, ‘jaccard’, ‘focal’, and ‘dice’ loss.

  • class_weights (Tensor | Sequence[float] | None) – Optional rescaling weight given to each class and used with ‘ce’ loss.

  • ignore_index (int | None) – Optional integer class index to ignore in the loss and metrics.

  • lr (float) – Learning rate for optimizer.

  • patience (int) – Patience for learning rate scheduler.

  • freeze_backbone (bool) – Freeze the backbone network to fine-tune the decoder and segmentation head.

  • freeze_decoder (bool) – Freeze the decoder network to linear probe the segmentation head.

Added in version 0.9: The labels parameter.

configure_models()[source]#

Initialize the model.

training_step(batch, batch_idx)[source]#

Compute the training loss and additional metrics.

Parameters:
  • batch (dict[str, Any]) – The output of your DataLoader.

  • batch_idx (int) – Integer displaying index of this batch.

Returns:

The loss tensor.

Return type:

Tensor

validation_step(batch, batch_idx)[source]#

Compute the validation loss and additional metrics.

Parameters:
  • batch (dict[str, Any]) – The output of your DataLoader.

  • batch_idx (int) – Integer displaying index of this batch.

test_step(batch, batch_idx)[source]#

Compute the test loss and additional metrics.

Parameters:
  • batch (dict[str, Any]) – The output of your DataLoader.

  • batch_idx (int) – Integer displaying index of this batch.

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

Compute the predicted class probabilities.

Parameters:
  • batch (dict[str, Any]) – The output of your DataLoader.

  • batch_idx (int) – Integer displaying index of this batch.

  • dataloader_idx (int) – Index of the current dataloader.

Returns:

Output predicted probabilities.

Return type:

Tensor