composer.models.loss#

composer.models.loss

Functions

check_for_index_targets

Checks if a given set of targets are indices by looking at the type.

ensure_targets_one_hot

composer.models.loss.ensure_targets_one_hot

soft_cross_entropy

Drop-in replacement for torch.CrossEntropy that can handle dense labels.

to_categorical

Converts a tensor of probabilities to a dense label tensor.

Classes

CrossEntropyLoss

Torchmetric cross entropy loss implementation.

Dice

The Dice Coefficient for evaluating image segmentation.

MIoU

Torchmetric mean Intersection-over-Union (mIoU) implementation.

Metric

Base class for all metrics present in the Metrics API.

Tensor

composer.models.loss.torch.Tensor

Attributes

  • Optional

  • TYPE_CHECKING

  • Tuple

  • annotations

class composer.models.loss.CrossEntropyLoss(ignore_index=- 100, dist_sync_on_step=False)[source]#

Bases: torchmetrics.metric.Metric

Torchmetric cross entropy loss implementation.

This class implements cross entropy loss as a torchmetric so that it can be returned by the metric() function in ComposerModel.

compute()[source]#

Aggregate state over all processes and compute the metric.

update(preds, target)[source]#

Update the state with new predictions and targets.

class composer.models.loss.Dice(nclass)[source]#

Bases: torchmetrics.metric.Metric

The Dice Coefficient for evaluating image segmentation.

The Dice Coefficient measures how similar predictions and targets are. More concretely, it is computed as 2 * the Area of Overlap divided by the total number of pixels in both images.

compute()[source]#

Aggregate the state over all processes to compute the metric.

update(pred, target)[source]#

Update the state based on new predictions and targets.

class composer.models.loss.MIoU(num_classes, ignore_index=- 1)[source]#

Bases: torchmetrics.metric.Metric

Torchmetric mean Intersection-over-Union (mIoU) implementation.

IoU calculates the intersection area between the predicted class mask and the label class mask. The intersection is then divided by the area of the union of the predicted and label masks. This measures the quality of predicted class mask with respect to the label. The IoU for each class is then averaged and the final result is the mIoU score. Implementation is primarily based on mmsegmentation: https://github.com/open-mmlab/mmsegmentation/blob/master/mmseg/core/evaluation/metrics.py#L132

Parameters
  • num_classes (int) โ€“ the number of classes in the segmentation task.

  • ignore_index (int) โ€“ the index to ignore when computing mIoU. Default is -1.

compute()[source]#

Aggregate state across all processes and compute final metric.

update(logits, targets)[source]#

Update the state with new predictions and targets.

composer.models.loss.check_for_index_targets(targets)[source]#

Checks if a given set of targets are indices by looking at the type.

composer.models.loss.soft_cross_entropy(input, target, weight=None, size_average=None, ignore_index=- 100, reduce=None, reduction='mean')[source]#

Drop-in replacement for torch.CrossEntropy that can handle dense labels.

This function will be obsolete with this update.