ps_roi_pool.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_pool(
  10. input: Tensor,
  11. boxes: Tensor,
  12. output_size: int,
  13. spatial_scale: float = 1.0,
  14. ) -> Tensor:
  15. """
  16. Performs Position-Sensitive Region of Interest (RoI) Pool operator
  17. described in R-FCN
  18. Args:
  19. input (Tensor[N, C, H, W]): The input tensor, i.e. a batch with ``N`` elements. Each element
  20. contains ``C`` feature maps of dimensions ``H x W``.
  21. boxes (Tensor[K, 5] or List[Tensor[L, 4]]): the box coordinates in (x1, y1, x2, y2)
  22. format where the regions will be taken from.
  23. The coordinate must satisfy ``0 <= x1 < x2`` and ``0 <= y1 < y2``.
  24. If a single Tensor is passed, then the first column should
  25. contain the index of the corresponding element in the batch, i.e. a number in ``[0, N - 1]``.
  26. If a list of Tensors is passed, then each Tensor will correspond to the boxes for an element i
  27. in the batch.
  28. output_size (int or Tuple[int, int]): the size of the output (in bins or pixels) after the pooling
  29. is performed, as (height, width).
  30. spatial_scale (float): a scaling factor that maps the box coordinates to
  31. the input coordinates. For example, if your boxes are defined on the scale
  32. of a 224x224 image and your input is a 112x112 feature map (resulting from a 0.5x scaling of
  33. the original image), you'll want to set this to 0.5. Default: 1.0
  34. Returns:
  35. Tensor[K, C / (output_size[0] * output_size[1]), output_size[0], output_size[1]]: The pooled RoIs.
  36. """
  37. if not torch.jit.is_scripting() and not torch.jit.is_tracing():
  38. _log_api_usage_once(ps_roi_pool)
  39. _assert_has_ops()
  40. check_roi_boxes_shape(boxes)
  41. rois = boxes
  42. output_size = _pair(output_size)
  43. if not isinstance(rois, torch.Tensor):
  44. rois = convert_boxes_to_roi_format(rois)
  45. output, _ = torch.ops.torchvision.ps_roi_pool(input, rois, spatial_scale, output_size[0], output_size[1])
  46. return output
  47. class PSRoIPool(nn.Module):
  48. """
  49. See :func:`ps_roi_pool`.
  50. """
  51. def __init__(self, output_size: int, spatial_scale: float):
  52. super().__init__()
  53. _log_api_usage_once(self)
  54. self.output_size = output_size
  55. self.spatial_scale = spatial_scale
  56. def forward(self, input: Tensor, rois: Tensor) -> Tensor:
  57. return ps_roi_pool(input, rois, self.output_size, self.spatial_scale)
  58. def __repr__(self) -> str:
  59. s = f"{self.__class__.__name__}(output_size={self.output_size}, spatial_scale={self.spatial_scale})"
  60. return s