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, 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
) – IfTrue
(default), a gaussian kernel is used, ifFalse
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 individual batch scores
'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 fullssim
image is returned as a second argument. Mutually exclusive withreturn_contrast_sensitivity
return_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
kwargs¶ (
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([3, 3, 256, 256]) >>> target = preds * 0.75 >>> ssim = StructuralSimilarityIndexMeasure(data_range=1.0) >>> 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 fullssim
image is returned as a second argument. Mutually exclusive withreturn_contrast_sensitivity
return_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
preds
andtarget
don’t have the same data type.ValueError – If
preds
andtarget
don’t haveBxCxHxW shape
.ValueError – If the length of
kernel_size
orsigma
is not2
.ValueError – If one of the elements of
kernel_size
is not anodd positive number
.ValueError – If one of the elements of
sigma
is not apositive number
.
Example
>>> from torchmetrics.functional 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)