Skip to content

Commit 6315358

Browse files
Exposing LevelMapper params in MultiScaleRoIAlign (#3129) (#3151)
* Exposing LevelMapper params in MultiScaleRoIAlign Ability to set the `canonical_scale` and `canonical_level` of LevelMapper from the init method of MultiScaleRoIAlign. Not sure if this is the right place to expose those parameters. Also, additional tests should be written. Discussion in issue #3129 * MultiScaleRoIAlign keyword-only parameters + docs (#3129) * Linter fix (trailing whitespace) Co-authored-by: Francisco Massa <[email protected]>
1 parent 3711754 commit 6315358

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

torchvision/ops/poolers.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,20 @@ class MultiScaleRoIAlign(nn.Module):
8585
"""
8686
Multi-scale RoIAlign pooling, which is useful for detection with or without FPN.
8787
88-
It infers the scale of the pooling via the heuristics present in the FPN paper.
88+
It infers the scale of the pooling via the heuristics specified in eq. 1
89+
of the `Feature Pyramid Network paper <https://arxiv.org/abs/1612.03144>`_.
90+
They keyword-only parameters ``canonical_scale`` and ``canonical_level``
91+
correspond respectively to ``224`` and ``k0=4`` in eq. 1, and
92+
have the following meaning: ``canonical_level`` is the target level of the pyramid from
93+
which to pool a region of interest with ``w x h = canonical_scale x canonical_scale``.
8994
9095
Args:
9196
featmap_names (List[str]): the names of the feature maps that will be used
9297
for the pooling.
9398
output_size (List[Tuple[int, int]] or List[int]): output size for the pooled region
9499
sampling_ratio (int): sampling ratio for ROIAlign
100+
canonical_scale (int, optional): canonical_scale for LevelMapper
101+
canonical_level (int, optional): canonical_level for LevelMapper
95102
96103
Examples::
97104
@@ -120,6 +127,9 @@ def __init__(
120127
featmap_names: List[str],
121128
output_size: Union[int, Tuple[int], List[int]],
122129
sampling_ratio: int,
130+
*,
131+
canonical_scale: int = 224,
132+
canonical_level: int = 4,
123133
):
124134
super(MultiScaleRoIAlign, self).__init__()
125135
if isinstance(output_size, int):
@@ -129,6 +139,8 @@ def __init__(
129139
self.output_size = tuple(output_size)
130140
self.scales = None
131141
self.map_levels = None
142+
self.canonical_scale = canonical_scale
143+
self.canonical_level = canonical_level
132144

133145
def convert_to_roi_format(self, boxes: List[Tensor]) -> Tensor:
134146
concat_boxes = torch.cat(boxes, dim=0)
@@ -173,7 +185,12 @@ def setup_scales(
173185
lvl_min = -torch.log2(torch.tensor(scales[0], dtype=torch.float32)).item()
174186
lvl_max = -torch.log2(torch.tensor(scales[-1], dtype=torch.float32)).item()
175187
self.scales = scales
176-
self.map_levels = initLevelMapper(int(lvl_min), int(lvl_max))
188+
self.map_levels = initLevelMapper(
189+
int(lvl_min),
190+
int(lvl_max),
191+
canonical_scale=self.canonical_scale,
192+
canonical_level=self.canonical_level,
193+
)
177194

178195
def forward(
179196
self,

0 commit comments

Comments
 (0)