total_variation.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.core import Module, Tensor
  19. from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_SHAPE
  20. def total_variation(img: Tensor, reduction: str = "sum") -> Tensor:
  21. r"""Compute Total Variation according to [1].
  22. Args:
  23. img: the input image with shape :math:`(*, H, W)`.
  24. reduction : Specifies the reduction to apply to the output: ``'mean'`` | ``'sum'``.
  25. ``'mean'``: the sum of the output will be divided by the number of elements
  26. in the output, ``'sum'``: the output will be summed.
  27. Return:
  28. a tensor with shape :math:`(*,)`.
  29. Examples:
  30. >>> total_variation(torch.ones(4, 4))
  31. tensor(0.)
  32. >>> total_variation(torch.ones(2, 5, 3, 4, 4)).shape
  33. torch.Size([2, 5, 3])
  34. .. note::
  35. See a working example `here <https://kornia.github.io/tutorials/nbs/total_variation_denoising.html>`__.
  36. Total Variation is formulated with summation, however this is not resolution invariant.
  37. Thus, `reduction='mean'` was added as an optional reduction method.
  38. Reference:
  39. [1] https://en.wikipedia.org/wiki/Total_variation
  40. """
  41. # TODO: here torchscript doesn't like KORNIA_CHECK_TYPE
  42. if not isinstance(img, Tensor):
  43. raise TypeError(f"Not a Tensor type. Got: {type(img)}")
  44. KORNIA_CHECK_SHAPE(img, ["*", "H", "W"])
  45. KORNIA_CHECK(reduction in ("mean", "sum"), f"Expected reduction to be one of 'mean'/'sum', but got '{reduction}'.")
  46. pixel_dif1 = img[..., 1:, :] - img[..., :-1, :]
  47. pixel_dif2 = img[..., :, 1:] - img[..., :, :-1]
  48. res1 = pixel_dif1.abs()
  49. res2 = pixel_dif2.abs()
  50. reduce_axes = (-2, -1)
  51. if reduction == "mean":
  52. if img.is_floating_point():
  53. res1 = res1.to(img).mean(dim=reduce_axes)
  54. res2 = res2.to(img).mean(dim=reduce_axes)
  55. else:
  56. res1 = res1.float().mean(dim=reduce_axes)
  57. res2 = res2.float().mean(dim=reduce_axes)
  58. elif reduction == "sum":
  59. res1 = res1.sum(dim=reduce_axes)
  60. res2 = res2.sum(dim=reduce_axes)
  61. else:
  62. raise NotImplementedError("Invalid reduction option.")
  63. return res1 + res2
  64. class TotalVariation(Module):
  65. r"""Compute the Total Variation according to [1].
  66. Shape:
  67. - Input: :math:`(*, H, W)`.
  68. - Output: :math:`(*,)`.
  69. Examples:
  70. >>> tv = TotalVariation()
  71. >>> output = tv(torch.ones((2, 3, 4, 4), requires_grad=True))
  72. >>> output.data
  73. tensor([[0., 0., 0.],
  74. [0., 0., 0.]])
  75. >>> output.sum().backward() # grad can be implicitly created only for scalar outputs
  76. Reference:
  77. [1] https://en.wikipedia.org/wiki/Total_variation
  78. """
  79. def forward(self, img: Tensor) -> Tensor:
  80. return total_variation(img)