skshapes.loss package

Loss functions for shape analysis.

Submodules

skshapes.loss.baseloss module

Abstract classes for losses.

class skshapes.loss.baseloss.BaseLoss

Bases: object

Base class for losses.

This class defines the + and * operators for losses, and each loss should inherit from this class. The + operator returns a SumLoss object, and the * operator returns a ProductLoss object.

This class is not meant to be used directly, if the constructor is called it raises an error.

Raises:

NotImplementedError – this class is abstract and should not be instantiated

class skshapes.loss.baseloss.EmptyLoss

Bases: BaseLoss

Empty loss, which always returns 0.

This loss is useful to serve as a default value for losses which are not specified.

class skshapes.loss.baseloss.ProductLoss(loss=None, scalar=1.0)

Bases: BaseLoss

Abstract class for losses which are the product of a loss and a scalar.

This class can be directly instantiated, but it is more convenient to use the * operator to multiply a loss by a scalar, which returns a ProductLoss object.

class skshapes.loss.baseloss.SumLoss(loss1=None, loss2=None)

Bases: BaseLoss

Abstract class for losses which are the sum of two losses.

This class can be directly instantiated, but it is more convenient to use the + operator to add two losses, which returns a SumLoss object.

Note that adding two losses that are not compatible (e.g. a loss for images and a loss for meshes) will not raise an error at the time of the addition. However it will raise an error when the __call__ method is used, thanks to the runtime type checker.

Parameters:

skshapes.loss.lp module

Lp losses for PolyData.

class skshapes.loss.lp.L2Loss

Bases: BaseLoss

L2 loss for PolyData.

This class defines the L2 loss for PolyData. It is a wrapper around the LpLoss class with p=2.

class skshapes.loss.lp.LandmarkLoss(p=2)

Bases: BaseLoss

Landmark loss for PolyData.

This class defines the Lp loss between the landmarks of two PolyData objects. If \(X = (x_i)\) and \(Y = (y_i)\) are the landmarks of two PolyData objects, the Lp loss is defined as:

\[Lp(X, Y) = \sum_i \Vert x_i - y_i \Vert^{(p/2)}\]

X and Y must have the same number of landmarks. What is more, the landmarks must be in correspondence, i.e. \(x_i\) and \(y_i\) must correspond to the same landmark. If this is not the case, the loss will be meaningless, consider using a loss function based on Optimal Transport or Nearest Neighbors instead.

Parameters:

p (int | float) – the indice of the Lp Norm. Defaults to 2.

class skshapes.loss.lp.LpLoss(p=2)

Bases: BaseLoss

Lp loss for PolyData.

This class defines the L2 loss for PolyData. If X = (x_i) and Y = (y_i) are the points of two PolyData objects, the Lp loss is defined as:

\[Lp(X, Y) = \sum_i \Vert x_i - y_i \Vert^{(p/2)}\]

where \(||.||\) is the Euclidean norm.

X and Y must have the same number of points. What is more, the points must be in correspondence, i.e. x_i and y_i must correspond to the same point. If this is not the case, the loss will be meaningless, consider using a loss function based on Optimal Transport or Nearest Neighbors instead.

Parameters:

p (int | float) – the indice of the Lp Norm. Default to 2.

skshapes.loss.nearest_neighbors module

Nearest Neighbors loss for PolyData.

class skshapes.loss.nearest_neighbors.NearestNeighborsLoss

Bases: BaseLoss

Loss based on nearest neighbors for PolyData.

This class defines a loss corresponding to the nearest neighbors distance between the points of two PolyData objects. More precisely, for each point in the source PolyData, we compute the distance to its nearest neighbor in the target PolyData. The loss is then the average of these distances.

The distances are computed using the lazy tensor library pykeops : https://www.kernel-operations.io/keops/index.html

skshapes.loss.optimal_transport module

Losses based on optimal transport for Polydata.

class skshapes.loss.optimal_transport.OptimalTransportLoss(loss='sinkhorn', **kwargs)

Bases: BaseLoss

Loss based on optimal transport for PolyData.

This class defines a loss based on optimal transport for PolyData. More precisely, it initializes a SamplesLoss object from the geomloss library. See its [documentation]((https://www.kernel-operations.io/geomloss/)) for more details. The default loss is the Sinkhorn loss.

Parameters:
  • loss (Literal['sinkhorn', 'hausdorff', 'energy', 'gaussian', 'laplacian']) – The loss function to compute. Supported values are “sinkhorn”, “hausdorff”, “energy”, “gaussian” and “laplacian”.

  • **kwargs – additional arguments passed to the geomloss.SamplesLoss object.

skshapes.loss.varifold module

Nearest Neighbors loss for PolyData.

class skshapes.loss.varifold.VarifoldLoss(radial_kernel='Gaussian', zonal_kernel='Cauchy-Binet', radial_bandwidth=0.1)

Bases: BaseLoss

Varifold Loss.

The formula implemented here is based on the paper “Elastic shape analysis of surfaces with second-order Sobolev metrics: a comprehensive numerical framework” (https://arxiv.org/abs/2204.04238), equation (4.4).

Parameters:
  • radial_kernel (Literal['Gaussian', 'uniform']) – The radial kernel (between point positions)

  • zonal_kernel (Literal['Cauchy-Binet']) – The zonal kernel (between triangles normals)

  • radial_bandwidth (float) – The bandwidth for the radial kernel

skshapes.loss.varifold.extract_geom(shape)

Utility function to extract the geometry of a PolyData object.

Return type:

tuple[Float32[Tensor, '_ 2'] | Float32[Tensor, '_ 3'], Float32[Tensor, '_ 2'] | Float32[Tensor, '_ 3'], Float32[Tensor, '_']]

skshapes.loss.varifold.varifold_scalar(shape1, shape2, radial_kernel, sigma, zonal_kernel)

Compute the varifold loss between two shapes.

The formula implemented here is based on the paper “Elastic shape analysis of surfaces with second-order Sobolev metrics: a comprehensive numerical framework” (https://arxiv.org/abs/2204.04238), equation (4.4).

Parameters:
  • shape1 (polydata_type) – the first shape

  • shape2 (polydata_type) – the second shape

  • sigma – the bandwidth of the Gaussian kernel

Return type:

Float32[Tensor, '']