torchjpeg.metrics

Metrics

The torchjpeg.metrcs package provides useful metrics for measuring JPEG quality. All the metrics in this package take inputs in format \((N, C, H, W)\) and produces outputs of format \((N)\) by averaging spatially and over channels. The batch dimension is not averaged. Inputs should be images in [0, 1].

torchjpeg.metrics.psnr(image: torch.Tensor, target: torch.Tensor) → torch.Tensor[source]

Computes the peak signal-to-noise ratio on a batch of images.

Parameters:
  • image (Tensor) – Input images in format \((N, C, H, W)\).

  • target (Tensor) – Target images in format \((N, C, H, W)\).

Returns:

PSNR for each image in the batch, of shape \((N)\).

Return type:

Tensor

Note

Peak signal-to-noise ratio is an inverse log scale of the mean squared error measured in decibels. The formula used here is

\[P(x, y) = 10 \log_{10}\left(\frac{1}{\text{MSE}(x, y)}\right)\]
torchjpeg.metrics.psnrb(image: torch.Tensor, target: torch.Tensor) → torch.Tensor[source]

Computes the peak signal-to-noise ratio with blocking effect factor from [1].

PSNR-B augments the PSNR measure by including the “blockiness” of the degraded image as a way to reduce the PSNR. For multichannel inputs, the PSNR-B is computed separately for each channel and then averaged.

Parameters:
  • image (Tensor) – The input images of shape \((N, C, H, W)\).

  • target (Tensor) – The target images of shape \((N, C, H, W)\).

Returns:

The PSNR-B of each image in the batch of shape \((N)\).

Return type:

Tensor

Warning

Unlike most metrics this is not symmetric and the order of the arguents is imporant. Blocking effect factor is only computed for the degraded image, so if the arguments are reversed, there will be very little difference between this and psnr().

Note

PSNR-B is computed as

\[P(x, y) = 10 \log_{10}\left(\frac{1}{\text{MSE}(x, y) + \text{BEF}(x)}\right) \]

[1] Tadala, Trinadh, and Sri E. Venkata Narayana. “A Novel PSNR-B Approach for Evaluating the Quality of De-blocked Images.” (2012).

torchjpeg.metrics.blocking_effect_factor(im: torch.Tensor) → torch.Tensor[source]

Computes the blocking effect factor (BEF) of an image as defined in [1].

Blocking effect factor is used as part of psnrb() but can also be used as an objective measure of “blockiness”.

Parameters:

im (Tensor) – Image of shape \((N, C, H, W)\).

Returns:

The BEF for each image of shape \((N)\).

Return type:

Tensor

Note

[1] Tadala, Trinadh, and Sri E. Venkata Narayana. “A Novel PSNR-B Approach for Evaluating the Quality of De-blocked Images.” (2012).

torchjpeg.metrics.ssim(image: torch.Tensor, target: torch.Tensor) → torch.Tensor[source]

Computes the structural similarity index of two images as defined in [1].

Parameters:
  • image (Tensor) – The input images of shape \((N, C, H, W)\).

  • target (Tensor) – The target images of shape \((N, C, H, W)\).

Returns:

The SSIM of each image of shape \((N)\).

Return type:

Tensor

Note

This function uses an \(8 \times 8\) uniform averaging window used in JPEG evaluation tasks instead of the \(11 \times 11\) gaussian window used in the original paper and by default in other SSIM implementations.

[1] Wang, Zhou, et al. “Image quality assessment: from error visibility to structural similarity.” IEEE transactions on image processing 13.4 (2004): 600-612.

torchjpeg.metrics.size()[source]

Computes the size in bytes of a JPEG

Parameters:
  • image (Tensor or PIL Image) – The image to compress

  • kwargs – Arguments to pass to the PIL JPEG compressor (like quality or quantization matrices)

Returns:

  • Tensor – A single element tensor containing the size in bytes of the image after JPEG compression

  • Tensor – The compressed image

Warning

The output of this function is not differentiable. It compresses the image to memory and reads the size of the resulting buffer.