rase.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright The PyTorch 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. import torch
  15. from torch import Tensor
  16. from torchmetrics.functional.image.rmse_sw import _rmse_sw_compute, _rmse_sw_update
  17. from torchmetrics.functional.image.utils import _uniform_filter
  18. def _rase_update(
  19. preds: Tensor, target: Tensor, window_size: int, rmse_map: Tensor, target_sum: Tensor, total_images: Tensor
  20. ) -> tuple[Tensor, Tensor, Tensor]:
  21. """Calculate the sum of RMSE map values for the batch of examples and update intermediate states.
  22. Args:
  23. preds: Deformed image
  24. target: Ground truth image
  25. window_size: Sliding window used for RMSE calculation
  26. rmse_map: Sum of RMSE map values over all examples
  27. target_sum: target...
  28. total_images: Total number of images
  29. Return:
  30. Intermediate state of RMSE map
  31. Updated total number of already processed images
  32. """
  33. _, rmse_map, total_images = _rmse_sw_update(
  34. preds, target, window_size, rmse_val_sum=None, rmse_map=rmse_map, total_images=total_images
  35. )
  36. target_sum += torch.sum(_uniform_filter(target, window_size) / (window_size**2), dim=0)
  37. return rmse_map, target_sum, total_images
  38. def _rase_compute(rmse_map: Tensor, target_sum: Tensor, total_images: Tensor, window_size: int) -> Tensor:
  39. """Compute RASE.
  40. Args:
  41. rmse_map: Sum of RMSE map values over all examples
  42. target_sum: target...
  43. total_images: Total number of images.
  44. window_size: Sliding window used for rmse calculation
  45. Return:
  46. Relative Average Spectral Error (RASE)
  47. """
  48. _, rmse_map = _rmse_sw_compute(rmse_val_sum=None, rmse_map=rmse_map, total_images=total_images)
  49. target_mean = target_sum / total_images
  50. target_mean = target_mean.mean(0) # mean over image channels
  51. rase_map = 100 / target_mean * torch.sqrt(torch.mean(rmse_map**2, 0))
  52. crop_slide = round(window_size / 2)
  53. return torch.mean(rase_map[crop_slide:-crop_slide, crop_slide:-crop_slide])
  54. def relative_average_spectral_error(preds: Tensor, target: Tensor, window_size: int = 8) -> Tensor:
  55. """Compute Relative Average Spectral Error (RASE) (RelativeAverageSpectralError_).
  56. Args:
  57. preds: Deformed image
  58. target: Ground truth image
  59. window_size: Sliding window used for rmse calculation
  60. Return:
  61. Relative Average Spectral Error (RASE)
  62. Example:
  63. >>> from torch import rand
  64. >>> from torchmetrics.functional.image import relative_average_spectral_error
  65. >>> preds = rand(4, 3, 16, 16)
  66. >>> target = rand(4, 3, 16, 16)
  67. >>> relative_average_spectral_error(preds, target)
  68. tensor(5326.40...)
  69. Raises:
  70. ValueError: If ``window_size`` is not a positive integer.
  71. """
  72. if not isinstance(window_size, int) or (isinstance(window_size, int) and window_size < 1):
  73. raise ValueError("Argument `window_size` is expected to be a positive integer.")
  74. img_shape = target.shape[1:] # [num_channels, width, height]
  75. rmse_map = torch.zeros(img_shape, dtype=target.dtype, device=target.device)
  76. target_sum = torch.zeros(img_shape, dtype=target.dtype, device=target.device)
  77. total_images = torch.tensor(0.0, device=target.device)
  78. rmse_map, target_sum, total_images = _rase_update(preds, target, window_size, rmse_map, target_sum, total_images)
  79. return _rase_compute(rmse_map, target_sum, total_images, window_size)