ssim.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # LICENSE HEADER MANAGED BY add-license-header
  2. #
  3. # Copyright 2018 Kornia Team
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. from __future__ import annotations
  18. import torch
  19. from torch import nn
  20. from kornia import metrics
  21. def ssim_loss(
  22. img1: torch.Tensor,
  23. img2: torch.Tensor,
  24. window_size: int,
  25. max_val: float = 1.0,
  26. eps: float = 1e-12,
  27. reduction: str = "mean",
  28. padding: str = "same",
  29. ) -> torch.Tensor:
  30. r"""Compute a loss based on the SSIM measurement.
  31. The loss, or the Structural dissimilarity (DSSIM) is described as:
  32. .. math::
  33. \text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}
  34. See :meth:`~kornia.losses.ssim` for details about SSIM.
  35. Args:
  36. img1: the first input image with shape :math:`(B, C, H, W)`.
  37. img2: the second input image with shape :math:`(B, C, H, W)`.
  38. window_size: the size of the gaussian kernel to smooth the images.
  39. max_val: the dynamic range of the images.
  40. eps: Small value for numerically stability when dividing.
  41. reduction : Specifies the reduction to apply to the
  42. output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
  43. ``'mean'``: the sum of the output will be divided by the number of elements
  44. in the output, ``'sum'``: the output will be summed.
  45. padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
  46. area to compute SSIM to match the MATLAB implementation of original SSIM paper.
  47. Returns:
  48. The loss based on the ssim index.
  49. Examples:
  50. >>> input1 = torch.rand(1, 4, 5, 5)
  51. >>> input2 = torch.rand(1, 4, 5, 5)
  52. >>> loss = ssim_loss(input1, input2, 5)
  53. """
  54. # compute the ssim map
  55. ssim_map: torch.Tensor = metrics.ssim(img1, img2, window_size, max_val, eps, padding)
  56. # compute and reduce the loss
  57. loss = torch.clamp((1.0 - ssim_map) / 2, min=0, max=1)
  58. if reduction == "mean":
  59. loss = torch.mean(loss)
  60. elif reduction == "sum":
  61. loss = torch.sum(loss)
  62. elif reduction == "none":
  63. pass
  64. else:
  65. raise NotImplementedError("Invalid reduction option.")
  66. return loss
  67. class SSIMLoss(nn.Module):
  68. r"""Create a criterion that computes a loss based on the SSIM measurement.
  69. The loss, or the Structural dissimilarity (DSSIM) is described as:
  70. .. math::
  71. \text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}
  72. See :meth:`~kornia.losses.ssim_loss` for details about SSIM.
  73. Args:
  74. window_size: the size of the gaussian kernel to smooth the images.
  75. max_val: the dynamic range of the images.
  76. eps: Small value for numerically stability when dividing.
  77. reduction : Specifies the reduction to apply to the
  78. output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
  79. ``'mean'``: the sum of the output will be divided by the number of elements
  80. in the output, ``'sum'``: the output will be summed.
  81. padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
  82. area to compute SSIM to match the MATLAB implementation of original SSIM paper.
  83. Returns:
  84. The loss based on the ssim index.
  85. Examples:
  86. >>> input1 = torch.rand(1, 4, 5, 5)
  87. >>> input2 = torch.rand(1, 4, 5, 5)
  88. >>> criterion = SSIMLoss(5)
  89. >>> loss = criterion(input1, input2)
  90. """
  91. def __init__(
  92. self, window_size: int, max_val: float = 1.0, eps: float = 1e-12, reduction: str = "mean", padding: str = "same"
  93. ) -> None:
  94. super().__init__()
  95. self.window_size: int = window_size
  96. self.max_val: float = max_val
  97. self.eps: float = eps
  98. self.reduction: str = reduction
  99. self.padding: str = padding
  100. def forward(self, img1: torch.Tensor, img2: torch.Tensor) -> torch.Tensor:
  101. return ssim_loss(img1, img2, self.window_size, self.max_val, self.eps, self.reduction, self.padding)