metrics – Common Metrics

neuralnet_pytorch.metrics.huber_loss(x, y, reduce='mean')[source]

An alias for torch.nn.functional.smooth_l1_loss().

neuralnet_pytorch.metrics.first_derivative_loss(x, y, p=2)[source]

Calculates lp loss between the first derivatives of the inputs.

Parameters:
  • x – a torch.Tensor.
  • y – a torch.Tensor of the same shape as x.
  • p – order of the norm.
Returns:

the scalar loss between the first derivatives of the inputs.

neuralnet_pytorch.metrics.lp_loss(x, y, p=2, reduction='mean')[source]

Calculates p-norm of (x - y).

Parameters:
  • x – a torch.Tensor.
  • y – a torch.Tensor of the same shape as x.
  • p – order of the norm.
  • reduction'mean' or 'sum'.
Returns:

the p-norm of (x - y).

neuralnet_pytorch.metrics.ssim(img1, img2, max_val=1.0, filter_size=11, filter_sigma=1.5, k1=0.01, k2=0.03, cs_map=False)[source]

Returns the Structural Similarity Map between img1 and img2. This function attempts to match the functionality of ssim_index_new.m by Zhou Wang: http://www.cns.nyu.edu/~lcv/ssim/msssim.zip

Parameters:
  • img1 – a 4D torch.Tensor.
  • img2 – a 4D torch.Tensor of the same shape as img1.
  • max_val – the dynamic range of the images (i.e., the difference between the maximum the and minimum allowed values).
  • filter_size – size of blur kernel to use (will be reduced for small images).
  • filter_sigma – standard deviation for Gaussian blur kernel (will be reduced for small images).
  • k1 – constant used to maintain stability in the SSIM calculation (0.01 in the original paper).
  • k2 – constant used to maintain stability in the SSIM calculation (0.03 in the original paper).
Returns:

pair containing the mean SSIM and contrast sensitivity between img1 and img2.

Raise:

RuntimeError: If input images don’t have the same shape or don’t have four dimensions: [batch_size, height, width, depth].

neuralnet_pytorch.metrics.psnr(x, y)[source]

Peak-signal-to-noise ratio for [0,1] images.

Parameters:
  • x – a torch.Tensor.
  • y – a torch.Tensor of the same shape as x.
neuralnet_pytorch.metrics.chamfer_loss(xyz1, xyz2, reduce='mean', c_code=False)[source]

Calculates the Chamfer distance between two batches of point clouds. The Pytorch code is adapted from DenseLidarNet. The CUDA code is adapted from AtlasNet.

Parameters:
  • xyz1 – a point cloud of shape (b, n1, k) or (n1, k).
  • xyz2 – a point cloud of shape (b, n2, k) or (n2, k).
  • reduce'mean' or 'sum'. Default: 'mean'.
  • c_code – whether to use CUDA implementation. This version is much more memory-friendly and slightly faster.
Returns:

the Chamfer distance between the inputs.

neuralnet_pytorch.metrics.emd_loss(xyz1, xyz2, reduce='mean', sinkhorn=False)[source]

Calculates the Earth Mover Distance (or Wasserstein metric) between two sets of points.

Parameters:
  • xyz1 – a point cloud of shape (b, n1, k) or (n1, k).
  • xyz2 – a point cloud of shape (b, n2, k) or (n2, k).
  • reduce'mean' or 'sum'. Default: 'mean'.
  • sinkhorn – whether to use the Sinkhorn approximation of the Wasserstein distance. False will fall back to a CUDA implementation, which is only available if the CUDA-extended neuralnet-pytorch is installed. Default: True.
Returns:

the EMD between the inputs.

neuralnet_pytorch.metrics.tv_reg(y)[source]

Total variation regularization.

Parameters:y – a tensor of at least 2D. The last 2 dimensions will be regularized.
Returns:the total variation loss.
neuralnet_pytorch.metrics.spectral_norm(module, name='weight', n_power_iterations=1, eps=1e-12, dim=None)[source]

Applies torch.nn.utils.spectral_norm() recursively to module and all of its submodules.

Parameters:
  • module – containing module.
  • name – name of weight parameter. Default: 'weight'.
  • n_power_iterations – number of power iterations to calculate spectral norm.
  • eps – epsilon for numerical stability in calculating norms.
  • dim – dimension corresponding to number of outputs, the default is 0, except for modules that are instances of ConvTranspose{1,2,3}d, when it is 1.
Returns:

the original module with the spectral norm hook.