dacapo.experiments.tasks.losses

Submodules

Classes

DummyLoss

A class representing a dummy loss function that calculates the absolute difference between each prediction and target.

MSELoss

A class used to represent the Mean Square Error Loss function (MSELoss). This class inherits from the Loss class.

Loss

A class used to represent a loss function. This class is an abstract class

AffinitiesLoss

A class representing a loss function that calculates the loss between affinities and local shape descriptors (LSDs).

HotDistanceLoss

A class used to represent the Hot Distance Loss function. This class inherits from the Loss class. The Hot Distance Loss

Package Contents

class dacapo.experiments.tasks.losses.DummyLoss

A class representing a dummy loss function that calculates the absolute difference between each prediction and target.

Inherits the Loss class.

name

str name of the loss function

compute(prediction, target, weight=None)

Calculate the total loss between prediction and target.

Note

The dummy loss is used to test the training loop and the loss calculation. It is not a real loss function. It is used to test the training loop and the loss calculation.

compute(prediction, target, weight=None)

Method to calculate the total dummy loss.

Parameters:
  • prediction – torch.Tensor the model’s prediction

  • target – torch.Tensor the target values

  • weight – torch.Tensor the weight to apply to the loss

Returns:

torch.Tensor

the total loss between prediction and target

Examples

>>> dummy_loss = DummyLoss()
>>> prediction = torch.tensor([1, 2, 3])
>>> target = torch.tensor([4, 5, 6])
>>> dummy_loss.compute(prediction, target)
tensor(9)

Note

The dummy loss is used to test the training loop and the loss calculation. It is not a real loss function. It is used to test the training loop and the loss calculation.

class dacapo.experiments.tasks.losses.MSELoss

A class used to represent the Mean Square Error Loss function (MSELoss). This class inherits from the Loss class.

compute(prediction, target, weight) torch.Tensor

Function to compute the MSELoss for the provided prediction and target, with respect to the weight.

Note

This class is abstract. Subclasses must implement the abstract methods. Once created, the values of its attributes cannot be changed.

compute(prediction, target, weight)

Function to compute the MSELoss for the provided prediction and target, with respect to the weight.

Parameters:
  • prediction – torch.Tensor The predicted tensor.

  • target – torch.Tensor The target tensor.

  • weight – torch.Tensor The weight tensor.

Returns:

torch.Tensor

The computed MSELoss tensor.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Examples

>>> loss = MSELoss()
>>> prediction = torch.tensor([1.0, 2.0, 3.0])
>>> target = torch.tensor([1.0, 2.0, 3.0])
>>> weight = torch.tensor([1.0, 1.0, 1.0])
>>> loss.compute(prediction, target, weight)
tensor(0.)

Note

This method must be implemented in the subclass. It should return the computed MSELoss tensor.

class dacapo.experiments.tasks.losses.Loss

A class used to represent a loss function. This class is an abstract class that should be inherited by any loss function class.

compute(prediction, target, weight) torch.Tensor

Function to compute the loss for the provided prediction and target, with respect to the weight.

Note

This class is abstract. Subclasses must implement the abstract methods. Once created, the values of its attributes cannot be changed.

abstract compute(prediction: torch.Tensor, target: torch.Tensor, weight: torch.Tensor | None = None) torch.Tensor

Compute the loss for the given prediction and target. Optionally, if given, a loss weight should be considered.

All arguments are torch tensors. The return type should be a torch scalar that can be used with an optimizer, just as usual when training with torch.

Parameters:
  • prediction – The predicted tensor.

  • target – The target tensor.

  • weight – The weight tensor.

Returns:

The computed loss tensor.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Examples

>>> loss = MSELoss()
>>> prediction = torch.tensor([1.0, 2.0, 3.0])
>>> target = torch.tensor([1.0, 2.0, 3.0])
>>> weight = torch.tensor([1.0, 1.0, 1.0])
>>> loss.compute(prediction, target, weight)
tensor(0.)

Note

This method must be implemented in the subclass. It should return the computed loss tensor.

class dacapo.experiments.tasks.losses.AffinitiesLoss(num_affinities: int, lsds_to_affs_weight_ratio: float)

A class representing a loss function that calculates the loss between affinities and local shape descriptors (LSDs).

num_affinities

int the number of affinities

lsds_to_affs_weight_ratio

float the ratio of the weight of the loss between affinities and LSDs

compute(prediction, target, weight=None)

Calculate the total loss between prediction and target.

Note

The AffinitiesLoss class is used to calculate the loss between affinities and local shape descriptors (LSDs).

num_affinities
lsds_to_affs_weight_ratio
compute(prediction, target, weight)

Method to calculate the total loss between affinities and LSDs.

Parameters:
  • prediction – torch.Tensor the model’s prediction

  • target – torch.Tensor the target values

  • weight – torch.Tensor the weight to apply to the loss

Returns:

torch.Tensor

the total loss between affinities and LSDs

Raises:

ValueError – if the number of affinities in the prediction and target does not match

Examples

>>> affinities_loss = AffinitiesLoss(3, 0.5)
>>> prediction = torch.tensor([[1, 2, 3, 4], [5, 6, 7, 8]])
>>> target = torch.tensor([[9, 10, 11, 12], [13, 14, 15, 16]])
>>> weight = torch.tensor([[1, 1, 1, 1], [1, 1, 1, 1]])
>>> affinities_loss.compute(prediction, target, weight)
tensor(0.5)

Note

The AffinitiesLoss class is used to calculate the loss between affinities and local shape descriptors (LSDs).

class dacapo.experiments.tasks.losses.HotDistanceLoss

A class used to represent the Hot Distance Loss function. This class inherits from the Loss class. The Hot Distance Loss function is used for predicting hot and distance maps at the same time. The first half of the channels are the hot maps, the second half are the distance maps. The loss is the sum of the BCELoss for the hot maps and the MSELoss for the distance maps. The model should predict twice the number of channels as the target.

hot_loss

The Binary Cross Entropy Loss function.

distance_loss

The Mean Square Error Loss function.

compute(prediction, target, weight) torch.Tensor

Function to compute the Hot Distance Loss for the provided prediction and target, with respect to the weight.

split(x) Tuple[torch.Tensor, torch.Tensor]

Function to split the input tensor into two tensors.

Note

This class is abstract. Subclasses must implement the abstract methods. Once created, the values of its attributes cannot be changed.

compute(prediction, target, weight)

Function to compute the Hot Distance Loss for the provided prediction and target, with respect to the weight.

Parameters:
  • prediction – torch.Tensor The predicted tensor.

  • target – torch.Tensor The target tensor.

  • weight – torch.Tensor The weight tensor.

Returns:

torch.Tensor

The computed Hot Distance Loss tensor.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Examples

>>> loss = HotDistanceLoss()
>>> prediction = torch.tensor([1.0, 2.0, 3.0])
>>> target = torch.tensor([1.0, 2.0, 3.0])
>>> weight = torch.tensor([1.0, 1.0, 1.0])
>>> loss.compute(prediction, target, weight)
tensor(0.)

Note

This method must be implemented in the subclass. It should return the computed Hot Distance Loss tensor.

hot_loss(prediction, target, weight)

The Binary Cross Entropy Loss function. This function computes the BCELoss for the hot maps.

Parameters:
  • prediction – torch.Tensor The predicted tensor.

  • target – torch.Tensor The target tensor.

  • weight – torch.Tensor The weight tensor.

Returns:

torch.Tensor

The computed BCELoss tensor.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Examples

>>> loss = HotDistanceLoss()
>>> prediction = torch.tensor([1.0, 2.0, 3.0])
>>> target = torch.tensor([1.0, 2.0, 3.0])
>>> weight = torch.tensor([1.0, 1.0, 1.0])
>>> loss.hot_loss(prediction, target, weight)
tensor(0.)

Note

This method must be implemented in the subclass. It should return the computed BCELoss tensor.

distance_loss(prediction, target, weight)

The Mean Square Error Loss function. This function computes the MSELoss for the distance maps.

Parameters:
  • prediction – torch.Tensor The predicted tensor.

  • target – torch.Tensor The target tensor.

  • weight – torch.Tensor The weight tensor.

Returns:

torch.Tensor

The computed MSELoss tensor.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Examples

>>> loss = HotDistanceLoss()
>>> prediction = torch.tensor([1.0, 2.0, 3.0])
>>> target = torch.tensor([1.0, 2.0, 3.0])
>>> weight = torch.tensor([1.0, 1.0, 1.0])
>>> loss.distance_loss(prediction, target, weight)
tensor(0.)

Note

This method must be implemented in the subclass. It should return the computed MSELoss tensor.

split(x)

Function to split the input tensor into two tensors.

Parameters:

x – torch.Tensor The input tensor.

Returns:

Tuple[torch.Tensor, torch.Tensor]

The two split tensors.

Raises:

NotImplementedError – If the method is not implemented in the subclass.

Examples

>>> loss = HotDistanceLoss()
>>> x = torch.tensor([1.0, 2.0, 3.0])
>>> loss.split(x)
(tensor([1.0]), tensor([2.0]))

Note

This method must be implemented in the subclass. It should return the two split tensors.