Structural Similarity Index Measure (SSIM)¶
Module Interface¶
- class torchmetrics.StructuralSimilarityIndexMeasure(gaussian_kernel=True, sigma=1.5, kernel_size=11, reduction='elementwise_mean', data_range=None, k1=0.01, k2=0.03, compute_on_step=None, return_full_image=False, return_contrast_sensitivity=False, **kwargs)[source]
Computes Structual Similarity Index Measure (SSIM).
- Parameters
preds¶ – estimated image
target¶ – ground truth image
gaussian_kernel¶ (
bool) – If true (default), a gaussian kernel is used, if false a uniform kernel is usedsigma¶ (
Union[float,Sequence[float]]) – Standard deviation of the gaussian kernel, anisotropic kernels are possible. Ignored if a uniform kernel is usedkernel_size¶ (
Union[int,Sequence[int]]) – the size of the uniform kernel, anisotropic kernels are possible. Ignored if a Gaussian kernel is usedreduction¶ (
Literal[‘elementwise_mean’, ‘sum’, ‘none’, None]) –a method to reduce metric score over labels.
'elementwise_mean': takes the mean'sum': takes the sum'none'orNone: no reduction will be applied
data_range¶ (
Optional[float]) – Range of the image. IfNone, it is determined from the image (max - min)return_full_image¶ (
bool) – If true, the fullssimimage is returned as a second argument. Mutually exclusive withreturn_contrast_sensitivityreturn_contrast_sensitivity¶ (
bool) – If true, the constant term is returned as a second argument. The luminance term can be obtained with luminance=ssim/contrast Mutually exclusive withreturn_full_imagecompute_on_step¶ (
Optional[bool]) –Forward only calls
update()and returns None if this is set to False.Deprecated since version v0.8: Argument has no use anymore and will be removed v0.9.
kwargs¶ (
Dict[str,Any]) – Additional keyword arguments, see Advanced metric settings for more info.
- Returns
Tensor with SSIM score
Example
>>> from torchmetrics import StructuralSimilarityIndexMeasure >>> import torch >>> preds = torch.rand([16, 1, 16, 16]) >>> target = preds * 0.75 >>> ssim = StructuralSimilarityIndexMeasure() >>> ssim(preds, target) tensor(0.9219)
Initializes internal Module state, shared by both nn.Module and ScriptModule.
Functional Interface¶
- torchmetrics.functional.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]
Computes Structual Similarity Index Measure.
- Parameters
gaussian_kernel¶ (
bool) – If true (default), a gaussian kernel is used, if false a uniform kernel is usedsigma¶ (
Union[float,Sequence[float]]) – Standard deviation of the gaussian kernel, anisotropic kernels are possible. Ignored if a uniform kernel is usedkernel_size¶ (
Union[int,Sequence[int]]) – the size of the uniform kernel, anisotropic kernels are possible. Ignored if a Gaussian kernel is usedreduction¶ (
Literal[‘elementwise_mean’, ‘sum’, ‘none’, None]) –a method to reduce metric score over labels.
'elementwise_mean': takes the mean'sum': takes the sum'none'orNone: no reduction will be applied
data_range¶ (
Optional[float]) – Range of the image. IfNone, it is determined from the image (max - min)return_full_image¶ (
bool) – If true, the fullssimimage is returned as a second argument. Mutually exclusive withreturn_contrast_sensitivityreturn_contrast_sensitivity¶ (
bool) – If true, the constant term is returned as a second argument. The luminance term can be obtained with luminance=ssim/contrast Mutually exclusive withreturn_full_image
- Return type
- Returns
Tensor with SSIM score
- Raises
TypeError – If
predsandtargetdon’t have the same data type.ValueError – If
predsandtargetdon’t haveBxCxHxW shape.ValueError – If the length of
kernel_sizeorsigmais not2.ValueError – If one of the elements of
kernel_sizeis not anodd positive number.ValueError – If one of the elements of
sigmais not apositive number.
Example
>>> from torchmetrics.functional import structural_similarity_index_measure >>> preds = torch.rand([16, 1, 16, 16]) >>> target = preds * 0.75 >>> structural_similarity_index_measure(preds, target) tensor(0.9219)