• Docs >
• Structural Similarity Index Measure (SSIM)
Shortcuts

# Structural Similarity Index Measure (SSIM)¶

## Module Interface¶

class torchmetrics.image.StructuralSimilarityIndexMeasure(gaussian_kernel=True, sigma=1.5, kernel_size=11, reduction='elementwise_mean', data_range=None, k1=0.01, k2=0.03, return_full_image=False, return_contrast_sensitivity=False, **kwargs)[source]

Compute Structural Similarity Index Measure (SSIM).

As input to forward and update the metric accepts the following input

As output of forward and compute the metric returns the following output

• ssim (Tensor): if reduction!='none' returns float scalar tensor with average SSIM value over sample else returns tensor of shape (N,) with SSIM values per sample

Parameters:

Example

>>> import torch
>>> from torchmetrics.image import StructuralSimilarityIndexMeasure
>>> preds = torch.rand([3, 3, 256, 256])
>>> target = preds * 0.75
>>> ssim = StructuralSimilarityIndexMeasure(data_range=1.0)
>>> ssim(preds, target)
tensor(0.9219)

plot(val=None, ax=None)[source]

Plot a single or multiple values from the metric.

Parameters:
Return type:
Returns:

Figure and Axes object

Raises:

ModuleNotFoundError – If matplotlib is not installed

>>> # Example plotting a single value
>>> import torch
>>> from torchmetrics.image import StructuralSimilarityIndexMeasure
>>> preds = torch.rand([3, 3, 256, 256])
>>> target = preds * 0.75
>>> metric = StructuralSimilarityIndexMeasure(data_range=1.0)
>>> metric.update(preds, target)
>>> fig_, ax_ = metric.plot()

>>> # Example plotting multiple values
>>> import torch
>>> from torchmetrics.image import StructuralSimilarityIndexMeasure
>>> preds = torch.rand([3, 3, 256, 256])
>>> target = preds * 0.75
>>> metric = StructuralSimilarityIndexMeasure(data_range=1.0)
>>> values = [ ]
>>> for _ in range(10):
...     values.append(metric(preds, target))
>>> fig_, ax_ = metric.plot(values)


## Functional Interface¶

torchmetrics.functional.image.structural_similarity_index_measure(preds, target, gaussian_kernel=True, sigma=1.5, kernel_size=11, reduction='elementwise_mean', data_range=None, k1=0.01, k2=0.03, return_full_image=False, return_contrast_sensitivity=False)[source]

Compute Structural Similarity Index Measure.

Parameters:
Return type:
Returns:

Tensor with SSIM score

Raises:
• TypeError – If preds and target don’t have the same data type.

• ValueError – If preds and target don’t have BxCxHxW shape.

• ValueError – If the length of kernel_size or sigma is not 2.

• ValueError – If one of the elements of kernel_size is not an odd positive number.

• ValueError – If one of the elements of sigma is not a positive number.

Example

>>> from torchmetrics.functional.image import structural_similarity_index_measure
>>> preds = torch.rand([3, 3, 256, 256])
>>> target = preds * 0.75
>>> structural_similarity_index_measure(preds, target)
tensor(0.9219)