InstanceSegmentationTask#

class torchgeo.trainers.InstanceSegmentationTask(model='mask-rcnn', backbone='resnet50', weights=None, weights_backbone=None, in_channels=3, num_classes=91, lr=0.001, patience=10, freeze_backbone=False)[source]#

Bases: BaseTask

Instance Segmentation.

Added in version 0.7.

ignore = ('weights', 'weights_backbone')#

Parameters to ignore when saving hyperparameters.

monitor = 'val_segm_map'#

Performance metric to monitor in learning rate scheduler and callbacks.

mode = 'max'#

Whether the goal is to minimize or maximize the performance metric to monitor.

__init__(model='mask-rcnn', backbone='resnet50', weights=None, weights_backbone=None, in_channels=3, num_classes=91, lr=0.001, patience=10, freeze_backbone=False)[source]#

Initialize a new InstanceSegmentationTask instance.

Note that we disable the internal normalize+resize transform of the MaskRCNN model. Please ensure your images are appropriately resized before passing them to the model.

Parameters:
  • model (str) – Name of the model to use.

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

  • weights (WeightsEnum | None) – Initial model weights.

  • weights_backbone (WeightsEnum | None) – Initial backbone weights.

  • in_channels (int) – Number of input channels to model.

  • num_classes (int) – Number of prediction classes (including the background).

  • 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.

Added in version 0.9: The weights_backbone parameter.

configure_models()[source]#

Initialize the model.

Raises:

ValueError – If model or backbone are invalid.

configure_metrics()[source]#

Initialize the performance metrics.

  • MeanAveragePrecision: Mean average precision (mAP) and mean average recall (mAR). Precision is the number of true positives divided by the number of true positives + false positives. Recall is the number of true positives divived by the number of true positives + false negatives. Uses ‘macro’ averaging. Higher values are better.

Note

  • ‘Micro’ averaging suits overall performance evaluation but may not reflect minority class accuracy.

  • ‘Macro’ averaging gives equal weight to each class, and is useful for balanced performance assessment across imbalanced classes.

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

Compute the training loss.

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:

The loss tensor.

Return type:

Tensor

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

Compute the validation metrics.

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.

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

Compute the test metrics.

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.

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

Compute the predicted masks.

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 masks.

Return type:

list[dict[str, Tensor]]