tv.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # Copyright The Lightning team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from typing import Optional, Union
  15. from torch import Tensor
  16. from typing_extensions import Literal
  17. def _total_variation_update(img: Tensor) -> tuple[Tensor, int]:
  18. """Compute total variation statistics on current batch."""
  19. if img.ndim != 4:
  20. raise RuntimeError(f"Expected input `img` to be an 4D tensor, but got {img.shape}")
  21. diff1 = img[..., 1:, :] - img[..., :-1, :]
  22. diff2 = img[..., :, 1:] - img[..., :, :-1]
  23. res1 = diff1.abs().sum([1, 2, 3])
  24. res2 = diff2.abs().sum([1, 2, 3])
  25. score = res1 + res2
  26. return score, img.shape[0]
  27. def _total_variation_compute(
  28. score: Tensor, num_elements: Union[int, Tensor], reduction: Optional[Literal["mean", "sum", "none"]]
  29. ) -> Tensor:
  30. """Compute final total variation score."""
  31. if reduction == "mean":
  32. return score.sum() / num_elements
  33. if reduction == "sum":
  34. return score.sum()
  35. if reduction is None or reduction == "none":
  36. return score
  37. raise ValueError("Expected argument `reduction` to either be 'sum', 'mean', 'none' or None")
  38. def total_variation(img: Tensor, reduction: Optional[Literal["mean", "sum", "none"]] = "sum") -> Tensor:
  39. """Compute total variation loss.
  40. Args:
  41. img: A `Tensor` of shape `(N, C, H, W)` consisting of images
  42. reduction: a method to reduce metric score over samples.
  43. - ``'mean'``: takes the mean over samples
  44. - ``'sum'``: takes the sum over samples
  45. - ``None`` or ``'none'``: return the score per sample
  46. Returns:
  47. A loss scalar value containing the total variation
  48. Raises:
  49. ValueError:
  50. If ``reduction`` is not one of ``'sum'``, ``'mean'``, ``'none'`` or ``None``
  51. RuntimeError:
  52. If ``img`` is not 4D tensor
  53. Example:
  54. >>> from torch import rand
  55. >>> from torchmetrics.functional.image import total_variation
  56. >>> img = rand(5, 3, 28, 28)
  57. >>> total_variation(img)
  58. tensor(7546.8018)
  59. """
  60. # code adapted from:
  61. # from kornia.losses import total_variation as kornia_total_variation
  62. score, num_elements = _total_variation_update(img)
  63. return _total_variation_compute(score, num_elements, reduction)