ps_roi_align.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import torch
  2. import torch.fx
  3. from torch import nn, Tensor
  4. from torch.nn.modules.utils import _pair
  5. from torchvision.extension import _assert_has_ops
  6. from ..utils import _log_api_usage_once
  7. from ._utils import check_roi_boxes_shape, convert_boxes_to_roi_format
  8. @torch.fx.wrap
  9. def ps_roi_align(
  10. input: Tensor,
  11. boxes: Tensor,
  12. output_size: int,
  13. spatial_scale: float = 1.0,
  14. sampling_ratio: int = -1,
  15. ) -> Tensor:
  16. """
  17. Performs Position-Sensitive Region of Interest (RoI) Align operator
  18. mentioned in Light-Head R-CNN.
  19. Args:
  20. input (Tensor[N, C, H, W]): The input tensor, i.e. a batch with ``N`` elements. Each element
  21. contains ``C`` feature maps of dimensions ``H x W``.
  22. boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
  23. format where the regions will be taken from.
  24. The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
  25. If a single Tensor is passed, then the first column should
  26. contain the index of the corresponding element in the batch, i.e. a number in ``[0, N - 1]``.
  27. If a list of Tensors is passed, then each Tensor will correspond to the boxes for an element i
  28. in the batch.
  29. output_size (int or Tuple[int, int]): the size of the output (in bins or pixels) after the pooling
  30. is performed, as (height, width).
  31. spatial_scale (float): a scaling factor that maps the box coordinates to
  32. the input coordinates. For example, if your boxes are defined on the scale
  33. of a 224x224 image and your input is a 112x112 feature map (resulting from a 0.5x scaling of
  34. the original image), you'll want to set this to 0.5. Default: 1.0
  35. sampling_ratio (int): number of sampling points in the interpolation grid
  36. used to compute the output value of each pooled output bin. If > 0,
  37. then exactly ``sampling_ratio x sampling_ratio`` sampling points per bin are used. If
  38. <= 0, then an adaptive number of grid points are used (computed as
  39. ``ceil(roi_width / output_width)``, and likewise for height). Default: -1
  40. Returns:
  41. Tensor[K, C / (output_size[0] * output_size[1]), output_size[0], output_size[1]]: The pooled RoIs
  42. """
  43. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  44. _log_api_usage_once(ps_roi_align)
  45. _assert_has_ops()
  46. check_roi_boxes_shape(boxes)
  47. rois = boxes
  48. output_size = _pair(output_size)
  49. if not isinstance(rois, torch.Tensor):
  50. rois = convert_boxes_to_roi_format(rois)
  51. output, _ = torch.ops.torchvision.ps_roi_align(
  52. input, rois, spatial_scale, output_size[0], output_size[1], sampling_ratio
  53. )
  54. return output
  55. class PSRoIAlign(nn.Module):
  56. """
  57. See :func:`ps_roi_align`.
  58. """
  59. def __init__(
  60. self,
  61. output_size: int,
  62. spatial_scale: float,
  63. sampling_ratio: int,
  64. ):
  65. super().__init__()
  66. _log_api_usage_once(self)
  67. self.output_size = output_size
  68. self.spatial_scale = spatial_scale
  69. self.sampling_ratio = sampling_ratio
  70. def forward(self, input: Tensor, rois: Tensor) -> Tensor:
  71. return ps_roi_align(input, rois, self.output_size, self.spatial_scale, self.sampling_ratio)
  72. def __repr__(self) -> str:
  73. s = (
  74. f"{self.__class__.__name__}("
  75. f"output_size={self.output_size}"
  76. f", spatial_scale={self.spatial_scale}"
  77. f", sampling_ratio={self.sampling_ratio}"
  78. f")"
  79. )
  80. return s