composer.models.loss#

A collection of common torchmetrics and loss functions.

Functions

soft_cross_entropy

Drop-in replacement for CrossEntropyLoss that can handle class indices or one-hot labels.

Classes

CrossEntropyLoss

Torchmetrics cross entropy loss implementation.

Dice

The Dice Coefficient for evaluating image segmentation.

MIoU

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

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

Bases: torchmetrics.metric.Metric

Torchmetrics cross entropy loss implementation.

This class implements cross entropy loss as a torchmetrics.Metric so that it can be returned by the metrics().

Parameters
  • ignore_index (int, optional) โ€“ Specifies a target value that is ignored and does not contribute to the input gradient. ignore_index is only applicable when the target contains class indices. Default: -100.

  • dist_sync_on_step (bool, optional) โ€“ sync distributed metrics every step. Default: False.

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(num_classes)[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.

Parameters

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

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

Torchmetrics 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

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

  • ignore_index (int, optional) โ€“ the index to ignore when computing mIoU. Default: -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.soft_cross_entropy(input, target, weight=None, size_average=None, ignore_index=- 100, reduce=None, reduction='mean')[source]#

Drop-in replacement for CrossEntropyLoss that can handle class indices or one-hot labels.

Parameters
  • input (Tensor) โ€“ \((N, C)\) where C = number of classes or \((N, C, H, W)\) in case of 2D Loss, or \((N, C, d_1, d_2, ..., d_K)\) where \(K \geq 1\) in the case of K-dimensional loss. input is expected to contain unnormalized scores (often referred to as logits).

  • target (Tensor) โ€“ If containing class indices, shape \((N)\) where each value is \(0 \leq \text{targets}[i] \leq C-1\), or \((N, d_1, d_2, ..., d_K)\) with \(K \geq 1\) in the case of K-dimensional loss. If containing class probabilities, same shape as the input.

  • weight (Tensor, optional) โ€“ a manual rescaling weight given to each class. If given, has to be a Tensor of size C. Default: None.

  • size_average (bool, optional) โ€“ Deprecated (see reduction). By default, the losses are averaged over each loss element in the batch. Note that for some losses, there multiple elements per sample. If the field size_average is set to False, the losses are instead summed for each minibatch. Ignored when reduce is False. Default: True

  • ignore_index (int, optional) โ€“ Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets. Note that ignore_index is only applicable when the target contains class indices. Default: -100

  • reduce (bool, optional) โ€“ Deprecated (see reduction). By default, the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch element instead and ignores size_average. Default: True

  • reduction (str, optional) โ€“ Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 'none': no reduction will be applied, 'mean': the sum of the output will be divided by the number of elements in the output, 'sum': the output will be summed. Note: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: 'mean'

This function will be obsolete with this update.