Skip to content

Metrics

Source

Metrics submodule.

This module contains various metrics and a metrics tracking class.

RunningPSNR

Compute the running PSNR during validation step in training.

This class allows to compute the PSNR on the entire validation set one batch at the time.

Attributes:

Name Type Description
N int

Number of elements seen so far during the epoch.

mse_sum float

Running sum of the MSE over the N elements seen so far.

max float

Running max value of the N target images seen so far.

min float

Running min value of the N target images seen so far.

get()

Get the actual PSNR value given the running statistics.

Returns:

Type Description
Optional[Tensor]

PSNR value.

reset()

Reset the running PSNR computation.

Usually called at the end of each epoch.

update(rec, tar)

Update the running PSNR statistics given a new batch.

Parameters:

Name Type Description Default
rec Tensor

Reconstructed batch.

required
tar Tensor

Target batch.

required

avg_psnr(target, prediction)

Compute the average PSNR over a batch of images.

Parameters:

Name Type Description Default
target ndarray

Array of ground truth images, shape is (N, C, H, W).

required
prediction ndarray

Array of predicted images, shape is (N, C, H, W).

required

Returns:

Type Description
float

Average PSNR value over the batch.

avg_range_inv_psnr(target, prediction)

Compute the average range-invariant PSNR over a batch of images.

Parameters:

Name Type Description Default
target ndarray

Array of ground truth images, shape is (N, C, H, W).

required
prediction ndarray

Array of predicted images, shape is (N, C, H, W).

required

Returns:

Type Description
float

Average range-invariant PSNR value over the batch.

avg_range_invariant_psnr(pred, target)

Compute the average range-invariant PSNR.

Parameters:

Name Type Description Default
pred ndarray

Predicted images.

required
target ndarray

Target images.

required

Returns:

Type Description
float

Average range-invariant PSNR value.

avg_ssim(target, prediction)

Compute the average Structural Similarity (SSIM) over a batch of images.

Parameters:

Name Type Description Default
target ndarray

Array of ground truth images, shape is (N, C, H, W).

required
prediction ndarray

Array of predicted images, shape is (N, C, H, W).

required

Returns:

Type Description
tuple[float, float]

Mean and standard deviation of SSIM values over the batch.

multiscale_ssim(gt_, pred_, range_invariant=True)

Compute channel-wise multiscale SSIM for each channel.

It allows to use either standard multiscale SSIM or its range-invariant version.

NOTE: images fed to this function should have channels dimension as the last one.

TODO: do we want to allow this behavior? or we want the usual (N, C, H, W)?

Parameters:

Name Type Description Default
gt_ Union[ndarray, Tensor]

Ground truth image with shape (N, H, W, C).

required
pred_ Union[ndarray, Tensor]

Predicted image with shape (N, H, W, C).

required
range_invariant bool

Whether to use standard or range invariant multiscale SSIM.

True

Returns:

Type Description
list[float]

List of SSIM values for each channel.

psnr(gt, pred, data_range)

Peak Signal to Noise Ratio.

This method calls skimage.metrics.peak_signal_noise_ratio. See: https://scikit-image.org/docs/dev/api/skimage.metrics.html.

NOTE: to avoid unwanted behaviors (e.g., data_range inferred from array dtype), the data_range parameter is mandatory.

Parameters:

Name Type Description Default
gt ndarray

Ground truth array.

required
pred ndarray

Predicted array.

required
data_range float

The images pixel range.

required

Returns:

Type Description
float

PSNR value.

scale_invariant_psnr(gt, pred)

Scale invariant PSNR.

Parameters:

Name Type Description Default
gt ndarray

Ground truth image.

required
pred ndarray

Predicted image.

required

Returns:

Type Description
Union[float, tensor]

Scale invariant PSNR value.