artist.core.loss_functions ========================== .. py:module:: artist.core.loss_functions Classes ------- .. autoapisummary:: artist.core.loss_functions.Loss artist.core.loss_functions.VectorLoss artist.core.loss_functions.FocalSpotLoss artist.core.loss_functions.PixelLoss artist.core.loss_functions.KLDivergenceLoss artist.core.loss_functions.AngleLoss Module Contents --------------- .. py:class:: Loss(loss_function: torch.nn.Module) Initialize the the base loss. Parameters ---------- loss_function : torch.nn.Module A torch module implementing a loss. .. py:attribute:: loss_function .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor :abstractmethod: Compute the loss. Parameters ---------- prediction : torch.Tensor The predicted values. Tensor of variable shape. ground_truth : torch.Tensor The ground truth. Tensor of variable shape. \*\*kwargs : Any Keyword arguments. Raises ------ NotImplementedError This abstract method must be overridden. .. py:class:: VectorLoss Bases: :py:obj:`Loss` Initialize the vector loss. .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the vector loss. Parameters ---------- prediction : torch.Tensor The predicted values. Tensor of shape [number_of_samples, ...]. ground_truth : torch.Tensor The ground truth. Tensor of shape [number_of_samples, ...]. \*\*kwargs : Any Keyword arguments. The ``reduction_dimensions`` is an expected keyword argument for the vector loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed MSE vector loss reduced along the specified dimensions. Tensor of shape [number_of_samples]. .. py:class:: FocalSpotLoss(scenario: artist.scenario.scenario.Scenario) Bases: :py:obj:`Loss` Initialize the focal spot loss. Parameters ---------- scenario : Scenario The scenario. .. py:attribute:: scenario .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the focal spot loss. First the focal spots of the prediction are computed, then the loss is computed and reduced along the specified dimensions. Parameters ---------- prediction : torch.Tensor The predicted values. Tensor of shape [number_of_samples, bitmap_resolution_e, bitmap_resolution_u]. ground_truth : torch.Tensor The ground truth. Tensor of shape [number_of_samples, 4]. \*\*kwargs : Any Keyword arguments. The ``reduction_dimensions``, ``target_area_mask`` and optionally ``device`` are expected keyword arguments for the focal spot loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed MSE focal spot loss reduced along the specified dimensions. Tensor of shape [number_of_samples]. .. py:class:: PixelLoss(scenario: artist.scenario.scenario.Scenario) Bases: :py:obj:`Loss` Initialize the pixel loss. Parameters ---------- scenario : Scenario The scenario. .. py:attribute:: scenario .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the pixel loss. First the predicted bitmaps and the ground truth are normalized, then the loss is computed and reduced along the specified dimensions. Parameters ---------- prediction : torch.Tensor The predicted values. Tensor of shape [number_of_samples, bitmap_resolution_e, bitmap_resolution_u]. ground_truth : torch.Tensor The ground truth. Tensor of shape [number_of_samples, bitmap_resolution_e, bitmap_resolution_u]. \*\*kwargs : Any Keyword arguments. The ``reduction_dimensions``, ``target_area_mask`` and optionally ``device`` are expected keyword arguments for the pixel loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed MSE pixel loss reduced along the specified dimensions. Tensor of shape [number_of_samples]. .. py:class:: KLDivergenceLoss Bases: :py:obj:`Loss` Initialize the Kullback-Leibler divergence loss. .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the Kullback-Leibler divergence loss :math:`D_{\mathrm{KL}}(P \parallel Q)`. The elements in the prediction and ground truth are normalized and shifted, to be greater or equal to zero. The kl-divergence is defined by: .. math:: D_{\mathrm{KL}}(P \parallel Q) = \sum_{x} P(x) \log \frac{P(x)}{Q(x)}, where :math:`P` is the ground truth distribution and :math:`Q` is the approximation or prediction of :math:`Q`. The kl-divergence is an asymmetric function. Switching :math:`P` and :math:`Q` has the following effect: :math:`P \parallel Q` Penalizes extra mass in the prediction where the ground truth has none. :math:`Q \parallel P` Penalizes missing mass in the prediction where the ground truth has mass. Parameters ---------- prediction : torch.Tensor The predicted values. Tensor of shape [number_of_samples, bitmap_resolution_e, bitmap_resolution_u]. ground_truth : torch.Tensor The ground truth. Tensor of shape [number_of_samples, bitmap_resolution_e, bitmap_resolution_u]. \*\*kwargs : Any Keyword arguments. The ``reduction_dimensions`` is an expected keyword argument for the kl-divergence loss. Raises ------ ValueError If expected keyword arguments are not passed. Returns ------- torch.Tensor The summed kl-divergence loss reduced along the specified dimensions. Tensor of shape [number_of_samples]. .. py:class:: AngleLoss Bases: :py:obj:`Loss` Initialize the angle loss. .. py:method:: __call__(prediction: torch.Tensor, ground_truth: torch.Tensor, **kwargs: Any) -> torch.Tensor Compute the cosine similarity between the prediction and ground truth. Parameters ---------- prediction : torch.Tensor The predicted values. Tensor of shape [number_of_samples, 4]. ground_truth : torch.Tensor The ground truth. Tensor of shape [number_of_samples, 4]. \*\*kwargs : Any Keyword arguments. Returns ------- torch.Tensor The summed loss reduced along the specified dimensions. Tensor of shape [number_of_samples].