roi_pool.py 2.8 KB

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