roi_pool.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. from typing import List, Union
  2. import torch
  3. import torch.fx
  4. from torch import nn, Tensor
  5. from torch.jit.annotations import BroadcastingList2
  6. from torch.nn.modules.utils import _pair
  7. from torchvision.extension import _assert_has_ops
  8. from ..utils import _log_api_usage_once
  9. from ._utils import check_roi_boxes_shape, convert_boxes_to_roi_format
  10. @torch.fx.wrap
  11. def roi_pool(
  12. input: Tensor,
  13. boxes: Union[Tensor, List[Tensor]],
  14. output_size: BroadcastingList2[int],
  15. spatial_scale: float = 1.0,
  16. ) -> Tensor:
  17. """
  18. Performs Region of Interest (RoI) Pool operator described in Fast 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 after the cropping
  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. Returns:
  36. Tensor[K, C, output_size[0], output_size[1]]: The pooled RoIs.
  37. """
  38. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  39. _log_api_usage_once(roi_pool)
  40. _assert_has_ops()
  41. check_roi_boxes_shape(boxes)
  42. rois = boxes
  43. output_size = _pair(output_size)
  44. if not isinstance(rois, torch.Tensor):
  45. rois = convert_boxes_to_roi_format(rois)
  46. output, _ = torch.ops.torchvision.roi_pool(input, rois, spatial_scale, output_size[0], output_size[1])
  47. return output
  48. class RoIPool(nn.Module):
  49. """
  50. See :func:`roi_pool`.
  51. """
  52. def __init__(self, output_size: BroadcastingList2[int], spatial_scale: float):
  53. super().__init__()
  54. _log_api_usage_once(self)
  55. self.output_size = output_size
  56. self.spatial_scale = spatial_scale
  57. def forward(self, input: Tensor, rois: Union[Tensor, List[Tensor]]) -> Tensor:
  58. return roi_pool(input, rois, self.output_size, self.spatial_scale)
  59. def __repr__(self) -> str:
  60. s = f"{self.__class__.__name__}(output_size={self.output_size}, spatial_scale={self.spatial_scale})"
  61. return s