SoFunction
Updated on 2025-03-01

pytorch ssim calculation detailed code example

Preface

In PyTorch, you can use the SSIM function in the torchvision library to calculate the Structural Similarity Index (SSIM).

The signature of the SSIM function is as follows:

(data_range: Union[int, float] = 1, win_size: int = 11, win_sigma: float = 1.5, k1: float = 0.01, k2: float = 0.03, nonnegative_ssim: bool = False, eps: float = 1e-8, reduction: str = 'mean')

Among them, the meaning of the parameters is as follows:

  • data_range: The range of input data, usually 1.0 or 255.0.
  • win_size: The size of the sliding window.
  • win_sigma: Gaussian kernel standard deviation of sliding window.
  • k1, k2: Constant in SSIM calculation formula.
  • nonnegative_ssim: Whether to limit SSIM to a non-negative range.
  • eps: parameter of numerical stability.
  • reduction: The dimensionality reduction method used to calculate the loss, which can be taken as mean, sum, or none.

Here is an example of using the SSIM function to calculate the SSIM value of two images:

import torch
import  as F
import  as metrics

# Read two picturesimg1 = F.to_tensor((F.pil_loader(''), (256, 256))).unsqueeze(0)
img2 = F.to_tensor((F.pil_loader(''), (256, 256))).unsqueeze(0)

# Calculate SSIMssim = (data_range=1.0, win_size=11, win_sigma=1.5, k1=0.01, k2=0.03, eps=1e-8, reduction='mean')
print(ssim(img1, img2))

Among them, and are two pictures to be compared. First, use the pil_loader function to read the picture, then use the resize function to resize the picture to 256x256, and finally use the to_tensor function to convert the object to a PyTorch tensor.

When calculating the SSIM value, we need to create an SSIM object first and then pass in two images as parameters. The result of the calculation will be a scalar tensor

Calculate using skimage

from  import compare_ssim as ssim


def ssim_metric(target: object, prediction: object, win_size: int=21):
    """
    introduce:
        calculate ssim.
        
    args:
        :param ndarray target: target, like ndarray[256, 256].
        :param ndarray prediction: prediction, like ndarray[256, 256].
        :param int win_size: default.
    
    return:
        :param float cur_ssim: return ssim, between [-1, 1], like 0.72.
    """
    cur_ssim = ssim(
        target,
        prediction,
        win_size=win_size,
        data_range=() - (),
    )

    return cur_ssim

SSIM value range

SSIM (Structural Similarity Index) is a method used to measure image quality. Its value range is between -1 and 1, where 1 means that the two images are exactly the same and -1 means that the two images are completely different. Generally speaking, the higher the SSIM value, the more similar the two images are, the better the quality. Common SSIM values ​​ranges are as follows:

1: Perfect match
0.9 - 1: Very good
0.7 - 0.9: Good
0.5 - 0.7: General
0.3 - 0.5: Poor
0 - 0.3: Very bad

It should be noted that SSIM is a relative measure, not an absolute measure. This means that the actual significance of the SSIM value depends on the results of its comparison with other images. Therefore, when evaluating image quality, multiple SSIM values ​​should be used for comparison in order to draw more accurate conclusions.

About getting negative numbers: SSIM (Structural Similarity Index) can obtain negative numbers. The value range of SSIM is between -1 and 1, where 1 means that the two images are exactly the same, 0 means that the two images have no similarity, and -1 means that the two images are completely different. In practical applications, the SSIM value is usually between 0 and 1, indicating that the higher the similarity of the image, the closer the SSIM value is to 1. However, in some cases, the SSIM value may be lower than 0, which usually occurs when one of the two images compared has a negative value pixel. In this case, SSIM will return a negative number. Therefore, when using SSIM as an image quality metric, it is necessary to pay attention to check whether the SSIM value is negative and explain it.

Attachment: Parameters of SSIM loss function in PyTorch

The F.ssim_loss function has some optional parameters that can be used to adjust the way the SSIM loss is calculated. Here are some common parameters:

  • data_range: Specifies the range of image pixel values, default is 1.0. If the image pixel value is between 0 and 1, the default value can be maintained.
  • size_average: Specifies whether to average SSIMs per pixel, default is True. If set to False, the SSIM value for each pixel will be returned.

For example, if you want to calculate the SSIM value for each pixel, and the image pixel value ranges from 0 to 255, you can use the following code:

import torch
import  as F

# Generate two images randomly, assuming the image size is 256x256x = ((1, 3, 256, 256))
y = ((1, 3, 256, 256))

# Calculate the SSIM loss for each pixel, with the image pixel value range from 0 to 255loss = 1 - F.ssim_loss(x, y, data_range=255, size_average=False)

print(loss)

Summarize

This is all about this article about pytorch ssim calculation. For more related pytorch ssim calculation content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!