Skip to content

deeplabcut.pose_estimation_pytorch.models.criterions.weighted

Classes:

Name Description
WeightedBCECriterion

Weighted Binary Cross Entropy (BCE) Loss.

WeightedCriterion

Base class for weighted criterions.

WeightedHuberCriterion

Weighted Huber Loss.

WeightedMSECriterion

Weighted Mean Squared Error (MSE) Loss.

WeightedBCECriterion

Bases: WeightedCriterion

Weighted Binary Cross Entropy (BCE) Loss.

This loss computes the Binary Cross Entropy loss between the prediction and target tensors, but it also incorporates weights to adjust the contribution of each element in the loss calculation. The loss is computed element-wise, and elements with a weight of 0 are excluded from the loss calculation.

Source code in deeplabcut/pose_estimation_pytorch/models/criterions/weighted.py
@CRITERIONS.register_module
class WeightedBCECriterion(WeightedCriterion):
    """Weighted Binary Cross Entropy (BCE) Loss.

    This loss computes the Binary Cross Entropy loss between the prediction and target
    tensors, but it also incorporates weights to adjust the contribution of each element
    in the loss calculation. The loss is computed element-wise, and elements with a
    weight of 0 are excluded from the loss calculation.
    """

    def __init__(self) -> None:
        super().__init__(nn.BCEWithLogitsLoss(reduction="none"))

WeightedCriterion

Bases: BaseCriterion

Base class for weighted criterions.

Methods:

Name Description
forward

Args:

Source code in deeplabcut/pose_estimation_pytorch/models/criterions/weighted.py
class WeightedCriterion(BaseCriterion):
    """Base class for weighted criterions."""

    def __init__(self, criterion: nn.Module):
        super().__init__()
        self.criterion = criterion

    def forward(
        self,
        output: torch.Tensor,
        target: torch.Tensor,
        weights: torch.Tensor | float = 1.0,
        **kwargs,
    ) -> torch.Tensor:
        """
        Args:
            output: predicted tensor
            target: target tensor
            weights: weights for each element in the loss calculation. If a float,
                weights all elements by that value. Defaults to 1.

        Returns:
            the weighted loss
        """
        # shape of loss: (batch_size, n_kpts, heatmap_size, heatmap_size)
        loss = self.criterion(output, target)
        n_elems = utils.count_nonzero_elems(loss, weights)
        if n_elems == 0:
            n_elems = 1

        return torch.sum(loss * weights) / n_elems

forward

forward(output: Tensor, target: Tensor, weights: Tensor | float = 1.0, **kwargs) -> torch.Tensor

Parameters:

Name Type Description Default

output

Tensor

predicted tensor

required

target

Tensor

target tensor

required

weights

Tensor | float

weights for each element in the loss calculation. If a float, weights all elements by that value. Defaults to 1.

1.0

Returns:

Type Description
Tensor

the weighted loss

Source code in deeplabcut/pose_estimation_pytorch/models/criterions/weighted.py
def forward(
    self,
    output: torch.Tensor,
    target: torch.Tensor,
    weights: torch.Tensor | float = 1.0,
    **kwargs,
) -> torch.Tensor:
    """
    Args:
        output: predicted tensor
        target: target tensor
        weights: weights for each element in the loss calculation. If a float,
            weights all elements by that value. Defaults to 1.

    Returns:
        the weighted loss
    """
    # shape of loss: (batch_size, n_kpts, heatmap_size, heatmap_size)
    loss = self.criterion(output, target)
    n_elems = utils.count_nonzero_elems(loss, weights)
    if n_elems == 0:
        n_elems = 1

    return torch.sum(loss * weights) / n_elems

WeightedHuberCriterion

Bases: WeightedCriterion

Weighted Huber Loss.

This loss computes the Huber loss between the prediction and target tensors, but it also incorporates weights to adjust the contribution of each element in the loss calculation. The loss is computed element-wise, and elements with a weight of 0 are excluded from the loss calculation.

Source code in deeplabcut/pose_estimation_pytorch/models/criterions/weighted.py
@CRITERIONS.register_module
class WeightedHuberCriterion(WeightedCriterion):
    """Weighted Huber Loss.

    This loss computes the Huber loss between the prediction and target tensors, but it
    also incorporates weights to adjust the contribution of each element in the loss
    calculation. The loss is computed element-wise, and elements with a weight of 0 are
    excluded from the loss calculation.
    """

    def __init__(self) -> None:
        super().__init__(nn.HuberLoss(reduction="none"))

WeightedMSECriterion

Bases: WeightedCriterion

Weighted Mean Squared Error (MSE) Loss.

This loss computes the Mean Squared Error between the prediction and target tensors, but it also incorporates weights to adjust the contribution of each element in the loss calculation. The loss is computed element-wise, and elements with a weight of 0 (masked items) are excluded from the loss calculation.

Methods:

Name Description
forward

Args:

Source code in deeplabcut/pose_estimation_pytorch/models/criterions/weighted.py
@CRITERIONS.register_module
class WeightedMSECriterion(WeightedCriterion):
    """Weighted Mean Squared Error (MSE) Loss.

    This loss computes the Mean Squared Error between the prediction and target tensors,
    but it also incorporates weights to adjust the contribution of each element in the
    loss calculation. The loss is computed element-wise, and elements with a weight of 0
    (masked items) are excluded from the loss calculation.
    """

    def __init__(self) -> None:
        super().__init__(nn.MSELoss(reduction="none"))

    def forward(
        self,
        output: torch.Tensor,
        target: torch.Tensor,
        weights: torch.Tensor | float = 1.0,
        **kwargs,
    ) -> torch.Tensor:
        """
        Args:
            output: predicted tensor
            target: target tensor
            weights: weights for each element in the loss calculation. If a float,
                weights all elements by that value. Defaults to 1.

        Returns:
            the weighted loss
        """
        # shape of loss: (batch_size, n_kpts, h, w)
        loss = self.criterion(output, target)
        n_elems = utils.count_nonzero_elems(loss, weights)
        if n_elems == 0:
            n_elems = 1

        return torch.sum(loss * weights) / n_elems

forward

forward(output: Tensor, target: Tensor, weights: Tensor | float = 1.0, **kwargs) -> torch.Tensor

Parameters:

Name Type Description Default

output

Tensor

predicted tensor

required

target

Tensor

target tensor

required

weights

Tensor | float

weights for each element in the loss calculation. If a float, weights all elements by that value. Defaults to 1.

1.0

Returns:

Type Description
Tensor

the weighted loss

Source code in deeplabcut/pose_estimation_pytorch/models/criterions/weighted.py
def forward(
    self,
    output: torch.Tensor,
    target: torch.Tensor,
    weights: torch.Tensor | float = 1.0,
    **kwargs,
) -> torch.Tensor:
    """
    Args:
        output: predicted tensor
        target: target tensor
        weights: weights for each element in the loss calculation. If a float,
            weights all elements by that value. Defaults to 1.

    Returns:
        the weighted loss
    """
    # shape of loss: (batch_size, n_kpts, h, w)
    loss = self.criterion(output, target)
    n_elems = utils.count_nonzero_elems(loss, weights)
    if n_elems == 0:
        n_elems = 1

    return torch.sum(loss * weights) / n_elems