roi_align.py 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from typing import List, Union
  2. import torch
  3. from torch import nn, Tensor
  4. from torch.jit.annotations import BroadcastingList2
  5. from torch.nn.modules.utils import _pair
  6. from torchvision.extension import _assert_has_ops
  7. from ..utils import _log_api_usage_once
  8. from ._utils import convert_boxes_to_roi_format, check_roi_boxes_shape
  9. def roi_align(
  10. input: Tensor,
  11. boxes: Union[Tensor, List[Tensor]],
  12. output_size: BroadcastingList2[int],
  13. spatial_scale: float = 1.0,
  14. sampling_ratio: int = -1,
  15. aligned: bool = False,
  16. ) -> Tensor:
  17. """
  18. Performs Region of Interest (RoI) Align operator with average pooling, as described in Mask 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. If the tensor is quantized, we expect a batch size of ``N == 1``.
  23. boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
  24. format where the regions will be taken from.
  25. The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
  26. If a single Tensor is passed, then the first column should
  27. contain the index of the corresponding element in the batch, i.e. a number in ``[0, N - 1]``.
  28. If a list of Tensors is passed, then each Tensor will correspond to the boxes for an element i
  29. in the batch.
  30. output_size (int or Tuple[int, int]): the size of the output (in bins or pixels) after the pooling
  31. is performed, as (height, width).
  32. spatial_scale (float): a scaling factor that maps the box coordinates to
  33. the input coordinates. For example, if your boxes are defined on the scale
  34. of a 224x224 image and your input is a 112x112 feature map (resulting from a 0.5x scaling of
  35. the original image), you'll want to set this to 0.5. Default: 1.0
  36. sampling_ratio (int): number of sampling points in the interpolation grid
  37. used to compute the output value of each pooled output bin. If > 0,
  38. then exactly ``sampling_ratio x sampling_ratio`` sampling points per bin are used. If
  39. <= 0, then an adaptive number of grid points are used (computed as
  40. ``ceil(roi_width / output_width)``, and likewise for height). Default: -1
  41. aligned (bool): If False, use the legacy implementation.
  42. If True, pixel shift the box coordinates it by -0.5 for a better alignment with the two
  43. neighboring pixel indices. This version is used in Detectron2
  44. Returns:
  45. Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.
  46. """
  47. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  48. _log_api_usage_once(roi_align)
  49. _assert_has_ops()
  50. check_roi_boxes_shape(boxes)
  51. rois = boxes
  52. output_size = _pair(output_size)
  53. if not isinstance(rois, torch.Tensor):
  54. rois = convert_boxes_to_roi_format(rois)
  55. return torch.ops.torchvision.roi_align(
  56. input, rois, spatial_scale, output_size[0], output_size[1], sampling_ratio, aligned
  57. )
  58. class RoIAlign(nn.Module):
  59. """
  60. See :func:`roi_align`.
  61. """
  62. def __init__(
  63. self,
  64. output_size: BroadcastingList2[int],
  65. spatial_scale: float,
  66. sampling_ratio: int,
  67. aligned: bool = False,
  68. ):
  69. super().__init__()
  70. _log_api_usage_once(self)
  71. self.output_size = output_size
  72. self.spatial_scale = spatial_scale
  73. self.sampling_ratio = sampling_ratio
  74. self.aligned = aligned
  75. def forward(self, input: Tensor, rois: Tensor) -> Tensor:
  76. return roi_align(input, rois, self.output_size, self.spatial_scale, self.sampling_ratio, self.aligned)
  77. def __repr__(self) -> str:
  78. s = (
  79. f"{self.__class__.__name__}("
  80. f"output_size={self.output_size}"
  81. f", spatial_scale={self.spatial_scale}"
  82. f", sampling_ratio={self.sampling_ratio}"
  83. f", aligned={self.aligned}"
  84. f")"
  85. )
  86. return s