ssim3d.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. from kornia import metrics
  19. from kornia.core import Module, Tensor
  20. def ssim3d_loss(
  21. img1: Tensor,
  22. img2: Tensor,
  23. window_size: int,
  24. max_val: float = 1.0,
  25. eps: float = 1e-12,
  26. reduction: str = "mean",
  27. padding: str = "same",
  28. ) -> Tensor:
  29. r"""Compute a loss based on the SSIM measurement.
  30. The loss, or the Structural dissimilarity (DSSIM) is described as:
  31. .. math::
  32. \text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}
  33. See :meth:`~kornia.losses.ssim` for details about SSIM.
  34. Args:
  35. img1: the first input image with shape :math:`(B, C, D, H, W)`.
  36. img2: the second input image with shape :math:`(B, C, D, H, W)`.
  37. window_size: the size of the gaussian kernel to smooth the images.
  38. max_val: the dynamic range of the images.
  39. eps: Small value for numerically stability when dividing.
  40. reduction : Specifies the reduction to apply to the
  41. output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
  42. ``'mean'``: the sum of the output will be divided by the number of elements
  43. in the output, ``'sum'``: the output will be summed.
  44. padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
  45. area to compute SSIM to match the MATLAB implementation of original SSIM paper.
  46. Returns:
  47. The loss based on the ssim index.
  48. Examples:
  49. >>> input1 = torch.rand(1, 4, 5, 5, 5)
  50. >>> input2 = torch.rand(1, 4, 5, 5, 5)
  51. >>> loss = ssim3d_loss(input1, input2, 5)
  52. """
  53. # compute the ssim map
  54. ssim_map: Tensor = metrics.ssim3d(img1, img2, window_size, max_val, eps, padding)
  55. # compute and reduce the loss
  56. loss = 1.0 - ssim_map
  57. if reduction == "mean":
  58. loss = loss.mean()
  59. elif reduction == "sum":
  60. loss = loss.sum()
  61. elif reduction == "none":
  62. pass
  63. else:
  64. raise NotImplementedError("Invalid reduction option.")
  65. return loss
  66. class SSIM3DLoss(Module):
  67. r"""Create a criterion that computes a loss based on the SSIM measurement.
  68. The loss, or the Structural dissimilarity (DSSIM) is described as:
  69. .. math::
  70. \text{loss}(x, y) = \frac{1 - \text{SSIM}(x, y)}{2}
  71. See :meth:`~kornia.losses.ssim_loss` for details about SSIM.
  72. Args:
  73. window_size: the size of the gaussian kernel to smooth the images.
  74. max_val: the dynamic range of the images.
  75. eps: Small value for numerically stability when dividing.
  76. reduction : Specifies the reduction to apply to the
  77. output: ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
  78. ``'mean'``: the sum of the output will be divided by the number of elements
  79. in the output, ``'sum'``: the output will be summed.
  80. padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
  81. area to compute SSIM to match the MATLAB implementation of original SSIM paper.
  82. Returns:
  83. The loss based on the ssim index.
  84. Examples:
  85. >>> input1 = torch.rand(1, 4, 5, 5, 5)
  86. >>> input2 = torch.rand(1, 4, 5, 5, 5)
  87. >>> criterion = SSIM3DLoss(5)
  88. >>> loss = criterion(input1, input2)
  89. """
  90. def __init__(
  91. self, window_size: int, max_val: float = 1.0, eps: float = 1e-12, reduction: str = "mean", padding: str = "same"
  92. ) -> None:
  93. super().__init__()
  94. self.window_size: int = window_size
  95. self.max_val: float = max_val
  96. self.eps: float = eps
  97. self.reduction: str = reduction
  98. self.padding: str = padding
  99. def forward(self, img1: Tensor, img2: Tensor) -> Tensor:
  100. return ssim3d_loss(img1, img2, self.window_size, self.max_val, self.eps, self.reduction, self.padding)