pytorch ssim calculation

In PyTorch, the Structural Similarity Index (SSIM) can be calculated using the SSIM function in the torchvision library.

The signature of the SSIM function is as follows:

torchvision.metrics.SSIM(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 the input data, usually 1.0 or 255.0.
win_size: The size of the sliding window.
win_sigma: Gaussian kernel standard deviation for sliding windows.
k1, k2: constants in the SSIM calculation formula.
nonnegative_ssim: Whether to limit SSIM to a non-negative range.
eps: parameter for numerical stability.
reduction: The dimensionality reduction method used to calculate the loss, which can take the value of mean, sum or none.
The following is an example of using the SSIM function to calculate the SSIM value of two images:

import torch
import torchvision.transforms.functional as F
import torchvision.metrics as metrics

# 读取两张图片
img1 = F.to_tensor(F.resize(F.pil_loader('img1.jpg'), (256, 256))).unsqueeze(0)
img2 = F.to_tensor(F.resize(F.pil_loader('img2.jpg'), (256, 256))).unsqueeze(0)

# 计算 SSIM
ssim = metrics.SSIM(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, img1.jpg and img2.jpg are two images to be compared. First use the pil_loader function to read the image, then use the resize function to resize the image to 256x256, and finally use the to_tensor function to convert the PIL.Image object to a PyTorch tensor.

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

Calculated using skimage

from skimage.measure 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=target.max() - target.min(),
    )

    return cur_ssim

SSIM value range

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

1:完美匹配
0.9 - 1:非常好
0.7 - 0.9:良好
0.5 - 0.7:一般
0.3 - 0.5:差
0 - 0.3:非常差

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 how it compares to other images. Therefore, when evaluating image quality, multiple SSIM values ​​should be used for comparison in order to draw more accurate conclusions.

About taking negative numbers : SSIM (Structural Similarity Index) can take negative numbers. The value range of SSIM is between -1 and 1, where 1 means that the two images are identical, 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 drop below 0, which usually occurs when one of the two images being compared has negative valued pixels. In this case, SSIM will return a negative number. Therefore, when using SSIM as an image quality metric, care needs to be taken to check for negative SSIM values ​​and interpret them.

Guess you like

Origin blog.csdn.net/qq_43369406/article/details/129211386