Skip to content

Psnr

Source

PSNR metrics compatible with torchmetrics.

SIPSNR

Bases: Metric

Scale Invariant PSNR metric using a global data range.

By default, the metric is averaged over channels, but it can also be computed for a specific channel by setting output_channel to the desired channel index.

Adapted from juglab/ScaleInvPSNR, this version of PSNR rescales the predictions and ground truth to have similar range, then computes the PSNR using a global data range accumulated over all batches. For a scale-invariant version of PSNR with per-sample data range, see SampleSIPSNR.

Scale invariance can be turned off using use_scale_invariance=False, in which case the metric is equivalent to torchmetrics.image.PeakSignalNoiseRatio, with data_range equal to the difference between the global max and min over all batches.

Note that as opposed to torchmetrics.image.PeakSignalNoiseRatio, this implementation is compatible with 3D and can be computed on a single channel.

Parameters:

  • n_channels (int) –

    Number of channels in the input images.

  • output_channel (int, default: -1 ) –

    Channel to compute the metric on. If -1, the metric is computed on all channels.

  • use_scale_invariance (bool, default: True ) –

    Whether to use scale invariance. If False, the metric is equivalent to PSNR with global data range.

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments passed to the parent Metric class.

Attributes:

  • glob_max (Tensor) –

    Global maximum values for each channel.

  • glob_min (Tensor) –

    Global minimum values for each channel.

  • mse_log (Tensor) –

    Logarithm of the mean squared error summed over batches.

  • total (Tensor) –

    Total number of samples processed.

__init__(n_channels, output_channel=-1, use_scale_invariance=True, **kwargs)

Initialize a global scale invariant PSNR metric.

Parameters:

  • n_channels (int) –

    Number of channels in the input images.

  • output_channel (int, default: -1 ) –

    Channel to compute the metric on. If -1, the metric is computed on all channels.

  • use_scale_invariance (bool, default: True ) –

    Whether to use scale invariance. If False, the metric is equivalent to PSNR with global data range.

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments passed to the parent Metric class.

compute()

Compute the final metric value.

Returns:

  • Tensor

    Tensor of length C containing the computed PSNR for each channel.

update(preds, target)

Update the metric states with values computed from a new batch.

Parameters:

  • preds (Tensor) –

    Predicted images tensor of shape (B, C, (Z), Y, X).

  • target (Tensor) –

    Ground truth images tensor of shape (B, C, (Z), Y, X).

SampleSIPSNR

Bases: Metric

Scale Invariant PSNR metric with per-sample data range.

By default, the metric is averaged over channels, but it can also be computed for a specific channel by setting output_channel to the desired channel index.

Adapted from juglab/ScaleInvPSNR, this version of PSNR rescales the predictions and ground truth to have similar range, then computes the PSNR using each patch's data range.

Scale invariance can be turned off using use_scale_invariance=False, in which case the metric is equivalent to torchmetrics.image.PeakSignalNoiseRatio, with data_range equal to the difference between each patch's max and min, for each patch, then averaged.

Note that as opposed to torchmetrics.image.PeakSignalNoiseRatio, this implementation is compatible with 3D and multi-channel images.

Parameters:

  • n_channels (int) –

    Number of channels in the input images.

  • output_channel (int, default: -1 ) –

    Channel to compute the metric on. If -1, the metric is computed on all channels.

  • use_scale_invariance (bool, default: True ) –

    Whether to use scale invariance. If False, the metric is equivalent to PSNR with per-sample data range.

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments passed to the parent Metric class.

Attributes:

  • psnr_sum (Tensor) –

    Sum of PSNR values for each channel.

  • total (Tensor) –

    Total number of samples processed.

__init__(n_channels, output_channel=-1, use_scale_invariance=True, **kwargs)

Initialize a per-sample scale invariant PSNR metric.

Parameters:

  • n_channels (int) –

    Number of channels in the input images.

  • output_channel (int, default: -1 ) –

    Channel to compute the metric on. If -1, the metric is computed on all channels.

  • use_scale_invariance (bool, default: True ) –

    Whether to use scale invariance. If False, the metric is equivalent to PSNR with per-sample data range.

  • **kwargs (Any, default: {} ) –

    Additional keyword arguments passed to the parent Metric class.

compute()

Compute the final metric value.

Returns:

  • Tensor

    Tensor of length C containing the computed PSNR for each channel.

update(preds, target)

Update the metric states with values computed from a new batch.

Parameters:

  • preds (Tensor) –

    Predicted images tensor of shape (B, C, (Z), Y, X).

  • target (Tensor) –

    Ground truth images tensor of shape (B, C, (Z), Y, X).