ssim3d.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 typing import List
  18. from kornia.core import Module, Tensor, pad
  19. from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE
  20. from kornia.filters import filter3d, get_gaussian_kernel3d
  21. from kornia.filters.filter import _compute_padding
  22. def _crop(img: Tensor, cropping_shape: List[int]) -> Tensor:
  23. """Crop out the part of "valid" convolution area."""
  24. return pad(
  25. img,
  26. (
  27. -cropping_shape[4],
  28. -cropping_shape[5],
  29. -cropping_shape[2],
  30. -cropping_shape[3],
  31. -cropping_shape[0],
  32. -cropping_shape[1],
  33. ),
  34. )
  35. def ssim3d(
  36. img1: Tensor, img2: Tensor, window_size: int, max_val: float = 1.0, eps: float = 1e-12, padding: str = "same"
  37. ) -> Tensor:
  38. r"""Compute the Structural Similarity (SSIM) index map between two images.
  39. Measures the (SSIM) index between each element in the input `x` and target `y`.
  40. The index can be described as:
  41. .. math::
  42. \text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)}
  43. {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}
  44. where:
  45. - :math:`c_1=(k_1 L)^2` and :math:`c_2=(k_2 L)^2` are two variables to
  46. stabilize the division with weak denominator.
  47. - :math:`L` is the dynamic range of the pixel-values (typically this is
  48. :math:`2^{\#\text{bits per pixel}}-1`).
  49. Args:
  50. img1: the first input image with shape :math:`(B, C, D, H, W)`.
  51. img2: the second input image with shape :math:`(B, C, D, H, W)`.
  52. window_size: the size of the gaussian kernel to smooth the images.
  53. max_val: the dynamic range of the images.
  54. eps: Small value for numerically stability when dividing.
  55. padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
  56. area to compute SSIM to match the MATLAB implementation of original SSIM paper.
  57. Returns:
  58. The ssim index map with shape :math:`(B, C, D, H, W)`.
  59. Examples:
  60. >>> input1 = torch.rand(1, 4, 5, 5, 5)
  61. >>> input2 = torch.rand(1, 4, 5, 5, 5)
  62. >>> ssim_map = ssim3d(input1, input2, 5) # 1x4x5x5x5
  63. """
  64. KORNIA_CHECK_IS_TENSOR(img1)
  65. KORNIA_CHECK_IS_TENSOR(img2)
  66. KORNIA_CHECK_SHAPE(img1, ["B", "C", "D", "H", "W"])
  67. KORNIA_CHECK_SHAPE(img2, ["B", "C", "D", "H", "W"])
  68. KORNIA_CHECK(img1.shape == img2.shape, f"img1 and img2 shapes must be the same. Got: {img1.shape} and {img2.shape}")
  69. if not isinstance(max_val, float):
  70. raise TypeError(f"Input max_val type is not a float. Got {type(max_val)}")
  71. # prepare kernel
  72. kernel: Tensor = get_gaussian_kernel3d((window_size, window_size, window_size), (1.5, 1.5, 1.5))
  73. # compute coefficients
  74. C1: float = (0.01 * max_val) ** 2
  75. C2: float = (0.03 * max_val) ** 2
  76. # compute local mean per channel
  77. mu1: Tensor = filter3d(img1, kernel)
  78. mu2: Tensor = filter3d(img2, kernel)
  79. cropping_shape: List[int] = []
  80. if padding == "valid":
  81. depth, height, width = kernel.shape[-3:]
  82. cropping_shape = _compute_padding([depth, height, width])
  83. mu1 = _crop(mu1, cropping_shape)
  84. mu2 = _crop(mu2, cropping_shape)
  85. elif padding == "same":
  86. pass
  87. mu1_sq = mu1**2
  88. mu2_sq = mu2**2
  89. mu1_mu2 = mu1 * mu2
  90. mu_img1_sq = filter3d(img1**2, kernel)
  91. mu_img2_sq = filter3d(img2**2, kernel)
  92. mu_img1_img2 = filter3d(img1 * img2, kernel)
  93. if padding == "valid":
  94. mu_img1_sq = _crop(mu_img1_sq, cropping_shape)
  95. mu_img2_sq = _crop(mu_img2_sq, cropping_shape)
  96. mu_img1_img2 = _crop(mu_img1_img2, cropping_shape)
  97. elif padding == "same":
  98. pass
  99. # compute local sigma per channel
  100. sigma1_sq = mu_img1_sq - mu1_sq
  101. sigma2_sq = mu_img2_sq - mu2_sq
  102. sigma12 = mu_img1_img2 - mu1_mu2
  103. # compute the similarity index map
  104. num: Tensor = (2.0 * mu1_mu2 + C1) * (2.0 * sigma12 + C2)
  105. den: Tensor = (mu1_sq + mu2_sq + C1) * (sigma1_sq + sigma2_sq + C2)
  106. return num / (den + eps)
  107. class SSIM3D(Module):
  108. r"""Create a module that computes the Structural Similarity (SSIM) index between two 3D images.
  109. Measures the (SSIM) index between each element in the input `x` and target `y`.
  110. The index can be described as:
  111. .. math::
  112. \text{SSIM}(x, y) = \frac{(2\mu_x\mu_y+c_1)(2\sigma_{xy}+c_2)}
  113. {(\mu_x^2+\mu_y^2+c_1)(\sigma_x^2+\sigma_y^2+c_2)}
  114. where:
  115. - :math:`c_1=(k_1 L)^2` and :math:`c_2=(k_2 L)^2` are two variables to
  116. stabilize the division with weak denominator.
  117. - :math:`L` is the dynamic range of the pixel-values (typically this is
  118. :math:`2^{\#\text{bits per pixel}}-1`).
  119. Args:
  120. window_size: the size of the gaussian kernel to smooth the images.
  121. max_val: the dynamic range of the images.
  122. eps: Small value for numerically stability when dividing.
  123. padding: ``'same'`` | ``'valid'``. Whether to only use the "valid" convolution
  124. area to compute SSIM to match the MATLAB implementation of original SSIM paper.
  125. Shape:
  126. - Input: :math:`(B, C, D, H, W)`.
  127. - Target :math:`(B, C, D, H, W)`.
  128. - Output: :math:`(B, C, D, H, W)`.
  129. Examples:
  130. >>> input1 = torch.rand(1, 4, 5, 5, 5)
  131. >>> input2 = torch.rand(1, 4, 5, 5, 5)
  132. >>> ssim = SSIM3D(5)
  133. >>> ssim_map = ssim(input1, input2) # 1x4x5x5x5
  134. """
  135. def __init__(self, window_size: int, max_val: float = 1.0, eps: float = 1e-12, padding: str = "same") -> None:
  136. super().__init__()
  137. self.window_size: int = window_size
  138. self.max_val: float = max_val
  139. self.eps = eps
  140. self.padding = padding
  141. def forward(self, img1: Tensor, img2: Tensor) -> Tensor:
  142. return ssim3d(img1, img2, self.window_size, self.max_val, self.eps, self.padding)